DeepSeek 工具调用(Tools)¶
DeepSeek 系列模型在本平台支持使用 OpenAI 风格的 tools / tool_choice 进行函数工具调用(以实际开通通道为准),可以用来:
- 封装联网搜索:例如把你自己的搜索服务包装成
web_search工具 - 调用业务 API:如订单查询、工单系统、知识库检索、数据库读写等
官方参考
⚠️ 注意:根据 DeepSeek 官方文档,
deepseek-reasoner当前不支持 Function Call / 工具调用,仅支持普通对话与思维链输出。需要使用工具时,请选用支持tools的对话模型(例如deepseek-chat,以平台实际发布为准)。
🧩 基本思路¶
DeepSeek 的工具调用遵循与 OpenAI Chat Completions 基本一致的流程:
- 在请求中通过
tools定义函数工具(包括名称、说明、JSON Schema 参数) - 首次请求
/v1/chat/completions,让模型根据需要决定是否调用工具 - 如果模型返回
tool_calls: - 你在后端真正调用对应函数 / 服务(例如自己的搜索接口)
- 将执行结果以
role: "tool"的消息形式回传给模型 - 再次调用
/v1/chat/completions,模型基于工具结果给出最终自然语言回答
下文示例将演示如何在 新 API 平台 + DeepSeek 模型 下,用工具调用的方式实现「联网搜索」。
🌐 示例:通过工具实现联网搜索(curl)¶
这个示例中,我们把自己的搜索服务(例如 /internal/search 接口)抽象成一个 web_search 工具,让 DeepSeek 模型自动决定何时搜索、如何组合搜索结果与自身知识来回答问题。
1. 定义工具并发送首轮请求(获取 tool_calls)¶
curl https://api-cs-al.naci-tech.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "deepseek-chat",
"messages": [
{
"role": "user",
"content": "帮我查一下 2026 年最新的 TypeScript 版本有哪些关键变化,并给出参考链接。"
}
],
"tool_choice": "auto",
"tools": [
{
"type": "function",
"function": {
"name": "web_search",
"description": "在互联网中搜索与用户问题最相关的最新信息,并返回简要结果。",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "要搜索的查询语句,例如“2026 年 TypeScript 最新特性”。"
},
"top_k": {
"type": "integer",
"description": "返回结果数量(1-5 条)。"
}
},
"required": ["query"]
}
}
}
]
}'
如果模型认为需要搜索,会在响应中返回类似结构(简化示意):
{
"choices": [
{
"message": {
"role": "assistant",
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "web_search",
"arguments": "{\"query\":\"2026 TypeScript 新特性\",\"top_k\":3}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}
其中:
id:本次工具调用的唯一标识,需要在下一步role: "tool"消息中带上function.arguments:JSON 字符串,解析后用于真正调用你自己的搜索服务
2. 执行搜索服务并回传结果(第二次请求)¶
你在后端解析 tool_calls[0],调用自己的搜索接口(伪代码):
然后把搜索结果作为 role: "tool" 消息写回到 messages 中,再发起第二次 /v1/chat/completions 请求:
curl https://api-cs-al.naci-tech.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "deepseek-chat",
"messages": [
{
"role": "user",
"content": "帮我查一下 2026 年最新的 TypeScript 版本有哪些关键变化,并给出参考链接。"
},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "web_search",
"arguments": "{\"query\":\"2026 TypeScript 新特性\",\"top_k\":3}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "{\"results\":[{\"title\":\"TypeScript 6.0 Release Notes\",\"url\":\"https://www.typescriptlang.org/...\",\"snippet\":\"Key changes in TypeScript 6.0 include ...\"}]}"
}
]
}'
这次请求中,DeepSeek 会读取 role: "tool" 的搜索结果,结合自身知识,生成最终的自然语言总结并返回给用户。
在整个流程中:
- 模型负责**判断是否需要联网搜索、构造查询词、理解搜索结果**
- 你的后端负责**真正访问搜索引擎 / 内部检索服务**
- 用户只看到一轮自然语言问答,具体工具调用过程可以选择是否在前端 UI 中展示
🔒 可选:严格模式(strict)简介¶
DeepSeek 官方提供了 strict(严格)模式,用于在模型输出 Function 调用时 严格遵守 JSON Schema,例如:
{
"type": "function",
"function": {
"name": "get_weather",
"strict": true,
"description": "Get weather of a location",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"],
"additionalProperties": false
}
}
}
要启用严格模式,官方要求:
- 使用
base_url="https://api.deepseek.com/beta" - 在
tools中为所有function设置strict: true - 严格遵守文档中支持的 JSON Schema 类型(
object/string/number/integer/boolean/array/enum/anyOf等)
是否支持 strict 取决于你是**直连 DeepSeek 官方 API** 还是通过本平台网关调用,建议结合自身接入方式参考官方文档说明:
👉 Function Calling | DeepSeek API Docs