okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
394de9291
main
1"""serve/test_effort_paths.py - every way a client sets the thinking level reaches the model's prompt (mock engine, no GPU). 2 3The level is nothing but a sentence in the rendered prompt (the template: xhigh / low sentences, none = the empty think 4block, medium = no sentence), so "the effort does not work" is always a request form whose level never reached the template. 5This pins the prompt each form produces, over the three APIs, the Chat settings shared with apps and the config's 6"reasoning_effort" (#1641). 0.1.44.1: the Responses API (/v1/responses, Codex) ignored the config's level. 7 8python -m unittest serve.test_effort_paths -v 9""" 10from __future__import annotations 11 12import json 13import sys 14import unittest 15import urllib .error 16import urllib .request 17from pathlib import Path 18 19sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 20from serve .frontend import ChatTemplate # noqa: E402 21from serve .server import ByteTokenizer ,MockEngine ,Service ,serve # noqa: E402 22 23ROOT = Path (__file__ ).resolve ().parents [1 ] 24MSG = [{"role" :"user" ,"content" :"hello" }] 25 26 27def level_of (prompt :str )-> str : 28"""What the template made of the level: xhigh and low have their sentence, none ends in the empty think block, and 29medium is the prompt with no sentence at all (the model's own wording).""" 30if prompt .rstrip ().endswith ("</think>" ): 31return "none" 32if "Reasoning effort is set to low." in prompt : 33return "low" 34if "Reasoning effort is set to xhigh." in prompt : 35return "xhigh" 36return "medium" 37 38 39class EffortPaths (unittest .TestCase ): 40@classmethod 41def setUpClass (cls ): 42cls .tok = ByteTokenizer () 43cls .engine = MockEngine (cls .tok ,"<think>\nhm\n</think>\n\nok" ,max_context = 8192 ) 44cls .svc = Service (cls .engine ,cls .tok ,ChatTemplate (ROOT / "serve/chat_template.jinja" )) 45cls .httpd = serve (cls .svc ,port = 0 ) 46cls .base = f"http://127.0.0.1: { cls . httpd . server_address [ 1 ] } " 47 48@classmethod 49def tearDownClass (cls ): 50cls .httpd .shutdown () 51cls .httpd .server_close () 52 53def setUp (self ): 54self .svc .config_effort = None 55self .svc .shared = {} 56 57def post (self ,path ,body ): 58req = urllib .request .Request (self .base + path ,data = json .dumps (body ).encode (), 59headers = {"Content-Type" :"application/json" }) 60self .engine .last_prompt = [] 61try : 62with urllib .request .urlopen (req ,timeout = 30 )as r : 63status = r .status 64r .read () 65except urllib .error .HTTPError as e : 66with e : 67status = e .code 68return status 69 70def level (self ,path ,body ): 71status = self .post (path ,body ) 72self .assertEqual (status ,200 , (path ,body )) 73return level_of (self .tok .decode (self .engine .last_prompt )) 74 75# ---------------------------------------------------------------- the request's own level, every spelling 76def test_openai_reasoning_effort (self ): 77for given ,want in ((None ,"xhigh" ), ("none" ,"none" ), ("off" ,"none" ), ("minimal" ,"none" ), ("low" ,"low" ), 78 ("medium" ,"medium" ), ("high" ,"xhigh" ), ("xhigh" ,"xhigh" ), ("max" ,"xhigh" ), 79 ("LOW" ,"low" ), (False ,"none" )): 80body = {"messages" :MSG ,** ({}if given is None else {"reasoning_effort" :given })} 81with self .subTest (given = given ): 82self .assertEqual (self .level ("/v1/chat/completions" ,body ),want ) 83 84def test_openai_reasoning_object_and_template_kwargs (self ): 85for given ,want in (("low" ,"low" ), ("medium" ,"medium" ), ("high" ,"xhigh" ), ("none" ,"none" )): 86with self .subTest (given = given ): 87self .assertEqual (self .level ("/v1/chat/completions" , {"messages" :MSG ,"reasoning" : {"effort" :given }}),want ) 88self .assertEqual (self .level ("/v1/chat/completions" , 89 {"messages" :MSG ,"chat_template_kwargs" : {"reasoning_effort" :given }}),want ) 90self .assertEqual (self .level ("/v1/chat/completions" , 91 {"messages" :MSG ,"chat_template_kwargs" : {"enable_thinking" :False }}),"none" ) 92 93def test_an_unknown_level_is_a_400_not_the_default (self ): 94self .assertEqual (self .post ("/v1/chat/completions" , {"messages" :MSG ,"reasoning_effort" :"huge" }),400 ) 95 96def test_anthropic_output_config_and_thinking (self ): 97for given ,want in (("low" ,"low" ), ("medium" ,"medium" ), ("high" ,"xhigh" )): 98with self .subTest (effort = given ): 99self .assertEqual (self .level ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 , 100"output_config" : {"effort" :given }}),want ) 101for budget ,want in ((500 ,"low" ), (2047 ,"low" ), (2048 ,"medium" ), (8191 ,"medium" ), (8192 ,"xhigh" ), (31999 ,"xhigh" )): 102with self .subTest (budget = budget ): 103self .assertEqual (self .level ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 , 104"thinking" : {"type" :"enabled" ,"budget_tokens" :budget }}),want ) 105self .assertEqual (self .level ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 ,"thinking" : {"type" :"disabled" }}), 106"none" ) 107self .assertEqual (self .level ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 }),"xhigh" )# nothing asked: it thinks 108 109def test_responses_reasoning_effort (self ): 110for given ,want in (("low" ,"low" ), ("medium" ,"medium" ), ("high" ,"xhigh" ), ("none" ,"none" ), ("minimal" ,"none" )): 111with self .subTest (given = given ): 112self .assertEqual (self .level ("/v1/responses" , {"input" :"hello" ,"reasoning" : {"effort" :given }}),want ) 113self .assertEqual (self .level ("/v1/responses" , {"input" :"hello" }),"xhigh" ) 114 115# ---------------------------------------------------------------- the level of a request that names none 116def test_config_level_reaches_every_api (self ): 117for cfg ,want in (("low" ,"low" ), ("medium" ,"medium" ), ("high" ,"xhigh" ), ("none" ,"none" )): 118self .svc .config_effort = cfg 119with self .subTest (config = cfg ): 120self .assertEqual (self .level ("/v1/chat/completions" , {"messages" :MSG }),want ) 121self .assertEqual (self .level ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 }),want ) 122self .assertEqual (self .level ("/v1/responses" , {"input" :"hello" }),want )# 0.1.44.1: it ignored the config 123 124def test_the_requests_own_level_beats_the_config (self ): 125self .svc .config_effort = "low" 126self .assertEqual (self .level ("/v1/chat/completions" , {"messages" :MSG ,"reasoning_effort" :"high" }),"xhigh" ) 127self .assertEqual (self .level ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 ,"output_config" : {"effort" :"high" }}), 128"xhigh" ) 129self .assertEqual (self .level ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 , 130"thinking" : {"type" :"enabled" ,"budget_tokens" :20000 }}),"xhigh" ) 131self .assertEqual (self .level ("/v1/responses" , {"input" :"hello" ,"reasoning" : {"effort" :"high" }}),"xhigh" ) 132self .assertEqual (self .level ("/v1/chat/completions" , {"messages" :MSG ,"reasoning_effort" :"none" }),"none" ) 133self .assertEqual (self .level ("/v1/chat/completions" , {"messages" :MSG ,"reasoning_effort" :False }),"none" ) 134 135def test_the_chat_settings_shared_with_apps_beat_the_config (self ): 136self .svc .config_effort = "low" 137self .svc .shared = {"reasoning_effort" :"high" } 138for path ,body in (("/v1/chat/completions" , {"messages" :MSG }), ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 }), 139 ("/v1/responses" , {"input" :"hello" })): 140with self .subTest (path = path ): 141self .assertEqual (self .level (path ,body ),"xhigh" ) 142self .svc .shared = {"reasoning_effort" :"none" } 143for path ,body in (("/v1/chat/completions" , {"messages" :MSG }), ("/v1/messages" , {"messages" :MSG ,"max_tokens" :50 }), 144 ("/v1/responses" , {"input" :"hello" })): 145with self .subTest (path = path ,shared = "none" ): 146self .assertEqual (self .level (path ,body ),"none" ) 147 148def test_the_web_apps_request_shape (self ): 149# serve/web/app.js: {model, messages, stream, reasoning_effort: settings.thinking} with none / low / medium / high 150for given ,want in (("none" ,"none" ), ("low" ,"low" ), ("medium" ,"medium" ), ("high" ,"xhigh" )): 151with self .subTest (given = given ): 152self .assertEqual (self .level ("/v1/chat/completions" , {"model" :"m" ,"messages" :MSG ,"stream" :True , 153"temperature" :0.6 ,"top_p" :0.95 ,"top_k" :20 , 154"reasoning_effort" :given }),want ) 155 156def test_tools_and_a_system_message_keep_the_level (self ): 157tools = [{"type" :"function" ,"function" : {"name" :"f" ,"description" :"d" , 158"parameters" : {"type" :"object" ,"properties" : {}}}}] 159body = {"messages" : [{"role" :"system" ,"content" :"be brief" }]+ MSG ,"tools" :tools } 160self .assertEqual (self .level ("/v1/chat/completions" , {** body ,"reasoning_effort" :"low" }),"low" ) 161self .assertEqual (self .level ("/v1/chat/completions" , {** body ,"reasoning_effort" :"high" }),"xhigh" ) 162self .assertEqual (self .level ("/v1/chat/completions" , {** body ,"reasoning_effort" :"none" }),"none" ) 163 164def test_the_level_follows_a_conversation (self ): 165history = MSG + [{"role" :"assistant" ,"content" :"hi" ,"reasoning_content" :"greet" }, 166 {"role" :"user" ,"content" :"and now?" }] 167for given ,want in (("low" ,"low" ), ("medium" ,"medium" ), ("none" ,"none" )): 168with self .subTest (given = given ): 169self .assertEqual (self .level ("/v1/chat/completions" , {"messages" :history ,"reasoning_effort" :given }),want ) 170 171 172def test_the_agents_request_shape_with_thinking_off (self ): 173"""The app's agent (Harness) sends no reasoning_effort when Thinking is Off, only chat_template_kwargs {enable_thinking: false}, with tools, a 174system message, earlier reasoning and a token cap. That one flag must reach the template as the empty think block, whatever else is in the 175request or configured (a thinking model that went on reasoning with the menu on Off was this request form). The same body with 176enable_thinking true / an effort keeps thinking.""" 177tools = [{"type" :"function" ,"function" : {"name" :"pwsh" ,"description" :"d" ,"parameters" : {"type" :"object" ,"properties" : {}}}}] 178history = [{"role" :"developer" ,"content" :"You are a helpful assistant." }, {"role" :"user" ,"content" :"first" }, 179 {"role" :"assistant" ,"content" :"ok" ,"reasoning_content" :"earlier thoughts" }, {"role" :"user" ,"content" :"now?" }] 180agent = {"model" :"m" ,"messages" :history ,"tools" :tools ,"stream" :True ,"stream_options" : {"include_usage" :True }, 181"max_completion_tokens" :4096 } 182for config in (None ,"high" ,"low" ): 183self .svc .config_effort = config 184with self .subTest (config = config ): 185self .assertEqual (self .level ("/v1/chat/completions" , {** agent ,"chat_template_kwargs" : {"enable_thinking" :False }}),"none" ) 186self .svc .config_effort = None 187self .assertEqual (self .level ("/v1/chat/completions" , {** agent ,"chat_template_kwargs" : {"enable_thinking" :True ,"reasoning_effort" :"low" }}),"low" ) 188self .assertEqual (self .level ("/v1/chat/completions" , {** agent ,"chat_template_kwargs" : {"enable_thinking" :True ,"reasoning_effort" :"high" }}),"xhigh" ) 189# the same flag in the plain (no tools) body the direct chat sends alongside a budget 190self .assertEqual (self .level ("/v1/chat/completions" , {"messages" :MSG ,"chat_template_kwargs" : {"enable_thinking" :False },"reasoning_effort" :"none" }),"none" ) 191 192 193if __name__ == "__main__" : 194unittest .main ()