Skip to content

Commit e1bff8f

Browse files
author
ochafik
committed
update deepseek r1 templates (+ put update commands in ./scripts/get_chat_template.py's comments)
1 parent 30dcfaa commit e1bff8f

File tree

4 files changed

+21
-61
lines changed

4 files changed

+21
-61
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{% 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='') %}{%- for message in messages %}{%- if message['role'] == 'system' %}{% set ns.system_prompt = message['content'] %}{%- endif %}{%- endfor %}{{bos_token}}{{ns.system_prompt}}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is none %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls']%}{%- if not ns.is_first %}{{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{%- set ns.is_first = true -%}{%- else %}{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- endfor %}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is not none %}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{% set content = message['content'] %}{% if '</think>' in content %}{% set content = content.split('</think>')[-1] %}{% endif %}{{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- set ns.is_output_first = false %}{%- else %}{{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{% endif %}{% if add_generation_prompt and not ns.is_tool %}{{'<|Assistant|>'}}{% endif %}
1+
{% 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='') %}{%- for message in messages %}{%- if message['role'] == 'system' %}{% set ns.system_prompt = message['content'] %}{%- endif %}{%- endfor %}{{bos_token}}{{ns.system_prompt}}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is none %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls']%}{%- if not ns.is_first %}{{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{%- set ns.is_first = true -%}{%- else %}{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- endfor %}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is not none %}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{% set content = message['content'] %}{% if '</think>' in content %}{% set content = content.split('</think>')[-1] %}{% endif %}{{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- set ns.is_output_first = false %}{%- else %}{{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{% endif %}{% if add_generation_prompt and not ns.is_tool %}{{'<|Assistant|><think>\n'}}{% endif %}
Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1 @@
1-
{% if not add_generation_prompt is defined %}
2-
{% set add_generation_prompt = false %}
3-
{% endif %}
4-
{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') %}
5-
{%- for message in messages %}
6-
{%- if message['role'] == 'system' %}
7-
{% set ns.system_prompt = message['content'] %}
8-
{%- endif %}
9-
{%- endfor %}
10-
{{bos_token}}
11-
{{ns.system_prompt}}
12-
{%- for message in messages %}
13-
{%- if message['role'] == 'user' %}
14-
{%- set ns.is_tool = false -%}
15-
{{'<|User|>' + message['content']}}
16-
{%- endif %}
17-
{%- if message['role'] == 'assistant' and message['content'] is none %}
18-
{%- set ns.is_tool = false -%}
19-
{%- for tool in message['tool_calls']%}
20-
{%- if not ns.is_first %}
21-
{{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
22-
{%- set ns.is_first = true -%}
23-
{%- else %}
24-
{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
25-
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
26-
{%- endif %}
27-
{%- endfor %}
28-
{%- endif %}
29-
{%- if message['role'] == 'assistant' and message['content'] is not none %}
30-
{%- if ns.is_tool %}
31-
{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}
32-
{%- set ns.is_tool = false -%}
33-
{%- else %}
34-
{% set content = message['content'] %}
35-
{% if '</think>' in content %}
36-
{% set content = content.split('</think>')[-1] %}
37-
{% endif %}
38-
{{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}
39-
{%- endif %}
40-
{%- endif %}
41-
{%- if message['role'] == 'tool' %}
42-
{%- set ns.is_tool = true -%}
43-
{%- if ns.is_output_first %}
44-
{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
45-
{%- set ns.is_output_first = false %}
46-
{%- else %}
47-
{{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
48-
{%- endif %}
49-
{%- endif %}
50-
{%- endfor -%}
51-
{% if ns.is_tool %}
52-
{{'<|tool▁outputs▁end|>'}}
53-
{% endif %}
54-
{% if add_generation_prompt and not ns.is_tool %}
55-
{{'<|Assistant|>'}}
56-
{% endif %}
1+
{% 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='') %}{%- for message in messages %}{%- if message['role'] == 'system' %}{% set ns.system_prompt = message['content'] %}{%- endif %}{%- endfor %}{{bos_token}}{{ns.system_prompt}}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is none %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls']%}{%- if not ns.is_first %}{{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{%- set ns.is_first = true -%}{%- else %}{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- endfor %}{%- endif %}{%- if message['role'] == 'assistant' and message['content'] is not none %}{%- if ns.is_tool %}{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{% set content = message['content'] %}{% if '</think>' in content %}{% set content = content.split('</think>')[-1] %}{% endif %}{{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_tool = true -%}{%- if ns.is_output_first %}{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- set ns.is_output_first = false %}{%- else %}{{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endif %}{%- endfor -%}{% if ns.is_tool %}{{'<|tool▁outputs▁end|>'}}{% endif %}{% if add_generation_prompt and not ns.is_tool %}{{'<|Assistant|><think>\n'}}{% endif %}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ Example function tool call syntax:
7272
{%- endfor -%}
7373
{{- flush_tool_outputs() -}}
7474
{%- if add_generation_prompt and not ns.is_tool_outputs -%}
75-
{{- '<|Assistant|>' -}}
75+
{{- '<|Assistant|><think>\n' -}}
7676
{%- endif -%}

scripts/get_chat_template.py

100644100755
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,24 @@
77
./scripts/get_chat_template.py model_id [variant]
88
99
Examples:
10-
./scripts/get_chat_template.py NousResearch/Meta-Llama-3-8B-Instruct
11-
./scripts/get_chat_template.py NousResearch/Hermes-3-Llama-3.1-8B tool_use
12-
./scripts/get_chat_template.py meta-llama/Llama-3.2-3B-Instruct
10+
./scripts/get_chat_template.py CohereForAI/c4ai-command-r-plus tool_use | tee models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja
11+
./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 default | tee models/templates/CohereForAI-c4ai-command-r7b-12-2024-default.jinja
12+
./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 rag | tee models/templates/CohereForAI-c4ai-command-r7b-12-2024-rag.jinja
13+
./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 tool_use | tee models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja
14+
./scripts/get_chat_template.py deepseek-ai/DeepSeek-R1-Distill-Llama-8B | tee models/templates/deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja
15+
./scripts/get_chat_template.py deepseek-ai/DeepSeek-R1-Distill-Qwen-32B | tee models/templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja
16+
./scripts/get_chat_template.py fireworks-ai/llama-3-firefunction-v2 | tee models/templates/fireworks-ai-llama-3-firefunction-v2.jinja
17+
./scripts/get_chat_template.py google/gemma-2-2b-it | tee models/templates/google-gemma-2-2b-it.jinja
18+
./scripts/get_chat_template.py meetkai/functionary-medium-v3. | tee models/templates/meetkai-functionary-medium-v3.jinja
19+
./scripts/get_chat_template.py meetkai/functionary-medium-v3.2 | tee models/templates/meetkai-functionary-medium-v3.2.jinja
20+
./scripts/get_chat_template.py meta-llama/Llama-3.1-8B-Instruct | tee models/templates/meta-llama-Llama-3.1-8B-Instruct.jinja
21+
./scripts/get_chat_template.py meta-llama/Llama-3.2-3B-Instruct | tee models/templates/meta-llama-Llama-3.2-3B-Instruct.jinja
22+
./scripts/get_chat_template.py meta-llama/Llama-3.3-70B-Instruct | tee models/templates/meta-llama-Llama-3.3-70B-Instruct.jinja
23+
./scripts/get_chat_template.py microsoft/Phi-3.5-mini-instruct | tee models/templates/microsoft-Phi-3.5-mini-instruct.jinja
24+
./scripts/get_chat_template.py mistralai/Mistral-Nemo-Instruct-2407 | tee models/templates/mistralai-Mistral-Nemo-Instruct-2407.jinja
25+
./scripts/get_chat_template.py NousResearch/Hermes-2-Pro-Llama-3-8B tool_use | tee models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja
26+
./scripts/get_chat_template.py NousResearch/Hermes-3-Llama-3.1-8B tool_use | tee models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja
27+
./scripts/get_chat_template.py Qwen/Qwen2.5-7B-Instruct | tee models/templates/Qwen-Qwen2.5-7B-Instruct.jinja
1328
'''
1429

1530
import json

0 commit comments

Comments
 (0)