> ## Documentation Index
> Fetch the complete documentation index at: https://docs.heihuzi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# DeepSeek 工具调用

> Chat Completions 与 Responses 的两轮真实工具闭环。

实测日期：**2026-09-19**。本页使用 `deepseek-flash`，分别完成 Chat Completions 和 Responses 的“模型给出工具参数 → 客户端执行函数 → 回传结果 → 模型给出答案”闭环。每个示例产生两次生成请求。

模型本身不执行 Python 函数。示例只在本地计算 `19 + 23`，并把 `42` 回传；真实业务只执行自己允许的工具，并校验函数名与参数。[官方 Tool Calls](https://api-docs.deepseek.com/zh-cn/guides/tool_calls/)

## Chat Completions 完整示例

第一轮必须返回 `finish_reason="tool_calls"`、`add` 函数及参数；回传完整 assistant 消息，保留 `tool_calls` 和可能存在的 `reasoning_content`。第二轮通过 `tool_call_id` 关联工具结果，实际返回 `42`。

```python theme={null}
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
import json
tools = [{'type': 'function', 'function': {'name': 'add', 'description': 'Add two integers.', 'parameters': {'type': 'object', 'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}}, 'required': ['a', 'b'], 'additionalProperties': False}}}]
messages = [{'role': 'user', 'content': 'Use the add tool to calculate 19 + 23, then reply with the number only.'}]
first = client.chat.completions.create(model='deepseek-flash', messages=messages, tools=tools, tool_choice={'type': 'function', 'function': {'name': 'add'}}, max_tokens=1024)
message = first.choices[0].message
assert first.choices[0].finish_reason == 'tool_calls'
assert len(message.tool_calls or []) == 1
messages.append(message.model_dump(exclude_none=True))
call = message.tool_calls[0]
assert call.function.name == 'add'
args = json.loads(call.function.arguments)
assert args == {'a': 19, 'b': 23}
messages.append({'role': 'tool', 'tool_call_id': call.id, 'content': str(args['a'] + args['b'])})
second = client.chat.completions.create(model='deepseek-flash', messages=messages, tools=tools, tool_choice='none', max_tokens=1024)
assert second.choices[0].finish_reason == 'stop'
assert second.choices[0].message.content.strip() == '42'
print(second.choices[0].message.content)
```

## Responses 完整示例

保留第一轮的全部 output 条目，将工具结果作为 `function_call_output`，用 `call_id` 关联。不要用 response ID 代替 call ID，也不要只回传最终文本。本次第二轮实际返回 `42`。

```python theme={null}
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
import json
tools = [{'type': 'function', 'name': 'add', 'description': 'Add two integers.', 'parameters': {'type': 'object', 'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}}, 'required': ['a', 'b'], 'additionalProperties': False}}]
history = [{'role': 'user', 'content': 'Use the add tool to calculate 19 + 23, then reply with the number only.'}]
first = client.responses.create(model='deepseek-flash', input=history, tools=tools, tool_choice={'type': 'function', 'name': 'add'}, max_output_tokens=1024)
assert first.status == 'completed'
calls = [item for item in first.output if item.type == 'function_call']
assert len(calls) == 1 and calls[0].name == 'add'
call = calls[0]
args = json.loads(call.arguments)
assert args == {'a': 19, 'b': 23}
history.extend((item.model_dump(exclude_none=True) for item in first.output))
history.append({'type': 'function_call_output', 'call_id': call.call_id, 'output': str(args['a'] + args['b'])})
second = client.responses.create(model='deepseek-flash', input=history, tools=tools, tool_choice='none', max_output_tokens=1024)
assert second.status == 'completed' and second.output_text.strip() == '42'
print(second.output_text)
```

## 本次验证的范围

两种格式均验证单个函数、两个整数参数、显式选择 `add` 函数、第二轮 `tool_choice="none"` 后给出最终答案。返回的函数名、参数、调用关联 ID 和最终 `42` 均已核对。请求中的 schema 仅用于本页这个函数示例。
