| | #!/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) |