Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- LICENSE +27 -0
- README.md +665 -3
- docs/deploy_guidance.md +196 -0
- docs/tool_call_guidance.md +241 -0
- figures/Base-Evaluation.png +3 -0
- figures/kimi-logo.png +0 -0
.gitattributes
CHANGED
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
model.safetensors.index.json filter=lfs diff=lfs merge=lfs -text
|
|
|
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
model.safetensors.index.json filter=lfs diff=lfs merge=lfs -text
|
37 |
+
figures/Base-Evaluation.png filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Modified MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2025 Moonshot AI
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the “Software”), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
22 |
+
|
23 |
+
Our only modification part is that, if the Software (or any derivative works
|
24 |
+
thereof) is used for any of your commercial products or services that have
|
25 |
+
more than 100 million monthly active users, or more than 20 million US dollars
|
26 |
+
(or equivalent in other currencies) in monthly revenue, you shall prominently
|
27 |
+
display "Kimi K2" on the user interface of such product or service.
|
README.md
CHANGED
@@ -1,3 +1,665 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!-- ---
|
2 |
+
library_name: transformers
|
3 |
+
--- -->
|
4 |
+
<!-- markdownlint-disable first-line-h1 -->
|
5 |
+
<!-- markdownlint-disable html -->
|
6 |
+
<!-- markdownlint-disable no-duplicate-header -->
|
7 |
+
|
8 |
+
|
9 |
+
<div align="center">
|
10 |
+
<picture>
|
11 |
+
<img src="figures/kimi-logo.png" width="30%" alt="Kimi K2: Open Agentic Intellignece">
|
12 |
+
</picture>
|
13 |
+
</div>
|
14 |
+
<hr>
|
15 |
+
<div align="center" style="line-height:1">
|
16 |
+
<a href="https://www.kimi.com" target="_blank"><img alt="Chat" src="https://img.shields.io/badge/🤖%20Chat-Kimi%20K2-ff6b6b?color=1783ff&logoColor=white"/></a>
|
17 |
+
<a href="https://www.moonshot.ai" target="_blank"><img alt="Homepage" src="https://img.shields.io/badge/Homepage-Moonshot%20AI-white?logo=Kimi&logoColor=white"/></a>
|
18 |
+
</div>
|
19 |
+
|
20 |
+
<div align="center" style="line-height: 1;">
|
21 |
+
<a href="https://huggingface.co/moonshotai" target="_blank"><img alt="Hugging Face" src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Moonshot%20AI-ffc107?color=ffc107&logoColor=white"/></a>
|
22 |
+
<a href="https://twitter.com/kimi_moonshot" target="_blank"><img alt="Twitter Follow" src="https://img.shields.io/badge/Twitter-Kimi.ai-white?logo=x&logoColor=white"/></a>
|
23 |
+
<a href="https://discord.gg/TYU2fdJykW" target="_blank"><img alt="Discord" src="https://img.shields.io/badge/Discord-Kimi.ai-white?logo=discord&logoColor=white"/></a>
|
24 |
+
</div>
|
25 |
+
|
26 |
+
<div align="center" style="line-height: 1;">
|
27 |
+
<a href="https://github.com/moonshotai/Kimi-K2/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/License-Modified_MIT-f5de53?&color=f5de53"/></a>
|
28 |
+
</div>
|
29 |
+
|
30 |
+
<p align="center">
|
31 |
+
<b>📰 <a href="https://moonshotai.github.io/Kimi-K2/">Tech Blog</a></b> | <b>📄 Paper Link (comming soon)</b>
|
32 |
+
</p>
|
33 |
+
|
34 |
+
## 0. Reminder: Remove this after you squash the commit history before release.
|
35 |
+
|
36 |
+
## 1. Model Introduction
|
37 |
+
|
38 |
+
Kimi K2 is a state-of-the-art mixture-of-experts (MoE) language model with 32 billion activated parameters and 1 trillion total parameters. Trained with the Muon optimizer, Kimi K2 achieves exceptional performance across frontier knowledge, reasoning, and coding tasks while being meticulously optimized for agentic capabilities.
|
39 |
+
|
40 |
+
### Key Features
|
41 |
+
- Large-Scale Training: Pre-trained a 1T parameter MoE model on 15.5T tokens with zero training instability.
|
42 |
+
- MuonClip Optimizer: We apply the Muon optimizer to an unprecedented scale, and develop novel optimization techniques to resolve instabilities while scaling up.
|
43 |
+
- Agentic Intelligence: Specifically designed for tool use, reasoning, and autonomous problem-solving.
|
44 |
+
|
45 |
+
### Model Variants
|
46 |
+
- **Kimi-K2-Base**: The foundation model, a strong start for researchers and builders who want full control for fine-tuning and custom solutions.
|
47 |
+
- **Kimi-K2-Instruct**: The post-trained model best for drop-in, general-purpose chat and agentic experiences. It is a reflex-grade model without long thinking.
|
48 |
+
|
49 |
+
|
50 |
+
<p align="center">
|
51 |
+
TODO this is a banner
|
52 |
+
<img width="80%" src="figures/logo.svg">
|
53 |
+
</p>
|
54 |
+
|
55 |
+
## 2. Model Summary
|
56 |
+
|
57 |
+
<div align="center">
|
58 |
+
|
59 |
+
|
60 |
+
| | |
|
61 |
+
|:---:|:---:|
|
62 |
+
| **Architecture** | Mixture-of-Experts (MoE) |
|
63 |
+
| **Total Parameters** | 1T |
|
64 |
+
| **Activated Parameters** | 32B |
|
65 |
+
| **Number of Layers** (Dense layer included) | 61 |
|
66 |
+
| **Number of Dense Layers** | 1 |
|
67 |
+
| **Attention Hidden Dimension** | 7168 |
|
68 |
+
| **MoE Hidden Dimension** (per Expert) | 2048 |
|
69 |
+
| **Number of Attention Heads** | 64 |
|
70 |
+
| **Number of Experts** | 384 |
|
71 |
+
| **Selected Experts per Token** | 8 |
|
72 |
+
| **Number of Shared Experts** | 1 |
|
73 |
+
| **Vocabulary Size** | 160K |
|
74 |
+
| **Context Length** | 128K |
|
75 |
+
| **Attention Mechanism** | MLA |
|
76 |
+
| **Activation Function** | SwiGLU |
|
77 |
+
</div>
|
78 |
+
|
79 |
+
## 3. Evaluation Results
|
80 |
+
|
81 |
+
#### Instruction model evaluation results
|
82 |
+
|
83 |
+
<div align="center">
|
84 |
+
<table>
|
85 |
+
<thead>
|
86 |
+
<tr>
|
87 |
+
<th align="center">Benchmark</th>
|
88 |
+
<th align="center">Metric</th>
|
89 |
+
<th align="center">Kimi K2 Instruct</th>
|
90 |
+
<th align="center">DeepSeek-V3-0324</th>
|
91 |
+
<th align="center">Qwen3-235B-A22B <br><sup>(non-thinking)</sup></th>
|
92 |
+
<th align="center">Claude Sonnet 4 <br><sup>(w/o extended thinking)</sup></th>
|
93 |
+
<th align="center">Claude Opus 4 <br><sup>(w/o extended thinking)</sup></th>
|
94 |
+
<th align="center">GPT-4.1</th>
|
95 |
+
<th align="center">Gemini 2.5 Flash <br> Preview (05-20)</th>
|
96 |
+
</tr>
|
97 |
+
</thead>
|
98 |
+
<tbody>
|
99 |
+
<tr>
|
100 |
+
<td align="center" colspan=9><strong>Coding Tasks</strong></td>
|
101 |
+
</tr>
|
102 |
+
<tr>
|
103 |
+
<td align="center">LiveCodeBench v6<br><sup>(Aug 24 - May 25)</sup></td>
|
104 |
+
<td align="center">Pass@1</td>
|
105 |
+
<td align="center"><strong>53.7</strong></td>
|
106 |
+
<td align="center">46.9</td>
|
107 |
+
<td align="center">37.0</td>
|
108 |
+
<td align="center">48.5</td>
|
109 |
+
<td align="center">47.4</t6
|
110 |
+
<td align="center">44.7</td>
|
111 |
+
<td align="center">44.7</td>
|
112 |
+
</tr>
|
113 |
+
<tr>
|
114 |
+
<td align="center">OJBench</td>
|
115 |
+
<td align="center">Pass@1</td>
|
116 |
+
<td align="center"><strong>27.1</strong></td>
|
117 |
+
<td align="center">24.0</td>
|
118 |
+
<td align="center">11.3</td>
|
119 |
+
<td align="center">15.3</td>
|
120 |
+
<td align="center">19.6</td>
|
121 |
+
<td align="center">19.5</td>
|
122 |
+
<td align="center">19.5</td>
|
123 |
+
</tr>
|
124 |
+
<tr>
|
125 |
+
<td align="center">MultiPL-E</td>
|
126 |
+
<td align="center">Pass@1</td>
|
127 |
+
<td align="center"><ins><strong>86.7</strong></ins></td>
|
128 |
+
<td align="center">83.1</td>
|
129 |
+
<td align="center">78.2</td>
|
130 |
+
<td align="center">88.6</td>
|
131 |
+
<td align="center"><strong>89.6</strong></td>
|
132 |
+
<td align="center">86.7</td>
|
133 |
+
<td align="center">85.6</td>
|
134 |
+
</tr>
|
135 |
+
<tr>
|
136 |
+
<td align="center">SWE-bench Verified <br/><sup>(Agentless Coding)</sup></td>
|
137 |
+
<td align="center">Single Patch</td>
|
138 |
+
<td align="center"><ins><strong>51.8</strong></ins></td>
|
139 |
+
<td align="center">36.6</td>
|
140 |
+
<td align="center">39.4</td>
|
141 |
+
<td align="center">50.2</td>
|
142 |
+
<td align="center"><strong>53.0</strong></td>
|
143 |
+
<td align="center">40.8</td>
|
144 |
+
<td align="center">32.6</td>
|
145 |
+
</tr>
|
146 |
+
|
147 |
+
<tr>
|
148 |
+
<td align="center" rowspan="2">SWE-bench Verified <br/> <sup>(Agentic Coding)</sup></td>
|
149 |
+
<td align="center">Single Attempt (Acc)</td>
|
150 |
+
<td align="center"><ins><strong>65.8</strong></ins></td>
|
151 |
+
<td align="center">38.8</td>
|
152 |
+
<td align="center">34.4</td>
|
153 |
+
<td align="center"><strong>72.7</strong></td>
|
154 |
+
<td align="center">72.5<sup>*</sup></td>
|
155 |
+
<td align="center">54.6</td>
|
156 |
+
<td align="center">—</td>
|
157 |
+
</tr>
|
158 |
+
<tr>
|
159 |
+
<!--<td align="center">(Agentic Coding)</td>-->
|
160 |
+
<td align="center">Multiple Attempts (Acc)</td>
|
161 |
+
<td align="center"><ins><strong>71.6</strong></ins></td>
|
162 |
+
<td align="center">—</td>
|
163 |
+
<td align="center">—</td>
|
164 |
+
<td align="center"><strong>80.2</strong></td>
|
165 |
+
<td align="center">79.4<sup>*</sup></td>
|
166 |
+
<td align="center">—</td>
|
167 |
+
<td align="center">—</td>
|
168 |
+
</tr>
|
169 |
+
|
170 |
+
<tr>
|
171 |
+
<td align="center" rowspan="2">SWE-bench Multilingual<br /> <sup>(Agentic Coding)</sup></td>
|
172 |
+
<td align="center">Single Attempt (Acc)</td>
|
173 |
+
<td align="center"><ins><strong>47.3</strong> </ins></td>
|
174 |
+
<td align="center">25.8</td>
|
175 |
+
<td align="center">20.9</td>
|
176 |
+
<td align="center"><strong>51.0</strong></td>
|
177 |
+
<td align="center">—</td>
|
178 |
+
<td align="center">31.5</td>
|
179 |
+
<td align="center">—</td>
|
180 |
+
</tr>
|
181 |
+
<tr>
|
182 |
+
<!--<td align="center">(Agentic Coding)</td>-->
|
183 |
+
<td align="center">Inhouse Framework (Acc)</td>
|
184 |
+
<td align="center"><ins><strong>30.0</strong> </ins></td>
|
185 |
+
<td align="center">—</td>
|
186 |
+
<td align="center">—</td>
|
187 |
+
<td align="center">35.5</td>
|
188 |
+
<td align="center"><strong>43.2</strong></td>
|
189 |
+
<td align="center">8.30</td>
|
190 |
+
<td align="center">—</td>
|
191 |
+
</tr>
|
192 |
+
<tr>
|
193 |
+
<td align="center">TerminalBench</td>
|
194 |
+
<td align="center">Acc</td>
|
195 |
+
<td align="center"><ins><strong>25.0</strong> </ins></td>
|
196 |
+
<td align="center">16.3</td>
|
197 |
+
<td align="center">6.60</td>
|
198 |
+
<td align="center">—</td>
|
199 |
+
<td align="center">—</td>
|
200 |
+
<td align="center"><strong>30.3</strong></td>
|
201 |
+
<td align="center">16.8</td>
|
202 |
+
</tr>
|
203 |
+
<tr>
|
204 |
+
<td align="center">Aider-Polyglot</td>
|
205 |
+
<td align="center">Acc</td>
|
206 |
+
<td align="center">60.0</td>
|
207 |
+
<td align="center">55.1</td>
|
208 |
+
<td align="center"><ins><strong>61.8</strong></ins></td>
|
209 |
+
<td align="center">56.4</td>
|
210 |
+
<td align="center"><strong>70.7</strong></td>
|
211 |
+
<td align="center">52.4</td>
|
212 |
+
<td align="center">44.0</td>
|
213 |
+
</tr>
|
214 |
+
<tr>
|
215 |
+
<td align="center" colspan=9><strong>Tool Use Tasks</strong></td>
|
216 |
+
</tr>
|
217 |
+
<tr>
|
218 |
+
<td align="center">Tau2 retail</td>
|
219 |
+
<td align="center">Avg@4</td>
|
220 |
+
<td align="center"><ins><strong>70.6</strong></ins></td>
|
221 |
+
<td align="center">69.1</td>
|
222 |
+
<td align="center">57.0</td>
|
223 |
+
<td align="center">75.0</td>
|
224 |
+
<td align="center"><strong>81.8</strong></td>
|
225 |
+
<td align="center">74.8</td>
|
226 |
+
<td align="center">64.3</td>
|
227 |
+
</tr>
|
228 |
+
<tr>
|
229 |
+
<td align="center">Tau2 airline</td>
|
230 |
+
<td align="center">Avg@4</td>
|
231 |
+
<td align="center"><ins><strong>56.5</strong></ins></td>
|
232 |
+
<td align="center">39.0</td>
|
233 |
+
<td align="center">26.5</td>
|
234 |
+
<td align="center">55.5</td>
|
235 |
+
<td align="center"><strong>60.0</strong></td>
|
236 |
+
<td align="center">54.5</td>
|
237 |
+
<td align="center">42.5</td>
|
238 |
+
</tr>
|
239 |
+
<tr>
|
240 |
+
<td align="center">Tau2 telecom</td>
|
241 |
+
<td align="center">Avg@4</td>
|
242 |
+
<td align="center"><strong>65.8</strong></td>
|
243 |
+
<td align="center">32.5</td>
|
244 |
+
<td align="center">22.1</td>
|
245 |
+
<td align="center">45.2</td>
|
246 |
+
<td align="center">57.0</td>
|
247 |
+
<td align="center">38.6</td>
|
248 |
+
<td align="center">16.9</td>
|
249 |
+
</tr>
|
250 |
+
<tr>
|
251 |
+
<td align="center">AceBench</td>
|
252 |
+
<td align="center">Acc</td>
|
253 |
+
<td align="center"><ins><strong>76.5</strong></ins></td>
|
254 |
+
<td align="center">72.7</td>
|
255 |
+
<td align="center">70.5</td>
|
256 |
+
<td align="center">76.2</td>
|
257 |
+
<td align="center">75.6</td>
|
258 |
+
<td align="center"><strong>80.1</strong></td>
|
259 |
+
<td align="center">74.5</td>
|
260 |
+
</tr>
|
261 |
+
<tr>
|
262 |
+
<td align="center" colspan=9><strong>Math & STEM Tasks</strong></td>
|
263 |
+
</tr>
|
264 |
+
<tr>
|
265 |
+
<td align="center">AIME 2024</td>
|
266 |
+
<td align="center">Avg@64</td>
|
267 |
+
<td align="center"><strong>69.6</strong></td>
|
268 |
+
<td align="center">59.4<sup>*</sup></td>
|
269 |
+
<td align="center">40.1<sup>*</sup></td>
|
270 |
+
<td align="center">43.4</td>
|
271 |
+
<td align="center">48.2</td>
|
272 |
+
<td align="center">46.5</td>
|
273 |
+
<td align="center">61.3</td>
|
274 |
+
</tr>
|
275 |
+
<tr>
|
276 |
+
<td align="center">AIME 2025</td>
|
277 |
+
<td align="center">Avg@64</td>
|
278 |
+
<td align="center"><strong>49.5</strong></td>
|
279 |
+
<td align="center">46.7</td>
|
280 |
+
<td align="center">24.7<sup>*</sup></td>
|
281 |
+
<td align="center">33.1<sup>*</sup></td>
|
282 |
+
<td align="center">33.9<sup>*</sup></td>
|
283 |
+
<td align="center">37.0</td>
|
284 |
+
<td align="center">46.6</td>
|
285 |
+
</tr>
|
286 |
+
<tr>
|
287 |
+
<td align="center">MATH-500</td>
|
288 |
+
<td align="center">Acc</td>
|
289 |
+
<td align="center"><strong>97.4</strong></td>
|
290 |
+
<td align="center">94.0<sup>*</sup></td>
|
291 |
+
<td align="center">91.2<sup>*</sup></td>
|
292 |
+
<td align="center">94.0</td>
|
293 |
+
<td align="center">94.4</td>
|
294 |
+
<td align="center">92.4/td>
|
295 |
+
<td align="center">95.4</td>
|
296 |
+
</tr>
|
297 |
+
<tr>
|
298 |
+
<td align="center">HMMT 2025</td>
|
299 |
+
<td align="center">Avg@32</td>
|
300 |
+
<td align="center"><strong>38.8</strong></td>
|
301 |
+
<td align="center">27.5</td>
|
302 |
+
<td align="center">11.9</td>
|
303 |
+
<td align="center">15.9</td>
|
304 |
+
<td align="center">15.8</td>
|
305 |
+
<td align="center">19.4</td>
|
306 |
+
<td align="center">34.7</td>
|
307 |
+
</tr>
|
308 |
+
<tr>
|
309 |
+
<td align="center">CNMO 2024</td>
|
310 |
+
<td align="center">Avg@16</td>
|
311 |
+
<td align="center">74.3</td>
|
312 |
+
<td align="center"><ins><strong>74.7</strong></ins></td>
|
313 |
+
<td align="center">48.6</td>
|
314 |
+
<td align="center">60.4</td>
|
315 |
+
<td align="center">57.6</td>
|
316 |
+
<td align="center">56.6</td>
|
317 |
+
<td align="center"><strong>75.0</strong></td>
|
318 |
+
</tr>
|
319 |
+
<tr>
|
320 |
+
<td align="center">PolyMath-en</td>
|
321 |
+
<td align="center">Avg@4</td>
|
322 |
+
<td align="center"><strong>65.1</strong></td>
|
323 |
+
<td align="center">59.5</td>
|
324 |
+
<td align="center">51.9</td>
|
325 |
+
<td align="center">52.8</td>
|
326 |
+
<td align="center">49.8</td>
|
327 |
+
<td align="center">54.0</td>
|
328 |
+
<td align="center">49.9</td>
|
329 |
+
</tr>
|
330 |
+
|
331 |
+
<tr>
|
332 |
+
<td align="center">ZebraLogic</td>
|
333 |
+
<td align="center">Acc</td>
|
334 |
+
<td align="center"><strong>89.0</strong></td>
|
335 |
+
<td align="center">84.0</td>
|
336 |
+
<td align="center">37.7</td>
|
337 |
+
<td align="center">73.7</td>
|
338 |
+
<td align="center">59.3</td>
|
339 |
+
<td align="center">58.5</td>
|
340 |
+
<td align="center">57.9</td>
|
341 |
+
</tr>
|
342 |
+
|
343 |
+
<tr>
|
344 |
+
<td align="center">AutoLogi</td>
|
345 |
+
<td align="center">Acc</td>
|
346 |
+
<td align="center"><ins><strong>89.5</strong></ins></td>
|
347 |
+
<td align="center">88.9</td>
|
348 |
+
<td align="center">83.3</td>
|
349 |
+
<td align="center"><strong>89.8</strong></td>
|
350 |
+
<td align="center">86.1</td>
|
351 |
+
<td align="center">88.2</td>
|
352 |
+
<td align="center">84.1</td>
|
353 |
+
</tr>
|
354 |
+
|
355 |
+
<tr>
|
356 |
+
<td align="center">GPQA-Diamond</td>
|
357 |
+
<td align="center">Avg@8</td>
|
358 |
+
<td align="center"><strong>75.1</strong></td>
|
359 |
+
<td align="center">68.4<sup>*</sup></td>
|
360 |
+
<td align="center">62.9<sup>*</sup></td>
|
361 |
+
<td align="center">70.0<sup>*</sup></td>
|
362 |
+
<td align="center">74.9<sup>*</sup></td>
|
363 |
+
<td align="center">66.3</td>
|
364 |
+
<td align="center">68.2</td>
|
365 |
+
</tr>
|
366 |
+
|
367 |
+
<tr>
|
368 |
+
<td align="center">SuperGPQA</td>
|
369 |
+
<td align="center">Acc</td>
|
370 |
+
<td align="center"><strong>57.2</strong></td>
|
371 |
+
<td align="center">53.7</td>
|
372 |
+
<td align="center">50.2</td>
|
373 |
+
<td align="center">55.7</td>
|
374 |
+
<td align="center">56.5</td>
|
375 |
+
<td align="center">50.8</td>
|
376 |
+
<td align="center">49.6</td>
|
377 |
+
</tr>
|
378 |
+
|
379 |
+
<tr>
|
380 |
+
<td align="center">Humanity’s Last</td>
|
381 |
+
<td align="center">(Text Only)</td>
|
382 |
+
<td align="center">4.7</td>
|
383 |
+
<td align="center">5.2</td>
|
384 |
+
<td align="center"><ins><strong>5.7</strong></ins></td>
|
385 |
+
<td align="center">5.8</td>
|
386 |
+
<td align="center"><strong>7.1</strong></td>
|
387 |
+
<td align="center">3.7</td>
|
388 |
+
<td align="center">5.6</td>
|
389 |
+
</tr>
|
390 |
+
|
391 |
+
<tr>
|
392 |
+
<td align="center" colspan=9><strong>General Tasks</strong></td>
|
393 |
+
</tr>
|
394 |
+
|
395 |
+
<tr>
|
396 |
+
<td align="center">MMLU</td>
|
397 |
+
<td align="center">EM</td>
|
398 |
+
<td align="center"><ins><strong>89.5</strong></ins></td>
|
399 |
+
<td align="center">89.4</td>
|
400 |
+
<td align="center">87.0</td>
|
401 |
+
<td align="center">91.5</td>
|
402 |
+
<td align="center"><strong>92.9</strong></td>
|
403 |
+
<td align="center">90.4</td>
|
404 |
+
<td align="center">90.1</td>
|
405 |
+
</tr>
|
406 |
+
|
407 |
+
<tr>
|
408 |
+
<td align="center">MMLU-Redux</td>
|
409 |
+
<td align="center">EM</td>
|
410 |
+
<td align="center"><ins><strong>92.7</strong></ins></td>
|
411 |
+
<td align="center">90.5</td>
|
412 |
+
<td align="center">89.2</td>
|
413 |
+
<td align="center">93.6</td>
|
414 |
+
<td align="center"><strong>94.2</strong></td>
|
415 |
+
<td align="center">92.4</td>
|
416 |
+
<td align="center">90.6</td>
|
417 |
+
</tr>
|
418 |
+
|
419 |
+
<tr>
|
420 |
+
<td align="center">MMLU-Pro</td>
|
421 |
+
<td align="center">EM</td>
|
422 |
+
<td align="center">81.1</td>
|
423 |
+
<td align="center"><ins><strong>81.2</strong></ins><sup>*</sup></td>
|
424 |
+
<td align="center">77.3</td>
|
425 |
+
<td align="center">83.7</td>
|
426 |
+
<td align="center"><strong>86.6</strong></td>
|
427 |
+
<td align="center">81.8</td>
|
428 |
+
<td align="center">79.4</td>
|
429 |
+
</tr>
|
430 |
+
|
431 |
+
<tr>
|
432 |
+
<td align="center">IFEval</td>
|
433 |
+
<td align="center">Prompt Strict</td>
|
434 |
+
<td align="center"><strong>89.8</strong></td>
|
435 |
+
<td align="center">81.1</td>
|
436 |
+
<td align="center">83.2<sup>*</sup></td>
|
437 |
+
<td align="center">87.6</td>
|
438 |
+
<td align="center">87.4</td>
|
439 |
+
<td align="center">88.0</td>
|
440 |
+
<td align="center">84.3</td>
|
441 |
+
</tr>
|
442 |
+
|
443 |
+
<tr>
|
444 |
+
<td align="center">Multi-Challenge</td>
|
445 |
+
<td align="center">Acc</td>
|
446 |
+
<td align="center"><strong>54.1</strong></td>
|
447 |
+
<td align="center">31.4</td>
|
448 |
+
<td align="center">34.0</td>
|
449 |
+
<td align="center">46.8</td>
|
450 |
+
<td align="center">49.0</td>
|
451 |
+
<td align="center">36.4</td>
|
452 |
+
<td align="center">39.5</td>
|
453 |
+
</tr>
|
454 |
+
|
455 |
+
<tr>
|
456 |
+
<td align="center">SimpleQA</td>
|
457 |
+
<td align="center">Correct</td>
|
458 |
+
<td align="center"><ins><strong>31.0</strong></ins></td>
|
459 |
+
<td align="center">27.7</td>
|
460 |
+
<td align="center">13.2</td>
|
461 |
+
<td align="center">15.9</td>
|
462 |
+
<td align="center">22.8</td>
|
463 |
+
<td align="center"><strong>42.3</strong></td>
|
464 |
+
<td align="center">23.3</td>
|
465 |
+
</tr>
|
466 |
+
|
467 |
+
<tr>
|
468 |
+
<td align="center">Livebench</td>
|
469 |
+
<td align="center">Pass@1</td>
|
470 |
+
<td align="center"><strong>76.4</strong></td>
|
471 |
+
<td align="center">72.4</td>
|
472 |
+
<td align="center">67.6</td>
|
473 |
+
<td align="center">74.8</td>
|
474 |
+
<td align="center">74.6</td>
|
475 |
+
<td align="center">69.8</td>
|
476 |
+
<td align="center">67.8</td>
|
477 |
+
</tr>
|
478 |
+
</tbody>
|
479 |
+
</table>
|
480 |
+
</div>
|
481 |
+
<sup>
|
482 |
+
• Bold denotes global SOTA, and underlined denotes open-source SOTA.
|
483 |
+
</sup><br/><sup>
|
484 |
+
• Data points marked with * are taken directly from the model's tech report or blog.
|
485 |
+
</sup><br/><sup>
|
486 |
+
• All metrics, except for SWE-bench Verified (Agentless), are evaluated with an 8k output token length. SWE-bench Verified (Agentless) is limited to a 16k output token length.
|
487 |
+
</sup><br/><sup>
|
488 |
+
• Kimi K2 achieves 65.8% pass@1 on the SWE-bench Verified tests with bash/editor tools (single-attempt patches, no test-time compute). It also achieves a 47.3% pass@1 on the SWE-bench Multilingual tests under the same conditions. Additionally, we report results on SWE-bench Verified tests (71.6%) that leverage parallel test-time compute by sampling multiple sequences and selecting the single best via an internal scoring model.
|
489 |
+
</sup><br/><sup>
|
490 |
+
• To ensure the stability of the evaluation, we employed avg@k on the AIME, HMMT, CNMO, PolyMath-en, GPQA-Diamond, EvalPlus, Tau2.
|
491 |
+
</sup><br/><sup>
|
492 |
+
• Some data points have been omitted due to prohibitively expensive evaluation costs.
|
493 |
+
</sup>
|
494 |
+
|
495 |
+
---
|
496 |
+
|
497 |
+
#### Base model evaluation results
|
498 |
+
|
499 |
+
<div align="center">
|
500 |
+
|
501 |
+
| Benchmark | Metric | Shot | Kimi K2 Base | Deepseek-V3-Base | Qwen2.5-72B | Llama 4 Maverick |
|
502 |
+
|:-------------------:|:----------:|:---------:|:--------------:|:------------------:|:-------------:|:------------------:|
|
503 |
+
| **General Tasks** | | | | | | |
|
504 |
+
| MMLU | EM | 5-shot | **87.79** | 87.1 | 86.08 | 84.87 |
|
505 |
+
| MMLU-pro | EM | 5-shot | **69.17** | 60.59 | 62.8 | 63.47 |
|
506 |
+
| MMLU-redux-2.0 | EM | 5-shot | **90.17** | 89.53 | 87.77 | 88.18 |
|
507 |
+
| SimpleQA | Correct | 5-shot | **35.25** | 26.49 | 10.31 | 23.74 |
|
508 |
+
| TriviaQA | EM | 5-shot | **85.09** | 84.11 | 76.03 | 79.25 |
|
509 |
+
| GPQA-Diamond | Avg@8 | 5-shot | 48.11 | **50.51** | 40.78 | 49.43 |
|
510 |
+
| SuperGPQA | EM | 5-shot | **44.67** | 39.2 | 34.23 | 38.84 |
|
511 |
+
| **Code Tasks** | | | | | | |
|
512 |
+
| LiveCodeBench v6 | Pass@1 | 1-shot | **26.29** | 22.86 | 21.14 | 25.14 |
|
513 |
+
| EvalPlus | Pass@1 | - | **80.33** | 65.61 | 66.04 | 65.48 |
|
514 |
+
| **Mathematics Tasks** | | | | | | |
|
515 |
+
| MATH | EM | 4-shot | **70.22** | 60.06 | 60.96 | 63.02 |
|
516 |
+
| GSM8k | EM | 8-shot | **92.12** | 91.66 | 90.37 | 86.35 |
|
517 |
+
| **Chinese Tasks** | | | | | | |
|
518 |
+
| C-Eval | EM | 5-shot | **92.5** | 90.04 | 90.86 | 80.91 |
|
519 |
+
| CSimpleQA | Correct | 5-shot | **77.57** | 72.13 | 50.53 | 53.47 |
|
520 |
+
|
521 |
+
</div>
|
522 |
+
<sup>
|
523 |
+
• We only evaluate open-source pretrained models in this work. We report results for Qwen2.5-72B because the base checkpoint for Qwen3-235B-A22B was not open-sourced at the time of our study.
|
524 |
+
</sup><br/><sup>
|
525 |
+
• All models are evaluated using the same evaluation protocol.
|
526 |
+
|
527 |
+
</sup>
|
528 |
+
|
529 |
+
|
530 |
+
## 4. Deployment
|
531 |
+
> [!Note]
|
532 |
+
> You can access Kimi K2's API on https://platform.moonshot.ai , we provide OpenAI/Anthropic-compatible API for you.
|
533 |
+
>
|
534 |
+
> The Anthropic-compatible API maps temperature by `real_temperature = request_temperature * 0.6` for better compatible with existing applications.
|
535 |
+
|
536 |
+
Our model checkpoints are stored in the block-fp8 format, you can find it on [Huggingface](https://huggingface.co/moonshotai/Kimi-K2-Instruct).
|
537 |
+
|
538 |
+
Currently, Kimi-K2 is recommended to run on the following inference engines:
|
539 |
+
|
540 |
+
* vLLM
|
541 |
+
* SGLang
|
542 |
+
* KTransformers
|
543 |
+
* TensorRT-LLM
|
544 |
+
|
545 |
+
Deployment examples for vLLM and SGLang can be found in the [Model Deployment Guide](docs/deploy_guidance.md).
|
546 |
+
|
547 |
+
---
|
548 |
+
|
549 |
+
## 5. Model Usage
|
550 |
+
|
551 |
+
### Chat Completion
|
552 |
+
|
553 |
+
Once the local inference service is up, you can interact with it through the chat endpoint:
|
554 |
+
|
555 |
+
```python
|
556 |
+
def simple_chat(client: OpenAI, model_name: str):
|
557 |
+
messages = [
|
558 |
+
{"role": "system", "content": "You are Kimi, an AI assistant created by Moonshot AI."},
|
559 |
+
{"role": "user", "content": [{"type": "text", "text": "Please give a brief self-introduction."}]},
|
560 |
+
]
|
561 |
+
response = client.chat.completions.create(
|
562 |
+
model=model_name,
|
563 |
+
messages=messages,
|
564 |
+
stream=False,
|
565 |
+
temperature=0.6,
|
566 |
+
max_tokens=256
|
567 |
+
)
|
568 |
+
print(response.choices[0].message.content)
|
569 |
+
```
|
570 |
+
|
571 |
+
> [!NOTE]
|
572 |
+
> The recommended temperature for Kimi-K2-Instruct is `temperature = 0.6`.
|
573 |
+
> If no special instructions are required, the system prompt above is a good default.
|
574 |
+
|
575 |
+
---
|
576 |
+
|
577 |
+
### Tool Calling
|
578 |
+
|
579 |
+
Kimi-K2-Instruct has strong tool-calling capabilities.
|
580 |
+
To enable them, you need to pass the list of available tools in each request, then the model will autonomously decide when and how to invoke them.
|
581 |
+
|
582 |
+
The following example demonstrates calling a weather tool end-to-end:
|
583 |
+
|
584 |
+
```python
|
585 |
+
# Your tool implementation
|
586 |
+
def get_weather(city: str) -> dict:
|
587 |
+
return {"weather": "Sunny"}
|
588 |
+
|
589 |
+
# Tool schema definition
|
590 |
+
tools = [{
|
591 |
+
"type": "function",
|
592 |
+
"function": {
|
593 |
+
"name": "get_weather",
|
594 |
+
"description": "Retrieve current weather information. Call this when the user asks about the weather.",
|
595 |
+
"parameters": {
|
596 |
+
"type": "object",
|
597 |
+
"required": ["city"],
|
598 |
+
"properties": {
|
599 |
+
"city": {
|
600 |
+
"type": "string",
|
601 |
+
"description": "Name of the city"
|
602 |
+
}
|
603 |
+
}
|
604 |
+
}
|
605 |
+
}
|
606 |
+
}]
|
607 |
+
|
608 |
+
# Map tool names to their implementations
|
609 |
+
tool_map = {
|
610 |
+
"get_weather": get_weather
|
611 |
+
}
|
612 |
+
|
613 |
+
def tool_call_with_client(client: OpenAI, model_name: str):
|
614 |
+
messages = [
|
615 |
+
{"role": "system", "content": "You are Kimi, an AI assistant created by Moonshot AI."},
|
616 |
+
{"role": "user", "content": "What's the weather like in Beijing today? Use the tool to check."}
|
617 |
+
]
|
618 |
+
finish_reason = None
|
619 |
+
while finish_reason is None or finish_reason == "tool_calls":
|
620 |
+
completion = client.chat.completions.create(
|
621 |
+
model=model_name,
|
622 |
+
messages=messages,
|
623 |
+
temperature=0.6,
|
624 |
+
tools=tools, # tool list defined above
|
625 |
+
tool_choice="auto"
|
626 |
+
)
|
627 |
+
choice = completion.choices[0]
|
628 |
+
finish_reason = choice.finish_reason
|
629 |
+
if finish_reason == "tool_calls":
|
630 |
+
messages.append(choice.message)
|
631 |
+
for tool_call in choice.message.tool_calls:
|
632 |
+
tool_call_name = tool_call.function.name
|
633 |
+
tool_call_arguments = json.loads(tool_call.function.arguments)
|
634 |
+
tool_function = tool_map[tool_call_name]
|
635 |
+
tool_result = tool_function(**tool_call_arguments)
|
636 |
+
print("tool_result:", tool_result)
|
637 |
+
|
638 |
+
messages.append({
|
639 |
+
"role": "tool",
|
640 |
+
"tool_call_id": tool_call.id,
|
641 |
+
"name": tool_call_name,
|
642 |
+
"content": json.dumps(tool_result)
|
643 |
+
})
|
644 |
+
print("-" * 100)
|
645 |
+
print(choice.message.content)
|
646 |
+
```
|
647 |
+
|
648 |
+
The `tool_call_with_client` function implements the pipeline from user query to tool execution.
|
649 |
+
This pipeline requires the inference engine to support Kimi-K2’s native tool-parsing logic.
|
650 |
+
For streaming output and manual tool-parsing, see the [Tool Calling Guide](docs/tool_call_guidance.md).
|
651 |
+
|
652 |
+
---
|
653 |
+
|
654 |
+
## 6. License
|
655 |
+
|
656 |
+
Both the code repository and the model weights are released under the [Modified MIT License](LICENSE).
|
657 |
+
|
658 |
+
In short, it is MIT License for most people, but you need to give credit to "Kimi K2" by displaying it prominently in your product, if you have more than 100 million monthly active users or annual revenue exceeding 20 million USD.
|
659 |
+
|
660 |
+
|
661 |
+
---
|
662 |
+
|
663 |
+
## 7. Contact Us
|
664 |
+
|
665 |
+
If you have any questions, please reach out at [[email protected]](mailto:[email protected]).
|
docs/deploy_guidance.md
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Kimi-K2 Deployment Guide
|
2 |
+
|
3 |
+
> [!Note]
|
4 |
+
> This guide only provides some examples of deployment commands for Kimi-K2, which may not be the optimal configuration. Since inference engines are still being updated frequenty, please continue to follow the guidance from their homepage if you want to achieve better inference performance.
|
5 |
+
|
6 |
+
|
7 |
+
## vLLM Deployment
|
8 |
+
|
9 |
+
The smallest deployment unit for Kimi-K2 FP8 weights with 128k seqlen on mainstream H200 or H20 platform is a cluster with 16 GPUs with either Tensor Parallel (TP) or "data parallel + expert parallel" (DP+EP).
|
10 |
+
Running parameters for this environment are provided below. You may scale up to more nodes and increase expert-parallelism to enlarge the inference batch size and overall throughput.
|
11 |
+
|
12 |
+
### Tensor Parallelism
|
13 |
+
|
14 |
+
When the parallelism degree ≤ 16, you can run inference with pure Tensor Parallelism. A sample launch command is:
|
15 |
+
|
16 |
+
``` bash
|
17 |
+
# start ray on node 0 and node 1
|
18 |
+
|
19 |
+
# node 0:
|
20 |
+
vllm serve $MODEL_PATH \
|
21 |
+
--port 8000 \
|
22 |
+
--served-model-name kimi-k2 \
|
23 |
+
--trust-remote-code \
|
24 |
+
--tensor-parallel-size 16 \
|
25 |
+
--enable-auto-tool-choice \
|
26 |
+
--tool-call-parser kimi_k2
|
27 |
+
```
|
28 |
+
|
29 |
+
**Key parameter notes:**
|
30 |
+
- `--tensor-parallel-size 16`: If using more than 16 GPUs, combine with pipeline-parallelism.
|
31 |
+
- `--enable-auto-tool-choice`: Required when enabling tool usage.
|
32 |
+
- `--tool-call-parser kimi_k2`: Required when enabling tool usage.
|
33 |
+
|
34 |
+
### Data Parallelism + Expert Parallelism
|
35 |
+
|
36 |
+
You can install libraries like DeepEP and DeepGEMM as needed. Then run the command (example on H200):
|
37 |
+
|
38 |
+
``` bash
|
39 |
+
# node 0
|
40 |
+
vllm serve $MODEL_PATH --port 8000 --served-model-name kimi-k2 --trust-remote-code --data-parallel-size 16 --data-parallel-size-local 8 --data-parallel-address $MASTER_IP --data-parallel-rpc-port $PORT --enable-expert-parallel --max-num-batched-tokens 8192 --max-num-seqs 256 --gpu-memory-utilization 0.85 --enable-auto-tool-choice --tool-call-parser kimi_k2
|
41 |
+
|
42 |
+
# node 1
|
43 |
+
vllm serve $MODEL_PATH --headless --data-parallel-start-rank 8 --port 8000 --served-model-name kimi-k2 --trust-remote-code --data-parallel-size 16 --data-parallel-size-local 8 --data-parallel-address $MASTER_IP --data-parallel-rpc-port $PORT --enable-expert-parallel --max-num-batched-tokens 8192 --max-num-seqs 256 --gpu-memory-utilization 0.85 --enable-auto-tool-choice --tool-call-parser kimi_k2
|
44 |
+
```
|
45 |
+
|
46 |
+
## SGLang Deployment
|
47 |
+
|
48 |
+
Similarly, we can use TP or DP+EP in SGLang for Deployment, here are the examples.
|
49 |
+
|
50 |
+
|
51 |
+
### Tensor Parallelism
|
52 |
+
|
53 |
+
Here is the simple example code to run TP16 with two nodes on H200:
|
54 |
+
|
55 |
+
``` bash
|
56 |
+
# Node 0
|
57 |
+
python -m sglang.launch_server --model-path $MODEL_PATH --tp 16 --dist-init-addr $MASTER_IP:50000 --nnodes 2 --node-rank 0 --trust-remote-code --tool-call-parser kimi_k2
|
58 |
+
|
59 |
+
# Node 1
|
60 |
+
python -m sglang.launch_server --model-path $MODEL_PATH --tp 16 --dist-init-addr $MASTER_IP:50000 --nnodes 2 --node-rank 1 --trust-remote-code --tool-call-parser kimi_k2
|
61 |
+
```
|
62 |
+
|
63 |
+
**Key parameter notes:**
|
64 |
+
- `--tool-call-parser kimi_k2`: Required when enabling tool usage.
|
65 |
+
|
66 |
+
### Data Parallelism + Expert Parallelism
|
67 |
+
|
68 |
+
Here is an example for large scale Prefill-Decode Disagreegation (4P12D H200) with DP+EP in SGLang:
|
69 |
+
|
70 |
+
``` bash
|
71 |
+
# for prefill node
|
72 |
+
MC_TE_METRIC=true SGLANG_DISAGGREGATION_HEARTBEAT_INTERVAL=10000000 SGLANG_DISAGGREGATION_BOOTSTRAP_TIMEOUT=100000 SGLANG_DISAGGREGATION_WAITING_TIMEOUT=100000 PYTHONUNBUFFERED=1 \
|
73 |
+
python -m sglang.launch_server --model-path $MODEL_PATH \
|
74 |
+
--trust-remote-code --disaggregation-mode prefill --dist-init-addr $PREFILL_NODE0$:5757 --tp-size 32 --dp-size 32 --enable-dp-attention --host $LOCAL_IP --decode-log-interval 1 --disable-radix-cache --enable-deepep-moe --moe-dense-tp-size 1 --enable-dp-lm-head --disable-shared-experts-fusion --watchdog-timeout 1000000 --enable-two-batch-overlap --disaggregation-ib-device $IB_DEVICE --chunked-prefill-size 131072 --mem-fraction-static 0.85 --deepep-mode normal --ep-dispatch-algorithm dynamic --eplb-algorithm deepseek --max-running-requests 1024 --nnodes 4 --node-rank $RANK --tool-call-parser kimi_k2
|
75 |
+
|
76 |
+
|
77 |
+
# for decode node
|
78 |
+
SGLANG_DEEPEP_NUM_MAX_DISPATCH_TOKENS_PER_RANK=480 MC_TE_METRIC=true SGLANG_DISAGGREGATION_HEARTBEAT_INTERVAL=10000000 SGLANG_DISAGGREGATION_BOOTSTRAP_TIMEOUT=100000 SGLANG_DISAGGREGATION_WAITING_TIMEOUT=100000 PYTHONUNBUFFERED=1 \
|
79 |
+
python -m sglang.launch_server --model-path $MODEL_PATH --trust-remote-code --disaggregation-mode decode --dist-init-addr $DECODE_NODE0:5757 --tp-size 96 --dp-size 96 --enable-dp-attention --host $LOCAL_IP --decode-log-interval 1 --context-length 2176 --disable-radix-cache --enable-deepep-moe --moe-dense-tp-size 1 --enable-dp-lm-head --disable-shared-experts-fusion --watchdog-timeout 1000000 --enable-two-batch-overlap --disaggregation-ib-device $IB_DEVICE --deepep-mode low_latency --mem-fraction-static 0.8 --cuda-graph-bs 480 --max-running-requests 46080 --ep-num-redundant-experts 96 --nnodes 12 --node-rank $RANK --tool-call-parser kimi_k2
|
80 |
+
|
81 |
+
# pdlb
|
82 |
+
PYTHONUNBUFFERED=1 python -m sglang.srt.disaggregation.launch_lb --prefill http://${PREFILL_NODE0}:30000 --decode http://${DECODE_NODE0}:30000
|
83 |
+
```
|
84 |
+
|
85 |
+
## KTransformers Deployment
|
86 |
+
|
87 |
+
Please copy all configuration files (i.e., everything except the .safetensors files) into the GGUF checkpoint folder at /path/to/K2. Then run:
|
88 |
+
``` bash
|
89 |
+
python ktransformers/server/main.py --model_path /path/to/K2 --gguf_path /path/to/K2 --cache_lens 30000
|
90 |
+
```
|
91 |
+
|
92 |
+
To enable AMX optimization, run:
|
93 |
+
|
94 |
+
``` bash
|
95 |
+
python ktransformers/server/main.py --model_path /path/to/K2 --gguf_path /path/to/K2 --cache_lens 30000 --optimize_config_path ktransformers/optimize/optimize_rules/DeepSeek-V3-Chat-fp8-linear-ggml-experts-serve-amx.yaml
|
96 |
+
```
|
97 |
+
|
98 |
+
## TensoRT-LLM Deployment
|
99 |
+
### Prerequisite
|
100 |
+
Please refer to [this guide](https://nvidia.github.io/TensorRT-LLM/installation/build-from-source-linux.html) to build TensorRT-LLM v1.0.0-rc2 from source and start a TRT-LLM docker container.
|
101 |
+
|
102 |
+
install blobfile by:
|
103 |
+
```bash
|
104 |
+
pip install blobfile
|
105 |
+
```
|
106 |
+
### Multi-node Serving
|
107 |
+
TensorRT-LLM supports multi-node inference. You can use mpirun to launch Kimi-K2 with multi-node jobs. We will use two nodes for this example.
|
108 |
+
|
109 |
+
#### mpirun
|
110 |
+
mpirun requires each node to have passwordless ssh access to the other node. We need to setup the environment inside the docker container. Run the container with host network and mount the current directory as well as model directory to the container.
|
111 |
+
|
112 |
+
```bash
|
113 |
+
# use host network
|
114 |
+
IMAGE=<YOUR_IMAGE>
|
115 |
+
NAME=test_2node_docker
|
116 |
+
# host1
|
117 |
+
docker run -it --name ${NAME}_host1 --ipc=host --gpus=all --network host --privileged --ulimit memlock=-1 --ulimit stack=67108864 -v ${PWD}:/workspace -v <YOUR_MODEL_DIR>:/models/DeepSeek-V3 -w /workspace ${IMAGE}
|
118 |
+
# host2
|
119 |
+
docker run -it --name ${NAME}_host2 --ipc=host --gpus=all --network host --privileged --ulimit memlock=-1 --ulimit stack=67108864 -v ${PWD}:/workspace -v <YOUR_MODEL_DIR>:/models/DeepSeek-V3 -w /workspace ${IMAGE}
|
120 |
+
```
|
121 |
+
|
122 |
+
Set up ssh inside the container
|
123 |
+
|
124 |
+
```bash
|
125 |
+
apt-get update && apt-get install -y openssh-server
|
126 |
+
|
127 |
+
# modify /etc/ssh/sshd_config
|
128 |
+
PermitRootLogin yes
|
129 |
+
PubkeyAuthentication yes
|
130 |
+
# modify /etc/ssh/sshd_config, change default port 22 to another unused port
|
131 |
+
port 2233
|
132 |
+
|
133 |
+
# modify /etc/ssh
|
134 |
+
```
|
135 |
+
|
136 |
+
Generate ssh key on host1 and copy to host2, vice versa.
|
137 |
+
|
138 |
+
```bash
|
139 |
+
# on host1
|
140 |
+
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519
|
141 |
+
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@<HOST2>
|
142 |
+
# on host2
|
143 |
+
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519
|
144 |
+
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@<HOST1>
|
145 |
+
|
146 |
+
# restart ssh service on host1 and host2
|
147 |
+
service ssh restart # or
|
148 |
+
/etc/init.d/ssh restart # or
|
149 |
+
systemctl restart ssh
|
150 |
+
```
|
151 |
+
|
152 |
+
Generate additional config for trtllm serve.
|
153 |
+
```bash
|
154 |
+
cat >/path/to/TensorRT-LLM/extra-llm-api-config.yml <<EOF
|
155 |
+
cuda_graph_config:
|
156 |
+
padding_enabled: true
|
157 |
+
batch_sizes:
|
158 |
+
- 1
|
159 |
+
- 2
|
160 |
+
- 4
|
161 |
+
- 8
|
162 |
+
- 16
|
163 |
+
- 32
|
164 |
+
- 64
|
165 |
+
- 128
|
166 |
+
print_iter_log: true
|
167 |
+
enable_attention_dp: true
|
168 |
+
EOF
|
169 |
+
```
|
170 |
+
|
171 |
+
|
172 |
+
After the preparations,you can run the trtllm-serve on two nodes using mpirun:
|
173 |
+
|
174 |
+
```bash
|
175 |
+
mpirun -np 16 \
|
176 |
+
-H <HOST1>:8,<HOST2>:8 \
|
177 |
+
-mca plm_rsh_args "-p 2233" \
|
178 |
+
--allow-run-as-root \
|
179 |
+
trtllm-llmapi-launch trtllm-serve serve \
|
180 |
+
--backend pytorch \
|
181 |
+
--tp_size 16 \
|
182 |
+
--ep_size 8 \
|
183 |
+
--kv_cache_free_gpu_memory_fraction 0.95 \
|
184 |
+
--trust_remote_code \
|
185 |
+
--max_batch_size 128 \
|
186 |
+
--max_num_tokens 4096 \
|
187 |
+
--extra_llm_api_options /path/to/TensorRT-LLM/extra-llm-api-config.yml \
|
188 |
+
--port 8000 \
|
189 |
+
<YOUR_MODEL_DIR>
|
190 |
+
```
|
191 |
+
|
192 |
+
## Others
|
193 |
+
|
194 |
+
Kimi-K2 reuses the `DeepSeekV3CausalLM` architecture and convert it's weight into proper shape to save redevelopment effort. To let inference engines distinguish it from DeepSeek-V3 and apply the best optimizations, we set `"model_type": "kimi_k2"` in `config.json`.
|
195 |
+
|
196 |
+
If you are using a framework that is not on the recommended list, you can still run the model by manually changing `model_type` to "deepseek_v3" in `config.json` as a temporary workaround. You may need to manually parse tool calls in case no tool call parser is available in your framework.
|
docs/tool_call_guidance.md
ADDED
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Tool Calling
|
2 |
+
To enable the tool calling feature, you may need to set certain tool calling parser options when starting the service. See [deploy_guidance](./deploy_guidance.md) for details.
|
3 |
+
In Kimi-K2, a tool calling process includes:
|
4 |
+
- Passing function descriptions to Kimi-K2
|
5 |
+
- Kimi-K2 decides to make a function call and returns the necessary information for the function call to the user
|
6 |
+
- The user performs the function call, collects the call results, and passes the function call results to Kimi-K2
|
7 |
+
- Kimi-K2 continues to generate content based on the function call results until the model believes it has obtained sufficient information to respond to the user
|
8 |
+
|
9 |
+
### Preparing Tools
|
10 |
+
Suppose we have a function `get_weather` that can query the weather conditions in real-time.
|
11 |
+
This function accepts a city name as a parameter and returns the weather conditions. We need to prepare a structured description for it so that Kimi-K2 can understand its functionality.
|
12 |
+
|
13 |
+
```python
|
14 |
+
def get_weather(city):
|
15 |
+
return {"weather": "Sunny"}
|
16 |
+
|
17 |
+
# Collect the tool descriptions in tools
|
18 |
+
tools = [{
|
19 |
+
"type": "function",
|
20 |
+
"function": {
|
21 |
+
"name": "get_weather",
|
22 |
+
"description": "Get weather information. Call this tool when the user needs to get weather information",
|
23 |
+
"parameters": {
|
24 |
+
"type": "object",
|
25 |
+
"required": ["city"],
|
26 |
+
"properties": {
|
27 |
+
"city": {
|
28 |
+
"type": "string",
|
29 |
+
"description": "City name",
|
30 |
+
}
|
31 |
+
}
|
32 |
+
}
|
33 |
+
}
|
34 |
+
}]
|
35 |
+
|
36 |
+
# Tool name->object mapping for easy calling later
|
37 |
+
tool_map = {
|
38 |
+
"get_weather": get_weather
|
39 |
+
}
|
40 |
+
```
|
41 |
+
### Chat with tools
|
42 |
+
We use `openai.OpenAI` to send messages to Kimi-K2 along with tool descriptions. Kimi-K2 will autonomously decide whether to use and how to use the provided tools.
|
43 |
+
If Kimi-K2 believes a tool call is needed, it will return a result with `finish_reason='tool_calls'`. At this point, the returned result includes the tool call information.
|
44 |
+
After calling tools with the provided information, we then need to append the tool call results to the chat history and continue calling Kimi-K2.
|
45 |
+
Kimi-K2 may need to call tools multiple times until the model believes the current results can answer the user's question. We should check `finish_reason` until it is not `tool_calls`.
|
46 |
+
|
47 |
+
The results obtained by the user after calling the tools should be added to `messages` with `role='tool'`.
|
48 |
+
|
49 |
+
```python
|
50 |
+
import json
|
51 |
+
from openai import OpenAI
|
52 |
+
model_name='moonshotai/Kimi-K2-Instruct'
|
53 |
+
client = OpenAI(base_url=endpoint,
|
54 |
+
api_key='xxx')
|
55 |
+
|
56 |
+
messages = [
|
57 |
+
{"role": "user", "content": "What's the weather like in Beijing today? Let's check using the tool."}
|
58 |
+
]
|
59 |
+
finish_reason = None
|
60 |
+
while finish_reason is None or finish_reason == "tool_calls":
|
61 |
+
completion = client.chat.completions.create(
|
62 |
+
model=model_name,
|
63 |
+
messages=messages,
|
64 |
+
temperature=0.3,
|
65 |
+
tools=tools,
|
66 |
+
tool_choice="auto",
|
67 |
+
)
|
68 |
+
choice = completion.choices[0]
|
69 |
+
finish_reason = choice.finish_reason
|
70 |
+
# Note: The finish_reason when tool calls end may vary across different engines, so this condition check needs to be adjusted accordingly
|
71 |
+
if finish_reason == "tool_calls":
|
72 |
+
messages.append(choice.message)
|
73 |
+
for tool_call in choice.message.tool_calls:
|
74 |
+
tool_call_name = tool_call.function.name
|
75 |
+
tool_call_arguments = json.loads(tool_call.function.arguments)
|
76 |
+
tool_function = tool_map[tool_call_name]
|
77 |
+
tool_result = tool_function(tool_call_arguments)
|
78 |
+
print("tool_result", tool_result)
|
79 |
+
|
80 |
+
messages.append({
|
81 |
+
"role": "tool",
|
82 |
+
"tool_call_id": tool_call.id,
|
83 |
+
"name": tool_call_name,
|
84 |
+
"content": json.dumps(tool_result),
|
85 |
+
})
|
86 |
+
print('-' * 100)
|
87 |
+
print(choice.message.content)
|
88 |
+
```
|
89 |
+
### Tool Calling in Streaming Mode
|
90 |
+
Tool calling can also be used in streaming mode. In this case, we need to collect the tool call information returned in the stream until we have a complete tool call. Please refer to the code below:
|
91 |
+
|
92 |
+
```python
|
93 |
+
messages = [
|
94 |
+
{"role": "user", "content": "What's the weather like in Beijing today? Let's check using the tool."}
|
95 |
+
]
|
96 |
+
finish_reason = None
|
97 |
+
msg = ''
|
98 |
+
while finish_reason is None or finish_reason == "tool_calls":
|
99 |
+
completion = client.chat.completions.create(
|
100 |
+
model=model_name,
|
101 |
+
messages=messages,
|
102 |
+
temperature=0.3,
|
103 |
+
tools=tools,
|
104 |
+
tool_choice="auto",
|
105 |
+
stream=True
|
106 |
+
)
|
107 |
+
tool_calls = []
|
108 |
+
for chunk in completion:
|
109 |
+
delta = chunk.choices[0].delta
|
110 |
+
if delta.content:
|
111 |
+
msg += delta.content
|
112 |
+
if delta.tool_calls:
|
113 |
+
for tool_call_chunk in delta.tool_calls:
|
114 |
+
if tool_call_chunk.index is not None:
|
115 |
+
# Extend the tool_calls list
|
116 |
+
while len(tool_calls) <= tool_call_chunk.index:
|
117 |
+
tool_calls.append({
|
118 |
+
"id": "",
|
119 |
+
"type": "function",
|
120 |
+
"function": {
|
121 |
+
"name": "",
|
122 |
+
"arguments": ""
|
123 |
+
}
|
124 |
+
})
|
125 |
+
|
126 |
+
tc = tool_calls[tool_call_chunk.index]
|
127 |
+
|
128 |
+
if tool_call_chunk.id:
|
129 |
+
tc["id"] += tool_call_chunk.id
|
130 |
+
if tool_call_chunk.function.name:
|
131 |
+
tc["function"]["name"] += tool_call_chunk.function.name
|
132 |
+
if tool_call_chunk.function.arguments:
|
133 |
+
tc["function"]["arguments"] += tool_call_chunk.function.arguments
|
134 |
+
|
135 |
+
finish_reason = chunk.choices[0].finish_reason
|
136 |
+
# Note: The finish_reason when tool calls end may vary across different engines, so this condition check needs to be adjusted accordingly
|
137 |
+
if finish_reason == "tool_calls":
|
138 |
+
for tool_call in tool_calls:
|
139 |
+
tool_call_name = tool_call['function']['name']
|
140 |
+
tool_call_arguments = json.loads(tool_call['function']['arguments'])
|
141 |
+
tool_function = tool_map[tool_call_name]
|
142 |
+
tool_result = tool_function(tool_call_arguments)
|
143 |
+
messages.append({
|
144 |
+
"role": "tool",
|
145 |
+
"tool_call_id": tool_call['id'],
|
146 |
+
"name": tool_call_name,
|
147 |
+
"content": json.dumps(tool_result),
|
148 |
+
})
|
149 |
+
# The text generated by the tool call is not the final version, reset msg
|
150 |
+
msg = ''
|
151 |
+
|
152 |
+
print(msg)
|
153 |
+
```
|
154 |
+
### Manually Parsing Tool Calls
|
155 |
+
The tool call requests generated by Kimi-K2 can also be parsed manually, which is especially useful when the service you are using does not provide a tool-call parser.
|
156 |
+
The tool call requests generated by Kimi-K2 are wrapped by `<|tool_calls_section_begin|>` and `<|tool_calls_section_end|>`,
|
157 |
+
with each tool call wrapped by `<|tool_call_begin|>` and `<|tool_call_end|>`. The tool ID and arguments are separated by `<|tool_call_argument_begin|>`.
|
158 |
+
The format of the tool ID is `functions.{func_name}:{idx}`, from which we can parse the function name.
|
159 |
+
|
160 |
+
Based on the above rules, we can directly post request to the completions interface and manually parse tool calls.
|
161 |
+
|
162 |
+
```python
|
163 |
+
import requests
|
164 |
+
from transformers import AutoTokenizer
|
165 |
+
messages = [
|
166 |
+
{"role": "user", "content": "What's the weather like in Beijing today? Let's check using the tool."}
|
167 |
+
]
|
168 |
+
msg = ''
|
169 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
170 |
+
while True:
|
171 |
+
text = tokenizer.apply_chat_template(
|
172 |
+
messages,
|
173 |
+
tokenize=False,
|
174 |
+
tools=tools,
|
175 |
+
add_generation_prompt=True,
|
176 |
+
)
|
177 |
+
payload = {
|
178 |
+
"model": model_name,
|
179 |
+
"prompt": text,
|
180 |
+
"max_tokens": 512
|
181 |
+
}
|
182 |
+
response = requests.post(
|
183 |
+
f"{endpoint}/completions",
|
184 |
+
headers={"Content-Type": "application/json"},
|
185 |
+
json=payload,
|
186 |
+
stream=False,
|
187 |
+
)
|
188 |
+
raw_out = response.json()
|
189 |
+
|
190 |
+
raw_output = raw_out["choices"][0]["text"]
|
191 |
+
tool_calls = extract_tool_call_info(raw_output)
|
192 |
+
if len(tool_calls) == 0:
|
193 |
+
# No tool calls
|
194 |
+
msg = raw_output
|
195 |
+
break
|
196 |
+
else:
|
197 |
+
for tool_call in tool_calls:
|
198 |
+
tool_call_name = tool_call['function']['name']
|
199 |
+
tool_call_arguments = json.loads(tool_call['function']['arguments'])
|
200 |
+
tool_function = tool_map[tool_call_name]
|
201 |
+
tool_result = tool_function(tool_call_arguments)
|
202 |
+
|
203 |
+
messages.append({
|
204 |
+
"role": "tool",
|
205 |
+
"tool_call_id": tool_call['id'],
|
206 |
+
"name": tool_call_name,
|
207 |
+
"content": json.dumps(tool_result),
|
208 |
+
})
|
209 |
+
print('-' * 100)
|
210 |
+
print(msg)
|
211 |
+
```
|
212 |
+
Here, `extract_tool_call_info` parses the model output and returns the model call information. A simple implementation would be:
|
213 |
+
```python
|
214 |
+
def extract_tool_call_info(tool_call_rsp: str):
|
215 |
+
if '<|tool_calls_section_begin|>' not in tool_call_rsp:
|
216 |
+
# No tool calls
|
217 |
+
return []
|
218 |
+
import re
|
219 |
+
pattern = r"<\|tool_calls_section_begin\|>(.*?)<\|tool_calls_section_end\|>"
|
220 |
+
|
221 |
+
tool_calls_sections = re.findall(pattern, tool_call_rsp, re.DOTALL)
|
222 |
+
|
223 |
+
# Extract multiple tool calls
|
224 |
+
func_call_pattern = r"<\|tool_call_begin\|>\s*(?P<tool_call_id>[\w\.]+:\d+)\s*<\|tool_call_argument_begin\|>\s*(?P<function_arguments>.*?)\s*<\|tool_call_end\|>"
|
225 |
+
tool_calls = []
|
226 |
+
for match in re.findall(func_call_pattern, tool_calls_sections[0], re.DOTALL):
|
227 |
+
function_id, function_args = match
|
228 |
+
# function_id: functions.get_weather:0
|
229 |
+
function_name = function_id.split('.')[1].split(':')[0]
|
230 |
+
tool_calls.append(
|
231 |
+
{
|
232 |
+
"id": function_id,
|
233 |
+
"type": "function",
|
234 |
+
"function": {
|
235 |
+
"name": function_name,
|
236 |
+
"arguments": function_args
|
237 |
+
}
|
238 |
+
}
|
239 |
+
)
|
240 |
+
return tool_calls
|
241 |
+
```
|
figures/Base-Evaluation.png
ADDED
![]() |
Git LFS Details
|
figures/kimi-logo.png
ADDED
![]() |