Skip to content

Commit 1c302e1

Browse files
author
Olivier Chafik
committed
simpler hacky fixes for original broken template (+ fix minja example syntax polyfill)
1 parent c6214ee commit 1c302e1

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

common/chat.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,25 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
569569
};
570570
}, grammar_options);
571571
auto prompt = tmpl.apply(inputs.messages, inputs.tools.empty() ? json() : inputs.tools, inputs.add_generation_prompt);
572-
// Hack to fix the official prompt, which leaves the chat dangling after tool results.
573-
if (string_ends_with(prompt, "<|tool▁outputs▁end|>")) {
574-
prompt += "<|end▁of▁sentence|>";
575-
if (inputs.add_generation_prompt) {
576-
prompt += "<|Assistant|>";
572+
573+
// Hacks to fix the official (broken) prompt.
574+
// It is advisable to use --chat-template-file models/templates/llama-cpp-deepseek-r1.jinja instead,
575+
// until the official template is fixed.
576+
if (tmpl.source().find("{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}") != std::string::npos) {
577+
// Don't leave the chat dangling after tool results
578+
if (string_ends_with(prompt, "<|tool▁outputs▁end|>")) {
579+
prompt += "<|end▁of▁sentence|>";
580+
if (inputs.add_generation_prompt) {
581+
prompt += "<|Assistant|>";
582+
}
583+
}
584+
// Fix up tool call delta example added by Minja
585+
std::string marker = "<|tool▁call▁end|>\n";
586+
auto pos = prompt.rfind(marker);
587+
if (pos != std::string::npos) {
588+
prompt.insert(pos + marker.size() - 1, "<|tool▁calls▁end|>");
589+
} else {
590+
LOG_WRN("Failed to find expected broken tool call example marker in prompt\n");
577591
}
578592
}
579593
data.prompt = prompt;

models/templates/llama-cpp-deepseek-r1.jinja

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{%- if not add_generation_prompt is defined -%}
22
{%- set add_generation_prompt = false -%}
33
{%- endif -%}
4-
{%- set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') -%}
4+
{%- set ns = namespace(is_first=false, is_tool_outputs=false, is_output_first=true, system_prompt='') -%}
55
{%- for message in messages -%}
66
{%- if message['role'] == 'system' -%}
77
{%- set ns.system_prompt = message['content'] -%}
@@ -25,9 +25,9 @@ Example function tool call syntax:
2525
{% endif -%}
2626
{{ns.system_prompt}}
2727
{%- macro flush_tool_outputs() -%}
28-
{%- if ns.is_tool -%}
28+
{%- if ns.is_tool_outputs -%}
2929
{{- '<|tool▁outputs▁end|><|end▁of▁sentence|>' -}}
30-
{%- set ns.is_tool = false -%}
30+
{%- set ns.is_tool_outputs = false -%}
3131
{%- endif -%}
3232
{%- endmacro -%}
3333
{{- flush_tool_outputs() -}}
@@ -62,7 +62,7 @@ Example function tool call syntax:
6262
{{- '<|Assistant|>' + content + '<|end▁of▁sentence|>'}}
6363
{%- endif -%}
6464
{%- if message['role'] == 'tool' -%}
65-
{%- set ns.is_tool = true -%}
65+
{%- set ns.is_tool_outputs = true -%}
6666
{%- if ns.is_output_first -%}
6767
{{- '<|tool▁outputs▁begin|>' -}}
6868
{%- set ns.is_output_first = false -%}
@@ -71,6 +71,6 @@ Example function tool call syntax:
7171
{%- endif -%}
7272
{%- endfor -%}
7373
{{- flush_tool_outputs() -}}
74-
{%- if add_generation_prompt and not ns.is_tool -%}
74+
{%- if add_generation_prompt and not ns.is_tool_outputs -%}
7575
{{- '<|Assistant|>' -}}
7676
{%- endif -%}

0 commit comments

Comments
 (0)