Skip to content

Commit f766fe0

Browse files
ochafikochafik
andcommitted
tweak order of <|python_tag|> vs <function= parsing for functionary v3.1 format. still not ideal but hopefully less prone to crash
Co-Authored-By: ochafik <[email protected]>
1 parent 62ae9f5 commit f766fe0

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

common/chat.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,12 +1476,6 @@ static void common_chat_parse_functionary_v3_1_llama_3_1(common_chat_msg_parser
14761476
// This version of Functionary still supports the llama 3.1 tool call format for the python tool.
14771477
static const common_regex python_tag_regex(regex_escape("<|python_tag|>"));
14781478

1479-
if (auto res = builder.try_find_regex(python_tag_regex)) {
1480-
auto arguments = wrap_code_as_arguments(builder, builder.consume_rest());
1481-
builder.add_tool_call("python", "", arguments);
1482-
return;
1483-
}
1484-
14851479
static const common_regex function_regex(R"(<function=(\w+)>)");
14861480
static const common_regex close_regex(R"(</function>)");
14871481

@@ -1492,6 +1486,12 @@ static void common_chat_parse_functionary_v3_1_llama_3_1(common_chat_msg_parser
14921486
function_regex,
14931487
close_regex,
14941488
std::nullopt);
1489+
1490+
if (auto res = builder.try_find_regex(python_tag_regex)) {
1491+
auto arguments = wrap_code_as_arguments(builder, builder.consume_rest());
1492+
builder.add_tool_call("python", "", arguments);
1493+
return;
1494+
}
14951495
}
14961496

14971497
static common_chat_params common_chat_params_init_hermes_2_pro(const common_chat_template & tmpl, const struct templates_params & inputs) {

tests/test-chat.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,13 @@ static void test_template_output_parsers() {
11011101
{COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1}));
11021102
}
11031103

1104+
assert_equals(
1105+
message_assist_call,
1106+
common_chat_parse(
1107+
"<function=special_function>{\"arg1\": 1}<",
1108+
/* is_partial= */ true,
1109+
{COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1}));
1110+
11041111
test_templates(tmpls.get(), end_tokens, message_assist, tools, "Hello, world!\nWhat's up?", /* expect_grammar_triggered= */ false);
11051112
test_templates(tmpls.get(), end_tokens, message_assist_call, tools,
11061113
"<function=special_function>{\"arg1\": 1}</function>");

0 commit comments

Comments
 (0)