Image-Text-to-Text
Transformers
Safetensors
kimi_k25
feature-extraction
compressed-tensors
conversational
custom_code
Eval Results
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)# pip install -U transformers accelerate # 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, device_map="auto") 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=256) 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
Escape reserved tokens in untrusted chat inputs
#19
by mvp468 - opened
- chat_template.jinja +17 -5
chat_template.jinja
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 content['type'] == 'image_url' -%}
|
|
@@ -9,7 +21,7 @@
|
|
| 9 |
{% elif content['type'] == 'video' or content['type']== 'video_url'-%}
|
| 10 |
<|kimi_k25_video_placeholder|>
|
| 11 |
{% else -%}
|
| 12 |
-
{{ content['text'] }}
|
| 13 |
{%- endif -%}
|
| 14 |
{%- endfor -%}
|
| 15 |
{%- endif -%}
|
|
@@ -17,11 +29,11 @@
|
|
| 17 |
{% macro set_roles(message) -%}
|
| 18 |
{%- set role_name = message.get('name') or message['role'] -%}
|
| 19 |
{%- if message['role'] == 'user' -%}
|
| 20 |
-
<|im_user|>{{role_name}}<|im_middle|>
|
| 21 |
{%- elif message['role'] == 'assistant' -%}
|
| 22 |
<|im_assistant|>{{role_name}}<|im_middle|>
|
| 23 |
{%- else -%}
|
| 24 |
-
<|im_system|>{{role_name}}<|im_middle|>
|
| 25 |
{%- endif -%}
|
| 26 |
{%- endmacro -%}
|
| 27 |
{%- macro render_toolcalls(message) -%}
|
|
@@ -48,7 +60,7 @@
|
|
| 48 |
{{render_toolcalls(message)}}
|
| 49 |
{%- endif -%}
|
| 50 |
{%- elif message['role'] == 'tool' -%}
|
| 51 |
-
{%- set tool_call_id = message.tool_call_id -%}
|
| 52 |
## Return of {{ tool_call_id }}
|
| 53 |
{{render_content(message)}}
|
| 54 |
{%- elif message['content'] is not none -%}
|
|
|
|
| 1 |
+
{%- macro sanitize_input(value) -%}
|
| 2 |
+
{{- value | replace('<|', '<|') | replace('|>', '|>') -}}
|
| 3 |
+
{%- endmacro -%}
|
| 4 |
+
|
| 5 |
+
{%- macro render_text(msg, text) -%}
|
| 6 |
+
{%- if msg['role'] == 'user' or msg['role'] == 'tool' -%}
|
| 7 |
+
{{- sanitize_input(text) -}}
|
| 8 |
+
{%- else -%}
|
| 9 |
+
{{- text -}}
|
| 10 |
+
{%- endif -%}
|
| 11 |
+
{%- endmacro -%}
|
| 12 |
+
|
| 13 |
{%- macro render_content(msg) -%}
|
| 14 |
{%- set c = msg.get('content') -%}
|
| 15 |
{%- if c is string -%}
|
| 16 |
+
{{ render_text(msg, c) }}
|
| 17 |
{%- elif c is not none -%}
|
| 18 |
{% for content in c -%}
|
| 19 |
{% if content['type'] == 'image' or content['type'] == 'image_url' -%}
|
|
|
|
| 21 |
{% elif content['type'] == 'video' or content['type']== 'video_url'-%}
|
| 22 |
<|kimi_k25_video_placeholder|>
|
| 23 |
{% else -%}
|
| 24 |
+
{{ render_text(msg, content['text']) }}
|
| 25 |
{%- endif -%}
|
| 26 |
{%- endfor -%}
|
| 27 |
{%- endif -%}
|
|
|
|
| 29 |
{% macro set_roles(message) -%}
|
| 30 |
{%- set role_name = message.get('name') or message['role'] -%}
|
| 31 |
{%- if message['role'] == 'user' -%}
|
| 32 |
+
<|im_user|>{{sanitize_input(role_name)}}<|im_middle|>
|
| 33 |
{%- elif message['role'] == 'assistant' -%}
|
| 34 |
<|im_assistant|>{{role_name}}<|im_middle|>
|
| 35 |
{%- else -%}
|
| 36 |
+
<|im_system|>{{sanitize_input(role_name)}}<|im_middle|>
|
| 37 |
{%- endif -%}
|
| 38 |
{%- endmacro -%}
|
| 39 |
{%- macro render_toolcalls(message) -%}
|
|
|
|
| 60 |
{{render_toolcalls(message)}}
|
| 61 |
{%- endif -%}
|
| 62 |
{%- elif message['role'] == 'tool' -%}
|
| 63 |
+
{%- set tool_call_id = sanitize_input(message.tool_call_id) -%}
|
| 64 |
## Return of {{ tool_call_id }}
|
| 65 |
{{render_content(message)}}
|
| 66 |
{%- elif message['content'] is not none -%}
|