okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-par' into f-139-relf5410ae2b

main
2.0 KiB47 linesraw
1#!/usr/bin/env python3
2"""A client that stops reading a streamed answer early must not leave its tokens to the next request.
3
4  STRATA_KEY=... python3 tools/early_close_test.py http://127.0.0.1:8090
5"""
6import json, os, sys, threading, urllib.request
7
8URL = sys.argv[1].rstrip("/") + "/v1/chat/completions"
9KEY = os.environ.get("STRATA_KEY", "")
10
11
12def req(content, stream, max_tokens=300):
13    body = json.dumps({"model": "m", "max_tokens": max_tokens, "temperature": 0, "stream": stream,
14                       "messages": [{"role": "user", "content": content}],
15                       "chat_template_kwargs": {"enable_thinking": False}}).encode()
16    return urllib.request.Request(URL, data=body, headers={"Content-Type": "application/json",
17                                                           "Authorization": f"Bearer {KEY}"})
18
19
20def early_close(content, keep_lines=5):
21    r = urllib.request.urlopen(req(content, True), timeout=600)
22    for _ in range(keep_lines):
23        r.readline()
24    r.close()                                   # the client goes away mid-answer
25
26
27def answer(content):
28    with urllib.request.urlopen(req(content, False, 60), timeout=600) as r:
29        m = json.load(r)["choices"][0]["message"]
30        return (m.get("content") or "") + " " + (m.get("reasoning_content") or m.get("reasoning") or "")
31
32
33ok = True
34# alone (solo path), then the same with a second request running (batch slots)
35for label, background in (("solo", False), ("batch", True)):
36    bg = None
37    if background:
38        bg = threading.Thread(target=answer, args=("Count slowly from 1 to 60, one number per line.",))
39        bg.start()
40    early_close("Describe the weather in Montpellier in great detail, at length.")
41    a = answer("Reply with exactly one word: the color of the sky on a clear day.")
42    good = "blue" in a.lower() and "montpellier" not in a.lower()
43    ok &= good
44    print(f"{label}: {'OK' if good else 'STALE'} -> {a[:120]!r}", flush=True)
45    if bg:
46        bg.join()
47sys.exit(0 if ok else 1)