Skip to content

Commit cf92deb

Browse files
authored
Improve Kimi-K2 chat template
1 parent ad4afd3 commit cf92deb

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
{%- set tool_response_queue = namespace(ids=[]) -%}
17+
{%- set tool_call_counter = namespace(value=0) -%}
18+
19+
{%- if tools -%}
20+
<|im_system|>tool_declare<|im_middle|>{{ tools | tojson }}<|im_end|>
21+
{%- endif -%}
22+
{% for message in messages %}
23+
{%- if loop.first and messages[0]['role'] != 'system' -%}
24+
<|im_system|>system<|im_middle|>You are Kimi, an AI assistant created by Moonshot AI.<|im_end|>
25+
{% endif %}
26+
27+
{%- set role_name = message.get('name') or message['role'] -%}
28+
{%- if message['role'] == 'user' -%}
29+
<|im_user|>{{role_name}}<|im_middle|>
30+
{%- elif message['role'] == 'assistant' -%}
31+
<|im_assistant|>{{role_name}}<|im_middle|>
32+
{%- else -%}
33+
<|im_system|>{{role_name}}<|im_middle|>
34+
{%- endif -%}
35+
36+
{%- if message['role'] == 'assistant' and message.get('tool_calls') -%}
37+
{{render_content(message)}}<|tool_calls_section_begin|>
38+
{%- for tool_call in message['tool_calls'] -%}
39+
{%- if tool_call['id'] is defined -%}
40+
{%- set formatted_id = tool_call['id'] -%}
41+
{%- else -%}
42+
{%- set formatted_id = 'functions.' + tool_call['function']['name'] + ':' + (tool_call_counter.value | string) -%}
43+
{%- set tool_call_counter.value = tool_call_counter.value + 1 -%}
44+
{%- endif -%}
45+
{%- set _ = tool_response_queue.ids.append(formatted_id) -%}
46+
<|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|>
47+
{%- endfor -%}
48+
<|tool_calls_section_end|>
49+
{%- elif message['role'] == 'tool' -%}
50+
{%- if tool_response_queue.ids -%}
51+
{%- set tool_call_id = tool_response_queue.ids.pop(0) -%}
52+
{%- else -%}
53+
{%- set tool_call_id = 'functions.' + message.get('name', 'unknown') + ':' + (tool_call_counter.value | string) -%}
54+
{%- endif -%}
55+
## Return of {{ tool_call_id }}
56+
{{render_content(message)}}
57+
{%- elif message['content'] is not none -%}
58+
{{render_content(message)}}
59+
{%- endif -%}
60+
<|im_end|>
61+
{%- endfor -%}
62+
{%- if add_generation_prompt -%}
63+
<|im_assistant|>assistant<|im_middle|>
64+
{%- endif -%}

models/templates/Kimi-K2-Thinking.jinja

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,30 @@
2424
{%- endif -%}
2525
{%- endmacro -%}
2626

27+
{%- set tool_response_queue = namespace(ids=[]) -%}
28+
{%- set tool_call_counter = namespace(value=0) -%}
2729

2830
{%- macro render_toolcalls(message) -%}
2931
<|tool_calls_section_begin|>
3032
{%- for tool_call in message['tool_calls'] -%}
31-
<|tool_call_begin|>functions.{{ tool_call['function']['name'] }}:{{ loop.index }}<|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+
{%- if tool_call['id'] is defined -%}
34+
{%- set formatted_id = tool_call['id'] -%}
35+
{%- else -%}
36+
{%- set formatted_id = 'functions.' + tool_call['function']['name'] + ':' + (tool_call_counter.value | string) -%}
37+
{%- set tool_call_counter.value = tool_call_counter.value + 1 -%}
38+
{%- endif -%}
39+
{%- set _ = tool_response_queue.ids.append(formatted_id) -%}
40+
<|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|>
3241
{%- endfor -%}
3342
<|tool_calls_section_end|>
3443
{%- endmacro -%}
3544

3645

3746
{# Find last non-tool-call assisitant message #}
38-
{%- set ns = namespace(found=false, last_non_tool_call_assistant_msg=-1) -%}
47+
{%- set ns = namespace(last_non_tool_call_assistant_msg=-1) -%}
3948
{%- for idx in range(messages|length-1, -1, -1) -%}
40-
{%- if not ns.found and messages[idx]['role'] == 'assistant' and not messages[idx].get('tool_calls') -%}
49+
{%- if messages[idx]['role'] == 'assistant' and not messages[idx].get('tool_calls') -%}
4150
{%- set ns.last_non_tool_call_assistant_msg = idx -%}
42-
{%- set ns.found = true -%}
4351
{%- endif -%}
4452
{%- endfor -%}
4553

@@ -48,7 +56,7 @@
4856
{%- set suffix_msgs = messages[ns.last_non_tool_call_assistant_msg+1:] -%}
4957

5058
{%- if tools -%}
51-
<|im_system|>tool_declare<|im_middle|>{{ tools | tojson }}<|im_end|>{# needs support for tojson(separators=(',', ':')) #}
59+
<|im_system|>tool_declare<|im_middle|>{{ tools | tojson }}<|im_end|>
5260
{%- endif -%}
5361

5462
{%- if messages|length == 0 or messages[0]['role'] != 'system' -%}
@@ -63,7 +71,11 @@
6371
{{render_toolcalls(message)}}
6472
{%- endif -%}
6573
{%- elif message['role'] == 'tool' -%}
66-
{%- set tool_call_id = message.tool_call_id -%}
74+
{%- if tool_response_queue.ids -%}
75+
{%- set tool_call_id = tool_response_queue.ids.pop(0) -%}
76+
{%- else -%}
77+
{%- set tool_call_id = 'functions.' + message.get('name', 'unknown') + ':' + (tool_call_counter.value | string) -%}
78+
{%- endif -%}
6779
## Return of {{ tool_call_id }}
6880
{{render_content(message)}}
6981
{%- elif message['content'] is not none -%}
@@ -81,7 +93,11 @@
8193
{{render_toolcalls(message)}}
8294
{%- endif -%}
8395
{%- elif message['role'] == 'tool' -%}
84-
{%- set tool_call_id = message.tool_call_id -%}
96+
{%- if tool_response_queue.ids -%}
97+
{%- set tool_call_id = tool_response_queue.ids.pop(0) -%}
98+
{%- else -%}
99+
{%- set tool_call_id = 'functions.' + message.get('name', 'unknown') + ':' + (tool_call_counter.value | string) -%}
100+
{%- endif -%}
85101
## Return of {{ tool_call_id }}
86102
{{render_content(message)}}
87103
{%- elif message['content'] is not none -%}

0 commit comments

Comments
 (0)