Instructions to use moonshotai/Kimi-K2-Thinking with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use moonshotai/Kimi-K2-Thinking with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="moonshotai/Kimi-K2-Thinking", 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-Thinking", trust_remote_code=True, dtype="auto") - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use moonshotai/Kimi-K2-Thinking with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "moonshotai/Kimi-K2-Thinking" # 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-Thinking", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/moonshotai/Kimi-K2-Thinking
- SGLang
How to use moonshotai/Kimi-K2-Thinking 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-Thinking" \ --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-Thinking", "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-Thinking" \ --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-Thinking", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use moonshotai/Kimi-K2-Thinking with Docker Model Runner:
docker model run hf.co/moonshotai/Kimi-K2-Thinking
tokenizer.apply_chat_template is right? Manually Parsing Tool Calls Failed! but openai api Success, Why?
I'm test the tool call following https://huggingface.co/moonshotai/Kimi-K2-Thinking/blob/main/docs/tool_call_guidance.md
Here I print out the text (result of tokenizer.apply_chat_template)
'<|im_system|>tool_declare<|im_middle|>[{"function":{"description":"Get weather information. Call this tool when the user needs to get weather information","name":"get_weather","parameters":{"properties":{"city":{"description":"City name","type":"string"}},"required":["city"],"type":"object"}},"type":"function"}]<|im_end|><|im_system|>system<|im_middle|>You are Kimi, an AI assistant created by Moonshot AI.<|im_end|><|im_user|>user<|im_middle|>What's the weather like in Beijing today? Let's check using the tool.<|im_end|><|im_assistant|>assistant<|im_middle|>'
and the raw_output (raw_output = raw_out["choices"][0]["text"])
' The user is asking about the weather in Beijing today and wants to check using a tool. I have access to a weather tool that can get weather information. However, I need the user's request—the user hasn't specified a location yet. The user mentioned "What's the weather like in Beijing today" a couple of times, so it seems they want the weather for Beijing.\n\nLet me respond to let the user know I'm going to call the tool to get the Beijing weather forecast. But looking at the information given, it seems like the available action is the functionality to get_weather, which enables me to fetch weather data. I'll now combine this with the function parameters for a call with the requested included. By the way, thank you for the details about the tool! Bring cloud and rain.\n\nI will call the function to get the weather information for Beijing. In preparation, I will get the arguments. The tool call references work normally and returns the result immediately after:\n\n{\n "name": "get_weself",\n "arguments": "北京"\n}'
So what's wrong? I doubt tokenizer.apply_chat_template is not right.
My code:
model_repo = 'moonshotai/Kimi-K2-Thinking'
tokenizer = AutoTokenizer.from_pretrained(model_repo, trust_remote_code=True)
while True:
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
tools=tools,
add_generation_prompt=True,
)
.....
