serve: the thinking budget can be shared with other apps through POST /settings (#1035)
reasoning_budget_tokens joins the shared Chat settings (0 means no budget, a whole number of tokens otherwise) and is read after the shared and config defaults are merged, as max_tokens is. Rebased onto the 0.1.40.1 Service.run (force argument).
This commit is contained in:
1 parent
e2d9a4bd7f
commit
2de5fa0a84
2 files changed
+25
-2
No files matched your search
+8
-2
@@ -2878,11 +2878,12 @@ class Service:
|
||||
def run(self, ids, thinking, tools, max_new, sampling, cancel, force=None) -> Iterator[tuple[str, object]]:
|
||||
"""Yields ("event", Event) as text arrives, then ("done", {"finish": .., "completion_tokens": ..}).
|
||||
`force` (forced_call): the opening of the call the reply must make - see prepare()."""
|
||||
budget = self.reasoning_budget(sampling) if thinking else None # #123: opt-in, off by default
|
||||
defaults = {**self.sampling_defaults, **self.shared} # the config's, then the Chat settings shared with apps
|
||||
if defaults: # the request's own fields win (explicit 0 stays greedy)
|
||||
req_values = {k: v for k, v in (sampling or {}).items() if v is not None}
|
||||
sampling = {**defaults, **req_values}
|
||||
# #123: read after the merge, so a budget shared through POST /settings is seen like the other keys
|
||||
budget = self.reasoning_budget(sampling) if thinking else None
|
||||
parser = OutputParser(thinking=thinking, tools=tools, stream_tools=True)
|
||||
detok, n, finish = Detokenizer(self.tok), 0, "length"
|
||||
run_tok, run_len, repeated = None, 0, False # #606: the current run of one repeated token
|
||||
@@ -4771,7 +4772,8 @@ def origins_of(value, key: str, wildcard: bool) -> list[str]:
|
||||
return out
|
||||
|
||||
|
||||
SHARED_KEYS = ("reasoning_effort", "temperature", "top_p", "top_k", "seed", "max_tokens", "experimental_speed_projection")
|
||||
SHARED_KEYS = ("reasoning_effort", "reasoning_budget_tokens", "temperature", "top_p", "top_k", "seed", "max_tokens",
|
||||
"experimental_speed_projection")
|
||||
|
||||
|
||||
def clean_shared_defaults(d) -> dict:
|
||||
@@ -4802,6 +4804,10 @@ def clean_shared_defaults(d) -> dict:
|
||||
if not number or value != int(value) or value <= 0:
|
||||
raise ValueError(f"{key}: a positive integer")
|
||||
value = int(value)
|
||||
elif key == "reasoning_budget_tokens": # #123: 0 means no budget, so 0 is allowed here
|
||||
if not number or value != int(value) or value < 0:
|
||||
raise ValueError("reasoning_budget_tokens: a whole number of tokens (0: no budget)")
|
||||
value = int(value)
|
||||
elif key == "experimental_speed_projection":
|
||||
if not isinstance(value, bool):
|
||||
raise ValueError("experimental_speed_projection: true or false")
|
||||
|
||||
@@ -2803,6 +2803,23 @@ class ThinkingBudget(unittest.TestCase):
|
||||
self.assertEqual(b["choices"][0]["message"]["reasoning_content"], ThinkingEngine.THOUGHT)
|
||||
self.assertEqual(len(self.engine.prompts), 3)
|
||||
|
||||
def test_a_shared_budget_reaches_a_request_that_sets_none(self):
|
||||
"""The Chat settings shared with other apps may carry a thinking budget too, like max_tokens and the effort."""
|
||||
self.svc.set_shared({"reasoning_budget_tokens": 20})
|
||||
code, b = self.openai() # a client that asks for no budget of its own
|
||||
self.assertEqual(code, 200, b)
|
||||
self.assertEqual(len(self.engine.prompts), 2)
|
||||
self.assertTrue(b["choices"][0]["message"]["reasoning_content"].startswith(ThinkingEngine.THOUGHT[:20] + "\n"))
|
||||
code, b = self.openai(reasoning_budget_tokens=0) # its own 0 still turns it off
|
||||
self.assertEqual(b["choices"][0]["message"]["reasoning_content"], ThinkingEngine.THOUGHT)
|
||||
self.assertEqual(len(self.engine.prompts), 3)
|
||||
|
||||
def test_a_shared_budget_must_be_a_whole_number_of_tokens(self):
|
||||
for bad in (-1, 1.5, "20"):
|
||||
with self.assertRaises(ValueError, msg=repr(bad)):
|
||||
self.svc.set_shared({"reasoning_budget_tokens": bad})
|
||||
self.assertEqual(self.svc.set_shared({"reasoning_budget_tokens": 0}), {"reasoning_budget_tokens": 0})
|
||||
|
||||
def test_without_thinking_there_is_nothing_to_limit(self):
|
||||
self.engine.THOUGHT = ""
|
||||
code, b = self.openai(reasoning_budget_tokens=5, reasoning_effort="none")
|
||||
|
||||
Reference in new issue
Block a user