From 9eec4c1eb314d5d208e802696d23ebb5592ea02c Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 12 Jul 2024 12:02:44 +0300 Subject: [PATCH 1/2] server : handle content array in chat API --- examples/server/utils.hpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/examples/server/utils.hpp b/examples/server/utils.hpp index 7ef2a519a10c7..4d6dcf550b290 100644 --- a/examples/server/utils.hpp +++ b/examples/server/utils.hpp @@ -122,8 +122,26 @@ inline std::string format_chat(const struct llama_model * model, const std::stri for (size_t i = 0; i < messages.size(); ++i) { const auto & curr_msg = messages[i]; - std::string role = json_value(curr_msg, "role", std::string("")); - std::string content = json_value(curr_msg, "content", std::string("")); + + std::string role = json_value(curr_msg, "role", std::string("")); + + std::string content; + if (curr_msg.contains("content")) { + if (curr_msg["content"].is_string()) { + content = curr_msg["content"].get(); + } else if (curr_msg["content"].is_array()) { + for (const auto & part : curr_msg["content"]) { + if (part.contains("text")) { + content += part["text"].get(); + } + } + } else { + throw std::runtime_error("Invalid 'content' type (ref: https://github.com/ggerganov/llama.cpp/issues/8367)"); + } + } else { + throw std::runtime_error("Missing 'content' (ref: https://github.com/ggerganov/llama.cpp/issues/8367)"); + } + chat.push_back({role, content}); } From 8428a98770cd0dae0d0badeebec66da6baf9c2d5 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Fri, 12 Jul 2024 12:37:59 +0300 Subject: [PATCH 2/2] Update examples/server/utils.hpp Co-authored-by: Xuan Son Nguyen --- examples/server/utils.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/server/utils.hpp b/examples/server/utils.hpp index 4d6dcf550b290..db6b3b74d1dd2 100644 --- a/examples/server/utils.hpp +++ b/examples/server/utils.hpp @@ -132,7 +132,7 @@ inline std::string format_chat(const struct llama_model * model, const std::stri } else if (curr_msg["content"].is_array()) { for (const auto & part : curr_msg["content"]) { if (part.contains("text")) { - content += part["text"].get(); + content += "\n" + part["text"].get(); } } } else {