#!/usr/bin/env python3 """Greedy chat decode of 5 varied prompts (math, code, German, tool call, summary) on a running server; saves tokens + text. With REF.json, compares token by token and prints the first divergence (with the reference's top-2 margin there) and both final answers. python3 tests/quality_prompts.py PORT OUT.json [REF.json] """ import json, sys, urllib.request port, out_path = int(sys.argv[1]), sys.argv[2] ref_path = sys.argv[3] if len(sys.argv) > 3 else None WEATHER_TOOL = {"type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a city.", "parameters": {"type": "object", "properties": {"city": {"type": "string"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["city"]}}} PROMPTS = { "math": {"messages": [{"role": "user", "content": "A train leaves at 09:40 and arrives at 13:15. It stops twice for 7 minutes each. " "How many minutes is it actually moving? Answer with the number."}]}, "code": {"messages": [{"role": "user", "content": "Write a Python function is_palindrome(s) that ignores case and non-alphanumeric " "characters. Only the code."}]}, "german": {"messages": [{"role": "user", "content": "Erkläre in zwei Sätzen, warum der Himmel blau ist. Antworte auf Deutsch."}]}, "tool_call": {"messages": [{"role": "user", "content": "What's the weather in Hamburg right now, in celsius?"}], "tools": [WEATHER_TOOL]}, "summary": {"messages": [{"role": "user", "content": "Summarize in one sentence: The city council voted 7 to 2 on Tuesday to convert the " "old freight depot into a public library and community workshop. Construction starts " "next spring and is funded by a state grant of 4.2 million euros plus 800,000 euros " "from the city budget. Opponents argued the site should be sold to reduce debt."}]}, } def run(name, p): body = {"model": "lfm3-1.7b", "max_tokens": 1500, "temperature": 0.0, "repetition_penalty": 1.0, "logprobs": True, "top_logprobs": 2, **p} req = urllib.request.Request(f"http://127.0.0.1:{port}/v1/chat/completions", json.dumps(body).encode(), {"Content-Type": "application/json"}) r = json.loads(urllib.request.urlopen(req, timeout=600).read()) c = r["choices"][0]; m = c["message"] lp = (c.get("logprobs") or {}).get("content") or [] return {"tokens": [t["token"] for t in lp], "margins": [(t["top_logprobs"][0]["logprob"] - t["top_logprobs"][1]["logprob"]) if len(t["top_logprobs"]) > 1 else None for t in lp], "reasoning": m.get("reasoning"), "content": m.get("content"), "tool_calls": [tc["function"] for tc in (m.get("tool_calls") or [])], "finish_reason": c["finish_reason"], "completion_tokens": r["usage"]["completion_tokens"]} res = {name: run(name, p) for name, p in PROMPTS.items()} json.dump(res, open(out_path, "w"), ensure_ascii=False, indent=1) def answer(r): return json.dumps(r["tool_calls"], ensure_ascii=False) if r["tool_calls"] else (r["content"] or "").strip() if ref_path: ref = json.load(open(ref_path)) for name, r in res.items(): a, b = ref[name]["tokens"], r["tokens"] div = next((i for i, (x, y) in enumerate(zip(a, b)) if x != y), None) if div is None and len(a) == len(b): print(f"== {name}: identical ({len(a)} tokens)") else: div = div if div is not None else min(len(a), len(b)) mg = ref[name]["margins"][div] if div < len(ref[name]["margins"]) else None print(f"== {name}: diverges at token {div}/{len(a)} (ref top-2 margin {mg if mg is None else round(mg, 3)}); " f"ref {a[div:div+1]} vs new {b[div:div+1]}") print(f" ref answer: {answer(ref[name])[:300]!r}") print(f" new answer: {answer(r)[:300]!r}")