Update chat_template.jinja
Browse files- chat_template.jinja +100 -13
chat_template.jinja
CHANGED
|
@@ -1,14 +1,101 @@
|
|
| 1 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
'
|
| 6 |
-
'
|
| 7 |
-
'
|
| 8 |
-
'
|
| 9 |
-
'
|
| 10 |
-
'
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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='', is_first_sp=true, is_last_user=false) %}
|
| 5 |
+
{%- for message in messages %}
|
| 6 |
+
{%- if message['role'] == 'system' %}
|
| 7 |
+
{%- if ns.is_first_sp %}
|
| 8 |
+
{%- set ns.system_prompt = ns.system_prompt + message['content'] %}
|
| 9 |
+
{%- set ns.is_first_sp = false %}
|
| 10 |
+
{%- else %}
|
| 11 |
+
{%- set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
|
| 12 |
+
{%- endif %}
|
| 13 |
+
{%- endif %}
|
| 14 |
+
{%- endfor %}
|
| 15 |
|
| 16 |
+
{#- Adapted from https://github.com/sgl-project/sglang/blob/main/examples/chat_template/tool_chat_template_deepseekr1.jinja #}
|
| 17 |
+
{%- if tools is defined and tools is not none %}
|
| 18 |
+
{%- set tool_ns = namespace(text='You are a helpful assistant with tool calling capabilities. '
|
| 19 |
+
'When a tool call is needed, you MUST use the following format to issue the call:\n'
|
| 20 |
+
'<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>FUNCTION_NAME\n'
|
| 21 |
+
'```json\n{"param1": "value1", "param2": "value2"}\n```<|tool▁call▁end|><|tool▁calls▁end|>\n\n'
|
| 22 |
+
'Make sure the JSON is valid.'
|
| 23 |
+
'## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
|
| 24 |
+
{%- for tool in tools %}
|
| 25 |
+
{%- set tool_ns.text = tool_ns.text + '\n```json\n' + (tool | tojson) + '\n```\n' %}
|
| 26 |
+
{%- endfor %}
|
| 27 |
+
{%- if ns.system_prompt|length != 0 %}
|
| 28 |
+
{%- set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
|
| 29 |
+
{%- else %}
|
| 30 |
+
{%- set ns.system_prompt = tool_ns.text %}
|
| 31 |
+
{%- endif %}
|
| 32 |
+
{%- endif %}
|
| 33 |
+
{{- bos_token }}
|
| 34 |
+
{{- ns.system_prompt }}
|
| 35 |
+
{%- set last_index = (messages|length - 1) %}
|
| 36 |
+
{%- for message in messages %}
|
| 37 |
+
{%- set content = message['content'] %}
|
| 38 |
+
{%- if message['role'] == 'user' %}
|
| 39 |
+
{%- set ns.is_tool = false -%}
|
| 40 |
+
{%- set ns.is_first = false -%}
|
| 41 |
+
{%- set ns.is_last_user = true -%}
|
| 42 |
+
{%- if loop.index0 == last_index %}
|
| 43 |
+
{{- '<|User|>' + content }}
|
| 44 |
+
{%- else %}
|
| 45 |
+
{{- '<|User|>' + content + '<|Assistant|>'}}
|
| 46 |
+
{%- endif %}
|
| 47 |
+
{%- endif %}
|
| 48 |
+
{%- if message['role'] == 'assistant' %}
|
| 49 |
+
{%- if '</think>' in content %}
|
| 50 |
+
{%- set content = content.split('</think>')[-1] %}
|
| 51 |
+
{%- endif %}
|
| 52 |
+
{%- endif %}
|
| 53 |
+
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
|
| 54 |
+
{%- set ns.is_last_user = false -%}
|
| 55 |
+
{%- if ns.is_tool %}
|
| 56 |
+
{{- '<|tool▁outputs▁end|>'}}
|
| 57 |
+
{%- endif %}
|
| 58 |
+
{%- set ns.is_first = false %}
|
| 59 |
+
{%- set ns.is_tool = false -%}
|
| 60 |
+
{%- set ns.is_output_first = true %}
|
| 61 |
+
{%- for tool in message['tool_calls'] %}
|
| 62 |
+
{%- if not ns.is_first %}
|
| 63 |
+
{%- if content is none %}
|
| 64 |
+
{{- '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
|
| 65 |
+
{%- else %}
|
| 66 |
+
{{- content + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
|
| 67 |
+
{%- endif %}
|
| 68 |
+
{%- set ns.is_first = true -%}
|
| 69 |
+
{%- else %}
|
| 70 |
+
{{- '\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
|
| 71 |
+
{%- endif %}
|
| 72 |
+
{%- endfor %}
|
| 73 |
+
{{- '<|tool▁calls▁end|><|end▁of▁sentence|>'}}
|
| 74 |
+
{%- endif %}
|
| 75 |
+
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
|
| 76 |
+
{%- set ns.is_last_user = false -%}
|
| 77 |
+
{%- if ns.is_tool %}
|
| 78 |
+
{{- '<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>'}}
|
| 79 |
+
{%- set ns.is_tool = false -%}
|
| 80 |
+
{%- else %}
|
| 81 |
+
{{- content + '<|end▁of▁sentence|>'}}
|
| 82 |
+
{%- endif %}
|
| 83 |
+
{%- endif %}
|
| 84 |
+
{%- if message['role'] == 'tool' %}
|
| 85 |
+
{%- set ns.is_last_user = false -%}
|
| 86 |
+
{%- set ns.is_tool = true -%}
|
| 87 |
+
{%- if ns.is_output_first %}
|
| 88 |
+
{{- '<|tool▁outputs▁begin|><|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
|
| 89 |
+
{%- set ns.is_output_first = false %}
|
| 90 |
+
{%- else %}
|
| 91 |
+
{{- '\n<|tool▁output▁begin|>' + content + '<|tool▁output▁end|>'}}
|
| 92 |
+
{%- endif %}
|
| 93 |
+
{%- endif %}
|
| 94 |
+
{%- endfor -%}
|
| 95 |
+
{%- if ns.is_tool %}
|
| 96 |
+
{{- '<|tool▁outputs▁end|>'}}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{#- if add_generation_prompt and not ns.is_last_user and not ns.is_tool #}
|
| 99 |
+
{%- if add_generation_prompt and not ns.is_tool %}
|
| 100 |
+
{{- '<|Assistant|>'}}
|
| 101 |
+
{%- endif %}
|