Responses API 使用指南
更新时间:2025-11-20
Responses API 是新一代API接口,继承了 chat API 的简洁性,同时在功能上进行了增强,内置支持函数调用、知识库检索等一系列强大的工具,特别适用于智能体构建场景。
支持模型
-
DeepSeek系列
- deepseek-v3.2(非思考模式)
- deepseek-v3.2-think(思考模式)
- deepseek-v3.1-250821(非思考模式)
- deepseek-v3.1-think-250821(思考模式)
- deepseek-v3
- deepseek-r1
- deepseek-r1-250528
-
kimi系列
- kimi-k2-instruct
-
Qwen系列
- qwen3-coder-480b-a35b-instruct
- qwen3-coder-30b-a3b-instruct
- qwen3-235b-a22b
- qwen3-235b-a22b-thinking-2507
- qwen3-235b-a22b-instruct-2507
- qwen3-30b-a3b
- qwen3-30b-a3b-instruct-2507
- qwen3-30b-a3b-thinking-2507
- qwen3-32b
- qwen3-14b
- qwen3-8b
- qwen3-4b
- qwen3-1.7b
- qwen3-0.6b
快速开始
HTTP请求
Plain Text
1curl --location 'https://qianfan.baidubce.com/v2/responses' \
2--header 'Authorization: Bearer bce-v3/ALTAK-*********/614fb**********'\
3--header 'Content-Type: application/json' \
4--data '{
5 "model": "deepseek-v3.2",
6 "input": "你好呀。"
7}'
OpenAI SDK 兼容
Plain Text
1from openai import OpenAI
2
3client = OpenAI(
4 base_url= "https://qianfan.baidubce.com/v2",
5 api_key= "bce-v3/ALTAK-*********/614fb**********",
6)
7
8response = client.responses.create(
9 model="deepseek-v3.2",
10 input="你好"
11)
12
13print(response)
与 Chat API 的对比
功能对比
| 功能项 | Chat API | Responses API |
|---|---|---|
| 工具调用 | 手动实现完整流程 | 自动化内置支持 |
| 内置工具 | 无,需要定制化集成 | 内置 web_search、knowledge_search 工具 |
| 企业工具集成 | 需开发适配 | 原生 MCP 协议支持 |
使用方式对比
Chat API 和 Responses API 都可以从模型中生成输出,但输入、输出结构不同:
- Chat API 的输入、输出都是由一组
messages组成的数组,其中包括 role、content 等信息. - Responses API 使用
Items作为输入和输出,支持传入字符串或消息列表,返回一个包含 id 的 response 对象.
Chat API
Plain Text
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer bce-v3/ALTAK-*********/614fb**********' \
4--data '{
5 "model": "deepseek-v3.2",
6 "messages": [
7 {
8 "role": "system",
9 "content": "You are a helpful assistant."
10 },
11 {
12 "role": "user",
13 "content": "你好"
14 }
15 ]
16}'
Plain Text
1{
2 "id": "as-qsp8w7ppnv",
3 "object": "chat.completion",
4 "created": 1755938117,
5 "model": "deepseek-v3.2",
6 "choices": [
7 {
8 "index": 0,
9 "message": {
10 "role": "assistant",
11 "content": "你好!很高兴和你交流。请问有什么我可以帮助你的吗?"
12 },
13 "finish_reason": "stop",
14 "flag": 0
15 }
16 ],
17 "usage": {
18 "prompt_tokens": 11,
19 "completion_tokens": 15,
20 "total_tokens": 26
21 }
22}
Responses API
Plain Text
1curl --location 'https://qianfan.baidubce.com/v2/responses' \
2--header 'Authorization: Bearer bce-v3/ALTAK-*********/614fb**********'\
3--header 'Content-Type: application/json' \
4--data '{
5 "model": "deepseek-v3.2",
6 "input": "你好呀。"
7}'
Plain Text
1{
2 "id": "rsn-esmpwb5tpa",
3 "log_id": "as-2y7h2vj4dx",
4 "object": "response",
5 "created_at": 1763464793,
6 "status": "completed",
7 "model": "deepseek-v3.2",
8 "output": [
9 {
10 "id": "msg-5yr0n821vh",
11 "type": "message",
12 "status": "completed",
13 "role": "assistant",
14 "content": [
15 {
16 "type": "output_text",
17 "text": "你好呀!很高兴见到你!我是DeepSeek,你的AI助手,随时准备为你提供帮助。\n\n今天有什么我可以协助你的吗?无论是回答问题、帮你分析问题、聊天交流,还是其他任何需要,我都很乐意为你服务。快告诉我你想了解什么或者需要什么帮助吧!"
18 }
19 ]
20 }
21 ],
22 "usage": {
23 "input_tokens": 10,
24 "output_tokens": 65,
25 "output_tokens_details": {
26 "reasoning_tokens": 0
27 },
28 "total_tokens": 75
29 }
30}
典型案例
Function calling
第一轮请求:触发工具调用
Plain Text
1{
2 "model": "deepseek-v3.1-250821",
3 "input": [
4 {
5 "type": "message",
6 "role": "user",
7 "content": "查一下北京现在的天气"
8 }
9 ],
10 "tools": [
11 {
12 "type": "function",
13 "name": "get_current_weather",
14 "description": "天气查询工具",
15 "parameters": {
16 "properties": {
17 "location": {
18 "description": "地理位置,精确到区县级别",
19 "type": "string"
20 },
21 "time": {
22 "description": "时间,格式为YYYY-MM-DD",
23 "type": "string"
24 }
25 },
26 "type": "object"
27 }
28 }
29 ]
30}
模型返回:
Plain Text
1{
2 "id": "rsn-9tiw5uvybq",
3 "object": "response",
4 "created_at": 1763642451,
5 "status": "completed",
6 "model": "deepseek-v3.1-250821",
7 "tools": [
8 {
9 "type": "function",
10 "name": "get_current_weather",
11 "description": "天气查询工具",
12 "parameters": {
13 "properties": {
14 "location": {
15 "description": "地理位置,精确到区县级别",
16 "type": "string"
17 },
18 "time": {
19 "description": "时间,格式为YYYY-MM-DD",
20 "type": "string"
21 }
22 },
23 "type": "object"
24 }
25 }
26 ],
27 "output": [
28 {
29 "id": "fc-ezq7k2jwue",
30 "type": "function_call",
31 "status": "completed",
32 "arguments": "{\"location\": \"北京\", \"time\": \"2023-11-14\"}",
33 "name": "get_current_weather",
34 "call_id": "e3b1d10d37d348bdb7a794089a5b7c35"
35 }
36 ],
37 "usage": {
38 "input_tokens": 511,
39 "output_tokens": 50,
40 "output_tokens_details": {
41 "reasoning_tokens": 0
42 },
43 "total_tokens": 561
44 }
45}
执行工具获取天气查询结果
Plain Text
1{\"temperature\": \"20\", \"unit\": \"摄氏度\", \"description\": \"北京\"}
第二轮请求:回传结果生成最终响应
Plain Text
1{
2 "model": "deepseek-v3.1-250821",
3 "input": [
4 {
5 "role": "user",
6 "type": "message",
7 "content": "查一下北京现在的天气"
8 },
9 {
10 "call_id": "e3b1d10d37d348bdb7a794089a5b7c35",
11 "arguments": "{\"location\": \"北京\", \"time\": \"2023-11-14\"}",
12 "name": "get_current_weather",
13 "type": "function_call"
14 },
15 {
16 "call_id": "e3b1d10d37d348bdb7a794089a5b7c35",
17 "output": "{\"temperature\": \"20\", \"unit\": \"摄氏度\", \"description\": \"北京\"}",
18 "type": "function_call_output"
19 }
20 ]
21}
模型最终回答:
Plain Text
1{
2 "id": "rsn-q0i1xezppr",
3 "object": "response",
4 "created_at": 1763642554,
5 "status": "completed",
6 "model": "deepseek-v3.1-250821",
7 "output": [
8 {
9 "id": "msg-6aedcch8m6",
10 "type": "message",
11 "status": "completed",
12 "role": "assistant",
13 "content": [
14 {
15 "type": "output_text",
16 "text": "根据最新的天气信息,北京市当前的天气情况如下:\n\n- **温度**:20 ℃摄氏度\n- **天气状况**:当前查询到的数据没有提供具体的天气描述(如晴、多云等),但温度信息显示为20度,属于较为舒适的秋季气温。\n\n如果您需要更详细的天气信息(如湿度、风速、未来预报等),可以提供更具体的要求,我会进一步为您查询。"
17 }
18 ]
19 }
20 ],
21 "usage": {
22 "input_tokens": 100,
23 "output_tokens": 83,
24 "output_tokens_details": {
25 "reasoning_tokens": 0
26 },
27 "total_tokens": 183
28 }
29}
