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:
@@ -121,7 +121,7 @@ class TestOpenAIResponseBuilder:
|
||||
assert p["choices"][0]["finish_reason"] is None
|
||||
|
||||
def test_format_chunk(self, builder):
|
||||
events = builder.format_chunk("hello", "hello")
|
||||
events = builder.format_chunk("hello", body="hello")
|
||||
payload = json.loads(events[0].split("data: ", 1)[1])
|
||||
assert payload["choices"][0]["delta"]["content"] == "hello"
|
||||
assert payload["choices"][0]["finish_reason"] is None
|
||||
@@ -192,7 +192,7 @@ class TestAnthropicResponseBuilder:
|
||||
assert payloads[1]["type"] == "content_block_start"
|
||||
|
||||
def test_format_chunk(self, builder):
|
||||
events = builder.format_chunk("tok", "tok")
|
||||
events = builder.format_chunk("tok", body="tok")
|
||||
payload = json.loads(events[0].split("data: ", 1)[1])
|
||||
assert payload["type"] == "content_block_delta"
|
||||
assert payload["delta"]["text"] == "tok"
|
||||
|
||||
@@ -643,3 +643,49 @@ def test_factory_create_with_tools_only():
|
||||
parser = ToolParserFactory.create("simple_json", tools=tools)
|
||||
assert parser.tools == tools
|
||||
assert parser.tool_choice == "auto"
|
||||
|
||||
|
||||
def test_feed_accepts_token_ids_and_ignores_them():
|
||||
parser = SimpleJsonToolParser()
|
||||
text = '{"name": "get_weather", "arguments": {"city": "Beijing"}}'
|
||||
deltas_with = parser.feed(text, current_token_ids=[123, 456], delta_token_ids=[456])
|
||||
assert len(deltas_with) > 0
|
||||
|
||||
|
||||
def test_feed_token_ids_do_not_affect_parsing():
|
||||
parser_no_ids = SimpleJsonToolParser()
|
||||
parser_with_ids = SimpleJsonToolParser()
|
||||
text = '{"name": "get_weather", "arguments": {"city": "Beijing"}}'
|
||||
result_no = parser_no_ids.feed(text)
|
||||
result_with = parser_with_ids.feed(
|
||||
text, current_token_ids=[1, 2, 3], delta_token_ids=[3]
|
||||
)
|
||||
assert len(result_no) == len(result_with)
|
||||
assert len(result_no) > 0
|
||||
assert (
|
||||
result_no[0]["tool_calls"][0]["function"]["name"]
|
||||
== result_with[0]["tool_calls"][0]["function"]["name"]
|
||||
)
|
||||
|
||||
|
||||
def test_parser_uses_token_ids_for_detection():
|
||||
class TokenIdParser(BaseToolParser):
|
||||
def __init__(self, tools=None, tool_choice="auto"):
|
||||
super().__init__(tools, tool_choice)
|
||||
self._detections = 0
|
||||
|
||||
def feed(self, body, current_token_ids=None, delta_token_ids=None):
|
||||
if current_token_ids and 999 in current_token_ids:
|
||||
self._detections += 1
|
||||
return []
|
||||
|
||||
def parse_complete(self, body):
|
||||
return None
|
||||
|
||||
@property
|
||||
def has_tool_calls(self):
|
||||
return self._detections > 0
|
||||
|
||||
parser = TokenIdParser()
|
||||
parser.feed("hello", current_token_ids=[1, 999, 3])
|
||||
assert parser.has_tool_calls
|
||||
|
||||
Reference in New Issue
Block a user