deepseek2api 现已支持 OpenAI 格式的 tools 参数,可用于:
- MCP (Model Context Protocol) 工具调用
- OpenClaw 本地技能集成
- 任何符合 OpenAI tools 规范的应用
✅ 支持 OpenAI 标准的 tools 参数格式
✅ 自动检测模型返回的 tool_calls
✅ 支持流式和非流式响应
✅ 兼容现有的 /v1/chat/completions 端点
✅ 返回标准的 finish_reason: "tool_calls"
import requests
import json
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称"
}
},
"required": ["location"]
}
}
}
]
response = requests.post(
"http://localhost:8000/v1/chat/completions",
json={
"model": "deepseek-chat",
"messages": [
{"role": "user", "content": "北京今天天气怎么样?"}
],
"tools": tools
}
)
result = response.json()
print(result["choices"][0]["message"])当模型决定调用工具时,响应格式如下:
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "deepseek-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_001",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\": \"北京\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {...}
}- 发送请求:在
messages中包含用户问题,在tools中定义可用工具 - 模型决策:模型分析是否需要调用工具
- 返回 tool_calls:如果需要,返回
tool_calls数组和finish_reason: "tool_calls" - 执行工具:客户端执行工具并获取结果
- 继续对话:将工具结果作为
tool角色的消息发送回模型
# 第一轮:用户提问
messages = [
{"role": "user", "content": "北京今天天气怎么样?"}
]
response1 = requests.post(url, json={
"model": "deepseek-chat",
"messages": messages,
"tools": tools
})
# 模型返回 tool_calls
tool_calls = response1.json()["choices"][0]["message"]["tool_calls"]
# 执行工具
tool_result = get_weather(location="北京") # 你的工具实现
# 第二轮:提供工具结果
messages.append({
"role": "assistant",
"content": "",
"tool_calls": tool_calls
})
messages.append({
"role": "tool",
"tool_call_id": tool_calls[0]["id"],
"content": json.dumps(tool_result)
})
response2 = requests.post(url, json={
"model": "deepseek-chat",
"messages": messages,
"tools": tools
})
# 模型基于工具结果生成最终回答
final_answer = response2.json()["choices"][0]["message"]["content"]对于 MCP 客户端(如 Claude Desktop、Cursor 等),只需配置 API 端点:
{
"mcpServers": {
"deepseek": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-everything"],
"env": {
"OPENAI_API_BASE": "http://localhost:8000/v1",
"OPENAI_API_KEY": "your-key"
}
}
}
}OpenClaw 可以通过标准的 OpenAI 客户端调用:
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="your-key"
)
# OpenClaw 会自动处理 tools 参数
response = client.chat.completions.create(
model="deepseek-chat",
messages=[...],
tools=[...]
)流式模式下,tool_calls 会在流的末尾发送:
response = requests.post(url, json={
"model": "deepseek-chat",
"messages": messages,
"tools": tools,
"stream": True
}, stream=True)
for line in response.iter_lines():
if line.startswith(b'data: '):
data = json.loads(line[6:])
delta = data["choices"][0]["delta"]
if "tool_calls" in delta:
print(f"Tool call: {delta['tool_calls']}")
elif "content" in delta:
print(delta["content"], end="")- 模型能力:DeepSeek 模型需要理解工具调用的指令格式,建议使用 deepseek-chat 或更新的模型
- JSON 格式:模型需要返回特定格式的 JSON 才能被识别为 tool_calls
- 参数验证:客户端应验证 tool_calls 的参数是否符合工具定义
- 错误处理:如果模型没有正确返回 tool_calls 格式,会作为普通文本返回
运行测试脚本:
python3 test_tools.py- Issue #50: MCP 工具支持 ✅
- Issue #52: OpenClaw 支持 ✅
- 2026-03-21: 添加 tools 参数支持,实现 MCP 和 OpenClaw 集成