simpler hacky fixes for original broken template (+ fix minja example syntax polyfill)
This commit is contained in:
parent
c6214ee9d6
commit
1c302e18ba
2 changed files with 24 additions and 10 deletions
|
@ -569,11 +569,25 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
|
||||||
};
|
};
|
||||||
}, grammar_options);
|
}, grammar_options);
|
||||||
auto prompt = tmpl.apply(inputs.messages, inputs.tools.empty() ? json() : inputs.tools, inputs.add_generation_prompt);
|
auto prompt = tmpl.apply(inputs.messages, inputs.tools.empty() ? json() : inputs.tools, inputs.add_generation_prompt);
|
||||||
// Hack to fix the official prompt, which leaves the chat dangling after tool results.
|
|
||||||
if (string_ends_with(prompt, "<|tool▁outputs▁end|>")) {
|
// Hacks to fix the official (broken) prompt.
|
||||||
prompt += "<|end▁of▁sentence|>";
|
// It is advisable to use --chat-template-file models/templates/llama-cpp-deepseek-r1.jinja instead,
|
||||||
if (inputs.add_generation_prompt) {
|
// until the official template is fixed.
|
||||||
prompt += "<|Assistant|>";
|
if (tmpl.source().find("{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}") != std::string::npos) {
|
||||||
|
// Don't leave the chat dangling after tool results
|
||||||
|
if (string_ends_with(prompt, "<|tool▁outputs▁end|>")) {
|
||||||
|
prompt += "<|end▁of▁sentence|>";
|
||||||
|
if (inputs.add_generation_prompt) {
|
||||||
|
prompt += "<|Assistant|>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Fix up tool call delta example added by Minja
|
||||||
|
std::string marker = "<|tool▁call▁end|>\n";
|
||||||
|
auto pos = prompt.rfind(marker);
|
||||||
|
if (pos != std::string::npos) {
|
||||||
|
prompt.insert(pos + marker.size() - 1, "<|tool▁calls▁end|>");
|
||||||
|
} else {
|
||||||
|
LOG_WRN("Failed to find expected broken tool call example marker in prompt\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data.prompt = prompt;
|
data.prompt = prompt;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{%- if not add_generation_prompt is defined -%}
|
{%- if not add_generation_prompt is defined -%}
|
||||||
{%- set add_generation_prompt = false -%}
|
{%- set add_generation_prompt = false -%}
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
{%- set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') -%}
|
{%- set ns = namespace(is_first=false, is_tool_outputs=false, is_output_first=true, system_prompt='') -%}
|
||||||
{%- for message in messages -%}
|
{%- for message in messages -%}
|
||||||
{%- if message['role'] == 'system' -%}
|
{%- if message['role'] == 'system' -%}
|
||||||
{%- set ns.system_prompt = message['content'] -%}
|
{%- set ns.system_prompt = message['content'] -%}
|
||||||
|
@ -25,9 +25,9 @@ Example function tool call syntax:
|
||||||
{% endif -%}
|
{% endif -%}
|
||||||
{{ns.system_prompt}}
|
{{ns.system_prompt}}
|
||||||
{%- macro flush_tool_outputs() -%}
|
{%- macro flush_tool_outputs() -%}
|
||||||
{%- if ns.is_tool -%}
|
{%- if ns.is_tool_outputs -%}
|
||||||
{{- '<|tool▁outputs▁end|><|end▁of▁sentence|>' -}}
|
{{- '<|tool▁outputs▁end|><|end▁of▁sentence|>' -}}
|
||||||
{%- set ns.is_tool = false -%}
|
{%- set ns.is_tool_outputs = false -%}
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
{%- endmacro -%}
|
{%- endmacro -%}
|
||||||
{{- flush_tool_outputs() -}}
|
{{- flush_tool_outputs() -}}
|
||||||
|
@ -62,7 +62,7 @@ Example function tool call syntax:
|
||||||
{{- '<|Assistant|>' + content + '<|end▁of▁sentence|>'}}
|
{{- '<|Assistant|>' + content + '<|end▁of▁sentence|>'}}
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
{%- if message['role'] == 'tool' -%}
|
{%- if message['role'] == 'tool' -%}
|
||||||
{%- set ns.is_tool = true -%}
|
{%- set ns.is_tool_outputs = true -%}
|
||||||
{%- if ns.is_output_first -%}
|
{%- if ns.is_output_first -%}
|
||||||
{{- '<|tool▁outputs▁begin|>' -}}
|
{{- '<|tool▁outputs▁begin|>' -}}
|
||||||
{%- set ns.is_output_first = false -%}
|
{%- set ns.is_output_first = false -%}
|
||||||
|
@ -71,6 +71,6 @@ Example function tool call syntax:
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
{%- endfor -%}
|
{%- endfor -%}
|
||||||
{{- flush_tool_outputs() -}}
|
{{- flush_tool_outputs() -}}
|
||||||
{%- if add_generation_prompt and not ns.is_tool -%}
|
{%- if add_generation_prompt and not ns.is_tool_outputs -%}
|
||||||
{{- '<|Assistant|>' -}}
|
{{- '<|Assistant|>' -}}
|
||||||
{%- endif -%}
|
{%- endif -%}
|
Loading…
Add table
Add a link
Reference in a new issue