进阶 api-docs.deepseek.com 2026-10-13 22:45:35 · 27 阅读

第18章 Tool Calls 工具调用机制详解

Tool Calls Tool Calls 允许模型调用外部工具来增强自身能力。 非思考模式​ 示例代码​ 下面的例子演示如何用 Tool Calls 获取用户所在地的实时天气信息,附完整 Python 代码。 Tool Calls 的具体 API 格式请参考 Chat Completion 文档。 from openai import OpenAI

def send_messages(messages):
response = client.chat.completions.create(
model="deepseek-flash",
messages=messages,
tools=tools
)
return response.choices[0].message

client = OpenAI(
api_key="",
base_url="https://api.deepseek.com",
)

tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of a location, the user should supply a location first.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["location"]
},
}
},
]

messages = [{"role": "user", "content": "How's the weather in Hangzhou, Zhejiang?"}]
message = send_messages(messages)
print(f"User>\t {messages[0]['content']}")

tool = message.tool_calls[0]
messages.append(message)

messages.append({"role": "tool", "tool_call_id": tool.id, "content": "24℃"})
message = send_messages(messages)
print(f"Model>\t {message.content}")

此示例的执行流程如下:

用户:询问杭州当前天气。

模型:返回函数调用 get_weather({location: 'Hangzhou'})。

用户:调用函数 get_weather({location: 'Hangzhou'}),并将执行结果提供给模型。

模型:以自然语言回复:“杭州当前气温为 24°C。”

注意:在上述代码中,get_weather 函数的具体功能需由用户实现。模型本身并不执行具体函数。

Thinking Mode

自 DeepSeek-V3.2 起,API 支持在思考模式下使用工具。更多详情请参阅 Thinking Mode。

在对话中插入工具调用

在某些 agent 场景中,客户端需要将模型未生成的工具调用及其结果动态插入到对话历史中间。各 API 格式对此的支持情况不同:

  • Anthropic API(/messages)和 Responses API 支持在对话中间插入工具调用消息,也支持在对话中间插入系统消息。
  • Chat Completion API 不支持在对话中间插入工具调用,但支持在对话中间插入系统消息;若需插入工具调用,请使用 Anthropic API 或 Responses API。

strict 模式(Beta)

在 strict 模式下,模型在输出工具调用时严格遵循 Function 的 JSON schema 格式要求,确保模型输出符合用户定义。该模式同时支持思考模式和非思考模式。
要使用 strict 模式,需要:
1. 使用 base_url="https://api.deepseek.com/beta" 以启用 Beta 功能
2. 在 tools 参数中,将所有 function 的 strict 属性设置为 true
3. 服务端将验证用户提供的 Function 的 JSON Schema。若 schema 不符合规范或包含服务端不支持的 JSON schema 类型,将返回错误信息

以下是 strict 模式下工具定义的示例:

{
"type": "function",
"function": {
"name": "get_weather",
"strict": true,
"description": "Get weather of a location, the user should supply a location first.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",

在严格模式下支持的 JSON Schema 类型

object string number integer boolean array enum anyOf

object​

object 定义了一个包含键值对的嵌套结构,其中 properties 指定了对象内每个键(或属性)的 schema。每个 object 的所有属性都必须标记为 required,且该 object 的 additionalProperties 必须设置为 false。

示例:

{
    "type": "object",
    "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer" }
    },
    "required": ["name", "age"],
    "additionalProperties": false
}

string​

支持的参数:

pattern: 使用正则表达式约束字符串格式 format: 根据预定义的常见格式验证字符串。当前支持的格式: email: 电子邮件地址 hostname: 主机名 ipv4: IPv4 地址 ipv6: IPv6 地址 uuid: UUID

不支持的参数:

minLength maxLength

示例:

{
    "type": "object",
    "properties": {
        "user_email": {
            "type": "string",
            "description": "用户的电子邮件地址",
            "format": "email" 
        },
        "zip_code": {
            "type": "string",
            "description": "六位邮政编码",
            "pattern": "^\\d{6}$"
        }
    }
}

number/integer​

支持的参数:

const: 指定一个常数数值 default: 定义数字的默认值 minimum: 指定最小值 maximum: 指定最大值 exclusiveMinimum: 定义数字必须大于的值 exclusiveMaximum: 定义数字必须小于的值 multipleOf: 确保数字是指定值的倍数

示例:

{
    "type": "object",
    "properties": {
        "score": {
            "type": "integer",
            "description": "1 到 5 之间的数字,代表你的评分,越高越好",
            "minimum": 1,
            "maximum": 5
        }
    },
    "required": ["score"],
    "additionalProperties": false
}

array​

不支持的参数:

minItems maxItems

示例:

{
    "type": "object",
    "properties": {
"properties": {
"keywords": {
"type": "array",
"description": "文章的五个关键词,按重要性排序",
"items": {
"type": "string",
"description": "简洁准确的关键词或短语。"
}
}
},
"required": ["keywords"],
"additionalProperties": false
}
enum​ enum 用于确保输出只能是预定义选项中的某一个。例如订单状态,就只能是指定状态集合中的某一种。 示例: {
"type": "object",
"properties": {
"order_status": {
"type": "string",
"description": "订单状态",
"enum": ["pending", "processing", "shipped", "cancelled"]
}
}
}
anyOf​ anyOf 可以匹配提供的任意一个 schema,让字段支持多种合法格式。例如,用户的账号既可以是邮箱,也可以是手机号: {
"type": "object",
"properties": {
"account": {
"anyOf": [
{ "type": "string", "format": "email", "description": "可以是电子邮件地址" },
{ "type": "string", "pattern": "^\\d{11}$", "description": "或11位手机号码" }
]
}
}
}
$ref and $def​ 你可以用 $def 定义可复用的模块,再用 $ref 引用它们,减少 schema 的重复,实现模块化。此外,$ref 也可以单独用来定义递归结构。 {
"type": "object",
"properties": {
"report_date": {
"type": "string",
"description": "报告发布的日期"
},
"authors": {
"type": "array",
"description": "报告的作者列表",
"items": {
"$ref": "#/$def/author"
}
}
},
"required": ["report_date", "authors"],
"additionalProperties": false,
"$def": {
"author": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "作者姓名"
},
"institution": {
"type": "string",
"description": "作者所在机构"
},
"email": {
"type": "string",
"format": "email",
"description": "作者邮箱"
}
},
"additionalProperties": false,
"required": ["name", "institution", "email"]
}
}
}

评论 (0)