Image-Text-to-Text
Transformers
Safetensors
kimi_k25
feature-extraction
compressed-tensors
conversational
custom_code
Instructions to use moonshotai/Kimi-K2.7-Code with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use moonshotai/Kimi-K2.7-Code with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="moonshotai/Kimi-K2.7-Code", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModel processor = AutoProcessor.from_pretrained("moonshotai/Kimi-K2.7-Code", trust_remote_code=True) model = AutoModel.from_pretrained("moonshotai/Kimi-K2.7-Code", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use moonshotai/Kimi-K2.7-Code with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "moonshotai/Kimi-K2.7-Code" # 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.7-Code", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/moonshotai/Kimi-K2.7-Code
- SGLang
How to use moonshotai/Kimi-K2.7-Code 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.7-Code" \ --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.7-Code", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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.7-Code" \ --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.7-Code", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use moonshotai/Kimi-K2.7-Code with Docker Model Runner:
docker model run hf.co/moonshotai/Kimi-K2.7-Code
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 +4 -55
- generation_config.json +3 -1
- tokenization_kimi.py +7 -10
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 |
-
|
| 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 |
-
|
| 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 |
|