Kimi Chat Completion API 申请及使用
Kimi 是一款非常强大的 AI 对话系统,只要输入提示词,就能在短短几秒内生成流畅自然的回复。Kimi 都能提供令人惊叹的智能协助,极大地提高了人类的工作效率和创造力。
本文档主要介绍 Kimi Chat Completion API 操作的使用流程,利用它我们可以轻松使用官方 Kimi 的对话功能。
申请流程
要使用 Gemini Chat Completion API,首先可以到 Kimi Chat Completion API 页面点击「Acquire」按钮,获取请求所需要的凭证:
如果你尚未登录或注册,会自动跳转到登录页面邀请您来注册和登录,登录注册之后会自动返回当前页面。
在首次申请时会有免费额度赠送,可以免费使用该 API。
基本使用
接下来就可以在界面上填写对应的内容,如图所示:
同时您可以注意到右侧有对应的调用代码生成,您可以复制代码直接运行,也可以直接点击「Try」按钮进行测试。
{
"id": "chatcmpl-b5d9e1b799c137e3",
"object": "chat.completion",
"created": 1770991864,
"model": "kimi-k2.5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": " Hello! How can I help you today?",
"refusal": null,
"reasoning_content": " The user has simply said \"Hello\". This is a straightforward greeting. I should respond in a friendly, helpful manner while being ready to assist with whatever they need next. Since there's no specific question or task yet, I'll acknowledge their greeting and ask how I can help.\n\nI should keep it:\n- Friendly and welcoming\n- Professional but warm\n- Open-ended to invite them to share what they need help with\n- Concise but not too brief\n\nPossible responses:\n1. \"Hello! How can I help you today?\"\n2. \"Hi there! What can I do for you?\"\n3. \"Hello! I'm ready to assist. What would you like to know or work on?\"\n4. \"Hey! Great to meet you. How can I be of service?\"\n\nI'll go with something warm and professional that invites them to share what they need. ",
"reasoning": " The user has simply said \"Hello\". This is a straightforward greeting. I should respond in a friendly, helpful manner while being ready to assist with whatever they need next. Since there's no specific question or task yet, I'll acknowledge their greeting and ask how I can help.\n\nI should keep it:\n- Friendly and welcoming\n- Professional but warm\n- Open-ended to invite them to share what they need help with\n- Concise but not too brief\n\nPossible responses:\n1. \"Hello! How can I help you today?\"\n2. \"Hi there! What can I do for you?\"\n3. \"Hello! I'm ready to assist. What would you like to know or work on?\"\n4. \"Hey! Great to meet you. How can I be of service?\"\n\nI'll go with something warm and professional that invites them to share what they need. ",
"tool_calls": []
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 184,
"total_tokens": 193,
"prompt_tokens_details": {
"cached_tokens_details": {}
},
"completion_tokens_details": {}
}
}
返回结果一共有多个字段,介绍如下:
id,生成此次对话任务的 ID,用于唯一标识此次对话任务。model,选择的 Kimi 官网模型。choicesKimi 针对提问词给于的回答信息。usage:针对本次问答对 token 的统计信息。
其中 choices 是包含了 Kimi 的回答信息,它里面的 choices 是 Kimi回答的具体信息,可以发现如图所示。
流式响应
该接口也支持流式响应,这对网页对接十分有用,可以让网页实现逐字显示效果。
如果想流式返回响应,可以更改请求头里面的 stream 参数,修改为 true。
修改如图所示,不过调用代码需要有对应的更改才能支持流式响应。
Python 样例调用代码:
import requests
url = "https://api.acedata.cloud/kimi/chat/completions"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "kimi-k2.5",
"messages": [{"role":"user","content":"Hello"}],
"stream": True
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
输出效果如下:
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"content": "", "role": "assistant"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " The", "reasoning": " The"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " user said \"Hello\". This is a simple greeting", "reasoning": " user said \"Hello\". This is a simple greeting"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": ". I should respond in a friendly, welcoming manner", "reasoning": ". I should respond in a friendly, welcoming manner"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": ". Since this is the start of a conversation,", "reasoning": ". Since this is the start of a conversation,"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " I should ask how I can help them today.\n\n", "reasoning": " I should ask how I can help them today.\n\n"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": "Let me craft a response:\n- Acknowledge the", "reasoning": "Let me craft a response:\n- Acknowledge the"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " greeting\n- Offer assistance\n- Keep it warm", "reasoning": " greeting\n- Offer assistance\n- Keep it warm"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " and professional\n\nSomething like: \"Hello! How", "reasoning": " and professional\n\nSomething like: \"Hello! How"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " can I help you today?\" or \"Hi there", "reasoning": " can I help you today?\" or \"Hi there"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": "! What can I do for you?\"\n\nActually", "reasoning": "! What can I do for you?\"\n\nActually"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": ", looking at the context, this seems to be", "reasoning": ", looking at the context, this seems to be"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " a general conversation starter. I'll keep it simple and", "reasoning": " a general conversation starter. I'll keep it simple and"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"reasoning_content": " open-ended to encourage them to share what they need", "reasoning": " open-ended to encourage them to share what they need"}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"content": " Hello! How can I help", "reasoning_content": " help with. ", "reasoning": " help with. "}, "logprobs": null, "finish_reason": null}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [{"index": 0, "delta": {"content": " you today?"}, "logprobs": null, "finish_reason": "stop"}], "usage": null}
data: {"id": "chatcmpl-952dd5e75583c4d1", "object": "chat.completion.chunk", "created": 1770992031, "model": "kimi-k2.5", "system_fingerprint": null, "choices": [], "usage": {"prompt_tokens": 9, "completion_tokens": 135, "total_tokens": 144, "prompt_tokens_details": {"cached_tokens_details": {}}, "completion_tokens_details": {}}}
data: [DONE]
可以看到,响应里面有许多 data ,data 里面的 choices 即为最新的回答内容,与上文介绍的内容一致。choices 是新增的回答内容,您可以根据结果来对接到您的系统中。同时流式响应的结束是根据 data 的内容来判断的,如果内容为 [DONE],则表示流式响应回答已经全部结束。返回的 data 结果一共有多个字段,介绍如下:
id,生成此次对话任务的 ID,用于唯一标识此次对话任务。model,选择的 Kimi 官网模型。choices,Kimi 针对提问词给于的回答信息。
JavaScript 也是支持的,比如 Node.js 的流式调用代码如下:
const options = {
method: "post",
headers: {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
},
body: JSON.stringify({
"model": "kimi-k2.5",
"messages": [{"role":"user","content":"Hello"}],
"stream": true
})
};
fetch("https://api.acedata.cloud/kimi/chat/completions", options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
Java 样例代码:
JSONObject jsonObject = new JSONObject();
jsonObject.put("model", "kimi-k2.5");
jsonObject.put("messages", [{"role":"user","content":"Hello"}]);
jsonObject.put("stream", true);
MediaType mediaType = "application/json; charset=utf-8".toMediaType();
RequestBody body = jsonObject.toString().toRequestBody(mediaType);
Request request = new Request.Builder()
.url("https://api.acedata.cloud/kimi/chat/completions")
.post(body)
.addHeader("accept", "application/json")
.addHeader("authorization", "Bearer {token}")
.addHeader("content-type", "application/json")
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
System.out.print(response.body!!.string())
其他语言可以另外自行改写,原理都是一样的。
多轮对话
如果您想要对接多轮对话功能,需要对 messages 字段上传多个提问词,多个提问词的具体示例如下图所示:
import requests
url = "https://api.acedata.cloud/kimi/chat/completions"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "kimi-k2.5",
"messages": [{"role":"assistant","content":"Hello! How can I help you today?"},{"role":"user","content":"What model are you?"}]
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
通过上传多个提问词,就可以轻松实现多轮对话,可以得到如下回答:
{
"id": "chatcmpl-81e5f161ea077f5e",
"object": "chat.completion",
"created": 1770992310,
"model": "kimi-k2.5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": " I'm Kimi, an AI assistant made by Moonshot AI. I'm from the **K2.5** series.",
"refusal": null,
"reasoning_content": " The user is asking \"What model are you?\" They want to know which AI model I am.\n\n I should identify myself as Kimi, an AI assistant made by Moonshot AI. I should mention that I'm Kimi from the K2.5 series specifically, as that's the model currently deployed.\n\n Key points:\n - I am Kimi\n - Made by Moonshot AI\n - Currently Kimi K2.5 (or just say I'm part of the K2.5 series)\n - I should be helpful and direct\n\n I should not:\n - Claim to be a different model (like GPT-4, Gemini, etc.)\n - Be evasive about my identity\n - Make up version numbers that aren't correct\n\n The current model identity is Kimi K2.5 (though sometimes the exact series designation might vary by deployment, but K2.5 is the current flagship). I'll identify myself as Kimi, an AI assistant by Moonshot AI, and mention I'm from the K2.5 series.\n\n Simple, direct, accurate. ",
"reasoning": " The user is asking \"What model are you?\" They want to know which AI model I am.\n\n I should identify myself as Kimi, an AI assistant made by Moonshot AI. I should mention that I'm Kimi from the K2.5 series specifically, as that's the model currently deployed.\n\n Key points:\n - I am Kimi\n - Made by Moonshot AI\n - Currently Kimi K2.5 (or just say I'm part of the K2.5 series)\n - I should be helpful and direct\n\n I should not:\n - Claim to be a different model (like GPT-4, Gemini, etc.)\n - Be evasive about my identity\n - Make up version numbers that aren't correct\n\n The current model identity is Kimi K2.5 (though sometimes the exact series designation might vary by deployment, but K2.5 is the current flagship). I'll identify myself as Kimi, an AI assistant by Moonshot AI, and mention I'm from the K2.5 series.\n\n Simple, direct, accurate. ",
"tool_calls": []
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 28,
"completion_tokens": 235,
"total_tokens": 263,
"prompt_tokens_details": {
"cached_tokens_details": {}
},
"completion_tokens_details": {}
}
}
可以看到,choices 包含的信息与基本使用的内容是一致的,这个包含了 Kimi 针对多个对话进行回复的具体内容,这样就可以根据多个对话内容来回答对应的问题了。
错误处理
在调用 API 时,如果遇到错误,API 会返回相应的错误代码和信息。例如:
400 token_mismatched:Bad request, possibly due to missing or invalid parameters.400 api_not_implemented:Bad request, possibly due to missing or invalid parameters.401 invalid_token:Unauthorized, invalid or missing authorization token.429 too_many_requests:Too many requests, you have exceeded the rate limit.500 api_error:Internal server error, something went wrong on the server.
错误响应示例
{
"success": false,
"error": {
"code": "api_error",
"message": "fetch failed"
},
"trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
结论
通过本文档,您已经了解了如何使用 Gemini Chat Completion API 轻松实现官方 Gemini 的对话功能。希望本文档能帮助您更好地对接和使用该 API。如有任何问题,请随时联系我们的技术支持团队。