[Bugfix] Fix Apertus streaming scientific notation (#61812)
Signed-off-by: bzsuni <bingzhe.sun@daocloud.io> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
@@ -244,6 +244,22 @@ class TestStreamingExtraction:
|
||||
assert tcs[0]["name"] == "get_weather"
|
||||
assert json.loads(tcs[0]["arguments"]) == {"location": "Paris, France"}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"arguments",
|
||||
['{"value": -2e-3}', '{"value": 1.5e3}', '{"values": [1.5e3, -2e-3]}'],
|
||||
)
|
||||
def test_streaming_scientific_notation(self, parser, mock_request, arguments):
|
||||
model_output = (
|
||||
f'{TOOL_CALLS_PREFIX}[{{"calculate": {arguments}}}]{TOOL_CALLS_SUFFIX}'
|
||||
)
|
||||
|
||||
results = self._simulate_streaming(parser, mock_request, list(model_output))
|
||||
tool_calls = self._collect_tool_calls(results)
|
||||
|
||||
assert len(tool_calls) == 1
|
||||
assert tool_calls[0]["name"] == "calculate"
|
||||
assert json.loads(tool_calls[0]["arguments"]) == json.loads(arguments)
|
||||
|
||||
def test_streaming_missing_tool_suffix(self, parser, mock_request):
|
||||
chunks = [
|
||||
"<|tools_prefix|>",
|
||||
|
||||
@@ -417,7 +417,9 @@ class ApertusToolParser(ToolParser):
|
||||
|
||||
"""
|
||||
try:
|
||||
parsed, _ = partial_json_loads(json_str, Allow.ALL)
|
||||
# An exponent can change a number's already-streamed representation.
|
||||
flags = Allow.ALL if is_final else Allow.ALL & ~Allow.NUM
|
||||
parsed, _ = partial_json_loads(json_str, flags)
|
||||
if not isinstance(parsed, list):
|
||||
parsed = [parsed] if parsed else []
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user