teknium commited on
Commit
d545dd9
·
verified ·
1 Parent(s): 9188bea

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +80 -83
chat_template.jinja CHANGED
@@ -1,91 +1,88 @@
1
- {%- macro render_content(msg) -%}
2
- {%- set c = msg.get('content') -%}
3
- {%- if c is string -%}
4
- {{ c }}
5
- {%- elif c is not none -%}
6
- {% for content in c -%}
7
- {% if content['type'] == 'image' or 'image' in content or 'image_url' in content -%}
8
- <|media_start|>image<|media_content|><|media_pad|><|media_end|>
9
- {% else -%}
10
- {{ content['text'] }}
11
- {%- endif -%}
12
- {%- endfor -%}
13
- {%- endif -%}
14
- {%- endmacro -%}
15
-
16
- {% macro set_roles(message) -%}
17
- {%- set role_name = message.get('name') or message['role'] -%}
18
- {%- if message['role'] == 'user' -%}
19
- <|im_user|>{{role_name}}<|im_middle|>
20
- {%- elif message['role'] == 'assistant' -%}
21
- <|im_assistant|>{{role_name}}<|im_middle|>
22
- {%- else -%}
23
- <|im_system|>{{role_name}}<|im_middle|>
24
- {%- endif -%}
25
- {%- endmacro -%}
26
-
27
-
28
- {%- macro render_toolcalls(message) -%}
29
- <|tool_calls_section_begin|>
30
- {%- for tool_call in message['tool_calls'] -%}
31
- {%- set formatted_id = tool_call['id'] -%}
32
- <|tool_call_begin|>{{ formatted_id }}<|tool_call_argument_begin|>{% if tool_call['function']['arguments'] is string %}{{ tool_call['function']['arguments'] }}{% else %}{{ tool_call['function']['arguments'] | tojson }}{% endif %}<|tool_call_end|>
33
- {%- endfor -%}
34
- <|tool_calls_section_end|>
35
- {%- endmacro -%}
36
-
37
-
38
- {# Find last non-tool-call assisitant message #}
39
- {%- set ns = namespace(last_non_tool_call_assistant_msg=-1) -%}
40
- {%- for idx in range(messages|length-1, -1, -1) -%}
41
- {%- if messages[idx]['role'] == 'assistant' and not messages[idx].get('tool_calls') -%}
42
- {%- set ns.last_non_tool_call_assistant_msg = idx -%}
43
- {%- break -%}
44
- {%- endif -%}
45
- {%- endfor -%}
46
-
47
- {# split all messages into history & suffix, reasoning_content in suffix should be reserved.#}
48
- {%- set hist_msgs = messages[:ns.last_non_tool_call_assistant_msg+1] -%}
49
- {%- set suffix_msgs = messages[ns.last_non_tool_call_assistant_msg+1:] -%}
50
 
51
  {%- if tools -%}
52
- <|im_system|>tool_declare<|im_middle|>{{ tools | tojson(separators=(',', ':')) }}<|im_end|>
53
- {%- endif -%}
54
-
55
- {%- for message in hist_msgs -%}
56
- {{set_roles(message)}}
57
- {%- if message['role'] == 'assistant' -%}
58
- {{render_content(message)}}
59
- {%- if message.get('tool_calls') -%}
60
- {{render_toolcalls(message)}}
61
  {%- endif -%}
62
- {%- elif message['role'] == 'tool' -%}
63
- {%- set tool_call_id = message.tool_call_id -%}
64
- ## Return of {{ tool_call_id }}
65
- {{render_content(message)}}
66
- {%- elif message['content'] is not none -%}
67
- {{render_content(message)}}
68
- {%- endif -%}
69
- <|im_end|>
70
- {%- endfor -%}
 
 
 
 
71
 
72
- {%- for message in suffix_msgs -%}
73
- {{set_roles(message)}}
74
- {%- if message['role'] == 'assistant' -%}
75
- {%- if message.get('tool_calls') -%}
76
- {{render_toolcalls(message)}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  {%- endif -%}
78
- {%- elif message['role'] == 'tool' -%}
79
- {%- set tool_call_id = message.tool_call_id -%}
80
- ## Return of {{ tool_call_id }}
81
- {{render_content(message)}}
82
- {%- elif message['content'] is not none -%}
83
- {{render_content(message)}}
84
- {%- endif -%}
85
- <|im_end|>
86
  {%- endfor -%}
87
 
88
-
89
  {%- if add_generation_prompt -%}
90
- <|im_assistant|>assistant<|im_middle|>
91
- {%- endif -%}
 
1
+ {%- if keep_thinking is not defined -%}
2
+ {%- set keep_thinking = true -%}
3
+ {%- endif -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  {%- if tools -%}
6
+ {{- bos_token + '<|start_header_id|>system<|end_header_id|>\n' -}}
7
+ {%- if messages[0]['role'] == 'system' -%}
8
+ {{- messages[0]['content'] + '\n\n' -}}
 
 
 
 
 
 
9
  {%- endif -%}
10
+ {{- "You are a function calling AI model. You are provided with function signatures within <tools> </tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. After calling & executing the functions, you will be provided with function results within <tool_response> </tool_response> XML tags.\n\n<tools>" -}}
11
+ {%- for tool in tools -%}
12
+ {{- "\n" -}}
13
+ {{- tool | tojson -}}
14
+ {%- endfor -%}
15
+ {{- "\n</tools>\n\nFor each function call return a JSON object, with the following pydantic model json schema:\n\n{\"title\": \"FunctionCall\", \"type\": \"object\", \"properties\": {\"name\": {\"title\": \"Name\", \"type\": \"string\"}, \"arguments\": {\"title\": \"Arguments\", \"type\": \"object\"}}, \"required\": [\"name\", \"arguments\"]}\n\nEach function call should be enclosed within <tool_call> </tool_call> XML tags.\n\nExample:\n<tool_call>\n{\"name\": \"read_file\", \"arguments\": {\"path\": \"/path/to/file.txt\"}}\n</tool_call>\n<|eot_id|>" -}}
16
+ {%- else -%}
17
+ {%- if messages[0]['role'] == 'system' -%}
18
+ {{- bos_token + '<|start_header_id|>system<|end_header_id|>\n\n' + messages[0]['content'] + '<|eot_id|>' -}}
19
+ {%- else -%}
20
+ {{- bos_token -}}
21
+ {%- endif -%}
22
+ {%- endif -%}
23
 
24
+ {%- for message in messages -%}
25
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) -%}
26
+ {{- '<|start_header_id|>' + message.role + '<|end_header_id|>\n\n' + message.content + '<|eot_id|>' -}}
27
+ {%- elif message.role == "assistant" -%}
28
+ {{- '<|start_header_id|>' + message.role + '<|end_header_id|>\n' -}}
29
+ {%- if message.content or message.reasoning_content is defined -%}
30
+ {#- Extract reasoning and content -#}
31
+ {%- set reasoning = '' -%}
32
+ {%- set content = message.content | default('', true) -%}
33
+ {%- if message.reasoning_content is defined and message.reasoning_content -%}
34
+ {#- Use explicit reasoning_content field if provided -#}
35
+ {%- set reasoning = message.reasoning_content -%}
36
+ {#- Strip any inline think tags from content when reasoning_content exists -#}
37
+ {%- if '</think>' in content -%}
38
+ {%- set content = content.split('</think>', 1)[1] -%}
39
+ {%- endif -%}
40
+ {%- elif '</think>' in content -%}
41
+ {#- Extract reasoning from <think> tags in content -#}
42
+ {%- set thinking_parts = content.split('</think>', 1) -%}
43
+ {%- if '<think>' in thinking_parts[0] -%}
44
+ {%- set reasoning = thinking_parts[0].split('<think>', 1)[1] -%}
45
+ {%- else -%}
46
+ {%- set reasoning = thinking_parts[0] -%}
47
+ {%- endif -%}
48
+ {%- set content = thinking_parts[1] if thinking_parts | length > 1 else '' -%}
49
+ {%- endif -%}
50
+ {#- Output based on keep_thinking setting -#}
51
+ {%- if keep_thinking and reasoning -%}
52
+ {{- '\n<think>' + reasoning.strip() + '</think>' -}}
53
+ {%- else -%}
54
+ {{- '\n' -}}
55
+ {%- endif -%}
56
+ {%- if content.strip() -%}
57
+ {{- content.strip() -}}
58
+ {%- endif -%}
59
+ {%- endif -%}
60
+ {%- if message.tool_calls -%}
61
+ {%- for tool_call in message.tool_calls -%}
62
+ {%- if tool_call.function is defined -%}
63
+ {%- set tool_call = tool_call.function -%}
64
+ {%- endif -%}
65
+ {{- '\n<tool_call>\n{"name": "' -}}
66
+ {{- tool_call.name -}}
67
+ {{- '", "arguments": ' -}}
68
+ {{- tool_call.arguments | tojson -}}
69
+ {{- '}\n</tool_call>' -}}
70
+ {%- endfor -%}
71
+ {%- endif -%}
72
+ {{- '<|eot_id|>' -}}
73
+ {%- elif message.role == "tool" -%}
74
+ {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") -%}
75
+ {{- '<|start_header_id|>tool<|end_header_id|>\n' -}}
76
+ {%- endif -%}
77
+ {{- '\n<tool_response>\n' -}}
78
+ {{- message.content -}}
79
+ {{- '\n</tool_response>' -}}
80
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") -%}
81
+ {{- '<|eot_id|>' -}}
82
+ {%- endif -%}
83
  {%- endif -%}
 
 
 
 
 
 
 
 
84
  {%- endfor -%}
85
 
 
86
  {%- if add_generation_prompt -%}
87
+ {{- '<|start_header_id|>assistant<|end_header_id|>\n\n<think>' -}}
88
+ {%- endif -%}