#!/usr/bin/env python3 """Quality eval for one served variant: perplexity on fixed wikitext-2 test tokens (vLLM prompt_logprobs, 20 x 2048 tokens, so the 256-token sliding window matters) + 8 greedy chat prompts. python3 eval/eval_quality.py PORT LABEL -> eval/results_LABEL.json """ import json, math, sys, urllib.request port, label = int(sys.argv[1]), sys.argv[2] data = json.load(open("eval/eval_data.json")) URL = f"http://127.0.0.1:{port}/v1" def post(path, body): req = urllib.request.Request(URL + path, json.dumps(body).encode(), {"Content-Type": "application/json"}) return json.loads(urllib.request.urlopen(req, timeout=900).read()) # --- perplexity: mean NLL of every token after the first, per chunk, pooled over all chunks nll, n = 0.0, 0 for ids in data["ppl_chunks"]: r = post("/completions", {"model": "lfm3-1.7b", "prompt": ids, "max_tokens": 1, "temperature": 0.0, "prompt_logprobs": 0}) for tid, lp in zip(ids[1:], r["choices"][0]["prompt_logprobs"][1:]): nll -= lp[str(tid)]["logprob"]; n += 1 ppl = math.exp(nll / n) print(f"{label}: PPL {ppl:.4f} over {n} tokens", flush=True) TOOLS = [ {"type": "function", "function": {"name": "get_weather", "description": "Current weather for a city.", "parameters": {"type": "object", "properties": {"city": {"type": "string"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["city"]}}}, {"type": "function", "function": {"name": "search_flights", "description": "Search flights.", "parameters": {"type": "object", "properties": {"origin": {"type": "string"}, "destination": {"type": "string"}, "date": {"type": "string", "description": "YYYY-MM-DD"}, "adults": {"type": "integer"}}, "required": ["origin", "destination", "date"]}}}, {"type": "function", "function": {"name": "convert_currency", "description": "Convert an amount between currencies.", "parameters": {"type": "object", "properties": {"amount": {"type": "number"}, "from": {"type": "string"}, "to": {"type": "string"}}, "required": ["amount", "from", "to"]}}}, ] PROMPTS = { "factual": "What is the capital of Australia, and why was it chosen instead of Sydney or Melbourne? Answer in two sentences.", "german": "Schreibe eine kurze, höfliche E-Mail an deine Kollegin Frau Weber: Das Meeting am Dienstag muss auf " "Donnerstag um 14 Uhr verschoben werden.", "math": "A shop sells pens at 3 for 2.40 EUR and notebooks at 4.50 EUR each. Anna buys 7 pens and 3 notebooks and " "pays with a 50 EUR note. How much change does she get? End with the amount.", "tool_weather": "Is it raining in Hamburg right now? Use celsius.", "tool_flights": "Find flights from Hamburg to Lisbon on 2026-11-14 for 2 adults.", "json": "Extract the data as a JSON object with keys name (string), age (integer), city (string), skills (list of " "strings). Output only the JSON.\n\nMaria Schmidt is 34, lives in Leipzig and works with Python, SQL and Tableau.", "summary_2k": "Summarize the following article in three sentences.\n\n" + data["summary_article"], "long_context": "The secret code word is 'BLUE HERON 47'. Remember it.\n\n" + data["filler_700"] + "\n\nWhat was the secret code word mentioned at the very beginning? Answer with the code word only.", } out = {"label": label, "ppl": ppl, "ppl_tokens": n, "prompts": {}} for name, text in PROMPTS.items(): body = {"model": "lfm3-1.7b", "messages": [{"role": "user", "content": text}], "max_tokens": 3000, "temperature": 0.0, "repetition_penalty": 1.0} if name.startswith("tool_"): body["tools"] = TOOLS r = post("/chat/completions", body) c = r["choices"][0]; m = c["message"] out["prompts"][name] = {"content": m.get("content"), "reasoning": m.get("reasoning"), "tool_calls": [tc["function"] for tc in (m.get("tool_calls") or [])], "finish_reason": c["finish_reason"], "completion_tokens": r["usage"]["completion_tokens"]} print(f" {name}: {c['finish_reason']}, {r['usage']['completion_tokens']} tok", flush=True) json.dump(out, open(f"eval/results_{label}.json", "w"), ensure_ascii=False, indent=1)