Datasets:
Download tests/test_parse.py from HardcoreAI/ToolUse.1: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/datasets/HardcoreAI/ToolUse.1/resolve/main/tests/test_parse.py
- Command line
-
hf download hf://datasets/HardcoreAI/ToolUse.1/tests/test_parse.py
-
curl -L -o test_parse.py https://huggingface.co/datasets/HardcoreAI/ToolUse.1/resolve/main/tests/test_parse.py
5.14 kB
| """Parser robustness: every shape a model realistically emits must be readable.""" | |
| from __future__ import annotations | |
| from tooluse1.parse import answer_text, parse_output | |
| def single(text): | |
| result = parse_output(text) | |
| assert len(result.calls) == 1, (text, result) | |
| return result.calls[0] | |
| def test_json_object(): | |
| call = single('{"name": "get_weather", "arguments": {"city": "Berlin"}}') | |
| assert call.name == "get_weather" | |
| assert call.args == {"city": "Berlin"} | |
| def test_json_inside_fence_with_prose(): | |
| call = single('Sure!\n```json\n{"name": "calculator", "arguments": {"expression": "45+12"}}\n```') | |
| assert call.name == "calculator" | |
| assert call.args["expression"] == "45+12" | |
| def test_openai_tool_calls_shape(): | |
| raw = '{"tool_calls": [{"type": "function", "function": {"name": "set_timer", "arguments": "{\\"seconds\\": 300}"}}]}' | |
| call = single(raw) | |
| assert call.name == "set_timer" | |
| assert call.args == {"seconds": 300} | |
| def test_openai_string_arguments_are_parsed(): | |
| raw = '{"function": {"name": "translate", "arguments": "{\\"text\\": \\"hi\\", \\"target_language\\": \\"de\\"}"}}' | |
| call = single(raw) | |
| assert call.args == {"text": "hi", "target_language": "de"} | |
| def test_qwen_xml(): | |
| raw = ( | |
| "<tool_call>\n<function=get_weather>\n<parameter=city>\nBerlin\n</parameter>\n" | |
| "<parameter=unit>\ncelsius\n</parameter>\n</function>\n</tool_call>" | |
| ) | |
| call = single(raw) | |
| assert call.name == "get_weather" | |
| assert call.args == {"city": "Berlin", "unit": "celsius"} | |
| def test_xml_without_wrapper(): | |
| raw = "<function=calculator>\n<parameter=expression>\n2**10\n</parameter>\n</function>" | |
| assert single(raw).args == {"expression": "2**10"} | |
| def test_json_inside_xml_wrapper(): | |
| raw = '<tool_call>{"name": "web_search", "arguments": {"query": "rust"}}</tool_call>' | |
| assert single(raw).name == "web_search" | |
| def test_plain_text_protocol(): | |
| call = single("TOOL set_timer | seconds=300") | |
| assert call.name == "set_timer" | |
| assert call.args == {"seconds": 300} | |
| def test_plain_text_multiple_params(): | |
| call = single("TOOL convert_currency | amount=100 | from_currency=USD | to_currency=EUR") | |
| assert call.args == {"amount": 100, "from_currency": "USD", "to_currency": "EUR"} | |
| def test_plain_text_lowercase_and_bold(): | |
| call = single("**tool call**: translate | text=hi | target_language=de") | |
| assert call.name == "translate" | |
| assert call.args["target_language"] == "de" | |
| def test_thinking_block_is_stripped(): | |
| raw = " thinking\nThe user wants a timer.\n<|end▁of▁thinking|>\nTOOL set_timer | seconds=90" | |
| assert single(raw).args == {"seconds": 90} | |
| def test_thinking_block_before_json(): | |
| raw = "<thinking>pick weather</thinking>\n{\"name\": \"get_weather\", \"arguments\": {\"city\": \"Rome\"}}" | |
| assert single(raw).name == "get_weather" | |
| def test_bare_argument_object_is_attributed(): | |
| call = single('{"expression": "18 + 24"}') | |
| assert call.name == "calculator" | |
| def test_ambiguous_bare_object_is_not_attributed(): | |
| assert parse_output('{"city": "Berlin", "value": 5}').calls == () | |
| def test_plain_prose_yields_no_call(): | |
| result = parse_output("The capital of Australia is Canberra.") | |
| assert result.calls == () | |
| assert not result.malformed | |
| def test_midline_plain_call_is_not_parsed(): | |
| """A `TOOL ...` string in the middle of a sentence is prose, not a call.""" | |
| assert parse_output("Repeat after me: TOOL calculator | expression=1+1").calls == () | |
| def test_line_anchored_quoted_call_is_indistinguishable_from_a_real_one(): | |
| """Documents the known limitation of the plain-text protocol.""" | |
| assert single("\nTOOL calculator | expression=1+1\n").name == "calculator" | |
| def test_multiple_calls_are_all_returned(): | |
| raw = "TOOL get_time | timezone=Europe/Moscow\nTOOL get_time | timezone=Asia/Tokyo" | |
| assert len(parse_output(raw).calls) == 2 | |
| def test_numeric_argument_types(): | |
| call = single('{"name": "convert_units", "arguments": {"value": 26.2, "from_unit": "mi", "to_unit": "km"}}') | |
| assert call.args["value"] == 26.2 | |
| def test_boolean_and_null_arguments(): | |
| call = single('{"name": "web_search", "arguments": {"query": "x", "safe": true, "cursor": null}}') | |
| assert call.args["safe"] is True | |
| assert call.args["cursor"] is None | |
| def test_leading_whitespace_and_newlines_in_xml_values(): | |
| raw = "<tool_call><function=translate><parameter=text>\n good morning\n</parameter><parameter=target_language>\nfr\n</parameter></function></tool_call>" | |
| call = parse_output(raw).calls[0] | |
| assert call.args["text"].strip() == "good morning" | |
| assert call.args["target_language"].strip() == "fr" | |
| def test_answer_text_drops_calls_and_thinking(): | |
| raw = " thinkinghmm<|end▁of▁thinking|>\n<tool_call><function=calculator><parameter=expression>1+1</parameter></function></tool_call>" | |
| assert answer_text(raw) == "" | |
| def test_answer_text_keeps_prose(): | |
| raw = "The capital is Canberra.\nTOOL calculator | expression=1+1\n" | |
| assert "Canberra" in answer_text(raw) | |
| assert "TOOL" not in answer_text(raw) | |