The live smoke showed the same quoted-example problem in the answer: a model that wrote the call format in a fenced example after </think> got a real tool call. The content parser now uses the fence / inline-code tracking of the reasoning rescue: an opener inside a ``` or ~~~ fence (tracked across stream chunks) or inside inline code stays text, the decision depending only on the text before it, so streamed and whole outputs agree. Openers at the top level fire as before, including one after a sentence on the same line (none of the 37 corpus specimens needed that, but the old tests do and nothing argues against it). A closed call at the end of an answer cut by max tokens stays a call. Tests: quoted calls stay text and real ones fire at every chunk split; OpenAI (whole and streamed), Anthropic and Responses over HTTP.
947 lines
47 KiB
Python
947 lines
47 KiB
Python
"""serve/frontend.py - plan v0.3 P8: the request/response layer between API clients and the engine.
|
|
|
|
Everything here is text: no model, no GPU. The engine consumes token ids and produces text deltas; this module
|
|
|
|
* normalizes OpenAI Chat Completions and Anthropic Messages requests into the chat template's message form,
|
|
* renders the model's own Jinja chat template (pack `tokenizer/chat_template.jinja`) exactly as Hugging Face
|
|
does (checked against the pack's `chat_golden.json`, 10 cases incl. thinking and tools),
|
|
* parses the streamed output incrementally into reasoning (`<think>...</think>`), content and tool calls in
|
|
the template's XML form (`<tool_call><function=NAME><parameter=P>VALUE</parameter>...</function></tool_call>`),
|
|
never emitting a partial tag to the client.
|
|
|
|
Design note: plan v0.3 describes a single C++ process. The frontend is Python first because the template is Jinja
|
|
and the formats change often; the engine boundary is token ids in, text deltas out, which keeps a later C++ port
|
|
(or a pybind wrapper) a local change.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import uuid
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
|
|
import jinja2
|
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ template
|
|
class TemplateRequestError(jinja2.exceptions.TemplateError, ValueError):
|
|
"""The template refused the request's messages (e.g. "No user query found in messages."): a ValueError, so the
|
|
client gets a 400 with the template's message instead of a dropped connection (#365)."""
|
|
|
|
|
|
class ChatTemplate:
|
|
"""The model's chat template, rendered with the same Jinja settings as transformers' apply_chat_template."""
|
|
|
|
def __init__(self, path: str | Path):
|
|
def raise_exception(message):
|
|
raise TemplateRequestError(message)
|
|
|
|
def tojson(x, ensure_ascii=False, indent=None, separators=None, sort_keys=False):
|
|
return json.dumps(x, ensure_ascii=ensure_ascii, indent=indent, separators=separators, sort_keys=sort_keys)
|
|
|
|
env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True, extensions=["jinja2.ext.loopcontrols"])
|
|
env.filters["tojson"] = tojson
|
|
env.globals["raise_exception"] = raise_exception
|
|
self.source = Path(path).read_text(encoding="utf-8")
|
|
self.template = env.from_string(self.source)
|
|
|
|
def render(self, messages: list[dict], tools: list[dict] | None = None, add_generation_prompt: bool = True,
|
|
**kwargs) -> str:
|
|
# Do not use completed empty assistant turns as examples for the next reply. Keep the final message.
|
|
# (#843. STRATA_KEEP_EMPTY_TURNS=1 renders them as before.)
|
|
messages = [m for i, m in enumerate(messages)
|
|
if i == len(messages) - 1 or os.environ.get("STRATA_KEEP_EMPTY_TURNS") == "1" or not (isinstance(m, dict) and m.get("role") == "assistant"
|
|
and not _text_of(m.get("content")).strip()
|
|
and not _has_image(m.get("content")) and not m.get("tool_calls"))]
|
|
return self.template.render(messages=messages, tools=tools, add_generation_prompt=add_generation_prompt,
|
|
**kwargs)
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ requests
|
|
def _text_of(content) -> str:
|
|
if content is None:
|
|
return ""
|
|
if isinstance(content, str):
|
|
return content
|
|
return "".join(part.get("text", "") for part in content if isinstance(part, dict) and part.get("type") in
|
|
("text", "input_text", None))
|
|
|
|
|
|
IMAGE_PARTS = ("image_url", "input_image", "image")
|
|
|
|
# Thinking levels. The model's template knows low, medium and xhigh (its default; "high" means xhigh), and
|
|
# enable_thinking=false for none. Clients spell these many ways; everything maps onto those four.
|
|
EFFORT = {"none": None, "off": None, "minimal": None, "disabled": None, "false": None,
|
|
"low": "low", "medium": "medium", "high": "xhigh", "xhigh": "xhigh", "max": "xhigh", "maximum": "xhigh"}
|
|
|
|
|
|
def effort_kwargs(value) -> dict:
|
|
"""A reasoning effort as given by a client -> the template's kwargs. Unknown values are a 400, not a crash."""
|
|
if value is None or value == "":
|
|
return {}
|
|
if value is False:
|
|
return {"enable_thinking": False}
|
|
key = str(value).strip().lower()
|
|
if key not in EFFORT:
|
|
raise ValueError(f"unknown reasoning effort {value!r}: use none, low, medium or high")
|
|
level = EFFORT[key]
|
|
return {"enable_thinking": False} if level is None else {"reasoning_effort": level}
|
|
|
|
|
|
def budget_effort(tokens) -> dict:
|
|
"""Anthropic's thinking budget (budget_tokens) -> a level: under 2K low, under 8K medium, else high."""
|
|
try:
|
|
n = int(tokens)
|
|
except (TypeError, ValueError):
|
|
return {}
|
|
return {"reasoning_effort": "low" if n < 2048 else "medium" if n < 8192 else "xhigh"}
|
|
|
|
|
|
def _has_image(content) -> bool:
|
|
return isinstance(content, list) and any(isinstance(p, dict) and p.get("type") in IMAGE_PARTS for p in content)
|
|
|
|
|
|
def _image_source(part: dict) -> str:
|
|
"""An image part's source as one string: a data: URL, an http(s) URL or a local file path.
|
|
OpenAI: {"type": "image_url", "image_url": {"url": ...}} (or "image_url": "..."), Responses-style
|
|
{"type": "input_image", "image_url": ...}; Anthropic: {"type": "image", "source": {"type": "base64",
|
|
"media_type": ..., "data": ...}} or {"source": {"type": "url", "url": ...}}."""
|
|
if part.get("type") == "image":
|
|
src = part.get("source") or {}
|
|
if src.get("type") == "base64":
|
|
return f"data:{src.get('media_type', 'image/png')};base64,{src.get('data', '')}"
|
|
return src.get("url") or src.get("path") or ""
|
|
url = part.get("image_url")
|
|
if isinstance(url, dict):
|
|
url = url.get("url")
|
|
return url or ""
|
|
|
|
|
|
def _parts_of(content):
|
|
"""Message content for the template: a string when there is no image (unchanged behaviour), otherwise the
|
|
template's list form - text items and image items, in order - whose image items carry their source."""
|
|
if not _has_image(content):
|
|
return _text_of(content)
|
|
items = []
|
|
for part in content:
|
|
if not isinstance(part, dict):
|
|
continue
|
|
if part.get("type") in IMAGE_PARTS:
|
|
items.append({"type": "image", "source": _image_source(part)})
|
|
elif part.get("type") in ("text", "input_text", None) and "text" in part:
|
|
items.append({"type": "text", "text": part.get("text", "")})
|
|
return items
|
|
|
|
|
|
def images_of(messages: list[dict]) -> list[str]:
|
|
"""The image sources of the rendered conversation, in prompt order (the template renders one
|
|
<|vision_start|><|image_pad|><|vision_end|> per image item, message by message)."""
|
|
return [item["source"] for m in messages if isinstance(m.get("content"), list)
|
|
for item in m["content"] if item.get("type") == "image"]
|
|
|
|
|
|
# #537: a literal <think> / </think> inside a message's text is plain text, not the model's reasoning markers. The
|
|
# tokenizer matches those two strings as their special tokens everywhere (GGUF token type 4, as llama.cpp does), so
|
|
# a user quoting "</think>" used to hand the model a real end-of-reasoning token. Before the template is rendered
|
|
# they are swapped for these private-use characters, and the server encodes the spans they mark as ordinary text.
|
|
THINK_TAGS = {"<think>": "\U000F0E01", "</think>": "\U000F0E02"}
|
|
# #554: the vision markers the same way. They are special tokens only where the template writes them for an image
|
|
# item; the same strings in a message's text (an agent reading chat_template.jinja, a tool result quoting it) became
|
|
# the same special ids, so text with <|vision_start|><|image_pad|> before a picture took that picture's embeddings
|
|
VISION_TAGS = {"<|vision_start|>": "\U000F0E03", "<|image_pad|>": "\U000F0E04", "<|vision_end|>": "\U000F0E05",
|
|
"<|video_pad|>": "\U000F0E06"}
|
|
LITERAL_TAGS = {**THINK_TAGS, **VISION_TAGS}
|
|
THINK_MARKS = {v: k for k, v in LITERAL_TAGS.items()}
|
|
CONTROL_MARK0 = 0xF0E10 # the control tokens' marks start here, clear of the two sets above
|
|
|
|
|
|
def literal_tags(controls) -> dict[str, str]:
|
|
"""LITERAL_TAGS plus a mark for each control token's text (`controls`: the tokenizer's CONTROL literals,
|
|
<|im_start|>, <|im_end|>, <|endoftext|>, ...). The rendered prompt is encoded with those literals parsed, so one
|
|
written inside a message - a file an agent reads, a pasted chat template - opened or ended a turn there. Longest
|
|
first, as the tokenizer matches them: a literal inside a longer one is not marked before it."""
|
|
tags = {**LITERAL_TAGS, **{c: chr(CONTROL_MARK0 + k) for k, c in enumerate(c for c in controls
|
|
if c not in LITERAL_TAGS)}}
|
|
return dict(sorted(tags.items(), key=lambda t: -len(t[0])))
|
|
|
|
|
|
def _mark(text: str, tags: dict[str, str] = LITERAL_TAGS) -> str:
|
|
for tag, mark in tags.items():
|
|
text = text.replace(tag, mark)
|
|
return text
|
|
|
|
|
|
def _mark_deep(v, tags: dict[str, str] = LITERAL_TAGS):
|
|
if isinstance(v, str):
|
|
return _mark(v, tags)
|
|
if isinstance(v, dict):
|
|
return {k: _mark_deep(x, tags) for k, x in v.items()}
|
|
if isinstance(v, list):
|
|
return [_mark_deep(x, tags) for x in v]
|
|
return v
|
|
|
|
|
|
def _has_tag(v, tags: dict[str, str] = LITERAL_TAGS) -> bool:
|
|
if isinstance(v, str):
|
|
return any(tag in v for tag in tags)
|
|
if isinstance(v, dict):
|
|
return any(_has_tag(x, tags) for x in v.values())
|
|
if isinstance(v, list):
|
|
return any(_has_tag(x, tags) for x in v)
|
|
return False
|
|
|
|
|
|
def mark_think_literals(messages: list[dict], tools: list[dict] | None, tags: dict[str, str] = LITERAL_TAGS):
|
|
"""#537: (messages, tools) with every literal <think> / </think> (#554: and vision marker) in their text swapped for
|
|
LITERAL_TAGS' marks (or `tags`': literal_tags() adds the control tokens' texts), and
|
|
whether there was one (None: no change, the same objects back - a prompt without them renders as it always did).
|
|
An assistant message whose content opens with a whole <think>...</think> block (clients that send the reasoning
|
|
inline) keeps that one block as the model's markers, as before."""
|
|
if not _has_tag(messages, tags) and not _has_tag(tools, tags):
|
|
return messages, tools, False
|
|
out = []
|
|
for m in messages:
|
|
m = dict(m)
|
|
content = m.get("content")
|
|
for k, v in m.items():
|
|
if k != "role":
|
|
m[k] = _mark_deep(v, tags)
|
|
if m.get("role") == "assistant" and isinstance(content, str) and content.lstrip().startswith("<think>") \
|
|
and "</think>" in content:
|
|
i, j = content.index("<think>") + len("<think>"), content.index("</think>")
|
|
m["content"] = (content[:i] + _mark(content[i:j], tags) + "</think>" +
|
|
_mark(content[j + len("</think>"):], tags))
|
|
out.append(m)
|
|
return out, _mark_deep(tools, tags), True
|
|
|
|
|
|
_THINK_MARK_RE = re.compile("|".join(THINK_MARKS))
|
|
|
|
|
|
def unmark_think_literals(prompt: str, tags: dict[str, str] = LITERAL_TAGS) -> tuple[str, list[tuple[int, int]]]:
|
|
"""The rendered prompt with THINK_TAGS' marks turned back into the tags' text, and the (start, end) spans of those
|
|
tags in it: the server encodes them as ordinary text (the tokenizer's encode_plain_spans)."""
|
|
marks = THINK_MARKS if tags is LITERAL_TAGS else {v: k for k, v in tags.items()}
|
|
mark_re = _THINK_MARK_RE if tags is LITERAL_TAGS else re.compile("|".join(map(re.escape, marks)))
|
|
out, spans, pos, n = [], [], 0, 0
|
|
for m in mark_re.finditer(prompt):
|
|
out.append(prompt[pos:m.start()])
|
|
n += m.start() - pos
|
|
tag = marks[m.group(0)]
|
|
out.append(tag)
|
|
spans.append((n, n + len(tag)))
|
|
n += len(tag)
|
|
pos = m.end()
|
|
out.append(prompt[pos:])
|
|
return "".join(out), spans
|
|
|
|
|
|
def _late_system_to_user(messages: list[dict]) -> list[dict]:
|
|
"""The chat template takes a system message only at the start ("System message must be at the beginning").
|
|
Clients also send them mid-conversation - Claude Code's hook context as {"role": "system"} after the first user
|
|
turn, some OpenAI clients a late "developer" message (issue #56) - so those become user messages, in place:
|
|
merging them into the first one would change the prompt's start and cost the conversation cache every turn."""
|
|
return [dict(m, role="user") if m.get("role") == "system" and i > 0 else m for i, m in enumerate(messages)]
|
|
|
|
|
|
def _object_list(value, name: str) -> list[dict]:
|
|
"""#460: a request's "messages" (or a message's "tool_calls") as a list of objects. Some clients send the array
|
|
double-encoded, as a JSON string, which used to be iterated character by character and crashed on m.get: such a
|
|
string is decoded. Anything that is still not a list of objects is a ValueError, which the server answers with
|
|
a 400 naming the field. None (or no field) is an empty list, as a missing field always was."""
|
|
if value is None:
|
|
return []
|
|
if isinstance(value, str):
|
|
try:
|
|
value = json.loads(value)
|
|
except ValueError:
|
|
raise ValueError(f"{name} must be a list of objects (a string was sent that is not JSON)") from None
|
|
if not isinstance(value, list) or not all(isinstance(m, dict) for m in value):
|
|
raise ValueError(f"{name} must be a list of objects")
|
|
return value
|
|
|
|
|
|
def _tool_list(value, wrapper: str | None) -> list[dict]:
|
|
"""#592: a request's "tools" as a list of tool objects, each with a name - in the OpenAI shape
|
|
{"type": "function", "function": {"name": ...}} (`wrapper` "function"; a bare {"name": ...} is still taken), or
|
|
in the Anthropic shape {"name": ...} (`wrapper` None). A value that is not (a string such as "auto", a list of
|
|
names, an object without a name) is a ValueError - a 400 naming the field - where it used to take the request
|
|
thread down with no reply at all. No value (or an empty one) is no tools, as always."""
|
|
if not value:
|
|
return []
|
|
shape = ('{"type": "function", "function": {"name": ..., "parameters": {...}}}' if wrapper else
|
|
'{"name": ..., "input_schema": {...}}')
|
|
try:
|
|
tools = _object_list(value, "tools")
|
|
except ValueError:
|
|
raise ValueError(f"tools must be a list of tool objects, each {shape}") from None
|
|
for i, t in enumerate(tools):
|
|
fn = t.get(wrapper, t) if wrapper and t.get("type") == wrapper else t
|
|
if not isinstance(fn, dict) or not isinstance(fn.get("name"), str) or not fn["name"]:
|
|
raise ValueError(f"tools[{i}] has no name: each tool must be {shape}")
|
|
return tools
|
|
|
|
|
|
def tool_arguments(raw) -> dict:
|
|
"""A call's arguments from a client's history -> the mapping the template renders (both APIs). A JSON object
|
|
(as text or already parsed) is used as it is; empty is {}; anything else (cut-off or malformed JSON, a list, a
|
|
number) is kept as text under "arguments" instead of refusing the whole conversation (#510)."""
|
|
if raw is None or (isinstance(raw, str) and not raw.strip()):
|
|
return {}
|
|
if isinstance(raw, dict):
|
|
return raw
|
|
if isinstance(raw, str):
|
|
try:
|
|
value = json.loads(raw)
|
|
except ValueError:
|
|
return {"arguments": raw}
|
|
return value if isinstance(value, dict) else {"arguments": raw}
|
|
return {"arguments": json.dumps(raw, ensure_ascii=False)}
|
|
|
|
|
|
def openai_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dict]:
|
|
"""OpenAI Chat Completions -> (template messages, template tools, template kwargs)."""
|
|
messages = []
|
|
for m in _object_list(req.get("messages"), "messages"):
|
|
role = m.get("role")
|
|
if role == "developer":
|
|
role = "system"
|
|
out = {"role": role, "content": _parts_of(m.get("content")) if role in ("user", "tool", "assistant") else _text_of(m.get("content"))}
|
|
if m.get("reasoning_content"):
|
|
out["reasoning_content"] = m["reasoning_content"]
|
|
if m.get("tool_calls"):
|
|
calls = []
|
|
for c in _object_list(m["tool_calls"], "tool_calls"):
|
|
fn = c.get("function", c)
|
|
if not isinstance(fn, dict):
|
|
raise ValueError("tool_calls must be a list of objects (each with a \"function\" object)")
|
|
calls.append({"function": {"name": fn.get("name"), "arguments": tool_arguments(fn.get("arguments"))}})
|
|
out["tool_calls"] = calls
|
|
messages.append(out)
|
|
tools = [t.get("function", t) if t.get("type") == "function" else t
|
|
for t in _tool_list(req.get("tools"), "function")] or None
|
|
kwargs = {}
|
|
# OpenAI Chat Completions: "reasoning_effort"; Responses style: "reasoning": {"effort": ...}
|
|
reasoning = req.get("reasoning") if isinstance(req.get("reasoning"), dict) else {}
|
|
kwargs.update(effort_kwargs(req.get("reasoning_effort") or reasoning.get("effort")))
|
|
# the vLLM / llama.cpp convention: {"chat_template_kwargs": {"enable_thinking": false, "reasoning_effort": "low"}}
|
|
for k, v in (req.get("chat_template_kwargs") or {}).items():
|
|
if k == "enable_thinking" and not v:
|
|
kwargs = {"enable_thinking": False}
|
|
elif k == "reasoning_effort" and "enable_thinking" not in kwargs:
|
|
kwargs.update(effort_kwargs(v))
|
|
return _late_system_to_user(messages), tools, kwargs
|
|
|
|
|
|
BILLING_HEADER = "x-anthropic-billing-header:"
|
|
|
|
|
|
def pin_billing_stamp(system: str) -> str:
|
|
"""Claude Code starts its system prompt with `x-anthropic-billing-header: cc_version=2.1.170.bf4;
|
|
cc_entrypoint=sdk-cli; cch=b145e;`. cch changes on EVERY request and the version's 4th part on every session, so
|
|
the prompt changed ~22K tokens in (after the tool list) on every turn and the conversation cache could only reuse
|
|
up to its last 16K checkpoint: half of every agent prompt was read again. Both stamps are pinned to f's, as
|
|
llama.cpp does (ggml-org/llama.cpp#21793); only a header at the very start of the system text is touched, and
|
|
only inside its first 160 characters."""
|
|
if not system.startswith(BILLING_HEADER):
|
|
return system
|
|
s = list(system)
|
|
cch = system.find("cch=", len(BILLING_HEADER))
|
|
if 0 <= cch <= 160:
|
|
v, end = cch + 4, system.find(";", cch + 4)
|
|
if end > v and end - v <= 16:
|
|
s[v:end] = "f" * (end - v)
|
|
cv = system.find("cc_version=")
|
|
if 0 <= cv <= 160:
|
|
v, end = cv + len("cc_version="), system.find(";", cv)
|
|
if v < end and end - v <= 64:
|
|
parts = system[v:end].split(".")
|
|
if len(parts) > 3:
|
|
tail = v + len(".".join(parts[:3])) + 1
|
|
s[tail:end] = "f" * (end - tail)
|
|
return "".join(s)
|
|
|
|
|
|
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:
|
|
messages.append({"role": "system", "content": pin_billing_stamp(_text_of(system))})
|
|
for m in _object_list(req.get("messages"), "messages"):
|
|
content = m.get("content")
|
|
if isinstance(content, str):
|
|
messages.append({"role": m["role"], "content": content})
|
|
continue
|
|
if m.get("role") == "user" and _has_image(content) and not any(
|
|
isinstance(b, dict) and b.get("type") == "tool_result" for b in content):
|
|
messages.append({"role": "user", "content": _parts_of(content)})
|
|
continue
|
|
text, reasoning, calls, parts = [], [], [], []
|
|
for block in content or []:
|
|
kind = block.get("type")
|
|
if kind == "text":
|
|
text.append(block.get("text", ""))
|
|
parts.append(block)
|
|
elif kind in IMAGE_PARTS:
|
|
parts.append(block)
|
|
elif kind == "thinking":
|
|
reasoning.append(block.get("thinking", ""))
|
|
elif kind == "tool_use":
|
|
calls.append({"function": {"name": block.get("name"), "arguments": tool_arguments(block.get("input"))}})
|
|
elif kind == "tool_result":
|
|
# A tool's image (Claude Code's Read of a picture) reaches the encoder like a user's, as the OpenAI
|
|
# path's tool messages already do; a text-only result is one string, as before.
|
|
messages.append({"role": "tool", "content": _parts_of(block.get("content"))})
|
|
if text or calls or reasoning or parts:
|
|
# an image sent beside tool results stays in this turn instead of being dropped
|
|
out = {"role": m["role"], "content": _parts_of(parts) if _has_image(parts) else "".join(text)}
|
|
if reasoning:
|
|
out["reasoning_content"] = "".join(reasoning)
|
|
if calls:
|
|
out["tool_calls"] = calls
|
|
messages.append(out)
|
|
tools = [{"name": t["name"], "description": t.get("description", ""), "parameters": t.get("input_schema", {})}
|
|
for t in _tool_list(req.get("tools"), None)] or None
|
|
kwargs = {}
|
|
# Anthropic: "thinking": {"type": "disabled"} or {"type": "enabled", "budget_tokens": N};
|
|
# "output_config": {"effort": "low" | "medium" | "high"}
|
|
thinking = req.get("thinking")
|
|
effort = (req.get("output_config") or {}).get("effort") if isinstance(req.get("output_config"), dict) else None
|
|
if isinstance(thinking, dict) and thinking.get("type") == "disabled":
|
|
kwargs["enable_thinking"] = False
|
|
elif effort:
|
|
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") 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.
|
|
kwargs["enable_thinking"] = False
|
|
return _late_system_to_user(messages), tools, kwargs
|
|
|
|
|
|
# ------------------------------------------------------------------------------------------------ output parser
|
|
@dataclass
|
|
class ToolCall:
|
|
name: str
|
|
arguments: dict
|
|
id: str = field(default_factory=lambda: "call_" + uuid.uuid4().hex[:24])
|
|
|
|
|
|
@dataclass
|
|
class Event:
|
|
kind: str # "reasoning" | "content" | "tool_call"
|
|
text: str = ""
|
|
call: ToolCall | None = None
|
|
|
|
|
|
THINK_END = "</think>"
|
|
CALL_START = "<tool_call>"
|
|
CALL_END = "</tool_call>"
|
|
|
|
|
|
PARAM_END = "</parameter>"
|
|
FUNC_START = "<function="
|
|
FUNC_END = "</function>"
|
|
|
|
|
|
def param_end(text: str, final: bool = False) -> int:
|
|
"""Where a parameter value in `text` ends: the first `</parameter>` followed (after whitespace) by the next
|
|
`<parameter=` or `</function>` - the same text inside a value (a file that documents the call format, #210) is
|
|
part of the value. -1: none yet; -2: a candidate whose follower has not arrived (streaming; `final` accepts it)."""
|
|
at = text.find(PARAM_END)
|
|
while at >= 0:
|
|
after = text[at + len(PARAM_END):].lstrip()
|
|
if after.startswith(("<parameter=", FUNC_END)):
|
|
return at
|
|
if not after or "<parameter=".startswith(after) or FUNC_END.startswith(after):
|
|
return at if final else -2
|
|
at = text.find(PARAM_END, at + 1)
|
|
return -1
|
|
|
|
|
|
def call_end(text: str) -> int:
|
|
"""Where a tool call's body ends (#210): the `</tool_call>` after the call's own `</function>`, found by walking
|
|
its parameters with param_end, so a value may contain either tag. -1: not complete yet. A body that is not in
|
|
the call format ends at the first `</tool_call>`, as before."""
|
|
s = text.lstrip()
|
|
pos = len(text) - len(s)
|
|
if not s.startswith("<function="):
|
|
return text.find(CALL_END) if not "<function=".startswith(s) else -1
|
|
gt = text.find(">", pos)
|
|
if gt < 0:
|
|
return -1
|
|
pos = gt + 1
|
|
while True:
|
|
rest = text[pos:]
|
|
s = rest.lstrip()
|
|
pos += len(rest) - len(s)
|
|
if s.startswith("<parameter="):
|
|
gt = text.find(">", pos)
|
|
if gt < 0:
|
|
return -1
|
|
end = param_end(text[gt + 1:])
|
|
if end < 0:
|
|
return -1
|
|
pos = gt + 1 + end + len(PARAM_END)
|
|
elif s.startswith(FUNC_END):
|
|
rest = text[pos + len(FUNC_END):]
|
|
s = rest.lstrip()
|
|
if s.startswith(CALL_END):
|
|
return pos + len(FUNC_END) + len(rest) - len(s)
|
|
return -1 if CALL_END.startswith(s) else text.find(CALL_END, pos)
|
|
elif not s or "<parameter=".startswith(s) or FUNC_END.startswith(s):
|
|
return -1
|
|
else:
|
|
return text.find(CALL_END, pos)
|
|
|
|
|
|
def tool_choice_of(tool_choice) -> tuple[str, str | None]:
|
|
"""A client's `tool_choice` in either API's shape -> ("auto" | "none" | "required" | "named", name). OpenAI:
|
|
"auto" / "none" / "required", {"type": "function", "function": {"name": N}} or the flat {"type": "function",
|
|
"name": N}; Anthropic: {"type": "auto" | "any" | "none"} and {"type": "tool", "name": N}. A value it does not
|
|
know is ("unknown", None)."""
|
|
if tool_choice is None:
|
|
return "auto", None
|
|
if isinstance(tool_choice, str):
|
|
return (tool_choice, None) if tool_choice in ("auto", "none", "required") else ("unknown", None)
|
|
if isinstance(tool_choice, dict):
|
|
kind = tool_choice.get("type")
|
|
if kind in ("auto", "none"):
|
|
return kind, None
|
|
if kind in ("any", "required"):
|
|
return "required", None
|
|
if kind in ("function", "tool"):
|
|
fn = tool_choice.get("function")
|
|
name = (fn.get("name") if isinstance(fn, dict) else None) or tool_choice.get("name")
|
|
if isinstance(name, str) and name:
|
|
return "named", name
|
|
return "unknown", None
|
|
|
|
|
|
def forced_call(tool_choice, tools: list[dict] | None) -> str | None:
|
|
"""`tool_choice` -> the text that opens the call the reply must make, or None (the model decides).
|
|
There is no grammar here: the server writes this opening itself, so the model can only go on with a call.
|
|
"required" / Anthropic "any": any of the tools; a named function: that one. A value it cannot honour (an
|
|
unknown shape, a name that is not one of the tools, "required" with no tools) is logged and acts as "auto",
|
|
not a 400: a client's odd choice must not stop its request. "none" is handled by the caller (no tools)."""
|
|
kind, name = tool_choice_of(tool_choice)
|
|
names = {t.get("name") for t in tools or [] if isinstance(t, dict)}
|
|
if kind in ("auto", "none"):
|
|
return None
|
|
if kind == "required" and len(names) == 1: # one tool to call: name it, the model cannot invent another
|
|
kind, name = "named", next(iter(names))
|
|
if kind == "required" and names:
|
|
return CALL_START + "\n<function="
|
|
if kind == "named" and name in names:
|
|
return CALL_START + f"\n<function={name}>\n"
|
|
print(f"[strata] tool_choice {json.dumps(tool_choice)[:200]} is not supported here (or names no tool of the "
|
|
"request): the model decides, as with \"auto\"", flush=True)
|
|
return None
|
|
|
|
|
|
def parse_tool_call(body: str, schema: dict | None = None) -> ToolCall:
|
|
"""`<function=NAME>\\n<parameter=P>\\nVALUE\\n</parameter>...</function>` -> ToolCall. Values are JSON-decoded
|
|
when the tool's schema says the parameter is not a string (or, without a schema, when they parse as JSON
|
|
objects/arrays/numbers/booleans)."""
|
|
body = body.strip()
|
|
if not body.startswith("<function=") or ">" not in body:
|
|
raise ValueError("malformed tool call: " + body[:80])
|
|
name = body[len("<function="):body.index(">")]
|
|
rest = body[body.index(">") + 1:]
|
|
props = ((schema or {}).get("parameters") or {}).get("properties") or {}
|
|
args = {}
|
|
while "<parameter=" in rest:
|
|
rest = rest[rest.index("<parameter=") + len("<parameter="):]
|
|
pname = rest[:rest.index(">")]
|
|
rest = rest[rest.index(">") + 1:]
|
|
end = param_end(rest, final=True)
|
|
value = rest[:end] if end >= 0 else rest
|
|
rest = rest[end + len(PARAM_END):] if end >= 0 else ""
|
|
if value.startswith("\n"):
|
|
value = value[1:]
|
|
if value.endswith("\n"):
|
|
value = value[:-1]
|
|
declared = (props.get(pname) or {}).get("type")
|
|
if declared == "string":
|
|
args[pname] = value
|
|
else:
|
|
try:
|
|
args[pname] = json.loads(value)
|
|
except ValueError:
|
|
args[pname] = value
|
|
return ToolCall(name=name, arguments=args)
|
|
|
|
|
|
RCALL_MAX = 32768 # the most of a `<tool_call>` inside the reasoning that is held while waiting for its end (#804)
|
|
|
|
|
|
class OutputParser:
|
|
"""Incremental parser of the model's text. Feed deltas; get events. A tag split across deltas is held back
|
|
until it is complete, so clients never see `<tool_` or `</thi`."""
|
|
|
|
def __init__(self, thinking: bool = True, tools: list[dict] | None = None, stream_tools: bool = False):
|
|
self.state = "reasoning" if thinking else "content"
|
|
self.buf = ""
|
|
self.lead = False
|
|
self.schemas = {t.get("name"): t for t in tools or []}
|
|
# stream_tools: a tool call is also reported while it is being written - "tool_start" (its name and id) as
|
|
# soon as the name is known, then "tool_args" pieces of its JSON arguments (string parameters character by
|
|
# character; other types whole, once complete) - before the final "tool_call". Without it, a client sees
|
|
# nothing until the call is complete, which for a large file write can be many minutes.
|
|
self.stream_tools = stream_tools
|
|
# #804/#1058: calls found inside the reasoning wait here as [raw text, ToolCall | None] until the turn shows
|
|
# they were acts: only whitespace (or more calls) after them, then the end of the turn or `</think>`. The
|
|
# reasoning text before them is tracked (code fence, inline code, current line) to tell an act from a quote.
|
|
self.pending: list[list] = []
|
|
self.rescued = 0 # calls delivered from the reasoning
|
|
self.refused = 0 # declared calls kept as reasoning text (quoted, or the turn was cut)
|
|
self.fence, self.line, self.ticks = "", "", 0
|
|
self._reset_scan()
|
|
|
|
def _track(self, text: str) -> str:
|
|
"""Follow the reasoning text that has gone out: the open code fence, the current line, and the backticks of
|
|
the current paragraph. Returns the text."""
|
|
parts = text.split("\n")
|
|
for k, part in enumerate(parts):
|
|
if k < len(parts) - 1:
|
|
line, self.line = self.line + part, ""
|
|
s = line.lstrip()
|
|
if self.fence:
|
|
if s.startswith(self.fence * 3):
|
|
self.fence = ""
|
|
elif s[:3] in ("```", "~~~") and (s[0] * 3) not in s[3:]:
|
|
self.fence = s[0]
|
|
elif not s:
|
|
self.ticks = 0
|
|
else:
|
|
self.ticks += line.count("`")
|
|
else:
|
|
self.line += part
|
|
return text
|
|
|
|
def _opener_ok(self) -> bool:
|
|
"""The reasoning text so far puts a `<tool_call>` at the start of a line, outside a code fence and outside
|
|
inline code."""
|
|
return not self.fence and not self.line.strip() and self.ticks % 2 == 0
|
|
|
|
def _in_code(self) -> bool:
|
|
"""The text so far leaves the next character inside a code fence or inline code."""
|
|
return bool(self.fence) or (self.ticks + self.line.count("`")) % 2 == 1
|
|
|
|
def _release(self, deliver: bool) -> list[Event]:
|
|
"""Settle the calls waiting in self.pending: events for real calls, or all of it back as reasoning text."""
|
|
out = []
|
|
for raw, call in self.pending:
|
|
if call is not None and deliver:
|
|
out.append(Event("tool_call", call=call))
|
|
self.rescued += 1
|
|
elif call is None or not deliver:
|
|
if call is not None:
|
|
self.refused += 1
|
|
if raw:
|
|
out.append(Event("reasoning", self._track(raw)))
|
|
self.pending = []
|
|
return out
|
|
|
|
def _reset_scan(self):
|
|
self.sp = 0 # how much of self.buf (the call body) the scanner has consumed
|
|
self.ss = "name" # name -> between -> str|raw -> ... -> done
|
|
self.scall = None # the ToolCall being streamed (its id is reused by the final event)
|
|
self.sfirst = True
|
|
self.sval_started = False
|
|
self.sdeclared = {}
|
|
|
|
def _scan(self) -> list[Event]:
|
|
"""Advance the streaming view of the call body in self.buf (see stream_tools)."""
|
|
out = []
|
|
|
|
def args(s):
|
|
if s:
|
|
out.append(Event("tool_args", s, call=self.scall))
|
|
while True:
|
|
rest = self.buf[self.sp:]
|
|
if self.ss == "name":
|
|
a = rest.find("<function=")
|
|
b = rest.find(">", a + 10) if a >= 0 else -1
|
|
if b < 0:
|
|
return out
|
|
name = rest[a + 10:b]
|
|
self.scall = ToolCall(name=name, arguments={})
|
|
props = ((self.schemas.get(name) or {}).get("parameters") or {}).get("properties") or {}
|
|
self.sdeclared = {k: (v or {}).get("type") for k, v in props.items()}
|
|
out.append(Event("tool_start", call=self.scall))
|
|
args("{")
|
|
self.sp += b + 1
|
|
self.ss = "between"
|
|
elif self.ss == "between":
|
|
stripped = rest.lstrip()
|
|
self.sp += len(rest) - len(stripped)
|
|
if stripped.startswith("<parameter="):
|
|
b = stripped.find(">")
|
|
if b < 0:
|
|
return out
|
|
pname = stripped[11:b]
|
|
args(("" if self.sfirst else ",") + json.dumps(pname) + ":")
|
|
self.sfirst = False
|
|
self.sp += b + 1
|
|
if self.sdeclared.get(pname) == "string":
|
|
args('"')
|
|
self.ss, self.sval_started = "str", False
|
|
else:
|
|
self.ss = "raw"
|
|
elif stripped.startswith("</function>"):
|
|
args("}")
|
|
self.sp += len("</function>")
|
|
self.ss = "done"
|
|
else:
|
|
return out # a tag still arriving (or trailing text): wait
|
|
elif self.ss == "str":
|
|
if not self.sval_started:
|
|
if not rest:
|
|
return out
|
|
if rest[0] == "\n": # the value's leading newline is not part of it
|
|
self.sp += 1
|
|
rest = rest[1:]
|
|
self.sval_started = True
|
|
end = param_end(rest)
|
|
if end >= 0:
|
|
value = rest[:end]
|
|
if value.endswith("\n"):
|
|
value = value[:-1]
|
|
args(json.dumps(value)[1:-1] + '"')
|
|
self.sp += end + len(PARAM_END)
|
|
self.ss = "between"
|
|
continue
|
|
# an undecided </parameter> (-2) is held from its start, like a tag still arriving
|
|
safe = rest.find(PARAM_END) if end == -2 else len(rest) - self._hold(rest, (PARAM_END,))
|
|
if safe > 0 and rest[safe - 1] == "\n": # may be the trailing newline before </parameter>
|
|
safe -= 1
|
|
if safe > 0:
|
|
args(json.dumps(rest[:safe])[1:-1])
|
|
self.sp += safe
|
|
return out
|
|
elif self.ss == "raw":
|
|
end = param_end(rest)
|
|
if end < 0:
|
|
return out
|
|
value = rest[:end]
|
|
if value.startswith("\n"):
|
|
value = value[1:]
|
|
if value.endswith("\n"):
|
|
value = value[:-1]
|
|
try:
|
|
v = json.loads(value)
|
|
except ValueError:
|
|
v = value
|
|
args(json.dumps(v, ensure_ascii=False))
|
|
self.sp += end + len(PARAM_END)
|
|
self.ss = "between"
|
|
else:
|
|
return out
|
|
|
|
def _close_scan(self) -> list[Event]:
|
|
"""A streamed call that ended without a clean </function>: close its JSON so clients can still parse it."""
|
|
out = []
|
|
if self.scall is None or self.ss == "done":
|
|
return out
|
|
tail = ""
|
|
if self.ss == "str":
|
|
tail += '"'
|
|
elif self.ss == "raw":
|
|
tail += json.dumps(self.buf[self.sp:].strip("\n"))
|
|
tail += "}"
|
|
out.append(Event("tool_args", tail, call=self.scall))
|
|
self.ss = "done"
|
|
return out
|
|
|
|
def _hold(self, text: str, tags: tuple[str, ...]) -> int:
|
|
"""Length of the longest suffix of `text` that is a proper prefix of one of `tags`."""
|
|
best = 0
|
|
for tag in tags:
|
|
for n in range(1, len(tag)):
|
|
if text.endswith(tag[:n]):
|
|
best = max(best, n)
|
|
return best
|
|
|
|
def feed(self, delta: str) -> list[Event]:
|
|
self.buf += delta
|
|
out: list[Event] = []
|
|
while True:
|
|
if self.state == "rcall":
|
|
# #804: a `<tool_call>` inside the reasoning, with tools declared. It is held whole (never streamed
|
|
# as a call) until it ends: a declared name is then a tool call, anything else stays reasoning text.
|
|
body = self.buf[len(CALL_START):]
|
|
end, think = call_end(body), body.find(THINK_END)
|
|
if end >= 0 and (think < 0 or think >= end):
|
|
call = None
|
|
try:
|
|
name = body[:end].strip()[len("<function="):].split(">", 1)[0]
|
|
if name in self.schemas:
|
|
call = parse_tool_call(body[:end], self.schemas.get(name))
|
|
except ValueError:
|
|
pass
|
|
raw = self.buf[:len(CALL_START) + end + len(CALL_END)]
|
|
if call is not None or self.pending:
|
|
self.pending.append([raw, call]) # settled by what follows it (see __init__)
|
|
else:
|
|
out.append(Event("reasoning", self._track(raw)))
|
|
self.buf = body[end + len(CALL_END):]
|
|
self.state = "reasoning"
|
|
elif think >= 0: # the thinking ended inside it: it never was a call
|
|
out += self._release(False)
|
|
out.append(Event("reasoning", self._track(self.buf[:len(CALL_START) + think])))
|
|
self.buf = body[think:]
|
|
self.state = "reasoning"
|
|
elif len(body) > RCALL_MAX: # a tag in the prose that never closes: stop holding the thinking back
|
|
out += self._release(False)
|
|
out.append(Event("reasoning", self._track(self.buf)))
|
|
self.buf = ""
|
|
self.state = "reasoning"
|
|
else:
|
|
return out
|
|
elif self.state == "reasoning":
|
|
if self.pending:
|
|
# calls wait for what follows: more calls or whitespace keep them, `</think>` makes them acts,
|
|
# any other text means they were quoted
|
|
stripped = self.buf.lstrip()
|
|
if len(stripped) < len(self.buf):
|
|
self.pending[-1][0] += self.buf[:len(self.buf) - len(stripped)]
|
|
self.buf = stripped
|
|
if not self.buf:
|
|
return out
|
|
if self.buf.startswith(CALL_START):
|
|
self.state = "rcall"
|
|
elif self.buf.startswith(THINK_END):
|
|
out += self._release(True)
|
|
elif CALL_START.startswith(self.buf) or THINK_END.startswith(self.buf):
|
|
return out
|
|
else:
|
|
out += self._release(False)
|
|
continue
|
|
i = self.buf.find(THINK_END)
|
|
tool = self.buf.find(CALL_START) if self.schemas else -1
|
|
if tool >= 0 and (i < 0 or tool < i):
|
|
if tool:
|
|
out.append(Event("reasoning", self._track(self.buf[:tool])))
|
|
if self._opener_ok():
|
|
self.buf = self.buf[tool:]
|
|
self.state = "rcall"
|
|
else: # mid-sentence, in a fence or in inline code: a quote
|
|
out.append(Event("reasoning", self._track(CALL_START)))
|
|
self.buf = self.buf[tool + len(CALL_START):]
|
|
continue
|
|
if i < 0:
|
|
keep = self._hold(self.buf, (THINK_END, CALL_START) if self.schemas else (THINK_END,))
|
|
if len(self.buf) > keep:
|
|
out.append(Event("reasoning", self._track(self.buf[:len(self.buf) - keep])))
|
|
self.buf = self.buf[len(self.buf) - keep:]
|
|
return out
|
|
if i:
|
|
out.append(Event("reasoning", self._track(self.buf[:i])))
|
|
self.buf = self.buf[i + len(THINK_END):]
|
|
self.state, self.lead = "content", True
|
|
self.fence, self.line, self.ticks = "", "", 0 # the answer's own text starts here
|
|
elif self.state == "content":
|
|
if self.lead: # newlines right after </think> or a call
|
|
stripped = self.buf.lstrip("\n")
|
|
if not stripped:
|
|
self.buf = ""
|
|
return out
|
|
self.buf, self.lead = stripped, False
|
|
i = self.buf.find(CALL_START)
|
|
if i < 0:
|
|
# Hold a partial tag AND the newlines before it: if a tool call follows, they are dropped,
|
|
# so emitting them early would make streamed and whole outputs differ.
|
|
j = len(self.buf) - self._hold(self.buf, (CALL_START,))
|
|
while j > 0 and self.buf[j - 1] == "\n":
|
|
j -= 1
|
|
if j > 0:
|
|
out.append(Event("content", self._track(self.buf[:j])))
|
|
self.buf = self.buf[j:]
|
|
return out
|
|
# #1058: an opener inside a code fence or inline code is text (a quoted example), never a call. The
|
|
# text before it decides, so streamed and whole outputs agree. Mid-sentence openers still count.
|
|
snap = (self.fence, self.line, self.ticks)
|
|
self._track(self.buf[:i])
|
|
in_code = self._in_code()
|
|
self.fence, self.line, self.ticks = snap
|
|
if in_code:
|
|
out.append(Event("content", self._track(self.buf[:i + len(CALL_START)])))
|
|
self.buf = self.buf[i + len(CALL_START):]
|
|
continue
|
|
# A call is `<tool_call>` and then (after whitespace) `<function=`; the tag with anything else after
|
|
# it is prose that names the format ("I'll use a <tool_call> block") - content, not a malformed call
|
|
# that ends the request. Until its follower has arrived it is held, like a partial tag.
|
|
after = self.buf[i + len(CALL_START):].lstrip()
|
|
if after and not after.startswith(FUNC_START) and not FUNC_START.startswith(after):
|
|
out.append(Event("content", self._track(self.buf[:i + len(CALL_START)])))
|
|
self.buf = self.buf[i + len(CALL_START):]
|
|
continue
|
|
if not after.startswith(FUNC_START):
|
|
j = i
|
|
while j > 0 and self.buf[j - 1] == "\n":
|
|
j -= 1
|
|
if j > 0:
|
|
out.append(Event("content", self._track(self.buf[:j])))
|
|
self.buf = self.buf[j:]
|
|
return out
|
|
if i and self.buf[:i].strip():
|
|
out.append(Event("content", self._track(self.buf[:i].rstrip("\n"))))
|
|
self.buf = self.buf[i + len(CALL_START):]
|
|
self.state = "call"
|
|
else:
|
|
i = call_end(self.buf)
|
|
if self.stream_tools:
|
|
if i >= 0:
|
|
whole, self.buf = self.buf, self.buf[:i] # scan only the body
|
|
out += self._scan()
|
|
out += self._close_scan()
|
|
self.buf = whole
|
|
else:
|
|
out += self._scan()
|
|
if i < 0:
|
|
return out
|
|
body = self.buf[:i]
|
|
self.buf = self.buf[i + len(CALL_END):]
|
|
name = body.strip()[len("<function="):].split(">", 1)[0]
|
|
call = parse_tool_call(body, self.schemas.get(name))
|
|
if self.scall is not None:
|
|
call.id = self.scall.id
|
|
out.append(Event("tool_call", call=call))
|
|
self._reset_scan()
|
|
self.state, self.lead = "content", True
|
|
|
|
def finish(self, reason: str | None = None) -> list[Event]:
|
|
"""End of generation: flush whatever is held (an unterminated tool call is returned as content; one that was
|
|
already announced stays unfinished: its JSON is not closed and no "tool_call" follows it, #211). `reason` is
|
|
how the turn ended: calls waiting from the reasoning become real calls only on a natural stop (None = stop);
|
|
a turn cut by max tokens (or cancelled, or failed) keeps them as reasoning text (#1058)."""
|
|
out = []
|
|
if self.pending:
|
|
out += self._release(reason in (None, "stop") and self.state == "reasoning" and not self.buf)
|
|
if self.state == "call" and self.stream_tools and self.scall is not None:
|
|
out += self._scan() # the output ended inside a call that was already announced
|
|
if self.ss == "done": # only its </tool_call> is missing: the call itself is whole
|
|
out.append(Event("tool_call", call=self.scall))
|
|
self.buf = ""
|
|
self._reset_scan()
|
|
return out
|
|
if self.buf:
|
|
# an unfinished call inside the reasoning (#804) is reasoning text, never a call
|
|
kind = {"reasoning": "reasoning", "rcall": "reasoning", "content": "content"}.get(self.state, "content")
|
|
text = self.buf if self.state != "call" else CALL_START + self.buf
|
|
out.append(Event(kind, text))
|
|
self.buf = ""
|
|
return out
|