Skip to content

Commit 9ef0780

Browse files
authored
Fix new line issue with chat template, disable template when in-prefix/suffix is set (#8203)
* preserve new line llama_chat_format_single * disable chat template if in-prefix/suffix is set * remove redundant change
1 parent 1c5eba6 commit 9ef0780

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

common/common.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,16 +1014,19 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
10141014
}
10151015
if (arg == "--in-prefix-bos") {
10161016
params.input_prefix_bos = true;
1017+
params.enable_chat_template = false;
10171018
return true;
10181019
}
10191020
if (arg == "--in-prefix") {
10201021
CHECK_ARG
10211022
params.input_prefix = argv[i];
1023+
params.enable_chat_template = false;
10221024
return true;
10231025
}
10241026
if (arg == "--in-suffix") {
10251027
CHECK_ARG
10261028
params.input_suffix = argv[i];
1029+
params.enable_chat_template = false;
10271030
return true;
10281031
}
10291032
if (arg == "--spm-infill") {
@@ -1406,7 +1409,7 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
14061409
"halt generation at PROMPT, return control in interactive mode\n"
14071410
"can be specified more than once for multiple prompts" });
14081411
options.push_back({ "main", "-sp, --special", "special tokens output enabled (default: %s)", params.special ? "true" : "false" });
1409-
options.push_back({ "main", "-cnv, --conversation", "run in conversation mode (does not print special tokens and suffix/prefix) (default: %s)", params.conversation ? "true" : "false" });
1412+
options.push_back({ "main", "-cnv, --conversation", "run in conversation mode (does not print special tokens and suffix/prefix, use default chat template) (default: %s)", params.conversation ? "true" : "false" });
14101413
options.push_back({ "main infill", "-i, --interactive", "run in interactive mode (default: %s)", params.interactive ? "true" : "false" });
14111414
options.push_back({ "main infill", "-if, --interactive-first", "run in interactive mode and wait for input right away (default: %s)", params.interactive_first ? "true" : "false" });
14121415
options.push_back({ "main infill", "-mli, --multiline-input", "allows you to write or paste multiple lines without ending each in '\\'" });
@@ -2668,12 +2671,19 @@ std::string llama_chat_format_single(const struct llama_model * model,
26682671
const std::vector<llama_chat_msg> & past_msg,
26692672
const llama_chat_msg & new_msg,
26702673
bool add_ass) {
2674+
std::ostringstream ss;
26712675
auto fmt_past_msg = llama_chat_apply_template(model, tmpl, past_msg, false);
26722676
std::vector<llama_chat_msg> chat_new(past_msg);
2677+
// if the past_msg ends with a newline, we must preserve it in the formatted version
2678+
if (add_ass && !fmt_past_msg.empty() && fmt_past_msg.back() == '\n') {
2679+
ss << "\n";
2680+
};
2681+
// format chat with new_msg
26732682
chat_new.push_back(new_msg);
26742683
auto fmt_new_msg = llama_chat_apply_template(model, tmpl, chat_new, add_ass);
2675-
auto formatted = fmt_new_msg.substr(fmt_past_msg.size(), fmt_new_msg.size() - fmt_past_msg.size());
2676-
return formatted;
2684+
// get the diff part
2685+
ss << fmt_new_msg.substr(fmt_past_msg.size(), fmt_new_msg.size() - fmt_past_msg.size());
2686+
return ss.str();
26772687
}
26782688

26792689
std::string llama_chat_format_example(const struct llama_model * model,

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ struct gpt_params {
200200
std::string public_path = "";
201201
std::string chat_template = "";
202202
std::string system_prompt = "";
203+
bool enable_chat_template = true;
203204

204205
std::vector<std::string> api_keys;
205206

examples/main/main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ int main(int argc, char ** argv) {
261261
std::vector<llama_token> embd_inp;
262262

263263
{
264-
auto prompt = params.conversation
264+
auto prompt = (params.conversation && params.enable_chat_template)
265265
? chat_add_and_format(model, chat_msgs, "system", params.prompt) // format the system prompt in conversation mode
266266
: params.prompt;
267267
if (params.interactive_first || !params.prompt.empty() || session_tokens.empty()) {
@@ -810,7 +810,9 @@ int main(int argc, char ** argv) {
810810
is_antiprompt = true;
811811
}
812812

813-
chat_add_and_format(model, chat_msgs, "assistant", assistant_ss.str());
813+
if (params.enable_chat_template) {
814+
chat_add_and_format(model, chat_msgs, "assistant", assistant_ss.str());
815+
}
814816
is_interacting = true;
815817
printf("\n");
816818
}
@@ -872,12 +874,13 @@ int main(int argc, char ** argv) {
872874
string_process_escapes(buffer);
873875
}
874876

875-
std::string user_inp = params.conversation
877+
bool format_chat = params.conversation && params.enable_chat_template;
878+
std::string user_inp = format_chat
876879
? chat_add_and_format(model, chat_msgs, "user", std::move(buffer))
877880
: std::move(buffer);
878881
// TODO: one inconvenient of current chat template implementation is that we can't distinguish between user input and special tokens (prefix/postfix)
879882
const auto line_pfx = ::llama_tokenize(ctx, params.input_prefix, false, true);
880-
const auto line_inp = ::llama_tokenize(ctx, user_inp, false, params.conversation);
883+
const auto line_inp = ::llama_tokenize(ctx, user_inp, false, format_chat);
881884
const auto line_sfx = ::llama_tokenize(ctx, params.input_suffix, false, true);
882885

883886
LOG("input tokens: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, line_inp).c_str());

tests/test-chat-template.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ int main(void) {
142142
std::cout << "fmt_single(" << tmpl << ")\n" << output << "\n-------------------------\n";
143143
return output;
144144
};
145-
assert(fmt_single("chatml") == "<|im_start|>user\nHow are you<|im_end|>\n<|im_start|>assistant\n");
145+
assert(fmt_single("chatml") == "\n<|im_start|>user\nHow are you<|im_end|>\n<|im_start|>assistant\n");
146146
assert(fmt_single("llama2") == "[INST] How are you [/INST]");
147-
assert(fmt_single("gemma") == "<start_of_turn>user\nHow are you<end_of_turn>\n<start_of_turn>model\n");
147+
assert(fmt_single("gemma") == "\n<start_of_turn>user\nHow are you<end_of_turn>\n<start_of_turn>model\n");
148148
assert(fmt_single("llama3") == "<|start_header_id|>user<|end_header_id|>\n\nHow are you<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n");
149149

150150
return 0;

0 commit comments

Comments
 (0)