update_chat_template_and_tokenizer
#12
by
bigmoyan
- opened
- chat_template.jinja +6 -8
- tokenization_kimi.py +19 -1
chat_template.jinja
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
{%- if tools -%}
|
| 2 |
-
<|im_system|>tool_declare<|im_middle|>
|
| 3 |
-
# Tools
|
| 4 |
-
{{ tools | tojson }}<|im_end|>
|
| 5 |
{%- endif -%}
|
| 6 |
-
{
|
| 7 |
{%- if loop.first and messages[0]['role'] != 'system' -%}
|
| 8 |
-
|
| 9 |
-
{
|
| 10 |
|
| 11 |
{%- set role_name = message.get('name') or message['role'] -%}
|
| 12 |
{%- if message['role'] == 'user' -%}
|
|
@@ -15,7 +13,7 @@
|
|
| 15 |
<|im_assistant|>{{role_name}}<|im_middle|>
|
| 16 |
{%- else -%}
|
| 17 |
<|im_system|>{{role_name}}<|im_middle|>
|
| 18 |
-
{
|
| 19 |
|
| 20 |
{%- if message['role'] == 'assistant' and message.get('tool_calls') -%}
|
| 21 |
{%- if message['content'] -%}{{ message['content'] }}{%- endif -%}
|
|
@@ -27,7 +25,7 @@
|
|
| 27 |
<|tool_calls_section_end|>
|
| 28 |
{%- elif message['role'] == 'tool' -%}
|
| 29 |
## Return of {{ message.tool_call_id }}
|
| 30 |
-
|
| 31 |
{%- elif message['content'] is string -%}
|
| 32 |
{{ message['content'] }}
|
| 33 |
{%- elif message['content'] is not none -%}
|
|
|
|
| 1 |
{%- if tools -%}
|
| 2 |
+
<|im_system|>tool_declare<|im_middle|>{{ tools | tojson(separators=(',', ':')) }}<|im_end|>
|
|
|
|
|
|
|
| 3 |
{%- endif -%}
|
| 4 |
+
{% for message in messages %}
|
| 5 |
{%- if loop.first and messages[0]['role'] != 'system' -%}
|
| 6 |
+
<|im_system|>system<|im_middle|>You are Kimi, an AI assistant created by Moonshot AI.<|im_end|>
|
| 7 |
+
{% endif %}
|
| 8 |
|
| 9 |
{%- set role_name = message.get('name') or message['role'] -%}
|
| 10 |
{%- if message['role'] == 'user' -%}
|
|
|
|
| 13 |
<|im_assistant|>{{role_name}}<|im_middle|>
|
| 14 |
{%- else -%}
|
| 15 |
<|im_system|>{{role_name}}<|im_middle|>
|
| 16 |
+
{%- endif -%}
|
| 17 |
|
| 18 |
{%- if message['role'] == 'assistant' and message.get('tool_calls') -%}
|
| 19 |
{%- if message['content'] -%}{{ message['content'] }}{%- endif -%}
|
|
|
|
| 25 |
<|tool_calls_section_end|>
|
| 26 |
{%- elif message['role'] == 'tool' -%}
|
| 27 |
## Return of {{ message.tool_call_id }}
|
| 28 |
+
{{ message['content'] }}
|
| 29 |
{%- elif message['content'] is string -%}
|
| 30 |
{{ message['content'] }}
|
| 31 |
{%- elif message['content'] is not none -%}
|
tokenization_kimi.py
CHANGED
|
@@ -17,12 +17,13 @@ from tiktoken.load import load_tiktoken_bpe
|
|
| 17 |
from tokenizers import AddedToken, pre_tokenizers, Regex
|
| 18 |
from transformers.tokenization_utils import PreTrainedTokenizer
|
| 19 |
from transformers.models.gpt2.tokenization_gpt2 import bytes_to_unicode
|
| 20 |
-
|
| 21 |
|
| 22 |
|
| 23 |
logger = getLogger(__name__)
|
| 24 |
VOCAB_FILES_NAMES = {"vocab_file": "tiktoken.model"}
|
| 25 |
|
|
|
|
| 26 |
class TikTokenTokenizer(PreTrainedTokenizer):
|
| 27 |
"""
|
| 28 |
Tokenizing and encoding/decoding text using the Tiktoken tokenizer. See megatron/tokenizer/tiktoken_tokenizer.py.
|
|
@@ -321,3 +322,20 @@ class TikTokenTokenizer(PreTrainedTokenizer):
|
|
| 321 |
copyfile(self.vocab_file, out_vocab_file)
|
| 322 |
|
| 323 |
return (out_vocab_file,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
from tokenizers import AddedToken, pre_tokenizers, Regex
|
| 18 |
from transformers.tokenization_utils import PreTrainedTokenizer
|
| 19 |
from transformers.models.gpt2.tokenization_gpt2 import bytes_to_unicode
|
| 20 |
+
from typing import Any
|
| 21 |
|
| 22 |
|
| 23 |
logger = getLogger(__name__)
|
| 24 |
VOCAB_FILES_NAMES = {"vocab_file": "tiktoken.model"}
|
| 25 |
|
| 26 |
+
|
| 27 |
class TikTokenTokenizer(PreTrainedTokenizer):
|
| 28 |
"""
|
| 29 |
Tokenizing and encoding/decoding text using the Tiktoken tokenizer. See megatron/tokenizer/tiktoken_tokenizer.py.
|
|
|
|
| 322 |
copyfile(self.vocab_file, out_vocab_file)
|
| 323 |
|
| 324 |
return (out_vocab_file,)
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
def apply_chat_template(
|
| 329 |
+
self, conversation, tools: Optional[list[dict]] = None, **kwargs
|
| 330 |
+
):
|
| 331 |
+
tools = deep_sort_dict(tools)
|
| 332 |
+
return super().apply_chat_template(conversation, tools=tools, **kwargs)
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
def deep_sort_dict(obj: Any) -> Any:
|
| 336 |
+
if isinstance(obj, dict):
|
| 337 |
+
return {k: deep_sort_dict(v) for k, v in sorted(obj.items())}
|
| 338 |
+
if isinstance(obj, list):
|
| 339 |
+
return [deep_sort_dict(item) for item in obj]
|
| 340 |
+
return obj
|
| 341 |
+
|