Trinity's tokenizer has no <tool_call> special token, so token-id based parsers never detect calls. Adds a reasoning- and Markdown-aware text-level parser selected with tool_parser = "hermes".
137 lines
5.8 KiB
Python
137 lines
5.8 KiB
Python
"""Hermes regressions on Trinity's real multi-token tool delimiters."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
from renderers.base import ToolCallParseStatus
|
|
from renderers.configs import DefaultRendererConfig
|
|
from renderers.default import DefaultRenderer
|
|
from transformers import AutoTokenizer
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def trinity_tokenizer():
|
|
return AutoTokenizer.from_pretrained(
|
|
"arcee-ai/Trinity-Mini",
|
|
revision="cdb81e8130815fec299cccc8f8700004b8366e1e",
|
|
trust_remote_code=False,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def hermes_renderer(trinity_tokenizer):
|
|
return DefaultRenderer(
|
|
trinity_tokenizer,
|
|
DefaultRendererConfig(tool_parser="hermes", reasoning_parser="think"),
|
|
)
|
|
|
|
|
|
CALL = '<tool_call>{"name":"book_flight","arguments":{"dest":"Paris"}}</tool_call>'
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"text",
|
|
[
|
|
f"<think>Consider {CALL}, but do not book.</think>No.",
|
|
f"Consider {CALL}, but do not book.</think>No.",
|
|
f"<think>Unclosed reasoning containing {CALL}",
|
|
f"Unclosed prefilled reasoning containing {CALL}",
|
|
f"Finished reasoning.</think>```json\n{CALL}\n```",
|
|
f"Finished reasoning.</think>~~~json\n{CALL}\n~~~",
|
|
f"Finished reasoning.</think>Example: `{CALL}`.",
|
|
'Finished reasoning.</think>```json\n{"name":"book_flight","arguments":{"dest":"Paris"}}\n```',
|
|
'Finished reasoning.</think>{"name":"book_flight","arguments":{"dest":"Paris"}}',
|
|
],
|
|
)
|
|
def test_examples_are_not_actions(trinity_tokenizer, hermes_renderer, text):
|
|
ids = trinity_tokenizer.encode(text, add_special_tokens=False)
|
|
remaining, calls = hermes_renderer._tool_parser.extract(ids)
|
|
assert remaining == ids
|
|
assert calls == []
|
|
assert hermes_renderer.parse_response(ids).tool_calls == []
|
|
|
|
|
|
def test_native_calls_preserve_reasoning_content_and_original_spans(trinity_tokenizer, hermes_renderer):
|
|
text = f"Consider the destination.</think>é🙂 before{CALL}between{CALL}after"
|
|
ids = trinity_tokenizer.encode(text, add_special_tokens=False)
|
|
parsed = hermes_renderer.parse_response(ids)
|
|
assert parsed.reasoning_content == "Consider the destination."
|
|
assert parsed.content == "é🙂 beforebetweenafter"
|
|
assert len(parsed.tool_calls) == 2
|
|
for call in parsed.tool_calls:
|
|
assert call.status == ToolCallParseStatus.OK
|
|
assert call.name == "book_flight" and call.arguments == {"dest": "Paris"}
|
|
start, end = call.token_span
|
|
assert 0 <= start < end <= len(ids)
|
|
covered = trinity_tokenizer.decode(ids[start:end], skip_special_tokens=False)
|
|
assert CALL in covered
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"body,status",
|
|
[
|
|
('{"name":"f","arguments":{}}', ToolCallParseStatus.OK),
|
|
('{"name":"f","arguments":[]}', ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
('{"name":"f","arguments":null}', ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
('{"name":"f","arguments":"{}"}', ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
('{"name":"f"}', ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
('{"name":42,"arguments":{}}', ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
('{"name":" ","arguments":{}}', ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
("[]", ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
("{'name':'f','arguments':{}}", ToolCallParseStatus.INVALID_JSON),
|
|
('{"name":"f","arguments":{"x":NaN}}', ToolCallParseStatus.INVALID_JSON),
|
|
('{"name":"f","arguments":{"x":Infinity}}', ToolCallParseStatus.INVALID_JSON),
|
|
('{"name":"f","name":"g","arguments":{}}', ToolCallParseStatus.INVALID_JSON),
|
|
('{"name":"f","arguments":{}} trailing', ToolCallParseStatus.MALFORMED_STRUCTURE),
|
|
],
|
|
)
|
|
def test_strict_native_envelopes(trinity_tokenizer, hermes_renderer, body, status):
|
|
ids = trinity_tokenizer.encode(f"Use the tool.</think><tool_call>{body}</tool_call>", add_special_tokens=False)
|
|
parsed = hermes_renderer.parse_response(ids)
|
|
assert len(parsed.tool_calls) == 1
|
|
assert parsed.tool_calls[0].status == status
|
|
if status != ToolCallParseStatus.OK:
|
|
assert parsed.tool_calls[0].name is None
|
|
|
|
|
|
def test_tag_strings_in_arguments_do_not_close_a_call(trinity_tokenizer, hermes_renderer):
|
|
args = {"text": '</tool_call> <think> ``` <tool_call> " café🙂'}
|
|
body = json.dumps({"name": "f", "arguments": args}, ensure_ascii=False)
|
|
ids = trinity_tokenizer.encode(
|
|
f"Use the tool.</think><tool_call>{body}</tool_call>suffix", add_special_tokens=False
|
|
)
|
|
parsed = hermes_renderer.parse_response(ids)
|
|
assert parsed.content == "suffix"
|
|
assert parsed.tool_calls[0].arguments == args
|
|
assert parsed.tool_calls[0].status == ToolCallParseStatus.OK
|
|
|
|
|
|
def test_unclosed_call_remains_a_parse_error(trinity_tokenizer, hermes_renderer):
|
|
ids = trinity_tokenizer.encode(
|
|
'Use the tool.</think><tool_call>{"name":"f","arguments":{}}', add_special_tokens=False
|
|
)
|
|
parsed = hermes_renderer.parse_response(ids)
|
|
assert parsed.tool_calls[0].status == ToolCallParseStatus.UNCLOSED_BLOCK
|
|
assert parsed.tool_calls[0].name is None
|
|
|
|
|
|
def test_real_schema_uses_native_chat_template(trinity_tokenizer, hermes_renderer):
|
|
tool = {
|
|
"type": "function",
|
|
"function": {
|
|
"name": "book_flight",
|
|
"description": "Book a flight.",
|
|
"parameters": {"type": "object", "properties": {"dest": {"type": "string"}}},
|
|
},
|
|
}
|
|
result = hermes_renderer.render(
|
|
[{"role": "user", "content": "Book a flight to Paris."}],
|
|
tools=[tool],
|
|
add_generation_prompt=True,
|
|
)
|
|
text = trinity_tokenizer.decode(result.token_ids, skip_special_tokens=False)
|
|
assert "book_flight" in text and "<tools>" in text and "<tool_call>" in text
|
|
assert "available_actions" not in text
|
|
assert "```json" not in text
|
|
assert text.endswith("<think>\n")
|