Talking Jev: standalone English chat via TypeSafe Jev 1.13, 254 tokens, temperature 0.3 A developer published Talking Jev, a standalone Python chat script that runs an English-language conversation loop against the Jev model through OpenRouter using only Python 3.9+ and curl, with no external packages or companion files. The script embeds its own tokenizer, 254-token vocabulary, sampling logic, and API client, defaulting to temperature 0.3 and top-p 0.9, and supports command-line prompts, seeds, and vocabulary inspection. | | /usr/bin/env python3 | | | """Talking Jev: a standalone English chat experiment with 254 output tokens. | | | Requirements: Python 3.9+ and curl; no Python packages or companion files. | | | Set OPENROUTER API KEY in the environment, or in .env beside this script. | | | python3 jev.py | | | python3 jev.py --prompt "Hi What is your name?" --seed 42 | | | python3 jev.py --temperature 0.3 --top-p 0.9 --max-tokens 80 | | | python3 jev.py --vocab | | | Defaults: temperature 0.3, top-p 0.9. /reset clears history; /quit exits. | | | Only Jev is called. Tokenizer, vocabulary, sampling, and API client are embedded. | | | """ | | | import argparse | | | import json | | | import math | | | import os | | | from pathlib import Path | | | import random | | | import subprocess | | | import sys | | | import time | | | COMMON WORDS = """the of and to a in is it you that he was for on are with as I | | | his they be at one have this from or had by not word but what some we can out | | | other were all there when up use your how said an each she which do their time | | | if will way about many then them write would like so these her long make thing | | | see him two has look more day could go come did number sound no most people my | | | over know water than call first who may down side been now find any new work | | | part take get place made live where after back little only round man year came | | | show every good me give our under name""".split | | | START WORDS = """Hello Hi Yes No Jev You The It We That This My Your How What Why | | | When Where Who Please Thanks Sorry Sure Good Let There Can Do""".split | | | ENGLISH PIECES = " " + word for word in COMMON WORDS + | | | " Jev" if word == "Jev" else word for word in START WORDS | | | | | | class Tokenizer: | | | user id = 251 | | | assistant id = 252 | | | eos id = 253 | | | specials = "<\|USER\| ", "<\|ASSISTANT\| ", "<\|EOS\| " | | | def init self : | | | characters = list chr n for n in range 32, 127 | | | characters += "\n", "\t" | | | self.pieces = characters + ENGLISH PIECES | | | assert len self.pieces == 251 and len set self.pieces == 251 | | | self.tokens = self.pieces + self.specials | | | self.by text = {text: i for i, text in enumerate self.pieces } | | | self.longest first = sorted self.pieces, key=len, reverse=True | | | def encode self, text : | | | """Greedy longest match; unsupported characters fail rather than disappear.""" | | | ids, offset = , 0 | | | while offset < len text : | | | for piece in self.longest first: | | | if text.startswith piece, offset : | | | ids.append self.by text piece | | | offset += len piece | | | break | | | else: | | | raise ValueError f"Unsupported character {text offset r} at position {offset}" | | | return ids | | | def decode self, ids, skip special=False : | | | pieces = | | | for token id in ids: | | | if type token id is not int or not 0 <= token id <= self.eos id: | | | raise ValueError f"Invalid token id: {token id r}" | | | if not skip special or token id < self.user id: | | | pieces.append self.tokens token id | | | return "".join pieces | | | def encode conversation self, history, prefix="" : | | | ids = | | | for message in history: | | | if message "role" not in {"user", "assistant"}: | | | raise ValueError "Conversation roles must be user or assistant" | | | ids.append self.user id if message "role" == "user" else self.assistant id | | | ids.extend self.encode message "content" | | | ids.append self.eos id | | | return ids + self.assistant id + self.encode prefix | | | def criteria self, prefix="" : | | | Show the resulting spelling, so the classifier can evaluate text prefixes. | | | Bound the repeated tail; the complete reply remains available in state. | | | tail = prefix -160: | | | criteria = { | | | f"t{i:03}": "A coherent, grammatically valid partial answer has this tail: " | | | + json.dumps tail + piece + "; appended fragment: " + json.dumps piece | | | for i, piece in enumerate self.pieces | | | } | | | criteria f"t{self.user id}" = "USER: boundary of a new user message. Never append inside an assistant reply." | | | criteria f"t{self.assistant id}" = "ASSISTANT: start of an assistant message. Already present; do not repeat." | | | criteria f"t{self.eos id}" = "EOS: end the reply only if this final reply already answers the user completely: " + json.dumps tail | | | return criteria | | | def export self : | | | return {"id": i, "text": text, "special": text 2:-2 if i = self.user id else None} | | | for i, text in enumerate self.tokens | | | ROOT = Path file .resolve .parent | | | MODEL = "typesafe/jev-1.13" | | | DEFAULT TEMPERATURE = 0.3 | | | ENDPOINT = "https://openrouter.ai/api/alpha/decisions" | | | INSTRUCTIONS = """Continue the assistant's reply in conversation. Select the SINGLE text | | | fragment that should come immediately after assistant prefix, at the cursor. | | | The fragment must fit the spelling of the next word, grammar, and the user's request. | | | Do not select a fragment merely because its meaning is relevant somewhere later. | | | Treat fragments as literal text, including spaces and case. Prefer a longer fragment | | | when it matches the intended continuation. If the prefix is empty, begin the answer. | | | Preserve spaces between words: "is Jev" is valid, "isJev" is not. Complete unfinished | | | words by appending letters without spaces; begin the following word with a space. | | | Write in English. Keep the reply short, natural, and helpful. The conversation uses | | | USER, ASSISTANT, and EOS boundary markers. Continue only the final ASSISTANT turn. | | | Do not emit USER or ASSISTANT. At the start, begin with a letter, not a space. | | | Choose end-of-reply only when assistant prefix already gives a complete answer. | | | Only judge the next fragment; previous text cannot be changed.""" | | | def load key : | | | key = os.environ.get "OPENROUTER API KEY", "" | | | if not key and ROOT / ".env" .exists : | | | for line in ROOT / ".env" .read text .splitlines : | | | if line.startswith "OPENROUTER API KEY=" : | | | key = line.split "=", 1 1 .strip .strip "\"'" | | | break | | | if not key: | | | raise RuntimeError "Set OPENROUTER API KEY in the environment or in .env beside this script." | | | return key | | | class Client: | | | def init self, key=None, model=MODEL, timeout=45 : | | | self.key = key or load key | | | self.model, self.timeout = model, timeout | | | def decide self, state, criteria : | | | payload = {"model": self.model, "state": state, "questions": { | | | "next": {"type": "choice", "instructions": INSTRUCTIONS, "criteria": criteria}}} | | | Supply the key through stdin, never argv, shell history, or logs. | | | config = "\n".join | | | "url = " + json.dumps ENDPOINT , 'request = "POST"', | | | "header = " + json.dumps "Authorization: Bearer " + self.key , | | | 'header = "Content-Type: application/json"', | | | 'header = "X-OpenRouter-Title: Talking Jev"', | | | "data = " + json.dumps json.dumps payload, ensure ascii=False , | | | | | | for attempt in range 3 : | | | result = subprocess.run | | | "curl", "-sS", "--max-time", str self.timeout , "--config", "-", | | | "--write-out", "\n%{http code}" , | | | input=config, text=True, capture output=True, | | | | | | if result.returncode: | | | raise RuntimeError "OpenRouter connection failed: " + result.stderr.replace self.key, " redacted " .strip | | | body, status = result.stdout.rsplit "\n", 1 | | | if status in {"429", "502", "503", "504"} and attempt < 2: | | | time.sleep 2 attempt | | | continue | | | if status = "200": | | | raise RuntimeError f"OpenRouter HTTP {status}: {body :1200 }".replace self.key, " redacted " | | | try: | | | response = json.loads body | | | answer = response "answers" "next" | | | if answer "choice" not in criteria: | | | raise ValueError "choice outside vocabulary" | | | return answer, response | | | except KeyError, ValueError, TypeError as exc: | | | raise RuntimeError "Invalid decision response from OpenRouter" from exc | | | raise RuntimeError "OpenRouter retries exhausted" | | | def sample answer, temperature, top p, rng : | | | """These are decision scores, not calibrated language-model token probabilities.""" | | | if temperature == 0: | | | return answer "choice" | | | weighted = | | | for key, probability in answer.get "probabilities", {} .items : | | | if isinstance probability, int, float and math.isfinite probability and probability 0: | | | weighted.append key, math.log probability / temperature | | | if not weighted: | | | return answer "choice" | | | largest = max weight for , weight in weighted | | | weighted = sorted key, math.exp weight - largest for key, weight in weighted , | | | key=lambda item: item 1 , reverse=True | | | total = sum weight for , weight in weighted | | | kept, cumulative = , 0 | | | for key, weight in weighted: | | | kept.append key, weight | | | cumulative += weight | | | if cumulative = total top p: | | | break | | | return rng.choices key for key, in kept , weight for , weight in kept 0 | | | def generate client, history, prefix="", max tokens=80, temperature=DEFAULT TEMPERATURE, | | | top p=0.9, seed=None, emit=None : | | | tokenizer = Tokenizer | | | Validate English input and insert actual role token IDs before rendering state. | | | tokenizer.encode conversation history, prefix | | | text, steps, cost, input tokens = prefix, , 0.0, 0 | | | rng = random.Random seed | | | started = time.monotonic | | | reason = "max tokens" | | | for step in range max tokens : | | | criteria = tokenizer.criteria text | | | ids = tokenizer.encode conversation history, text | | | state = {"conversation": tokenizer.decode ids , "assistant prefix": text, | | | "task": "Write the assistant's next reply by appending one literal fragment at a time."} | | | before = time.monotonic | | | answer, response = client.decide state, criteria | | | choice = sample answer, temperature, top p, rng | | | if choice not in criteria: | | | raise RuntimeError "Sampled choice outside vocabulary" | | | token id = int choice 1: | | | piece = tokenizer.tokens token id | | | usage = response.get "usage", {} | | | cost += float usage.get "cost", 0 or 0 | | | input tokens += int usage.get "input tokens", usage.get "inputTokens", 0 or 0 | | | candidates = sorted answer.get "probabilities", {} .items , key=lambda item: item 1 , reverse=True :5 | | | steps.append {"step": step, "token id": token id, "piece": piece, | | | "choice probability": answer.get "probabilities", {} .get choice , | | | "confidence": answer.get "confidence" , "top candidates": candidates, | | | "seconds": round time.monotonic - before, 4 , "usage": usage, | | | "request id": response.get "id" , "model": response.get "model" } | | | if token id = tokenizer.user id: | | | reason = "eos" if token id == tokenizer.eos id else "role boundary" | | | break | | | text += piece | | | if emit: | | | emit piece | | | Stop unbounded degeneracy, but preserve and report the actual model output. | | | if len steps = 8 and len {item "token id" for item in steps -8: } == 1: | | | reason = "repetition" | | | break | | | return {"text": text, "stop reason": reason, "decisions": len steps , | | | "seconds": round time.monotonic - started, 3 , "cost usd": cost, | | | "input tokens": input tokens, "temperature": temperature, "top p": top p, | | | "seed": seed, "steps": steps} | | | def print stats result : | | | print f"\n {result 'decisions' } decisions · {result 'seconds' :.1f} s · " | | | f"${result 'cost usd' :.5f} · {result 'stop reason' } ", file=sys.stderr | | | def main : | | | parser = argparse.ArgumentParser description="Chat with Jev using 254 English tokens." | | | parser.add argument "--prompt", help="One message instead of interactive chat" | | | parser.add argument "--prefix", default="", help="An answer prefix for Jev to continue" | | | parser.add argument "--max-tokens", type=int, default=80, help="Maximum decisions per reply 80 " | | | parser.add argument "--temperature", type=float, default=DEFAULT TEMPERATURE, | | | help="Local sampling temperature default: 0.3; 0: Jev's choice " | | | parser.add argument "--top-p", type=float, default=0.9 | | | parser.add argument "--seed", type=int | | | parser.add argument "--trace", type=Path, help="Save results and decisions as JSONL" | | | parser.add argument "--vocab", action="store true", help="Show all 254 tokens without calling the API" | | | args = parser.parse args | | | if args.vocab: | | | print json.dumps Tokenizer .export , ensure ascii=False, indent=2 | | | return | | | if not 1 <= args.max tokens <= 1024 or not math.isfinite args.temperature or args.temperature < 0 or not 0 < args.top p <= 1: | | | parser.error "max-tokens: 1..1024; temperature: =0; top-p: 0,1 " | | | client = Client | | | history = | | | if args.prompt is None: | | | print "Talking Jev · 254 English tokens · /reset: clear history · /quit: exit" | | | while True: | | | try: | | | prompt = args.prompt if args.prompt is not None else input "\nYou: " | | | except EOFError: | | | break | | | if prompt.strip == "/quit": | | | break | | | if prompt.strip == "/reset" and args.prompt is None: | | | history.clear | | | print "History cleared." | | | continue | | | if not prompt.strip : | | | if args.prompt is not None: | | | parser.error "prompt must not be empty" | | | continue | | | history.append {"role": "user", "content": prompt} | | | print "Jev: " + args.prefix, end="", flush=True | | | result = generate client, history, prefix=args.prefix, max tokens=args.max tokens, | | | temperature=args.temperature, top p=args.top p, seed=args.seed, | | | emit=lambda piece: print piece, end="", flush=True | | | print | | | print stats result | | | history.append {"role": "assistant", "content": result "text" } | | | if args.trace: | | | args.trace.parent.mkdir parents=True, exist ok=True | | | with args.trace.open "a" as handle: | | | handle.write json.dumps {"conversation": history, "result": result}, ensure ascii=False + "\n" | | | if args.prompt is not None: | | | break | | | if name == " main ": | | | try: | | | main | | | except KeyboardInterrupt: | | | print "\nStopped.", file=sys.stderr | | | sys.exit 130 | | | except RuntimeError, ValueError, FileNotFoundError as exc: | | | print f"Error: {exc}", file=sys.stderr | | | sys.exit 1 |