Create chat_template.jinja
Browse files- chat_template.jinja +113 -0
chat_template.jinja
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{{- bos_token }}
|
2 |
+
{%- if not tools is defined %}
|
3 |
+
{%- set tools = none %}
|
4 |
+
{%- endif %}
|
5 |
+
{%- if not enable_thinking is defined %}
|
6 |
+
{%- set enable_thinking = false %}
|
7 |
+
{%- endif %}
|
8 |
+
{#- This block extracts the system message, so we can slot it into the right place. #}
|
9 |
+
{%- if messages[0]['role'] == 'system' %}
|
10 |
+
{%- set system_message = messages[0]['content']|trim %}
|
11 |
+
{%- set messages = messages[1:] %}
|
12 |
+
{%- else %}
|
13 |
+
{%- set system_message = "" %}
|
14 |
+
{%- endif %}
|
15 |
+
{#- Set the system message. If enable_thinking is true, add the "Enable deep thinking subroutine." #}
|
16 |
+
{%- if enable_thinking %}
|
17 |
+
{%- if system_message != "" %}
|
18 |
+
{%- set system_message = "Enable deep thinking subroutine.
|
19 |
+
" ~ system_message %}
|
20 |
+
{%- else %}
|
21 |
+
{%- set system_message = "Enable deep thinking subroutine." %}
|
22 |
+
{%- endif %}
|
23 |
+
{%- endif %}
|
24 |
+
{#- Set the system message. In case there are tools present, add them to the system message. #}
|
25 |
+
{%- if tools is not none or system_message != '' %}
|
26 |
+
{{- "<|start_header_id|>system<|end_header_id|>
|
27 |
+
" }}
|
28 |
+
{{- system_message }}
|
29 |
+
{%- if tools is not none %}
|
30 |
+
{%- if system_message != "" %}
|
31 |
+
{{- "
|
32 |
+
" }}
|
33 |
+
{%- endif %}
|
34 |
+
{{- "Available Tools:
|
35 |
+
" }}
|
36 |
+
{%- for t in tools %}
|
37 |
+
{{- t | tojson(indent=4) }}
|
38 |
+
{{- "
|
39 |
+
" }}
|
40 |
+
{%- endfor %}
|
41 |
+
{%- endif %}
|
42 |
+
{{- "<|eot_id|>" }}
|
43 |
+
{%- endif %}
|
44 |
+
{#- Rest of the messages #}
|
45 |
+
{%- for message in messages %}
|
46 |
+
{#- The special cases are when the message is from a tool (via role ipython/tool/tool_results) or when the message is from the assistant, but has "tool_calls". If not, we add the message directly as usual. #}
|
47 |
+
{#- Case 1 - Usual, non tool related message. #}
|
48 |
+
{%- if not (message.role == "ipython" or message.role == "tool" or message.role == "tool_results" or (message.tool_calls is defined and message.tool_calls is not none)) %}
|
49 |
+
{{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>
|
50 |
+
' }}
|
51 |
+
{%- if message['content'] is string %}
|
52 |
+
{{- message['content'] | trim }}
|
53 |
+
{%- else %}
|
54 |
+
{%- for item in message['content'] %}
|
55 |
+
{%- if item.type == 'text' %}
|
56 |
+
{{- item.text | trim }}
|
57 |
+
{%- endif %}
|
58 |
+
{%- endfor %}
|
59 |
+
{%- endif %}
|
60 |
+
{{- '<|eot_id|>' }}
|
61 |
+
|
62 |
+
{#- Case 2 - the response is from the assistant, but has a tool call returned. The assistant may also have returned some content along with the tool call. #}
|
63 |
+
{%- elif message.tool_calls is defined and message.tool_calls is not none %}
|
64 |
+
{{- "<|start_header_id|>assistant<|end_header_id|>
|
65 |
+
" }}
|
66 |
+
{%- if message['content'] is string %}
|
67 |
+
{{- message['content'] | trim }}
|
68 |
+
{%- else %}
|
69 |
+
{%- for item in message['content'] %}
|
70 |
+
{%- if item.type == 'text' %}
|
71 |
+
{{- item.text | trim }}
|
72 |
+
{%- if item.text | trim != "" %}
|
73 |
+
{{- "
|
74 |
+
" }}
|
75 |
+
{%- endif %}
|
76 |
+
{%- endif %}
|
77 |
+
{%- endfor %}
|
78 |
+
{%- endif %}
|
79 |
+
{{- "[" }}
|
80 |
+
{%- for tool_call in message.tool_calls %}
|
81 |
+
{%- set out = tool_call.function|tojson %}
|
82 |
+
{%- if not tool_call.id is defined %}
|
83 |
+
{{- out }}
|
84 |
+
{%- else %}
|
85 |
+
{{- out[:-1] }}
|
86 |
+
{{- ', "id": "' + tool_call.id + '"}' }}
|
87 |
+
{%- endif %}
|
88 |
+
{%- if not loop.last %}
|
89 |
+
{{- ", " }}
|
90 |
+
{%- else %}
|
91 |
+
{{- "]<|eot_id|>" }}
|
92 |
+
{%- endif %}
|
93 |
+
{%- endfor %}
|
94 |
+
|
95 |
+
{#- Case 3 - the response is from a tool call. The tool call may have an id associated with it as well. If it does, we add it to the prompt. #}
|
96 |
+
{%- elif message.role == "ipython" or message["role"] == "tool_results" or message["role"] == "tool" %}
|
97 |
+
{{- "<|start_header_id|>ipython<|end_header_id|>
|
98 |
+
" }}
|
99 |
+
{%- if message.tool_call_id is defined and message.tool_call_id != '' %}
|
100 |
+
{{- '{"content": ' + (message.content | tojson) + ', "call_id": "' + message.tool_call_id + '"}' }}
|
101 |
+
{%- else %}
|
102 |
+
{{- '{"content": ' + (message.content | tojson) + '}' }}
|
103 |
+
{%- endif %}
|
104 |
+
{{- "<|eot_id|>" }}
|
105 |
+
{%- endif %}
|
106 |
+
{%- endfor %}
|
107 |
+
{%- if add_generation_prompt %}
|
108 |
+
{{- '<|start_header_id|>assistant<|end_header_id|>
|
109 |
+
' }}
|
110 |
+
{%- if enable_thinking %}
|
111 |
+
{{- '<think>\n' }}
|
112 |
+
{%- endif %}
|
113 |
+
{%- endif %}
|