feat : BaseToolParser.feed 增加可选 token_ids 参数

- format_chunk ABC 改为 (token, **kwargs),body/token_ids 通过 kw 传入
- ProtocolHandler._handle_stream 逐 token encode 并透传
- Anthropic builder 用 **kwargs 吸收不使用的参数,零变更
- 新增 3 个 token_ids 参数测试
This commit is contained in:
2026-06-06 11:19:30 +08:00
parent 52aa4d01d5
commit 9e31d4ef2b
6 changed files with 105 additions and 13 deletions
+1 -1
View File
@@ -73,7 +73,7 @@ class AnthropicResponseBuilder(ResponseBuilder):
),
]
def format_chunk(self, token: str, body: str) -> List[str]:
def format_chunk(self, token: str, **kwargs) -> List[str]:
return [
sse_event(
{
+9 -4
View File
@@ -113,9 +113,10 @@ class OpenAIResponseBuilder(ResponseBuilder):
)
]
def format_chunk(self, token: str, body: str) -> List[str]:
def format_chunk(self, token: str, **kwargs) -> List[str]:
body = kwargs.get("body", "")
if self._parser is not None:
return self._format_tool_chunk(body)
return self._format_tool_chunk(body, **kwargs)
return [
sse_event(
@@ -135,8 +136,12 @@ class OpenAIResponseBuilder(ResponseBuilder):
)
]
def _format_tool_chunk(self, body: str) -> List[str]:
deltas = self._parser.feed(body)
def _format_tool_chunk(self, body: str, **kwargs) -> List[str]:
deltas = self._parser.feed(
body,
current_token_ids=kwargs.get("current_token_ids"),
delta_token_ids=kwargs.get("delta_token_ids"),
)
events: List[str] = []
for d in deltas:
if "content" in d:
+15 -4
View File
@@ -78,11 +78,13 @@ class ResponseBuilder(ABC):
"""SSE events that open the stream."""
@abstractmethod
def format_chunk(self, token: str, body: str) -> List[str]:
def format_chunk(self, token: str, **kwargs) -> List[str]:
"""SSE events for a single generated token.
Receives the current token and the full accumulated *body* so
that tool-call parsers can re-parse the complete text each step.
``body`` (the full accumulated text so far) is always provided
as a keyword argument. Additional keyword arguments such as
``current_token_ids`` and ``delta_token_ids`` may be included
for tool parsers that need token-level information.
Returns a list of SSE event strings (may be empty).
"""
@@ -142,15 +144,24 @@ class ProtocolHandler:
body = ""
yielded = ""
matched = None
token_ids: List[int] = []
async for token in agen:
body += token
new_ids = self.engine.tokenizer.encode(token)
token_ids.extend(new_ids)
matched = checker.check(body)
if matched:
break
ctx.completion_tokens += 1
for event in self.builder.format_chunk(token, body):
for event in self.builder.format_chunk(
token,
body=body,
current_token_ids=token_ids,
delta_token_ids=new_ids,
):
yield event
yielded += token
+32 -2
View File
@@ -2,6 +2,9 @@
Patterned after vLLM's ToolParser abstraction. Each parser knows how to
detect and incrementally extract tool calls from raw generated text.
Subclasses may optionally consume ``token_ids`` for token-level parsing
(e.g. Harmony / VLM-style parsers).
"""
import re
@@ -17,6 +20,14 @@ class BaseToolParser(ABC):
Maintains streaming state internally so that each call to :meth:`feed`
can diff against previously emitted content.
Parameters
----------
tools : list of dict, optional
Tool definitions from the request.
tool_choice : str
``"auto"`` / ``"required"`` / ``"none"`` or a named tool choice
dict.
"""
def __init__(self, tools: Optional[List[Dict]] = None, tool_choice: str = "auto"):
@@ -24,7 +35,12 @@ class BaseToolParser(ABC):
self.tool_choice = tool_choice
@abstractmethod
def feed(self, body: str) -> List[Dict]:
def feed(
self,
body: str,
current_token_ids: Optional[List[int]] = None,
delta_token_ids: Optional[List[int]] = None,
) -> List[Dict]:
"""Feed the *full* accumulated text each step.
Returns a list of delta dicts to emit. Each delta is one of:
@@ -33,6 +49,15 @@ class BaseToolParser(ABC):
- ``{"tool_calls": [...]}`` — tool-call delta (OpenAI format)
Returns an empty list when nothing new should be emitted.
Parameters
----------
body : str
The complete accumulated generated text so far.
current_token_ids : list of int, optional
All token IDs decoded into *body* (cumulative).
delta_token_ids : list of int, optional
Only the token IDs for this chunk.
"""
@abstractmethod
@@ -199,7 +224,12 @@ class SimpleJsonToolParser(BaseToolParser):
# -------------------------------------------------------------- feed
def feed(self, body: str) -> List[Dict]:
def feed(
self,
body: str,
current_token_ids: Optional[List[int]] = None,
delta_token_ids: Optional[List[int]] = None,
) -> List[Dict]:
deltas: List[Dict] = []
completed = _find_tool_calls(body)