#278 follow-up: an Anthropic request with its own reasoning_budget_tokens still thinks
"thinking" absent meant no thinking, also for a request that gives reasoning_budget_tokens (#123), which asks for thinking with a cap (serve.test_server's test_anthropic_stream failed: no thinking at all). Such a request renders as before. test_anthropic_thinks_only_when_asked pins the five cases.
This commit is contained in:
1 parent
6cd29d9850
commit
ca85ebdee2
2 files changed
+14
-2
No files matched your search
+3
-2
@@ -219,10 +219,11 @@ def anthropic_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dic
|
||||
kwargs.update(effort_kwargs(effort))
|
||||
elif isinstance(thinking, dict) and thinking.get("budget_tokens"):
|
||||
kwargs.update(budget_effort(thinking["budget_tokens"]))
|
||||
elif thinking is None:
|
||||
elif thinking is None and not req.get("reasoning_budget_tokens"):
|
||||
# Anthropic's thinking is opt-in. Claude Code's helper calls (a session title, a topic check) ask for none
|
||||
# and allow a few dozen tokens, which the model otherwise spent thinking and answered with no text at all.
|
||||
# A config's reasoning_effort still applies: Service.with_shared sets output_config before this runs.
|
||||
# A config's reasoning_effort still applies: Service.with_shared sets output_config before this runs. A
|
||||
# request that gives its own reasoning_budget_tokens (#123) asks for thinking, so it thinks as before.
|
||||
kwargs["enable_thinking"] = False
|
||||
return _late_system_to_user(messages), tools, kwargs
|
||||
|
||||
|
||||
@@ -65,6 +65,17 @@ class MaxTokens(unittest.TestCase):
|
||||
u = b.get("usage", {})
|
||||
return s, b, u.get("input_tokens"), u.get("output_tokens")
|
||||
|
||||
def test_anthropic_thinks_only_when_asked(self):
|
||||
# #278: Anthropic's thinking is opt-in; "thinking", an effort or a reasoning_budget_tokens (#123) asks for it
|
||||
from serve.frontend import anthropic_to_messages
|
||||
msgs = [{"role": "user", "content": "u"}]
|
||||
kw = lambda **r: anthropic_to_messages({"messages": msgs, **r})[2] # noqa: E731
|
||||
self.assertEqual(kw(), {"enable_thinking": False})
|
||||
self.assertEqual(kw(thinking={"type": "disabled"}), {"enable_thinking": False})
|
||||
self.assertNotIn("enable_thinking", kw(thinking={"type": "enabled", "budget_tokens": 2048}))
|
||||
self.assertNotIn("enable_thinking", kw(output_config={"effort": "high"}))
|
||||
self.assertNotIn("enable_thinking", kw(reasoning_budget_tokens=30))
|
||||
|
||||
def test_count_tokens_is_the_prompt_messages_reads(self):
|
||||
# /v1/messages/count_tokens renders and tokenizes the same prompt /v1/messages would read, without running it
|
||||
msgs = [{"role": "user", "content": "how many tokens is this?"}]
|
||||
|
||||
Reference in new issue
Block a user