Skip to content

Commit 227f05c

Browse files
committed
allow non-tool call json in llama 3.x parser
1 parent 2d677e8 commit 227f05c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

common/chat.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,18 @@ static void common_chat_parse_llama_3_1(common_chat_msg_parser & builder, bool w
11771177
}
11781178
} else if (auto tc = builder.try_consume_json_with_dumped_args({{"parameters"}})) {
11791179
if (!builder.add_tool_call(tc->value, "parameters")) {
1180-
builder.move_to(initial_pos);
1180+
auto has_unknown_keys = false;
1181+
for (const auto & [key, value] : tc->value.items()) {
1182+
if (key != "parameters" && key != "name" && key != "type") {
1183+
has_unknown_keys = true;
1184+
break;
1185+
}
1186+
}
1187+
if (has_unknown_keys) {
1188+
builder.move_to(initial_pos);
1189+
} else {
1190+
throw common_chat_msg_partial_exception("incomplete tool call");
1191+
}
11811192
}
11821193
}
11831194
builder.add_content(builder.consume_rest());

tests/test-chat.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,24 @@ static void test_template_output_parsers() {
10741074
"<|python_tag|>{\"type\": \"function\", \"name\": \"special_function\", \"parameters\": {\"arg1\": 1}}",
10751075
/* is_partial= */ false,
10761076
{COMMON_CHAT_FORMAT_LLAMA_3_X}));
1077+
assert_equals(
1078+
simple_assist_msg("{\"something\": \"else\"}"),
1079+
common_chat_parse(
1080+
"{\"something\": \"else\"}",
1081+
/* is_partial= */ false,
1082+
{COMMON_CHAT_FORMAT_LLAMA_3_X}));
1083+
assert_equals(
1084+
message_assist_empty,
1085+
common_chat_parse(
1086+
"{\"some",
1087+
/* is_partial= */ true,
1088+
{COMMON_CHAT_FORMAT_LLAMA_3_X}));
1089+
assert_equals(
1090+
message_assist_empty,
1091+
common_chat_parse(
1092+
"{\"parameters\": {\"arg1\": 1}",
1093+
/* is_partial= */ true,
1094+
{COMMON_CHAT_FORMAT_LLAMA_3_X}));
10771095

10781096
// test_templates(tmpls.get(), end_tokens, message_assist, tools, R"(?)", /* expect_grammar_triggered= */ false);
10791097
test_templates(tmpls.get(), end_tokens, message_assist_call_code_interpreter, llama_3_1_tools,

0 commit comments

Comments
 (0)