import json, time, urllib.request

def call(think):
    body = {
        "model": "gemma4:e4b",
        "messages": [
            {"role": "system", "content": "You are CRITRAWKETS, a loud TF2 soldier bot on a livestream. Reply with ONE line of in-game chat, under 120 characters, in character. No quotes."},
            {"role": "user", "content": "JUST HAPPENED: Doc Halberd captured your intelligence.\n\nYour line:"},
        ],
        "stream": False,
        "think": think,
        "options": {"temperature": 0.85, "num_predict": 48, "num_ctx": 1024},
    }
    req = urllib.request.Request("http://localhost:11434/api/chat",
                                 data=json.dumps(body).encode(),
                                 headers={"Content-Type": "application/json"})
    t0 = time.time()
    r = json.loads(urllib.request.urlopen(req, timeout=180).read())
    dt = time.time() - t0
    msg = r.get("message", {})
    return dt, r, msg

for think in (False, True):
    try:
        dt, r, msg = call(think)
        content = msg.get("content", "")
        thinking = msg.get("thinking") or ""
        print(f"--- think={think} ---")
        print(f"  wall        : {dt:.1f}s")
        print(f"  prompt_tok  : {r.get('prompt_eval_count')}  in {r.get('prompt_eval_duration',0)/1e9:.2f}s")
        print(f"  gen_tok     : {r.get('eval_count')}  in {r.get('eval_duration',0)/1e9:.2f}s")
        if r.get("eval_duration"):
            print(f"  decode tok/s: {r['eval_count']/(r['eval_duration']/1e9):.1f}")
        if r.get("prompt_eval_duration"):
            print(f"  prefill tk/s: {r['prompt_eval_count']/(r['prompt_eval_duration']/1e9):.1f}")
        print(f"  thinking len: {len(thinking)}")
        print(f"  content     : {content!r}")
    except Exception as e:
        print(f"--- think={think} FAILED: {e}")
