okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f5410ae2b
main
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 4STRATA_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 ): 13body = 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 () 16return 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 ): 21r = urllib .request .urlopen (req (content ,True ),timeout = 600 ) 22for _ in range (keep_lines ): 23r .readline () 24r .close ()# the client goes away mid-answer 25 26 27def answer (content ): 28with urllib .request .urlopen (req (content ,False ,60 ),timeout = 600 )as r : 29m = json .load (r )["choices" ][0 ]["message" ] 30return (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 )): 36bg = None 37if background : 38bg = threading .Thread (target = answer ,args = ("Count slowly from 1 to 60, one number per line." ,)) 39bg .start () 40early_close ("Describe the weather in Montpellier in great detail, at length." ) 41a = answer ("Reply with exactly one word: the color of the sky on a clear day." ) 42good = "blue" in a .lower ()and "montpellier" not in a .lower () 43ok &= good 44f" { label } : { 'OK' if good else 'STALE' } -> { a [: 120 ]!r } " ,flush = True ) 45if bg : 46bg .join () 47sys .exit (0 if ok else 1 )