Skip to content

Commit d92f518

Browse files
committed
Simplify logics even further
* if no `chat_template` is passed, we can rely on `common_chat_apply_template` function
1 parent b2cf6e7 commit d92f518

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

examples/server/server.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,6 @@ int main(int argc, char ** argv) {
32263226
LOG_WRN("%s: Prefix and suffix will be used for a custom chat template. This may cause the model to output suboptimal responses\n", __func__);
32273227
} else if (!ctx_server.validate_model_chat_template()) {
32283228
LOG_WRN("%s: The chat template that comes with this model is not yet supported, falling back to chatml. This may cause the model to output suboptimal responses\n", __func__);
3229-
params.chat_template = "chatml";
32303229
}
32313230
} else if (!params.input_prefix.empty() || !params.input_suffix.empty()) {
32323231
LOG_WRN("%s: Prefix and suffix are defined, but will not be used because a chat template '%s' is chosen.\n", __func__, params.chat_template.c_str());

examples/server/utils.hpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ inline std::string format_chat(const struct llama_model * model, const std::stri
304304
std::vector<common_chat_msg> chat;
305305
std::string formatted_chat;
306306

307-
bool is_custom = !prefix.empty() || !suffix.empty();
307+
bool is_custom = tmpl.empty() && (!prefix.empty() || !suffix.empty());
308308

309309
for (size_t i = 0; i < messages.size(); ++i) {
310310
const auto & curr_msg = messages[i];
@@ -337,7 +337,13 @@ inline std::string format_chat(const struct llama_model * model, const std::stri
337337
}
338338
}
339339

340-
if (!is_custom) formatted_chat = common_chat_apply_template(model, tmpl, chat, true);
340+
if (!is_custom) {
341+
LOG_WRN("Using '%s' template, prefix and suffix are ignored.\n", tmpl.c_str());
342+
formatted_chat = common_chat_apply_template(model, tmpl, chat, true);
343+
} else {
344+
LOG_WRN("Used prefix '%s' and suffix '%s'.\n", prefix.c_str(), suffix.c_str());
345+
}
346+
341347
LOG_DBG("formatted_chat using '%s': '%s'\n", tmpl.c_str(), formatted_chat.c_str());
342348

343349
return formatted_chat;
@@ -353,7 +359,7 @@ inline std::string format_chat_example(const struct llama_model * model, const s
353359

354360
std::string formatted_example;
355361

356-
if (!prefix.empty() || !suffix.empty()) {
362+
if (tmpl.empty() && (!prefix.empty() || !suffix.empty())) {
357363
for (auto message : msgs) {
358364
if (message.role == "user") formatted_example += prefix + message.content + suffix;
359365
else formatted_example += message.content;
@@ -640,20 +646,12 @@ static json oaicompat_completion_params_parse(
640646
std::string prefix = (body.contains("input_prefix") ? body.at("input_prefix").get<std::string>() : "");
641647
std::string suffix = (body.contains("input_suffix") ? body.at("input_suffix").get<std::string>() : "");
642648

643-
// if template is sent in data, ignore prefix and suffix
644-
if (!chat_tmpl.empty()) {
645-
LOG_WRN("\nUsing '%s' template, prefix and suffix are ignored.\n", chat_tmpl.c_str());
646-
prefix = "";
647-
suffix = "";
648-
} else {
649-
if (prefix.empty()) {
650-
prefix = input_prefix;
651-
}
649+
if (prefix.empty()) {
650+
prefix = input_prefix;
651+
}
652652

653-
if (suffix.empty()) {
654-
suffix = input_suffix;
655-
}
656-
LOG_WRN("\nUsing prefix '%s' and suffix '%s'.\n", prefix.c_str(), suffix.c_str());
653+
if (suffix.empty()) {
654+
suffix = input_suffix;
657655
}
658656

659657
llama_params["prompt"] = format_chat(model, chat_tmpl, prefix, suffix, body.at("messages"));

0 commit comments

Comments
 (0)