chat : trigger lazy grammar on bare JSON tool calls (#32497)
Models like Llama 3.x emit tool calls as plain {"name": ...} JSON without a start marker, so with tool_choice=auto no grammar was produced at all. Use a pattern trigger on the opening of the call object instead.
Assisted-by: Claude Code
This commit is contained in:
1 parent
9c10c94e16
commit
42f6a58fd1
2 files changed
+41
-4
No files matched your search
@@ -1118,6 +1118,7 @@ struct peg_test_case {
|
||||
common_chat_msg expect;
|
||||
bool is_partial = false;
|
||||
bool expect_reconstruction = false;
|
||||
bool expect_grammar = false;
|
||||
std::vector<std::string> expect_rules;
|
||||
};
|
||||
|
||||
@@ -1191,6 +1192,10 @@ static void test_peg_parser(common_chat_templates * tmpls,
|
||||
}
|
||||
}
|
||||
|
||||
if (tc.expect_grammar && session.grammar().empty()) {
|
||||
throw std::runtime_error("expected a grammar constraining the tool calls, but none was produced");
|
||||
}
|
||||
|
||||
common_chat_msg msg_accum;
|
||||
common_chat_msg msg_prev;
|
||||
msg_accum.role = msg_prev.role = "assistant";
|
||||
@@ -1614,6 +1619,11 @@ class peg_test_builder {
|
||||
return *this;
|
||||
}
|
||||
|
||||
peg_test_builder & expect_grammar() {
|
||||
tc_.expect_grammar = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
peg_test_builder & tool_choice(common_chat_tool_choice choice) {
|
||||
tc_.params.tool_choice = choice;
|
||||
return *this;
|
||||
@@ -7108,6 +7118,19 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
|
||||
auto tst = peg_tester("models/templates/meta-llama-Llama-3.2-3B-Instruct.jinja", detailed_debug);
|
||||
tst.test("Hello, world!\nWhat's up?").tools({ special_function_tool }).expect(message_assist).expect_reconstruction().expect_rules({ "tool-0" }).run();
|
||||
|
||||
// bare JSON calls have no start marker, auto mode must still produce a lazy grammar
|
||||
tst.test("{\"name\": \"special_function\", \"parameters\": {\"arg1\": 1}}")
|
||||
.tools({ special_function_tool })
|
||||
.expect(message_assist_call)
|
||||
.expect_grammar()
|
||||
.run();
|
||||
// same without spaces
|
||||
tst.test("{\"name\":\"special_function\",\"parameters\":{\"arg1\":1}}")
|
||||
.tools({ special_function_tool })
|
||||
.expect(message_assist_call)
|
||||
.expect_grammar()
|
||||
.run();
|
||||
|
||||
// Continuation tests
|
||||
tst.test("world!\nWhat's up?")
|
||||
.messages({ message_user, message_assist_prefill_content })
|
||||
|
||||
Reference in new issue
Block a user