weichu commited on
Commit
74797c9
·
1 Parent(s): 78b5c46

force thinking & preserve_thinking in jinja; set default sampling params in generation_config.json

Browse files
chat_template.jinja CHANGED
@@ -14,7 +14,6 @@
14
  {%- endfor -%}
15
  {%- endif -%}
16
  {%- endmacro -%}
17
-
18
  {% macro set_roles(message) -%}
19
  {%- set role_name = message.get('name') or message['role'] -%}
20
  {%- if message['role'] == 'user' -%}
@@ -25,8 +24,6 @@
25
  <|im_system|>{{role_name}}<|im_middle|>
26
  {%- endif -%}
27
  {%- endmacro -%}
28
-
29
-
30
  {%- macro render_toolcalls(message) -%}
31
  <|tool_calls_section_begin|>
32
  {%- for tool_call in message['tool_calls'] -%}
@@ -35,24 +32,6 @@
35
  {%- endfor -%}
36
  <|tool_calls_section_end|>
37
  {%- endmacro -%}
38
-
39
-
40
- {%- set preserve_thinking = preserve_thinking | default(true) -%}
41
- {# Find last non-tool-call assistant message. If preserve_thinking, keep -1 so hist is empty and all msgs use suffix (retain reasoning). #}
42
- {%- set ns = namespace(last_non_tool_call_assistant_msg=-1) -%}
43
- {%- if not preserve_thinking -%}
44
- {%- for idx in range(messages|length-1, -1, -1) -%}
45
- {%- if messages[idx]['role'] == 'assistant' and not messages[idx].get('tool_calls') -%}
46
- {%- set ns.last_non_tool_call_assistant_msg = idx -%}
47
- {%- break -%}
48
- {%- endif -%}
49
- {%- endfor -%}
50
- {%- endif -%}
51
-
52
- {# split all messages into history & suffix, reasoning_content in suffix should be reserved.#}
53
- {%- set hist_msgs = messages[:ns.last_non_tool_call_assistant_msg+1] -%}
54
- {%- set suffix_msgs = messages[ns.last_non_tool_call_assistant_msg+1:] -%}
55
-
56
  {%- if tools -%}
57
  {%- if tools_ts_str -%}
58
  <|im_system|>tool_declare<|im_middle|>{{ tools_ts_str }}<|im_end|>
@@ -60,36 +39,13 @@
60
  <|im_system|>tool_declare<|im_middle|>{{ tools | tojson(separators=(',', ':')) }}<|im_end|>
61
  {%- endif -%}
62
  {%- endif -%}
63
-
64
-
65
- {%- for message in hist_msgs -%}
66
- {{set_roles(message)}}
67
- {%- if message['role'] == 'assistant' -%}
68
- <think></think>{{render_content(message)}}
69
- {%- if message.get('tool_calls') -%}
70
- {{render_toolcalls(message)}}
71
- {%- endif -%}
72
- {%- elif message['role'] == 'tool' -%}
73
- {%- set tool_call_id = message.tool_call_id -%}
74
- ## Return of {{ tool_call_id }}
75
- {{render_content(message)}}
76
- {%- elif message['content'] is not none -%}
77
- {{render_content(message)}}
78
- {%- endif -%}
79
- <|im_end|>
80
- {%- endfor -%}
81
-
82
- {%- for message in suffix_msgs -%}
83
  {{set_roles(message)}}
84
  {%- if message['role'] == 'assistant' -%}
85
- {%- if thinking is defined and thinking is false and preserve_thinking is false -%}
86
- <think></think>{{render_content(message)}}
87
- {%- else -%}
88
  {%- set rc = message.get('reasoning', message.get('reasoning_content', '')) -%}
89
  <think>{{rc}}</think>{{render_content(message)}}
90
- {%- endif -%}
91
  {%- if message.get('tool_calls') -%}
92
- {{render_toolcalls(message)}}
93
  {%- endif -%}
94
  {%- elif message['role'] == 'tool' -%}
95
  {%- set tool_call_id = message.tool_call_id -%}
@@ -100,13 +56,6 @@
100
  {%- endif -%}
101
  <|im_end|>
102
  {%- endfor -%}
103
-
104
-
105
  {%- if add_generation_prompt -%}
106
- <|im_assistant|>assistant<|im_middle|>
107
- {%- if thinking is defined and thinking is false -%}
108
- <think></think>
109
- {%- else -%}
110
- <think>
111
- {%- endif -%}
112
- {%- 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' -%}
 
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'] -%}
 
32
  {%- endfor -%}
33
  <|tool_calls_section_end|>
34
  {%- endmacro -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  {%- if tools -%}
36
  {%- if tools_ts_str -%}
37
  <|im_system|>tool_declare<|im_middle|>{{ tools_ts_str }}<|im_end|>
 
39
  <|im_system|>tool_declare<|im_middle|>{{ tools | tojson(separators=(',', ':')) }}<|im_end|>
40
  {%- endif -%}
41
  {%- endif -%}
42
+ {%- for message in messages -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  {{set_roles(message)}}
44
  {%- if message['role'] == 'assistant' -%}
 
 
 
45
  {%- set rc = message.get('reasoning', message.get('reasoning_content', '')) -%}
46
  <think>{{rc}}</think>{{render_content(message)}}
 
47
  {%- if message.get('tool_calls') -%}
48
+ {{render_toolcalls(message)}}
49
  {%- endif -%}
50
  {%- elif message['role'] == 'tool' -%}
51
  {%- set tool_call_id = message.tool_call_id -%}
 
56
  {%- endif -%}
57
  <|im_end|>
58
  {%- endfor -%}
 
 
59
  {%- if add_generation_prompt -%}
60
+ <|im_assistant|>assistant<|im_middle|><think>
61
+ {%- endif -%}
 
 
 
 
 
generation_config.json CHANGED
@@ -1,4 +1,6 @@
1
  {
2
  "max_length": 262144,
3
- "eos_token_id": 163586
 
 
4
  }
 
1
  {
2
  "max_length": 262144,
3
+ "eos_token_id": 163586,
4
+ "temperature": 1.0,
5
+ "top_p": 0.95
6
  }
tokenization_kimi.py CHANGED
@@ -328,6 +328,13 @@ class TikTokenTokenizer(PreTrainedTokenizer):
328
  thinking: bool = True,
329
  preserve_thinking: bool = True,
330
  **kwargs):
 
 
 
 
 
 
 
331
 
332
  tools = deep_sort_dict(tools)
333
 
@@ -345,21 +352,11 @@ class TikTokenTokenizer(PreTrainedTokenizer):
345
  if tools_ts_str is not None:
346
  kwargs['tools_ts_str'] = tools_ts_str
347
 
348
- if not thinking:
349
- logger.warning("thinking=False is not supported, overriding to True")
350
- thinking = True
351
-
352
- if not preserve_thinking:
353
- logger.warning("preserve_thinking=False is not supported, overriding to True")
354
- preserve_thinking = True
355
-
356
  return super().apply_chat_template(
357
  conversation,
358
  tools=tools,
359
  tokenize=tokenize,
360
  add_generation_prompt=add_generation_prompt,
361
- thinking=thinking,
362
- preserve_thinking=preserve_thinking,
363
  **kwargs)
364
 
365
 
 
328
  thinking: bool = True,
329
  preserve_thinking: bool = True,
330
  **kwargs):
331
+ """Apply chat template to conversation.
332
+
333
+ ``thinking`` and ``preserve_thinking`` are kept in the signature for
334
+ backward compatibility, but k27 always enables thinking and preserves
335
+ reasoning content. They are intentionally not forwarded to the chat
336
+ template.
337
+ """
338
 
339
  tools = deep_sort_dict(tools)
340
 
 
352
  if tools_ts_str is not None:
353
  kwargs['tools_ts_str'] = tools_ts_str
354
 
 
 
 
 
 
 
 
 
355
  return super().apply_chat_template(
356
  conversation,
357
  tools=tools,
358
  tokenize=tokenize,
359
  add_generation_prompt=add_generation_prompt,
 
 
360
  **kwargs)
361
 
362