Instructions to use moonshotai/Kimi-K2-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use moonshotai/Kimi-K2-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="moonshotai/Kimi-K2-Instruct", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("moonshotai/Kimi-K2-Instruct", trust_remote_code=True, dtype="auto") - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use moonshotai/Kimi-K2-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "moonshotai/Kimi-K2-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moonshotai/Kimi-K2-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/moonshotai/Kimi-K2-Instruct
- SGLang
How to use moonshotai/Kimi-K2-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "moonshotai/Kimi-K2-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moonshotai/Kimi-K2-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "moonshotai/Kimi-K2-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moonshotai/Kimi-K2-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use moonshotai/Kimi-K2-Instruct with Docker Model Runner:
docker model run hf.co/moonshotai/Kimi-K2-Instruct
wangzhengtao commited on
Commit ·
1ecacf8
1
Parent(s): 2a19363
update
Browse files- chat_template.jinja +5 -7
- 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' -%}
|
|
@@ -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' -%}
|
|
|
|
| 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 |
+
|