diff --git a/common/chat.cpp b/common/chat.cpp index c33a3c991..a7a51b645 100644 --- a/common/chat.cpp +++ b/common/chat.cpp @@ -569,11 +569,25 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_ }; }, grammar_options); 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|>")) { - prompt += "<|end▁of▁sentence|>"; - if (inputs.add_generation_prompt) { - prompt += "<|Assistant|>"; + + // Hacks to fix the official (broken) prompt. + // It is advisable to use --chat-template-file models/templates/llama-cpp-deepseek-r1.jinja instead, + // until the official template is fixed. + 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; diff --git a/models/templates/llama-cpp-deepseek-r1.jinja b/models/templates/llama-cpp-deepseek-r1.jinja index 94b41f09b..598113b4a 100644 --- a/models/templates/llama-cpp-deepseek-r1.jinja +++ b/models/templates/llama-cpp-deepseek-r1.jinja @@ -1,7 +1,7 @@ {%- if not add_generation_prompt is defined -%} {%- set add_generation_prompt = false -%} {%- 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 -%} {%- if message['role'] == 'system' -%} {%- set ns.system_prompt = message['content'] -%} @@ -25,9 +25,9 @@ Example function tool call syntax: {% endif -%} {{ns.system_prompt}} {%- macro flush_tool_outputs() -%} - {%- if ns.is_tool -%} + {%- if ns.is_tool_outputs -%} {{- '<|tool▁outputs▁end|><|end▁of▁sentence|>' -}} - {%- set ns.is_tool = false -%} + {%- set ns.is_tool_outputs = false -%} {%- endif -%} {%- endmacro -%} {{- flush_tool_outputs() -}} @@ -62,7 +62,7 @@ Example function tool call syntax: {{- '<|Assistant|>' + content + '<|end▁of▁sentence|>'}} {%- endif -%} {%- if message['role'] == 'tool' -%} - {%- set ns.is_tool = true -%} + {%- set ns.is_tool_outputs = true -%} {%- if ns.is_output_first -%} {{- '<|tool▁outputs▁begin|>' -}} {%- set ns.is_output_first = false -%} @@ -71,6 +71,6 @@ Example function tool call syntax: {%- endif -%} {%- endfor -%} {{- flush_tool_outputs() -}} -{%- if add_generation_prompt and not ns.is_tool -%} +{%- if add_generation_prompt and not ns.is_tool_outputs -%} {{- '<|Assistant|>' -}} {%- endif -%} \ No newline at end of file