Appearance
DeepSeek API Tool Calls 允许模型调用外部函数,标准接口与 OpenAI function calling 完全兼容。Strict 模式("strict": true)强制模型输出严格符合 JSON Schema 的结构体,消除格式不确定性,是构建生产级 Agent 的推荐选项。
DeepSeek API Tool Calls
Tool Calls 让模型在生成回答前,先"调用"你定义的函数获取数据,再基于函数返回结果给出最终答案。
基本流程
用户提问 → 模型返回 function call → 你执行函数 → 结果传回模型 → 模型生成最终回答代码示例
typescript
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.deepseek.com",
apiKey: process.env.DEEPSEEK_API_KEY,
});
// 第一轮:让模型决定是否调用函数
const response = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "北京今天天气怎么样?" }],
tools: [
{
type: "function",
function: {
name: "get_weather",
description: "获取指定城市的天气信息",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "城市名称" },
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["city"],
},
},
},
],
tool_choice: "auto",
});
const toolCall = response.choices[0].message.tool_calls?.[0];
if (toolCall) {
// 第二轮:执行函数并传回结果
const weatherData = await getWeather(JSON.parse(toolCall.function.arguments));
const finalResponse = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [
{ role: "user", content: "北京今天天气怎么样?" },
response.choices[0].message, // 包含 tool_calls 的 assistant 消息
{
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify(weatherData),
},
],
tools: [...], // 重复传入相同的 tools 定义
});
console.log(finalResponse.choices[0].message.content);
}Strict 模式
标准 Tool Calls 中,模型可能偶尔输出不完全符合 schema 的 JSON。Strict 模式 强制模型严格遵守 JSON Schema,消除这种不确定性。
启用方法:
typescript
tools: [
{
type: "function",
function: {
name: "get_weather",
strict: true, // 开启 Strict 模式
parameters: {
type: "object",
properties: {
city: { type: "string" },
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["city", "unit"], // 所有属性必须在 required 中
additionalProperties: false, // 必须设为 false
},
},
},
],同时将 baseURL 改为 Beta 端点:
typescript
const client = new OpenAI({
baseURL: "https://api.deepseek.com/beta", // 注意:strict 模式使用 /beta 端点
apiKey: process.env.DEEPSEEK_API_KEY,
});Strict 模式 JSON Schema 约束
| 类型 | 支持的关键字 |
|---|---|
object | properties、required(所有 key 必须在 required 中)、additionalProperties: false |
string | enum、format(email、hostname、date、date-time、uuid) |
number/integer | minimum、maximum、multipleOf |
array | items(单一类型) |
| 复合 | anyOf(有限制,不支持嵌套 anyOf) |
| 引用 | $ref、$defs |
常见踩坑:Strict 模式下,
required必须列出properties中的所有字段,且additionalProperties必须显式设为false,否则请求返回 400。
标准模式 vs Strict 模式
| 标准模式 | Strict 模式 | |
|---|---|---|
| 格式一致性 | 偶尔不符合 schema | 100% 符合 |
| 端点 | api.deepseek.com | api.deepseek.com/beta |
| schema 限制 | 无 | 有(见上表) |
| 适用场景 | 原型阶段 | 生产环境 |
与 OpenAI function calling 对比:接口完全兼容,DeepSeek Strict 模式的 schema 约束比 OpenAI 稍严格(不支持
anyOf嵌套)。
常见问题
Q: tool_choice 应该用 "auto" 还是 "required"?
A: 通常用 "auto",让模型自主决定是否调用函数。"required" 强制模型必须调用某个函数,适合你确定需要结构化输出的场景。{"type": "function", "function": {"name": "xxx"}} 可指定调用特定函数。
Q: Strict 模式返回 400 怎么排查?
A: 最常见原因是 required 没有列出所有 properties 中的字段,或者忘记加 "additionalProperties": false。检查 schema 中每个 object 类型是否满足这两个条件。
Q: 可以一次定义多个函数吗?
A: 可以,tools 数组支持多个函数定义。模型会根据用户意图选择最合适的函数,或在一次回答中调用多个函数(parallel tool calls)。