Godcat252 commited on
Commit
e6a1d4d
·
verified ·
1 Parent(s): 21feead

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +101 -85
chat_template.jinja CHANGED
@@ -1,85 +1,101 @@
1
- {%- if not add_generation_prompt is defined %}
2
- {%- set add_generation_prompt = false %}
3
- {%- endif %}
4
- {%- if tools %}
5
- {{- '<|im_start|>system\n' }}
6
- {%- if messages[0].role == 'system' %}
7
- {{- messages[0].content + '\n\n' }}
8
- {%- endif %}
9
- {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
10
- {%- for tool in tools %}
11
- {{- "\n" }}
12
- {{- tool | tojson }}
13
- {%- endfor %}
14
- {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
15
- {%- else %}
16
- {%- if messages[0].role == 'system' %}
17
- {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
18
- {%- endif %}
19
- {%- endif %}
20
- {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
21
- {%- for message in messages[::-1] %}
22
- {%- set index = (messages|length - 1) - loop.index0 %}
23
- {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
24
- {%- set ns.multi_step_tool = false %}
25
- {%- set ns.last_query_index = index %}
26
- {%- endif %}
27
- {%- endfor %}
28
- {%- for message in messages %}
29
- {%- if message.content is string %}
30
- {%- set content = message.content %}
31
- {%- else %}
32
- {%- set content = '' %}
33
- {%- endif %}
34
- {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
35
- {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
36
- {%- elif message.role == "assistant" %}
37
- {%- set reasoning_content = '' %}
38
- {%- if message.reasoning_content is string %}
39
- {%- set reasoning_content = message.reasoning_content %}
40
- {%- else %}
41
- {%- if '</think>' in content %}
42
- {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
43
- {%- set content = content.split('</think>')[-1].lstrip('\n') %}
44
- {%- endif %}
45
- {%- endif %}
46
- {%- if loop.index0 > ns.last_query_index and (loop.last or reasoning_content) and reasoning_content|trim != '' %}
47
- {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
48
- {%- else %}
49
- {{- '<|im_start|>' + message.role + '\n' + content }}
50
- {%- endif %}
51
- {%- if message.tool_calls %}
52
- {%- for tool_call in message.tool_calls %}
53
- {%- if (loop.first and content) or (not loop.first) %}
54
- {{- '\n' }}
55
- {%- endif %}
56
- {%- if tool_call.function %}
57
- {%- set tool_call = tool_call.function %}
58
- {%- endif %}
59
- {{- '<tool_call>\n{"name": "' }}
60
- {{- tool_call.name }}
61
- {{- '", "arguments": ' }}
62
- {%- if tool_call.arguments is string %}
63
- {{- tool_call.arguments }}
64
- {%- else %}
65
- {{- tool_call.arguments | tojson }}
66
- {%- endif %}
67
- {{- '}\n</tool_call>' }}
68
- {%- endfor %}
69
- {%- endif %}
70
- {{- '<|im_end|>\n' }}
71
- {%- elif message.role == "tool" %}
72
- {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
73
- {{- '<|im_start|>user' }}
74
- {%- endif %}
75
- {{- '\n<tool_response>\n' }}
76
- {{- content }}
77
- {{- '\n</tool_response>' }}
78
- {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
79
- {{- '<|im_end|>\n' }}
80
- {%- endif %}
81
- {%- endif %}
82
- {%- endfor %}
83
- {%- if add_generation_prompt %}
84
- {{- '<|im_start|>assistant\n<think>\n' }}
85
- {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 content['type'] == 'image_url' -%}
8
+ <|media_begin|>image<|media_content|><|media_pad|><|media_end|>
9
+ {% elif content['type'] == 'video' or content['type']== 'video_url'-%}
10
+ <|kimi_k25_video_placeholder|>
11
+ {% else -%}
12
+ {{ content['text'] }}
13
+ {%- endif -%}
14
+ {%- endfor -%}
15
+ {%- endif -%}
16
+ {%- endmacro -%}
17
+ {% macro set_roles(message) -%}
18
+ {%- set role_name = message.get('name') or message['role'] -%}
19
+ {%- if message['role'] == 'user' -%}
20
+ <|im_user|>{{role_name}}<|im_middle|>
21
+ {%- elif message['role'] == 'assistant' -%}
22
+ <|im_assistant|>{{role_name}}<|im_middle|>
23
+ {%- else -%}
24
+ <|im_system|>{{role_name}}<|im_middle|>
25
+ {%- endif -%}
26
+ {%- endmacro -%}
27
+ {%- macro render_toolcalls(message) -%}
28
+ <|tool_calls_section_begin|>
29
+ {%- for tool_call in message['tool_calls'] -%}
30
+ {%- set formatted_id = tool_call['id'] -%}
31
+ <|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|>
32
+ {%- endfor -%}
33
+ <|tool_calls_section_end|>
34
+ {%- endmacro -%}
35
+ {%- set preserve_thinking = preserve_thinking | default(false) -%}
36
+ {# Find last non-tool-call assistant message. If preserve_thinking, keep -1 so hist is empty and all msgs use suffix (retain reasoning). #}
37
+ {%- set ns = namespace(last_non_tool_call_assistant_msg=-1) -%}
38
+ {%- if not preserve_thinking -%}
39
+ {%- for idx in range(messages|length-1, -1, -1) -%}
40
+ {%- if messages[idx]['role'] == 'assistant' and not messages[idx].get('tool_calls') -%}
41
+ {%- set ns.last_non_tool_call_assistant_msg = idx -%}
42
+ {%- break -%}
43
+ {%- endif -%}
44
+ {%- endfor -%}
45
+ {%- endif -%}
46
+ {# split all messages into history & suffix, reasoning_content in suffix should be reserved.#}
47
+ {%- set hist_msgs = messages[:ns.last_non_tool_call_assistant_msg+1] -%}
48
+ {%- set suffix_msgs = messages[ns.last_non_tool_call_assistant_msg+1:] -%}
49
+ {%- if tools -%}
50
+ {%- if tools_ts_str -%}
51
+ <|im_system|>tool_declare<|im_middle|>{{ tools_ts_str }}<|im_end|>
52
+ {%- else -%}
53
+ <|im_system|>tool_declare<|im_middle|>{{ tools | tojson(separators=(',', ':')) }}<|im_end|>
54
+ {%- endif -%}
55
+ {%- endif -%}
56
+
57
+ {%- for message in hist_msgs -%}
58
+ {{set_roles(message)}}
59
+ {%- if message['role'] == 'assistant' -%}
60
+ <think></think>{{render_content(message)}}
61
+ {%- if message.get('tool_calls') -%}
62
+ {{render_toolcalls(message)}}
63
+ {%- endif -%}
64
+ {%- elif message['role'] == 'tool' -%}
65
+ {%- set tool_call_id = message.tool_call_id -%}
66
+ ## Return of {{ tool_call_id }}
67
+ {{render_content(message)}}
68
+ {%- elif message['content'] is not none -%}
69
+ {{render_content(message)}}
70
+ {%- endif -%}
71
+ <|im_end|>
72
+ {%- endfor -%}
73
+ {%- for message in suffix_msgs -%}
74
+ {{set_roles(message)}}
75
+ {%- if message['role'] == 'assistant' -%}
76
+ {%- if thinking is defined and thinking is false and preserve_thinking is false -%}
77
+ <think></think>{{render_content(message)}}
78
+ {%- else -%}
79
+ {%- set rc = message.get('reasoning', message.get('reasoning_content', '')) -%}
80
+ <think>{{rc}}</think>{{render_content(message)}}
81
+ {%- endif -%}
82
+ {%- if message.get('tool_calls') -%}
83
+ {{render_toolcalls(message)}}
84
+ {%- endif -%}
85
+ {%- elif message['role'] == 'tool' -%}
86
+ {%- set tool_call_id = message.tool_call_id -%}
87
+ ## Return of {{ tool_call_id }}
88
+ {{render_content(message)}}
89
+ {%- elif message['content'] is not none -%}
90
+ {{render_content(message)}}
91
+ {%- endif -%}
92
+ <|im_end|>
93
+ {%- endfor -%}
94
+ {%- if add_generation_prompt -%}
95
+ <|im_assistant|>assistant<|im_middle|>
96
+ {%- if thinking is defined and thinking is false -%}
97
+ <think></think>
98
+ {%- else -%}
99
+ <think>
100
+ {%- endif -%}
101
+ {%- endif -%}