Skip to content

Commit 4e24cff

Browse files
ggerganovngxson
andauthored
server : handle content array in chat API (#8449)
* server : handle content array in chat API * Update examples/server/utils.hpp Co-authored-by: Xuan Son Nguyen <[email protected]> --------- Co-authored-by: Xuan Son Nguyen <[email protected]>
1 parent 6af51c0 commit 4e24cff

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

examples/server/utils.hpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,26 @@ inline std::string format_chat(const struct llama_model * model, const std::stri
122122

123123
for (size_t i = 0; i < messages.size(); ++i) {
124124
const auto & curr_msg = messages[i];
125-
std::string role = json_value(curr_msg, "role", std::string(""));
126-
std::string content = json_value(curr_msg, "content", std::string(""));
125+
126+
std::string role = json_value(curr_msg, "role", std::string(""));
127+
128+
std::string content;
129+
if (curr_msg.contains("content")) {
130+
if (curr_msg["content"].is_string()) {
131+
content = curr_msg["content"].get<std::string>();
132+
} else if (curr_msg["content"].is_array()) {
133+
for (const auto & part : curr_msg["content"]) {
134+
if (part.contains("text")) {
135+
content += "\n" + part["text"].get<std::string>();
136+
}
137+
}
138+
} else {
139+
throw std::runtime_error("Invalid 'content' type (ref: https://github.com/ggerganov/llama.cpp/issues/8367)");
140+
}
141+
} else {
142+
throw std::runtime_error("Missing 'content' (ref: https://github.com/ggerganov/llama.cpp/issues/8367)");
143+
}
144+
127145
chat.push_back({role, content});
128146
}
129147

0 commit comments

Comments
 (0)