#278 follow-up: an Anthropic request that doesn't ask for thinking renders without it only with "anthropic_thinking": "on_request" in the config - the default thinks as 0.1.31 did (a client that never asks would otherwise lose the thinking on every turn); docs
This commit is contained in:
1 parent
e12ecf6ac8
commit
3b7162131d
4 files changed
+22
-7
No files matched your search
@@ -424,6 +424,10 @@ print(r.choices[0].message.content)
|
||||
part of the thinking the client sees and counts as output tokens. `"reasoning_budget_tokens": N` in
|
||||
`strata-<model>.json` sets it for every request; a request's own value wins, and `0` means no budget. Off by default;
|
||||
Anthropic's `"thinking": {"budget_tokens": N}` still only chooses the level, as above.
|
||||
- **Anthropic requests that don't ask for thinking (opt-in, 0.1.32, #278).** By default a `/v1/messages` request
|
||||
with no `"thinking"`, effort or budget thinks as the model's template does. `"anthropic_thinking": "on_request"` in
|
||||
`strata-<model>.json` renders such a request without thinking - Anthropic's own rule, and what Claude Code's short
|
||||
helper calls (a session title in a few dozen tokens) need; its real turns ask for thinking when it is on there.
|
||||
- **Streaming.** With `"stream": true` everything arrives as it is made: the thinking, the answer, and tool calls
|
||||
(the tool's name first, then its arguments piece by piece, like OpenAI and Anthropic do). While the model reads a
|
||||
long prompt the stream sends keep-alives, so agents do not time out; the server window prints progress every
|
||||
|
||||
+8
-4
@@ -173,8 +173,10 @@ def openai_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dict]:
|
||||
return _late_system_to_user(messages), tools, kwargs
|
||||
|
||||
|
||||
def anthropic_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dict]:
|
||||
"""Anthropic Messages -> (template messages, template tools, template kwargs)."""
|
||||
def anthropic_to_messages(req: dict, think_unasked: bool = True) -> tuple[list[dict], list[dict] | None, dict]:
|
||||
"""Anthropic Messages -> (template messages, template tools, template kwargs). `think_unasked`: a request
|
||||
without "thinking", an effort or a budget gets the template's default (it thinks), as through 0.1.31; False
|
||||
renders it without thinking (#278, the config's "anthropic_thinking": "on_request")."""
|
||||
messages = []
|
||||
system = req.get("system")
|
||||
if system:
|
||||
@@ -219,8 +221,10 @@ 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 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
|
||||
elif thinking is None and not req.get("reasoning_budget_tokens") and not think_unasked:
|
||||
# Opt-in (the config's "anthropic_thinking": "on_request"; the default thinks as 0.1.31 did, since a
|
||||
# client that never asks would otherwise lose the thinking on every turn). Anthropic's thinking is
|
||||
# opt-in there. 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
|
||||
# request that gives its own reasoning_budget_tokens (#123) asks for thinking, so it thinks as before.
|
||||
|
||||
+7
-2
@@ -730,6 +730,7 @@ class Service:
|
||||
self.min_free_vram_mib = 0
|
||||
self.before_load = None
|
||||
self.reasoning_budget_tokens = 0 # #123: the config's default thinking budget (0: none)
|
||||
self.anthropic_think_unasked = True # #278: "anthropic_thinking": "on_request" -> False
|
||||
self.stop_ids = set(tokenizer.encode(IM_END, parse_special=True) +
|
||||
tokenizer.encode("<|endoftext|>", parse_special=True))
|
||||
|
||||
@@ -2063,14 +2064,14 @@ def make_handler(svc: Service):
|
||||
"""Anthropic's token count, which Claude Code asks for its context figures: the prompt this server would
|
||||
read for the same request, rendered and tokenized - the model does not run."""
|
||||
req = svc.with_shared(req, "anthropic")
|
||||
messages, tools, kw = anthropic_to_messages(req)
|
||||
messages, tools, kw = anthropic_to_messages(req, svc.anthropic_think_unasked)
|
||||
prompt = svc.template.render(messages, tools=tools, **kw)
|
||||
self._json(200, {"input_tokens": len(svc.tok.encode(prompt, parse_special=True))})
|
||||
|
||||
def _anthropic(self, req):
|
||||
svc.load()
|
||||
req = svc.with_shared(req, "anthropic")
|
||||
messages, tools, kw = anthropic_to_messages(req)
|
||||
messages, tools, kw = anthropic_to_messages(req, svc.anthropic_think_unasked)
|
||||
max_new = int(req.get("max_tokens") or 0) # 0/-1: the rest of the context
|
||||
svc.reasoning_budget(req) # a bad value is a 400 before anything is sent
|
||||
ids, thinking, max_new = svc.prepare(messages, tools, kw, max_new)
|
||||
@@ -2405,6 +2406,10 @@ def main() -> int:
|
||||
svc.min_free_vram_mib = a.min_free_vram_mib if a.min_free_vram_mib is not None else \
|
||||
int(cfg.get("min_free_vram_mib") or 0)
|
||||
svc.before_load = a.before_load or cfg.get("before_load") or None
|
||||
mode = str(cfg.get("anthropic_thinking") or "model") # #278: "on_request" = only when the request asks
|
||||
if mode not in ("model", "on_request"):
|
||||
raise SystemExit(f"[strata] config anthropic_thinking must be \"model\" or \"on_request\", not {mode!r}")
|
||||
svc.anthropic_think_unasked = mode == "model"
|
||||
if cfg.get("reasoning_budget_tokens") is not None: # #123: a default thinking budget for every request
|
||||
try:
|
||||
svc.reasoning_budget_tokens = cfg["reasoning_budget_tokens"]
|
||||
|
||||
@@ -69,7 +69,9 @@ class MaxTokens(unittest.TestCase):
|
||||
# #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
|
||||
kw = lambda **r: anthropic_to_messages({"messages": msgs, **r}, think_unasked=False)[2] # noqa: E731
|
||||
# the default ("anthropic_thinking": "model") renders an unasked request as 0.1.31 did
|
||||
self.assertNotIn("enable_thinking", anthropic_to_messages({"messages": msgs})[2])
|
||||
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}))
|
||||
|
||||
Reference in new issue
Block a user