Upload chat_template.jinja
Browse files- chat_template.jinja +129 -0
chat_template.jinja
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{%- macro tool_call_id_to_int(messages, tool_call_id) %}
|
2 |
+
{%- set counter = namespace(value=0) %}
|
3 |
+
{%- set tool_call_id_seen = namespace(value=false) %}
|
4 |
+
{%- for msg in messages %}
|
5 |
+
{%- if msg.tool_calls %}
|
6 |
+
{%- for tool_call in msg.tool_calls %}
|
7 |
+
{%- if tool_call.id == tool_call_id and not tool_call_id_seen.value -%}
|
8 |
+
{{ counter.value }}
|
9 |
+
{%- set tool_call_id_seen.value = true %}
|
10 |
+
{%- endif %}
|
11 |
+
{%- set counter.value = counter.value + 1 %}
|
12 |
+
{%- endfor %}
|
13 |
+
{%- endif %}
|
14 |
+
{%- endfor %}
|
15 |
+
{%- endmacro %}
|
16 |
+
|
17 |
+
{%- macro check_is_error_tool(tool_msg) -%}
|
18 |
+
{%- if tool_msg.is_error_tool -%}
|
19 |
+
True
|
20 |
+
{%- else -%}
|
21 |
+
False
|
22 |
+
{%- endif -%}
|
23 |
+
{%- endmacro -%}
|
24 |
+
|
25 |
+
{%- macro format_tool_message(messages, tool_msg) -%}
|
26 |
+
{# format tool message #}
|
27 |
+
{
|
28 |
+
"tool_call_id": "{{ tool_msg.tool_call_id }}",
|
29 |
+
"results": {
|
30 |
+
"0": {{ tool_msg.content|tojson }}
|
31 |
+
},
|
32 |
+
"is_error_tool": {{ check_is_error_tool(tool_msg) }}
|
33 |
+
}
|
34 |
+
{%- endmacro -%}
|
35 |
+
|
36 |
+
{%- if messages and messages[0]['role']|lower == 'system' %}{%- set developer_preamble = messages[0]['content'] %}{% endif %}
|
37 |
+
{%- set tool_idx = namespace(value=0) %}
|
38 |
+
{%- set tool_ids_seen = namespace(value=[]) %}
|
39 |
+
{{- "<|im_start|>system
|
40 |
+
" -}}
|
41 |
+
{% if tools %}
|
42 |
+
You have been trained to have advanced reasoning and tool-use capabilities and you should make best use of these skills to serve user's requests.
|
43 |
+
|
44 |
+
# Tool Use
|
45 |
+
Think about how you can make best use of the provided tools to help with the task and come up with a high level plan that you will execute first.
|
46 |
+
|
47 |
+
0. Start by writing <|start_thinking|> followed by a detailed step by step plan of how you will solve the problem. For each step explain your thinking fully and give details of required tool calls (if needed). Unless specified otherwise, you write your plan in natural language. When you finish, close it out with <|end_thinking|>.
|
48 |
+
You can optionally choose to skip this step when the user request is so straightforward to address that only a trivial plan would be needed.
|
49 |
+
NOTE: You MUST skip this step when you are directly responding to the user's request without using any tools.
|
50 |
+
|
51 |
+
Then carry out your plan by repeatedly executing the following steps.
|
52 |
+
1. Action: write <tool_call> followed by a list of JSON-formatted tool calls, with each one containing "tool_name" and "parameters" fields.
|
53 |
+
When there are multiple tool calls which are completely independent of each other (i.e. they can be executed in parallel), you should list them out all together in one step. When you finish, close it out with </tool_call>.
|
54 |
+
2. Observation: you will then receive results of those tool calls in JSON format in the very next turn, wrapped around by <tool_response> and </tool_response>. Carefully observe those results and think about what to do next. Note that these results will be provided to you in a separate turn. NEVER hallucinate results.
|
55 |
+
Every tool call produces a list of results (when a tool call produces no result or a single result, it'll still get wrapped inside a list). Each result is clearly linked to its originating tool call via its "tool_call_id".
|
56 |
+
3. Reflection: start the next turn by writing <|start_thinking|> followed by what you've figured out so far, any changes you need to make to your plan, and what you will do next. When you finish, close it out with <|end_thinking|>.
|
57 |
+
You can optionally choose to skip this step when everything is going according to plan and no special pieces of information or reasoning chains need to be recorded.
|
58 |
+
NOTE: You MUST skip this step when you are done with tool-use actions and are ready to respond to the user.
|
59 |
+
|
60 |
+
You can repeat the above 3 steps multiple times (could be 0 times too if no suitable tool calls are available or needed), until you decide it's time to finally respond to the user.
|
61 |
+
|
62 |
+
4. Response: then break out of the loop and write <|start_response|> followed by a piece of text which serves as a response to the user's last request. Use all previous tool calls and results to help you when formulating your response. When you finish, close it out with <|end_response|>.
|
63 |
+
|
64 |
+
# Available Tools
|
65 |
+
Here is the list of tools that you have available to you.
|
66 |
+
You can ONLY use the tools listed here. When a tool is not listed below, it is NOT available and you should NEVER attempt to use it.
|
67 |
+
Each tool is represented as a JSON object with fields like "name", "description", "parameters" (per JSON Schema), and optionally, "responses" (per JSON Schema).
|
68 |
+
|
69 |
+
```json
|
70 |
+
[
|
71 |
+
{% for tool in tools %}
|
72 |
+
{"name": "{{ tool['function']['name'] }}", "description": "{{tool['function']['description']}}", "parameters": {{ tool['function']['parameters']|tojson }}, "responses": null}{%- if not loop.last %},{% endif %}
|
73 |
+
|
74 |
+
{% endfor %}
|
75 |
+
]
|
76 |
+
```
|
77 |
+
|
78 |
+
{% endif %}
|
79 |
+
{%- if developer_preamble %}
|
80 |
+
{{ developer_preamble }}
|
81 |
+
{%- endif -%}
|
82 |
+
{{- "<|im_end|>" -}}
|
83 |
+
{%- for message in messages %}
|
84 |
+
{%- if message.role|lower == 'system' and not (loop.first and developer_preamble)%}
|
85 |
+
{{- "
|
86 |
+
<|im_start|>system
|
87 |
+
" -}}
|
88 |
+
{{- message.content + "<|im_end|>" -}}
|
89 |
+
{%- elif message.role|lower == 'user' -%}
|
90 |
+
{{- "
|
91 |
+
<|im_start|>user
|
92 |
+
" -}}
|
93 |
+
{{- message.content + "<|im_end|>" -}}
|
94 |
+
{%- elif message.role|lower == 'assistant' %}
|
95 |
+
{{- "
|
96 |
+
<|im_start|>assistant
|
97 |
+
" -}}
|
98 |
+
{% if message.tool_calls %}<|start_thinking|>{{message.tool_plan}}<|end_thinking|><tool_call>[
|
99 |
+
{% for tc in message.tool_calls %}
|
100 |
+
{"tool_call_id": "{{ tc.id }}", "tool_name": "{{ tc['function']['name'] }}", "parameters": {{ tc['function']['arguments']|tojson }}}{% if not loop.last %},{% endif %}
|
101 |
+
|
102 |
+
{% set tool_idx.value = tool_idx.value + 1 %}
|
103 |
+
{% endfor %}
|
104 |
+
]</tool_call>{{- "<|im_end|>" -}}
|
105 |
+
{%- else -%}<|start_response|>{{message.content}}<|end_response|>{{- "<|im_end|>" -}}
|
106 |
+
{% endif %}
|
107 |
+
{% elif message.role|lower == 'tool' and message.tool_call_id not in tool_ids_seen.value %}
|
108 |
+
{{- "
|
109 |
+
<|im_start|>system
|
110 |
+
<tool_response>[
|
111 |
+
" -}}
|
112 |
+
{{ format_tool_message(messages, message) }}
|
113 |
+
{%- for msg in messages[loop.index0 + 1:] %}
|
114 |
+
{%- if msg.role|lower == 'tool' %},
|
115 |
+
{{ format_tool_message(messages, msg) }}
|
116 |
+
{%- set tool_ids_seen.value = tool_ids_seen.value + [msg.tool_call_id] %}
|
117 |
+
{%- else %}
|
118 |
+
{%- break %}
|
119 |
+
{%- endif %}
|
120 |
+
{%- endfor %}
|
121 |
+
{{- "
|
122 |
+
]</tool_response><|im_end|>" -}}
|
123 |
+
{%- endif -%}
|
124 |
+
{%- endfor -%}
|
125 |
+
{%- if add_generation_prompt -%}
|
126 |
+
{{- "
|
127 |
+
<|im_start|>assistant
|
128 |
+
" -}}
|
129 |
+
{%- endif -%}
|