import urllib.request, json, time, sys
try: sys.stdout.reconfigure(encoding='utf-8')
except: pass

BASE = "http://127.0.0.1:8000"
prompt = "Write a Python function to calculate fibonacci numbers efficiently using memoization. Include type hints and docstring."

payload = json.dumps({
    "model": "m",
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": prompt}
    ],
    "max_tokens": 500,
    "temperature": 0.0
}).encode('utf-8')

req = urllib.request.Request(
    f"{BASE}/v1/chat/completions",
    data=payload,
    headers={"Content-Type": "application/json"}
)

print("Sending request...")
t0 = time.time()
resp = json.loads(urllib.request.urlopen(req, timeout=300).read())
dt = time.time() - t0

u = resp.get("usage", {})
tokens = u.get("completion_tokens", 0)
speed = tokens / dt if dt > 0 else 0

print(f"\n=== 122B Benchmark ===")
print(f"Time: {dt:.1f}s")
print(f"Completion Tokens: {tokens}")
print(f"Speed: {speed:.2f} t/s")
print(f"\n--- Response Preview ---")
print(resp["choices"][0]["message"]["content"][:300])
