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
14 changes: 13 additions & 1 deletion common/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2877,7 +2877,19 @@ static common_chat_params common_chat_templates_apply_jinja(
: *tmpls->template_default;
const auto & src = tmpl.source();
const auto & caps = tmpl.original_caps();
params.messages = common_chat_msgs_to_json_oaicompat(inputs.messages, /* concat_text= */ !tmpl.original_caps().requires_typed_content);
params.messages = common_chat_msgs_to_json_oaicompat(inputs.messages, /* concat_text= */ !caps.requires_typed_content);

// If template requires typed content, ensure all string contents are converted into json arrays
if (caps.requires_typed_content) {
for (auto & msg : params.messages) {
if (msg.contains("content") && msg.at("content").is_string()) {
const auto text = msg.at("content").get<std::string>();
json content_arr = json::array();
content_arr.push_back(json::object({ {"type", "text"}, {"text", text} }));
msg["content"] = std::move(content_arr);
}
}
}
params.add_generation_prompt = inputs.add_generation_prompt;
params.tool_choice = inputs.tool_choice;
params.reasoning_format = inputs.reasoning_format;
Expand Down
8 changes: 7 additions & 1 deletion common/jinja/caps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,19 @@ caps caps_get(jinja::program & prog) {
// tools
return json{nullptr};
},
[&](bool, value & messages, value &) {
[&](bool success, value & messages, value &) {
auto & content = messages->at(0)->at("content");
caps_print_stats(content, "messages[0].content");
if (has_op(content, "selectattr") || has_op(content, "array_access")) {
// accessed as an array
result.requires_typed_content = true;
}

// If the template uses content and fails with a string, it likely expects an array
if (!success && content->stats.used) {
JJ_DEBUG("%s", "Template failed with string content, likely expects typed content array");
result.requires_typed_content = true;
}
}
);

Expand Down
Loading