Skip to content

Kimi API 兼容 OpenAI SDK,替换 baseURL 为 https://api.moonshot.cn/v1 即可迁移。本文列出关键差异点:temperature 范围限制、kimi-k2.6 参数固定值、tool_choice 不支持 required、function_call 已废弃需换 tool_calls。

从 OpenAI 迁移到 Kimi API

核心迁移步骤

只需两步:

python
# Python
from openai import OpenAI

client = OpenAI(
    api_key="MOONSHOT_API_KEY",     # 替换为 Kimi API Key
    base_url="https://api.moonshot.cn/v1",  # 替换 base_url
)
typescript
// TypeScript / Node.js
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.MOONSHOT_API_KEY,
  baseURL: "https://api.moonshot.cn/v1",
});

与 DeepSeek API 迁移方式相同,都是只改 baseURL 和 Key。


兼容的端点

以下端点与 OpenAI 完全兼容:

  • /v1/chat/completions
  • /v1/files
  • /v1/files/{file_id}
  • /v1/files/{file_id}/content

关键差异点

1. temperature 取值范围

OpenAIKimi API
取值范围[0, 2][0, 1]
kimi-k2.6 thinking固定 1.0,不可修改
kimi-k2.6 非 thinking固定 0.6,不可修改

迁移建议:如果原代码设置 temperature > 1,迁移到 Kimi API 时需改为 <= 1。使用 kimi-k2.6 时建议不设置 temperature,使用默认值。

2. temperature=0 时不能设 n>1

OpenAI 允许 temperature=0, n>1;Kimi API 中 temperature 接近 0 时只能返回 1 个 choice,设置 n>1 会报 invalid_request_error

3. function_call 已废弃

Kimi API 不支持已废弃的 functions 参数。需迁移到 tools(工具调用):

python
# 旧写法(function_call,不支持)
response = client.chat.completions.create(
    functions=[{"name": "get_weather", ...}],
    function_call="auto"
)

# 新写法(tool_calls,推荐)
response = client.chat.completions.create(
    tools=[{"type": "function", "function": {"name": "get_weather", ...}}],
    tool_choice="auto"
)

4. tool_choice 不支持 required

当前 Kimi API 支持的 tool_choice 值:"none""auto"null

不支持 tool_choice="required"。如果原逻辑依赖此特性,临时方案是在 system prompt 中明确要求模型调用特定工具,但不能 100% 保证触发。

5. 流式输出 usage 字段

Kimi API 在流式响应中,每个 choice 的结束 chunk 会包含该 choice 的 usage 信息,可通过 choice.usage 访问。这是 Kimi API 的扩展功能,不影响标准 OpenAI 用法。


迁移检查清单

  • [ ] 替换 baseURLhttps://api.moonshot.cn/v1
  • [ ] 替换 API Key 环境变量
  • [ ] 更新模型名称(如 gpt-4okimi-k2.6
  • [ ] 检查 temperature 参数(kimi-k2.6 不可设置)
  • [ ] 将 functions 参数改为 tools
  • [ ] 检查 tool_choice 是否使用了 required

常见问题

Q: Kimi API 的 kimi-k2.6 和 OpenAI 的 GPT-4o 哪个更适合中文场景?

A: kimi-k2.6 对中文的理解更原生,且上下文窗口(256K)远超 GPT-4o(128K),更适合中文长文档处理。同等场景下 kimi-k2.6 价格也更低。

Q: 能用 OpenAI 的官方 Python SDK 调用 Kimi API 吗?

A: 可以,完全兼容。安装 openai 包后直接用,无需安装额外依赖。

Q: 从 DeepSeek API 迁移到 Kimi API 有什么需要注意的?

A: 两者都兼容 OpenAI SDK 格式,迁移步骤相同。注意模型名称不同(DeepSeek 用 deepseek-chat,Kimi 用 kimi-k2.6),以及 kimi-k2.6 的 temperature 参数有固定值约束。

Kimi API 完全兼容 OpenAI SDK,迁移只需替换 baseURLapiKey 两处配置。但有几个关键差异需要注意:temperature 范围不同、kimi-k2 系列模型的特殊 temperature 要求、不支持 function_calltool_choice: "required" 暂不支持。本文给出完整迁移清单。

从 OpenAI 迁移到 Kimi API

最简迁移

只需替换两处配置:

typescript
// 原 OpenAI 配置
const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  // baseURL 默认为 https://api.openai.com/v1
});

// 迁移到 Kimi
const client = new OpenAI({
  apiKey: process.env.MOONSHOT_API_KEY,       // ← 换成 Kimi Key
  baseURL: "https://api.moonshot.cn/v1",      // ← 换成 Kimi baseURL
});
python
# 原 OpenAI 配置
client = OpenAI(api_key="sk-...")

# 迁移到 Kimi
client = OpenAI(
    api_key="MOONSHOT_API_KEY",
    base_url="https://api.moonshot.cn/v1",
)

与 DeepSeek API 迁移方式相同:替换 baseURLapiKey,其他代码不变。

已兼容的接口

接口路径说明
/v1/chat/completions对话补全(含流式)
/v1/files文件上传
/v1/files/{file_id}获取文件信息
/v1/files/{file_id}/content获取文件内容

关键差异

1. temperature 取值范围

OpenAIKimi
范围[0, 2][0, 1]
temperature=0 时支持 n>1❌(会报 invalid_request_error)

kimi-k2.6 / kimi-k2.5 的特殊要求

typescript
// ❌ 错误:这两个模型不接受任意 temperature 值
{ model: "kimi-k2.6", temperature: 0.7 }

// ✅ 思考模式:固定用 1.0
{ model: "kimi-k2.6", temperature: 1.0 }

// ✅ 非思考模式:固定用 0.6
{ model: "kimi-k2.6", temperature: 0.6 }

// ✅ 最简单:不设置 temperature,使用默认值
{ model: "kimi-k2.6" }

2. function_call 已废弃

Kimi API 不支持已废弃的 functions 参数,需迁移到 tools

typescript
// ❌ 旧 function_call 格式(Kimi 不支持)
const completion = await client.chat.completions.create({
  functions: [{ name: "search", ... }],
  function_call: "auto",
});

// ✅ 新 tool_calls 格式
const completion = await client.chat.completions.create({
  tools: [{ type: "function", function: { name: "search", ... } }],
  tool_choice: "auto",
});

functionstools 的快速转换:

typescript
// 旧的 function 定义格式
const functions = [{ name: "search", description: "...", parameters: {...} }];

// 转换为 tools 格式:每个 function 包装进 { type: "function", function: ... }
const tools = functions.map(fn => ({ type: "function" as const, function: fn }));

3. tool_choice 支持

tool_choice 值Kimi 支持
"none"
"auto"
null
"required"❌ 暂不支持

tool_choice: "required" 的替代方案

typescript
// 第一次请求后,如果模型没有调用工具,追加一条 user 消息再请求
const completion = await client.chat.completions.create({
  model: "kimi-k2.6",
  messages,
  tools,
});

if (completion.choices[0].finish_reason !== "tool_calls") {
  // 提示模型使用工具
  messages.push(completion.choices[0].message);
  messages.push({
    role: "user",
    content: "请选择一个工具来处理当前问题。",
  });
  const retry = await client.chat.completions.create({ model: "kimi-k2.6", messages, tools });
}

4. stream 模式下的 usage

Kimi API 除了支持 stream_options: { include_usage: true } 外,还在每个 choice 结束时额外放置 usage 字段。如果需要统计每个 choice 的独立 token 消耗,可以读取 choice.usage

迁移检查清单

  • [ ] 替换 baseURLhttps://api.moonshot.cn/v1(国内)或 https://api.moonshot.ai/v1(海外)
  • [ ] 替换 apiKey 为 Moonshot API Key(MOONSHOT_API_KEY
  • [ ] 检查 temperature 是否超过 1.0
  • [ ] 如使用 kimi-k2.6/k2.5,移除显式 temperature 设置,或按模式设为 0.6/1.0
  • [ ] 将 functions + function_call 迁移为 tools + tool_choice
  • [ ] 如依赖 tool_choice: "required",实现应用层重试逻辑
  • [ ] 更新模型名称:gpt-4kimi-k2.6gpt-3.5-turbomoonshot-v1-8k

常见问题

Q: 迁移后出现 invalid_request_error,怎么排查?

A: 最常见原因是 temperature 值超过 1.0,或者在 kimi-k2.6 上设置了不支持的 temperature 值。另外,检查是否还在使用废弃的 functions 参数。

Q: 模型名称怎么对应?

A: 没有完全固定的对应关系,但一般:长文本场景用 kimi-k2.6(256K)或 moonshot-v1-128k(128K);短文本轻量场景用 moonshot-v1-8k

Q: 海外用户应该用哪个 API 地址?

A: 海外用户用 https://api.moonshot.ai/v1,API Key 来自 platform.kimi.ai。国内用户用 https://api.moonshot.cn/v1,Key 来自 platform.kimi.com。两套账号体系独立,Key 不能混用。