Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions common/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,15 @@ static void system_message_not_supported(json & messages) {
}
}

static void requires_non_null_content(json & messages) {
GGML_ASSERT(messages.is_array());
for (auto & message : messages) {
if (message.contains("tool_calls") && !message.contains("content")) {
message["content"] = "";
}
}
}

static void func_args_not_string(json & messages) {
GGML_ASSERT(messages.is_array());
for (auto & message : messages) {
Expand Down Expand Up @@ -2885,6 +2894,13 @@ static common_chat_params common_chat_templates_apply_jinja(
workaround::system_message_not_supported(params.messages);
}

if (tmpl.original_caps().supports_tool_calls) {
// some templates will require the content field in tool call messages
// to still be non-null, this puts an empty string everywhere where the
// content field is null
workaround::requires_non_null_content(params.messages);
}

params.extra_context = json::object();
for (auto el : inputs.chat_template_kwargs) {
params.extra_context[el.first] = json::parse(el.second);
Expand Down
8 changes: 4 additions & 4 deletions common/jinja/caps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ caps caps_get(jinja::program & prog) {
{"content", "Assistant message"},
{"tool_calls", json::array({
{
{"id", "call1"},
{"id", "call00001"},
{"type", "function"},
{"function", {
{"name", "tool1"},
Expand All @@ -170,10 +170,10 @@ caps caps_get(jinja::program & prog) {
}}
},
{
{"id", "call2"},
{"id", "call00002"},
{"type", "function"},
{"function", {
{"name", "tool2"},
{"name", "tool1"},
{"arguments", {
{"arg", "value"}
}}
Expand All @@ -194,7 +194,7 @@ caps caps_get(jinja::program & prog) {
{"name", "tool"},
{"type", "function"},
{"function", {
{"name", "tool"},
{"name", "tool1"},
{"description", "Tool description"},
{"parameters", {
{"type", "object"},
Expand Down
13 changes: 13 additions & 0 deletions tests/test-jinja.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,19 @@ static void test_tests(testing & t) {
{{"x", {{"a", 1}}}},
"yes"
);

test_template(t, "something in undefined",
"{% if x in y %}yes{% else %}no{% endif %}",
{{"x", 1}},
"no"
);

test_template(t, "null is undefined",
"{% if null is not defined %}yes{% else %}no{% endif %}",
json::object(),
"yes"
);

}

static void test_string_methods(testing & t) {
Expand Down