Skip to content

Commit f06140f

Browse files
grf53ochafik
andauthored
Consider "tool_calls" instead of "content" in messages (#63)
* fix: require one of 'content' or 'tool_calls' in messages * fix: check content is given during polyfill tool_calls --------- Co-authored-by: Olivier Chafik <ochafik@users.noreply.github.com>
1 parent 17a767a commit f06140f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

include/minja/chat-template.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ class chat_template {
395395

396396
for (const auto & message_ : adjusted_messages) {
397397
auto message = message_;
398-
if (!message.contains("role") || !message.contains("content")) {
399-
throw std::runtime_error("message must have 'role' and 'content' fields: " + message.dump());
398+
if (!message.contains("role") || (!message.contains("content") && !message.contains("tool_calls"))) {
399+
throw std::runtime_error("message must have 'role' and one of 'content' or 'tool_calls' fields: " + message.dump());
400400
}
401401
std::string role = message.at("role");
402402

@@ -417,7 +417,6 @@ class chat_template {
417417
}
418418
}
419419
if (polyfill_tool_calls) {
420-
auto content = message.at("content");
421420
auto tool_calls = json::array();
422421
for (const auto & tool_call : message.at("tool_calls")) {
423422
if (tool_call.at("type") != "function") {
@@ -436,8 +435,11 @@ class chat_template {
436435
auto obj = json {
437436
{"tool_calls", tool_calls},
438437
};
439-
if (!content.is_null() && !content.empty()) {
440-
obj["content"] = content;
438+
if (message.contains("content")) {
439+
auto content = message.at("content");
440+
if (!content.is_null() && !content.empty()) {
441+
obj["content"] = content;
442+
}
441443
}
442444
message["content"] = obj.dump(2);
443445
message.erase("tool_calls");

0 commit comments

Comments
 (0)