Skip to content

Commit e2ca8be

Browse files
author
ochafik
committed
constrain llama json output regardless of function name if matches at beginning
1 parent 14a4388 commit e2ca8be

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

common/chat.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -988,10 +988,11 @@ static common_chat_params common_chat_params_init_llama_3_1_tool_calls(const com
988988
" \"\\\"name\\\"\" space \":\" space \"\\\"" + name + "\\\"\" space \",\" space "
989989
" \"\\\"parameters\\\"\" space \":\" space " + builder.add_schema(name + "-args", parameters) + " "
990990
"\"}\" space"));
991-
data.grammar_triggers.push_back({
992-
COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_START,
993-
"\\{\\s*(?:\"type\"\\s*:\\s*\"function\"\\s*,\\s*)?\"name\"\\s*:\\s*\"" + name + "\"[\\s\\S]*",
994-
});
991+
});
992+
// Small models may hallucinate function names so we match anything (*at the start*) that looks like the JSON of a function call, regardless of the name.
993+
data.grammar_triggers.push_back({
994+
COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_START,
995+
"\\{\\s*(?:\"type\"\\s*:\\s*\"function\"\\s*,\\s*)?\"name\"\\s*:\\s*\"", // + name + "\"[\\s\\S]*",
995996
});
996997
if (!builtin_tools.empty()) {
997998
data.grammar_triggers.push_back({COMMON_GRAMMAR_TRIGGER_TYPE_WORD, "<|python_tag|>"});

0 commit comments

Comments
 (0)