aicodeswitch 2.0.11 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2162 @@
1
+ # OpenAI API Schema 标准参考
2
+
3
+ OpenAI 官方 API 数据格式规范和完整使用指南。包括经典 Completions API、Chat Completions API 和最新 Responses API(实时对话)。
4
+
5
+ ## 目录
6
+ - [API 概览](#api-概览)
7
+ - [Completions API(文本补全)](#completions-api文本补全)
8
+ - [Chat Completions API(对话模型)](#chat-completions-api对话模型)
9
+ - [Responses API(实时对话)](#responses-api实时对话)
10
+ - [模型选择](#模型选择)
11
+ - [流式事件](#流式事件)
12
+ - [错误处理](#错误处理)
13
+ - [快速参考](#快速参考)
14
+ - [最佳实践](#最佳实践)
15
+
16
+ ---
17
+
18
+ ## API 概览
19
+
20
+ ### 官方 API 端点
21
+
22
+ OpenAI 提供三个主要 API 接口,各有不同的用途:
23
+
24
+ | API 类型 | 端点 | 用途 | 状态 |
25
+ |---------|------|------|------|
26
+ | **Responses API** | `/v1/responses` | 新一代响应接口(推荐) | ✅ 最新标准 |
27
+ | **Chat Completions API** | `/v1/chat/completions` | 对话、代码生成 | ✅ 仍可用 |
28
+ | **Completions API** | `/v1/completions` | 文本补全(已弃用) | ⚠️ 维护模式 |
29
+
30
+ ### API 基础配置
31
+
32
+ ```
33
+ 基础 URL: https://api.openai.com/v1
34
+
35
+ 认证方式:
36
+ Authorization: Bearer sk-xxxxx
37
+
38
+ x-api-key: sk-xxxxx
39
+
40
+ Content-Type: application/json
41
+ ```
42
+
43
+ ---
44
+
45
+ ## Completions API(文本补全)
46
+
47
+ ### 概述
48
+
49
+ **Completions API** 是 OpenAI 早期的文本生成接口,适用于纯文本补全任务。此 API 已不推荐用于新项目,推荐使用 Chat Completions API。
50
+
51
+ ### 请求格式
52
+
53
+ ```typescript
54
+ POST /v1/completions
55
+ Host: api.openai.com
56
+ Authorization: Bearer sk-xxx
57
+ Content-Type: application/json
58
+
59
+ {
60
+ // ===== 必需字段 =====
61
+ "model": string, // 模型ID (例: gpt-3.5-turbo-instruct)
62
+ "prompt": string | string[], // 输入提示词,支持数组批量
63
+
64
+ // ===== 常用可选字段 =====
65
+ "max_tokens"?: number, // 最大输出长度,默认16
66
+ "temperature"?: number, // 0.0-2.0,采样温度
67
+ "top_p"?: number, // 0.0-1.0,核采样
68
+ "frequency_penalty"?: number, // -2.0-2.0,频率惩罚
69
+ "presence_penalty"?: number, // -2.0-2.0,出现惩罚
70
+ "best_of"?: number, // 生成n个补全,返回最佳的
71
+ "echo"?: boolean, // 回显输入提示词
72
+ "stop"?: string | string[], // 停止序列
73
+ "stream"?: boolean, // 是否流式响应
74
+ "suffix"?: string, // 补全文本的后缀
75
+ "logit_bias"?: object, // 调整特定token的概率
76
+ "n"?: number // 生成多少个补全,默认1
77
+ }
78
+ ```
79
+
80
+ ### 请求示例
81
+
82
+ ```bash
83
+ curl -X POST https://api.openai.com/v1/completions \
84
+ -H "Authorization: Bearer sk-xxx" \
85
+ -H "Content-Type: application/json" \
86
+ -d '{
87
+ "model": "gpt-3.5-turbo-instruct",
88
+ "prompt": "将这个句子翻译为英文:'你好世界'",
89
+ "max_tokens": 100,
90
+ "temperature": 0.7
91
+ }'
92
+ ```
93
+
94
+ ### 响应格式
95
+
96
+ ```json
97
+ {
98
+ "id": "cmpl-8R8e...",
99
+ "object": "text_completion",
100
+ "created": 1699000000,
101
+ "model": "gpt-3.5-turbo-instruct",
102
+ "choices": [
103
+ {
104
+ "text": "\nHello, world!",
105
+ "index": 0,
106
+ "logprobs": null,
107
+ "finish_reason": "length" | "stop" | "content_filter"
108
+ }
109
+ ],
110
+ "usage": {
111
+ "prompt_tokens": 15,
112
+ "completion_tokens": 10,
113
+ "total_tokens": 25
114
+ }
115
+ }
116
+ ```
117
+
118
+ ### 参数说明
119
+
120
+ - **max_tokens**: 补全的最大token数。设置越高,响应越长但成本更高
121
+ - **temperature**: 越低越稳定(0)、越高越随机(2)
122
+ - **best_of**: 生成多个补全并返回最佳的,n必须≤best_of
123
+ - **suffix**: 在补全后附加的文本
124
+
125
+ ---
126
+
127
+ ## Chat Completions API(对话模型)
128
+
129
+ ### 概述
130
+
131
+ **Chat Completions API** 是 OpenAI 推荐的通用 API,支持多轮对话、系统提示、函数调用等高级功能。
132
+
133
+ ### 请求格式
134
+
135
+ ```typescript
136
+ POST /v1/chat/completions
137
+ Host: api.openai.com
138
+ Authorization: Bearer sk-xxx
139
+ Content-Type: application/json
140
+
141
+ {
142
+ // ===== 必需字段 =====
143
+ "model": string, // 模型ID
144
+ "messages": Array<Message>, // 消息历史
145
+
146
+ // ===== 常用可选字段 =====
147
+ "temperature"?: number, // 0-2,采样温度
148
+ "top_p"?: number, // 0-1,核采样
149
+ "max_tokens"?: number, // 最大输出长度
150
+ "frequency_penalty"?: number, // -2-2,频率惩罚
151
+ "presence_penalty"?: number, // -2-2,出现惩罚
152
+ "stream"?: boolean, // 是否流式响应
153
+ "stop"?: string | string[], // 停止序列
154
+ "seed"?: number, // 随机种子,确保可复现
155
+ "top_logprobs"?: number, // 返回每个位置的概率分布
156
+ "logprobs"?: boolean, // 是否返回log概率
157
+ "user"?: string, // 终端用户标识,用于滥用检测
158
+ "tools"?: Tool[], // 函数定义
159
+ "tool_choice"?: "auto" | "required" | object, // 工具调用策略
160
+ "function_call"?: "auto" | { "name": string }, // 函数调用(已弃用)
161
+ "functions"?: Function[], // 函数定义(已弃用)
162
+ "response_format"?: object, // 响应格式(JSON模式)
163
+ "logit_bias"?: object // 调整特定token概率
164
+ }
165
+ ```
166
+
167
+ ### 消息格式
168
+
169
+ ```typescript
170
+ interface Message {
171
+ role: "system" | "user" | "assistant" | "tool";
172
+ content: string | ContentBlock[];
173
+ name?: string; // 消息发送者名字
174
+ tool_call_id?: string; // tool响应时必需
175
+ tool_calls?: ToolCall[]; // assistant消息中的工具调用
176
+ }
177
+
178
+ type ContentBlock =
179
+ | TextContent
180
+ | ImageContent
181
+ | ToolCallContent
182
+ | ToolResultContent;
183
+
184
+ // 文本内容
185
+ interface TextContent {
186
+ type: "text";
187
+ text: string;
188
+ }
189
+
190
+ // 图像内容(仅vision模型支持)
191
+ interface ImageContent {
192
+ type: "image_url";
193
+ image_url: {
194
+ url: string; // URL或data:URI
195
+ detail?: "low" | "high" | "auto"; // 图像详细程度
196
+ };
197
+ }
198
+
199
+ // 工具调用
200
+ interface ToolCallContent {
201
+ type: "tool_call";
202
+ id: string;
203
+ function: {
204
+ name: string;
205
+ arguments: string; // JSON字符串
206
+ };
207
+ }
208
+
209
+ // 工具结果
210
+ interface ToolResultContent {
211
+ type: "tool_result";
212
+ tool_use_id: string;
213
+ content: string;
214
+ is_error?: boolean;
215
+ }
216
+ ```
217
+
218
+ ### 工具定义格式
219
+
220
+ ```typescript
221
+ interface Tool {
222
+ type: "function";
223
+ function: {
224
+ name: string; // 函数名
225
+ description: string; // 功能描述
226
+ parameters: {
227
+ type: "object";
228
+ properties: {
229
+ [key: string]: {
230
+ type: string; // string, number, boolean, array等
231
+ description: string; // 参数说明
232
+ enum?: string[]; // 可选值列表
233
+ items?: object; // 数组项的类型
234
+ };
235
+ };
236
+ required: string[]; // 必需参数列表
237
+ };
238
+ strict?: boolean; // 严格模式(GPT-4 Turbo+)
239
+ };
240
+ }
241
+ ```
242
+
243
+ ### 请求示例
244
+
245
+ ```bash
246
+ curl -X POST https://api.openai.com/v1/chat/completions \
247
+ -H "Authorization: Bearer sk-xxx" \
248
+ -H "Content-Type: application/json" \
249
+ -d '{
250
+ "model": "gpt-4-turbo",
251
+ "messages": [
252
+ {
253
+ "role": "system",
254
+ "content": "You are a helpful assistant."
255
+ },
256
+ {
257
+ "role": "user",
258
+ "content": "What is 2+2?"
259
+ }
260
+ ],
261
+ "max_tokens": 100,
262
+ "temperature": 0.7
263
+ }'
264
+ ```
265
+
266
+ ### 多轮对话示例
267
+
268
+ ```bash
269
+ curl -X POST https://api.openai.com/v1/chat/completions \
270
+ -H "Authorization: Bearer sk-xxx" \
271
+ -d '{
272
+ "model": "gpt-4-turbo",
273
+ "messages": [
274
+ {
275
+ "role": "system",
276
+ "content": "You are a Python expert."
277
+ },
278
+ {
279
+ "role": "user",
280
+ "content": "如何读取文件?"
281
+ },
282
+ {
283
+ "role": "assistant",
284
+ "content": "在Python中,你可以使用 open() 函数..."
285
+ },
286
+ {
287
+ "role": "user",
288
+ "content": "写一个例子"
289
+ }
290
+ ]
291
+ }'
292
+ ```
293
+
294
+ ### 函数调用示例
295
+
296
+ ```bash
297
+ curl -X POST https://api.openai.com/v1/chat/completions \
298
+ -H "Authorization: Bearer sk-xxx" \
299
+ -d '{
300
+ "model": "gpt-4-turbo",
301
+ "messages": [
302
+ {
303
+ "role": "user",
304
+ "content": "旧金山现在的天气如何?"
305
+ }
306
+ ],
307
+ "tools": [
308
+ {
309
+ "type": "function",
310
+ "function": {
311
+ "name": "get_weather",
312
+ "description": "获取指定城市的天气信息",
313
+ "parameters": {
314
+ "type": "object",
315
+ "properties": {
316
+ "location": {
317
+ "type": "string",
318
+ "description": "城市名称"
319
+ }
320
+ },
321
+ "required": ["location"]
322
+ }
323
+ }
324
+ }
325
+ ],
326
+ "tool_choice": "auto"
327
+ }'
328
+ ```
329
+
330
+ ### 响应格式
331
+
332
+ ```json
333
+ {
334
+ "id": "chatcmpl-8R8e...",
335
+ "object": "chat.completion",
336
+ "created": 1699000000,
337
+ "model": "gpt-4-turbo",
338
+ "choices": [
339
+ {
340
+ "index": 0,
341
+ "message": {
342
+ "role": "assistant",
343
+ "content": "2 + 2 = 4"
344
+ },
345
+ "finish_reason": "stop" | "length" | "tool_calls" | "content_filter"
346
+ }
347
+ ],
348
+ "usage": {
349
+ "prompt_tokens": 20,
350
+ "completion_tokens": 10,
351
+ "total_tokens": 30
352
+ },
353
+ "system_fingerprint": "fp_a8d8f..."
354
+ }
355
+ ```
356
+
357
+ ### 工具调用响应示例
358
+
359
+ ```json
360
+ {
361
+ "choices": [
362
+ {
363
+ "message": {
364
+ "role": "assistant",
365
+ "content": null,
366
+ "tool_calls": [
367
+ {
368
+ "id": "call_abc123",
369
+ "type": "function",
370
+ "function": {
371
+ "name": "get_weather",
372
+ "arguments": "{\"location\": \"San Francisco\"}"
373
+ }
374
+ }
375
+ ]
376
+ },
377
+ "finish_reason": "tool_calls"
378
+ }
379
+ ]
380
+ }
381
+ ```
382
+
383
+ ### JSON 响应格式
384
+
385
+ ```bash
386
+ # 强制JSON输出
387
+ curl -X POST https://api.openai.com/v1/chat/completions \
388
+ -d '{
389
+ "model": "gpt-4-turbo",
390
+ "messages": [
391
+ {
392
+ "role": "user",
393
+ "content": "生成一个JSON格式的用户信息"
394
+ }
395
+ ],
396
+ "response_format": {
397
+ "type": "json_object"
398
+ }
399
+ }'
400
+ ```
401
+
402
+ ---
403
+
404
+ ## Responses API(新一代响应接口)
405
+
406
+ ### 概述
407
+
408
+ **Responses API** 是 OpenAI 最新的通用响应生成接口,是 Chat Completions API 的升级版本。提供更强大和灵活的能力:
409
+
410
+ **主要特性**:
411
+ - 文本和图像输入,文本输出
412
+ - 有状态的多轮对话(通过 conversation 或 previous_response_id)
413
+ - 内置工具:网络搜索、文件搜索、计算机使用等
414
+ - 函数调用(自定义工具)
415
+ - 后台处理和长期运行任务
416
+ - 响应存储和检索
417
+ - 流式和非流式响应
418
+ - 推理模型支持(gpt-5、o-series)
419
+ - 对话压缩(conversation compaction)
420
+
421
+ ### API 端点
422
+
423
+ ```bash
424
+ # 创建响应
425
+ POST /v1/responses
426
+
427
+ # 获取响应
428
+ GET /v1/responses/{response_id}
429
+
430
+ # 删除响应
431
+ DELETE /v1/responses/{response_id}
432
+
433
+ # 取消后台响应
434
+ POST /v1/responses/{response_id}/cancel
435
+
436
+ # 压缩对话
437
+ POST /v1/responses/compact
438
+
439
+ # 列出响应的输入项
440
+ GET /v1/responses/{response_id}/input_items
441
+
442
+ # 计算输入 token 数
443
+ POST /v1/responses/input_tokens
444
+ ```
445
+
446
+ ### 请求格式 - 创建响应
447
+
448
+ ```typescript
449
+ POST /v1/responses
450
+ Host: api.openai.com
451
+ Authorization: Bearer sk-xxx
452
+ Content-Type: application/json
453
+
454
+ {
455
+ // ===== 必需字段 =====
456
+ "model": string, // 模型ID (gpt-4o, gpt-5, o3等)
457
+
458
+ // ===== 输入和对话 =====
459
+ "input"?: string | object, // 文本、图像或文件输入
460
+ "instructions"?: string, // 系统提示词
461
+ "conversation"?: string | object, // 对话ID或对象
462
+ "previous_response_id"?: string, // 上一个响应ID,用于多轮对话
463
+
464
+ // ===== 响应配置 =====
465
+ "max_output_tokens"?: number, // 最大输出 token 数
466
+ "temperature"?: number, // 0-2,采样温度
467
+ "top_p"?: number, // 0-1,核采样
468
+ "text"?: object, // 文本响应格式配置
469
+ "stream"?: boolean, // 是否流式响应
470
+ "stream_options"?: object, // 流式选项
471
+
472
+ // ===== 工具和函数调用 =====
473
+ "tools"?: Tool[], // 内置工具和函数定义
474
+ "tool_choice"?: string | object, // 工具选择策略
475
+ "max_tool_calls"?: number, // 最大工具调用次数
476
+ "parallel_tool_calls"?: boolean, // 是否并行调用工具
477
+
478
+ // ===== 后台处理 =====
479
+ "background"?: boolean, // 后台运行响应
480
+ "store"?: boolean, // 是否存储响应
481
+
482
+ // ===== 高级特性 =====
483
+ "reasoning"?: object, // 推理模型配置
484
+ "prompt_cache_key"?: string, // 提示词缓存键
485
+ "prompt_cache_retention"?: string, // 缓存保留策略(24h)
486
+ "truncation"?: string, // 截断策略(auto/disabled)
487
+ "include"?: string[], // 包含的额外输出字段
488
+ "metadata"?: object, // 自定义元数据
489
+ "safety_identifier"?: string, // 用户安全标识
490
+ "service_tier"?: string // 服务等级(auto/default/flex/priority)
491
+ }
492
+ ```
493
+
494
+ ### 输入格式
495
+
496
+ ```typescript
497
+ // 文本输入
498
+ {
499
+ "input": "Tell me a joke about programming"
500
+ }
501
+
502
+ // 图像输入
503
+ {
504
+ "input": [
505
+ {
506
+ "type": "image",
507
+ "source": {
508
+ "type": "url",
509
+ "url": "https://example.com/image.jpg"
510
+ }
511
+ },
512
+ {
513
+ "type": "text",
514
+ "text": "What's in this image?"
515
+ }
516
+ ]
517
+ }
518
+
519
+ // 文件输入
520
+ {
521
+ "input": [
522
+ {
523
+ "type": "document",
524
+ "source": {
525
+ "type": "file",
526
+ "file_id": "file-abc123"
527
+ }
528
+ },
529
+ {
530
+ "type": "text",
531
+ "text": "Summarize this document"
532
+ }
533
+ ]
534
+ }
535
+ ```
536
+
537
+ ### 工具定义
538
+
539
+ ```typescript
540
+ // 函数调用工具
541
+ {
542
+ "tools": [
543
+ {
544
+ "type": "function",
545
+ "function": {
546
+ "name": "get_weather",
547
+ "description": "Get weather for a location",
548
+ "parameters": {
549
+ "type": "object",
550
+ "properties": {
551
+ "location": {
552
+ "type": "string",
553
+ "description": "City name"
554
+ }
555
+ },
556
+ "required": ["location"]
557
+ }
558
+ }
559
+ }
560
+ ]
561
+ }
562
+
563
+ // 内置工具示例
564
+ {
565
+ "tools": [
566
+ {
567
+ "type": "web_search" // 网络搜索
568
+ },
569
+ {
570
+ "type": "file_search" // 文件搜索
571
+ },
572
+ {
573
+ "type": "code_interpreter" // 代码执行
574
+ },
575
+ {
576
+ "type": "computer" // 计算机使用
577
+ }
578
+ ]
579
+ }
580
+ ```
581
+
582
+ ### 请求示例
583
+
584
+ ```bash
585
+ # 基础文本请求
586
+ curl https://api.openai.com/v1/responses \
587
+ -H "Content-Type: application/json" \
588
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
589
+ -d '{
590
+ "model": "gpt-4o",
591
+ "input": "Tell me a three sentence bedtime story about a unicorn."
592
+ }'
593
+
594
+ # 多轮对话
595
+ curl https://api.openai.com/v1/responses \
596
+ -H "Content-Type: application/json" \
597
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
598
+ -d '{
599
+ "model": "gpt-4o",
600
+ "input": "What is your favorite color from the story?",
601
+ "previous_response_id": "resp_67ccd2bed1ec8190b14f964abc054267..."
602
+ }'
603
+
604
+ # 使用网络搜索
605
+ curl https://api.openai.com/v1/responses \
606
+ -H "Content-Type: application/json" \
607
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
608
+ -d '{
609
+ "model": "gpt-4o",
610
+ "input": "What is the latest news about AI?",
611
+ "tools": [
612
+ {
613
+ "type": "web_search"
614
+ }
615
+ ]
616
+ }'
617
+
618
+ # 后台处理
619
+ curl https://api.openai.com/v1/responses \
620
+ -H "Content-Type: application/json" \
621
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
622
+ -d '{
623
+ "model": "gpt-4o",
624
+ "input": "Process a large file and generate a report",
625
+ "background": true,
626
+ "store": true
627
+ }'
628
+ ```
629
+
630
+ ### 响应格式
631
+
632
+ ```json
633
+ {
634
+ "id": "resp_67ccd2bed1ec8190b14f964abc0542670bb6a6b452d3795b",
635
+ "object": "response",
636
+ "created_at": 1741476542,
637
+ "status": "completed",
638
+ "completed_at": 1741476543,
639
+ "error": null,
640
+ "incomplete_details": null,
641
+ "instructions": null,
642
+ "max_output_tokens": null,
643
+ "model": "gpt-4o-2024-08-06",
644
+ "output": [
645
+ {
646
+ "type": "message",
647
+ "id": "msg_67ccd2bf17f0819081ff3bb2cf6508e60bb6a6b452d3795b",
648
+ "status": "completed",
649
+ "role": "assistant",
650
+ "content": [
651
+ {
652
+ "type": "output_text",
653
+ "text": "In a peaceful grove beneath a silver moon, a unicorn named Lumina discovered a hidden pool that reflected the stars...",
654
+ "annotations": []
655
+ }
656
+ ]
657
+ }
658
+ ],
659
+ "parallel_tool_calls": true,
660
+ "previous_response_id": null,
661
+ "reasoning": {
662
+ "effort": null,
663
+ "summary": null
664
+ },
665
+ "store": true,
666
+ "temperature": 1.0,
667
+ "text": {
668
+ "format": {
669
+ "type": "text"
670
+ }
671
+ },
672
+ "tool_choice": "auto",
673
+ "tools": [],
674
+ "top_p": 1.0,
675
+ "truncation": "disabled",
676
+ "usage": {
677
+ "input_tokens": 36,
678
+ "input_tokens_details": {
679
+ "cached_tokens": 0
680
+ },
681
+ "output_tokens": 87,
682
+ "output_tokens_details": {
683
+ "reasoning_tokens": 0
684
+ },
685
+ "total_tokens": 123
686
+ },
687
+ "metadata": {}
688
+ }
689
+ ```
690
+
691
+ ### 获取响应
692
+
693
+ ```bash
694
+ # 获取已完成的响应
695
+ curl https://api.openai.com/v1/responses/resp_abc123 \
696
+ -H "Authorization: Bearer $OPENAI_API_KEY"
697
+
698
+ # 流式获取背景响应
699
+ curl https://api.openai.com/v1/responses/resp_abc123?stream=true \
700
+ -H "Authorization: Bearer $OPENAI_API_KEY"
701
+ ```
702
+
703
+ ### 删除响应
704
+
705
+ ```bash
706
+ curl -X DELETE https://api.openai.com/v1/responses/resp_abc123 \
707
+ -H "Authorization: Bearer $OPENAI_API_KEY"
708
+ ```
709
+
710
+ ### 取消后台响应
711
+
712
+ ```bash
713
+ curl -X POST https://api.openai.com/v1/responses/resp_abc123/cancel \
714
+ -H "Content-Type: application/json" \
715
+ -H "Authorization: Bearer $OPENAI_API_KEY"
716
+ ```
717
+
718
+ ### 压缩长对话
719
+
720
+ ```bash
721
+ curl -X POST https://api.openai.com/v1/responses/compact \
722
+ -H "Content-Type: application/json" \
723
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
724
+ -d '{
725
+ "model": "gpt-5",
726
+ "input": [
727
+ {
728
+ "role": "user",
729
+ "content": "Create a landing page"
730
+ },
731
+ {
732
+ "type": "message",
733
+ "role": "assistant",
734
+ "content": [
735
+ {
736
+ "type": "output_text",
737
+ "text": "Here is a landing page..."
738
+ }
739
+ ]
740
+ }
741
+ ]
742
+ }'
743
+ ```
744
+
745
+ ### 计算输入 Token 数
746
+
747
+ ```bash
748
+ curl -X POST https://api.openai.com/v1/responses/input_tokens \
749
+ -H "Content-Type: application/json" \
750
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
751
+ -d '{
752
+ "model": "gpt-4o",
753
+ "input": "Tell me a joke."
754
+ }'
755
+
756
+ # 响应
757
+ {
758
+ "object": "response.input_tokens",
759
+ "input_tokens": 11
760
+ }
761
+ ```
762
+
763
+ ### 响应状态
764
+
765
+ ```
766
+ completed - 已完成
767
+ failed - 失败
768
+ in_progress - 进行中
769
+ cancelled - 已取消(仅后台任务)
770
+ queued - 队列中(仅后台任务)
771
+ incomplete - 不完整
772
+ ```
773
+
774
+ ### 流式响应
775
+
776
+ ```bash
777
+ curl https://api.openai.com/v1/responses \
778
+ -H "Content-Type: application/json" \
779
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
780
+ -d '{
781
+ "model": "gpt-4o",
782
+ "input": "Write a poem",
783
+ "stream": true
784
+ }'
785
+ ```
786
+
787
+ ### 流式事件类型
788
+
789
+ #### **生命周期事件**
790
+
791
+ ```typescript
792
+ // 1. response.created - 响应创建
793
+ {
794
+ "type": "response.created",
795
+ "sequence_number": 1,
796
+ "response": {
797
+ "id": "resp_67ccfcdd16748190a91872c75d38539e...",
798
+ "status": "in_progress",
799
+ ...
800
+ }
801
+ }
802
+
803
+ // 2. response.in_progress - 响应进行中
804
+ {
805
+ "type": "response.in_progress",
806
+ "sequence_number": 1,
807
+ "response": {...}
808
+ }
809
+
810
+ // 3. response.completed - 响应完成
811
+ {
812
+ "type": "response.completed",
813
+ "sequence_number": 1,
814
+ "response": {
815
+ "id": "resp_123",
816
+ "status": "completed",
817
+ "completed_at": 1740855870,
818
+ "output": [...]
819
+ }
820
+ }
821
+
822
+ // 4. response.failed - 响应失败
823
+ {
824
+ "type": "response.failed",
825
+ "sequence_number": 1,
826
+ "response": {
827
+ "id": "resp_123",
828
+ "status": "failed",
829
+ "error": {
830
+ "code": "server_error",
831
+ "message": "The model failed to generate a response."
832
+ }
833
+ }
834
+ }
835
+
836
+ // 5. response.incomplete - 响应不完整
837
+ {
838
+ "type": "response.incomplete",
839
+ "sequence_number": 1,
840
+ "response": {
841
+ "id": "resp_123",
842
+ "status": "incomplete",
843
+ "incomplete_details": {
844
+ "reason": "max_tokens"
845
+ }
846
+ }
847
+ }
848
+
849
+ // 6. response.queued - 响应队列中
850
+ {
851
+ "type": "response.queued",
852
+ "sequence_number": 1,
853
+ "response": {
854
+ "id": "res_123",
855
+ "status": "queued"
856
+ }
857
+ }
858
+ ```
859
+
860
+ #### **输出项事件**
861
+
862
+ ```typescript
863
+ // 1. response.output_item.added - 输出项添加
864
+ {
865
+ "type": "response.output_item.added",
866
+ "output_index": 0,
867
+ "sequence_number": 1,
868
+ "item": {
869
+ "id": "msg_123",
870
+ "status": "in_progress",
871
+ "type": "message",
872
+ "role": "assistant",
873
+ "content": []
874
+ }
875
+ }
876
+
877
+ // 2. response.output_item.done - 输出项完成
878
+ {
879
+ "type": "response.output_item.done",
880
+ "output_index": 0,
881
+ "sequence_number": 1,
882
+ "item": {
883
+ "id": "msg_123",
884
+ "status": "completed",
885
+ "type": "message",
886
+ "role": "assistant",
887
+ "content": [...]
888
+ }
889
+ }
890
+ ```
891
+
892
+ #### **内容部分事件**
893
+
894
+ ```typescript
895
+ // 1. response.content_part.added - 内容部分添加
896
+ {
897
+ "type": "response.content_part.added",
898
+ "item_id": "msg_123",
899
+ "output_index": 0,
900
+ "content_index": 0,
901
+ "sequence_number": 1,
902
+ "part": {
903
+ "type": "output_text",
904
+ "text": "",
905
+ "annotations": []
906
+ }
907
+ }
908
+
909
+ // 2. response.content_part.done - 内容部分完成
910
+ {
911
+ "type": "response.content_part.done",
912
+ "item_id": "msg_123",
913
+ "output_index": 0,
914
+ "content_index": 0,
915
+ "sequence_number": 1,
916
+ "part": {
917
+ "type": "output_text",
918
+ "text": "Complete text here...",
919
+ "annotations": []
920
+ }
921
+ }
922
+ ```
923
+
924
+ #### **文本输出事件**
925
+
926
+ ```typescript
927
+ // 1. response.output_text.delta - 文本增量
928
+ {
929
+ "type": "response.output_text.delta",
930
+ "item_id": "msg_123",
931
+ "output_index": 0,
932
+ "content_index": 0,
933
+ "delta": "Hello",
934
+ "sequence_number": 1,
935
+ "logprobs": []
936
+ }
937
+
938
+ // 2. response.output_text.done - 文本完成
939
+ {
940
+ "type": "response.output_text.done",
941
+ "item_id": "msg_123",
942
+ "output_index": 0,
943
+ "content_index": 0,
944
+ "text": "Complete response text...",
945
+ "sequence_number": 1,
946
+ "logprobs": []
947
+ }
948
+
949
+ // 3. response.output_text.annotation.added - 文本注释添加
950
+ {
951
+ "type": "response.output_text.annotation.added",
952
+ "item_id": "item-abc",
953
+ "output_index": 0,
954
+ "content_index": 0,
955
+ "annotation_index": 0,
956
+ "annotation": {
957
+ "type": "text_annotation",
958
+ "text": "Citation text",
959
+ "start": 0,
960
+ "end": 10
961
+ },
962
+ "sequence_number": 1
963
+ }
964
+ ```
965
+
966
+ #### **拒绝事件**
967
+
968
+ ```typescript
969
+ // 1. response.refusal.delta - 拒绝文本增量
970
+ {
971
+ "type": "response.refusal.delta",
972
+ "item_id": "msg_123",
973
+ "output_index": 0,
974
+ "content_index": 0,
975
+ "delta": "I cannot",
976
+ "sequence_number": 1
977
+ }
978
+
979
+ // 2. response.refusal.done - 拒绝文本完成
980
+ {
981
+ "type": "response.refusal.done",
982
+ "item_id": "item-abc",
983
+ "output_index": 1,
984
+ "content_index": 2,
985
+ "refusal": "I cannot help with this request.",
986
+ "sequence_number": 1
987
+ }
988
+ ```
989
+
990
+ #### **函数调用事件**
991
+
992
+ ```typescript
993
+ // 1. response.function_call_arguments.delta - 函数参数增量
994
+ {
995
+ "type": "response.function_call_arguments.delta",
996
+ "item_id": "item-abc",
997
+ "output_index": 0,
998
+ "delta": "{\"arg\":",
999
+ "sequence_number": 1
1000
+ }
1001
+
1002
+ // 2. response.function_call_arguments.done - 函数参数完成
1003
+ {
1004
+ "type": "response.function_call_arguments.done",
1005
+ "item_id": "item-abc",
1006
+ "name": "get_weather",
1007
+ "output_index": 1,
1008
+ "arguments": "{\"arg\": 123}",
1009
+ "sequence_number": 1
1010
+ }
1011
+ ```
1012
+
1013
+ #### **网络搜索事件**
1014
+
1015
+ ```typescript
1016
+ // 1. response.web_search_call.in_progress
1017
+ {
1018
+ "type": "response.web_search_call.in_progress",
1019
+ "output_index": 0,
1020
+ "item_id": "ws_123",
1021
+ "sequence_number": 0
1022
+ }
1023
+
1024
+ // 2. response.web_search_call.searching
1025
+ {
1026
+ "type": "response.web_search_call.searching",
1027
+ "output_index": 0,
1028
+ "item_id": "ws_123",
1029
+ "sequence_number": 0
1030
+ }
1031
+
1032
+ // 3. response.web_search_call.completed
1033
+ {
1034
+ "type": "response.web_search_call.completed",
1035
+ "output_index": 0,
1036
+ "item_id": "ws_123",
1037
+ "sequence_number": 0
1038
+ }
1039
+ ```
1040
+
1041
+ #### **文件搜索事件**
1042
+
1043
+ ```typescript
1044
+ // 1. response.file_search_call.in_progress
1045
+ {
1046
+ "type": "response.file_search_call.in_progress",
1047
+ "output_index": 0,
1048
+ "item_id": "fs_123",
1049
+ "sequence_number": 1
1050
+ }
1051
+
1052
+ // 2. response.file_search_call.searching
1053
+ {
1054
+ "type": "response.file_search_call.searching",
1055
+ "output_index": 0,
1056
+ "item_id": "fs_123",
1057
+ "sequence_number": 1
1058
+ }
1059
+
1060
+ // 3. response.file_search_call.completed
1061
+ {
1062
+ "type": "response.file_search_call.completed",
1063
+ "output_index": 0,
1064
+ "item_id": "fs_123",
1065
+ "sequence_number": 1
1066
+ }
1067
+ ```
1068
+
1069
+ #### **推理事件**(仅 gpt-5、o-series)
1070
+
1071
+ ```typescript
1072
+ // 1. response.reasoning_summary_part.added
1073
+ {
1074
+ "type": "response.reasoning_summary_part.added",
1075
+ "item_id": "rs_6806bfca0b2481918a5748308061a26...",
1076
+ "output_index": 0,
1077
+ "summary_index": 0,
1078
+ "sequence_number": 1,
1079
+ "part": {
1080
+ "type": "summary_text",
1081
+ "text": ""
1082
+ }
1083
+ }
1084
+
1085
+ // 2. response.reasoning_summary_text.delta
1086
+ {
1087
+ "type": "response.reasoning_summary_text.delta",
1088
+ "item_id": "rs_6806bfca0b2481918a5748308061a26...",
1089
+ "output_index": 0,
1090
+ "summary_index": 0,
1091
+ "delta": "**Analyzing the problem**\n\nThe user asked...",
1092
+ "sequence_number": 1
1093
+ }
1094
+
1095
+ // 3. response.reasoning_summary_text.done
1096
+ {
1097
+ "type": "response.reasoning_summary_text.done",
1098
+ "item_id": "rs_6806bfca0b2481918a5748308061a26...",
1099
+ "output_index": 0,
1100
+ "summary_index": 0,
1101
+ "text": "Full reasoning summary...",
1102
+ "sequence_number": 1
1103
+ }
1104
+
1105
+ // 4. response.reasoning_text.delta - 完整推理过程增量
1106
+ {
1107
+ "type": "response.reasoning_text.delta",
1108
+ "item_id": "rs_123",
1109
+ "output_index": 0,
1110
+ "content_index": 0,
1111
+ "delta": "The",
1112
+ "sequence_number": 1
1113
+ }
1114
+
1115
+ // 5. response.reasoning_text.done - 完整推理过程完成
1116
+ {
1117
+ "type": "response.reasoning_text.done",
1118
+ "item_id": "rs_123",
1119
+ "output_index": 0,
1120
+ "content_index": 0,
1121
+ "text": "The user is asking...",
1122
+ "sequence_number": 4
1123
+ }
1124
+ ```
1125
+
1126
+ #### **代码解释器事件**
1127
+
1128
+ ```typescript
1129
+ // 1. response.code_interpreter_call.in_progress
1130
+ {
1131
+ "type": "response.code_interpreter_call.in_progress",
1132
+ "output_index": 0,
1133
+ "item_id": "ci_12345",
1134
+ "sequence_number": 1
1135
+ }
1136
+
1137
+ // 2. response.code_interpreter_call.interpreting
1138
+ {
1139
+ "type": "response.code_interpreter_call.interpreting",
1140
+ "output_index": 4,
1141
+ "item_id": "ci_12345",
1142
+ "sequence_number": 1
1143
+ }
1144
+
1145
+ // 3. response.code_interpreter_call.completed
1146
+ {
1147
+ "type": "response.code_interpreter_call.completed",
1148
+ "output_index": 5,
1149
+ "item_id": "ci_12345",
1150
+ "sequence_number": 1
1151
+ }
1152
+
1153
+ // 4. response.code_interpreter_call_code.delta
1154
+ {
1155
+ "type": "response.code_interpreter_call_code.delta",
1156
+ "output_index": 0,
1157
+ "item_id": "ci_12345",
1158
+ "delta": "print('Hello, world')",
1159
+ "sequence_number": 1
1160
+ }
1161
+
1162
+ // 5. response.code_interpreter_call_code.done
1163
+ {
1164
+ "type": "response.code_interpreter_call_code.done",
1165
+ "output_index": 3,
1166
+ "item_id": "ci_12345",
1167
+ "code": "print('done')",
1168
+ "sequence_number": 1
1169
+ }
1170
+ ```
1171
+
1172
+ #### **图像生成事件**
1173
+
1174
+ ```typescript
1175
+ // 1. response.image_generation_call.in_progress
1176
+ {
1177
+ "type": "response.image_generation_call.in_progress",
1178
+ "output_index": 0,
1179
+ "item_id": "item-123",
1180
+ "sequence_number": 0
1181
+ }
1182
+
1183
+ // 2. response.image_generation_call.generating
1184
+ {
1185
+ "type": "response.image_generation_call.generating",
1186
+ "output_index": 0,
1187
+ "item_id": "item-123",
1188
+ "sequence_number": 0
1189
+ }
1190
+
1191
+ // 3. response.image_generation_call.partial_image
1192
+ {
1193
+ "type": "response.image_generation_call.partial_image",
1194
+ "output_index": 0,
1195
+ "item_id": "item-123",
1196
+ "partial_image_index": 0,
1197
+ "partial_image_b64": "...",
1198
+ "sequence_number": 0
1199
+ }
1200
+
1201
+ // 4. response.image_generation_call.completed
1202
+ {
1203
+ "type": "response.image_generation_call.completed",
1204
+ "output_index": 0,
1205
+ "item_id": "item-123",
1206
+ "sequence_number": 1
1207
+ }
1208
+ ```
1209
+
1210
+ #### **MCP 工具事件**
1211
+
1212
+ ```typescript
1213
+ // 1. response.mcp_call.in_progress
1214
+ {
1215
+ "type": "response.mcp_call.in_progress",
1216
+ "sequence_number": 1,
1217
+ "output_index": 0,
1218
+ "item_id": "mcp_682d437d90a88191bf88cd03aae0c3e5..."
1219
+ }
1220
+
1221
+ // 2. response.mcp_call_arguments.delta
1222
+ {
1223
+ "type": "response.mcp_call_arguments.delta",
1224
+ "output_index": 0,
1225
+ "item_id": "item-abc",
1226
+ "delta": "{",
1227
+ "sequence_number": 1
1228
+ }
1229
+
1230
+ // 3. response.mcp_call_arguments.done
1231
+ {
1232
+ "type": "response.mcp_call_arguments.done",
1233
+ "output_index": 0,
1234
+ "item_id": "item-abc",
1235
+ "arguments": "{\"arg1\": \"value1\", \"arg2\": \"value2\"}",
1236
+ "sequence_number": 1
1237
+ }
1238
+
1239
+ // 4. response.mcp_call.completed
1240
+ {
1241
+ "type": "response.mcp_call.completed",
1242
+ "sequence_number": 1,
1243
+ "item_id": "mcp_682d437d90a88191bf88cd03aae0c3e5...",
1244
+ "output_index": 0
1245
+ }
1246
+
1247
+ // 5. response.mcp_call.failed
1248
+ {
1249
+ "type": "response.mcp_call.failed",
1250
+ "sequence_number": 1,
1251
+ "item_id": "mcp_682d437d90a88191bf88cd03aae0c3e5...",
1252
+ "output_index": 0
1253
+ }
1254
+
1255
+ // 6. response.mcp_list_tools.in_progress
1256
+ {
1257
+ "type": "response.mcp_list_tools.in_progress",
1258
+ "sequence_number": 1,
1259
+ "output_index": 0,
1260
+ "item_id": "mcpl_682d4379df088191886b70f4ec39f904..."
1261
+ }
1262
+
1263
+ // 7. response.mcp_list_tools.completed
1264
+ {
1265
+ "type": "response.mcp_list_tools.completed",
1266
+ "sequence_number": 1,
1267
+ "output_index": 0,
1268
+ "item_id": "mcpl_682d4379df088191886b70f4ec39f904..."
1269
+ }
1270
+
1271
+ // 8. response.mcp_list_tools.failed
1272
+ {
1273
+ "type": "response.mcp_list_tools.failed",
1274
+ "sequence_number": 1,
1275
+ "output_index": 0,
1276
+ "item_id": "mcpl_682d4379df088191886b70f4ec39f904..."
1277
+ }
1278
+ ```
1279
+
1280
+ #### **自定义工具事件**
1281
+
1282
+ ```typescript
1283
+ // 1. response.custom_tool_call_input.delta
1284
+ {
1285
+ "type": "response.custom_tool_call_input.delta",
1286
+ "output_index": 0,
1287
+ "item_id": "ctc_1234567890abcdef",
1288
+ "delta": "partial input text",
1289
+ "sequence_number": 1
1290
+ }
1291
+
1292
+ // 2. response.custom_tool_call_input.done
1293
+ {
1294
+ "type": "response.custom_tool_call_input.done",
1295
+ "output_index": 0,
1296
+ "item_id": "ctc_1234567890abcdef",
1297
+ "input": "final complete input text",
1298
+ "sequence_number": 1
1299
+ }
1300
+ ```
1301
+
1302
+ #### **错误事件**
1303
+
1304
+ ```typescript
1305
+ {
1306
+ "type": "error",
1307
+ "code": "ERR_SOMETHING",
1308
+ "message": "Something went wrong",
1309
+ "param": null,
1310
+ "sequence_number": 1
1311
+ }
1312
+ ```
1313
+
1314
+ ### 流式处理的事件顺序
1315
+
1316
+ 一个典型的流式响应事件序列:
1317
+
1318
+ 1. `response.created` - 响应开始创建
1319
+ 2. `response.in_progress` - 响应开始处理
1320
+ 3. `response.output_item.added` - 添加输出项(消息)
1321
+ 4. `response.content_part.added` - 添加内容部分
1322
+ 5. `response.output_text.delta`(多次) - 文本流式输出
1323
+ 6. `response.output_text.done` - 文本输出完成
1324
+ 7. `response.output_item.done` - 输出项完成
1325
+ 8. `response.completed` - 响应完成
1326
+
1327
+ 对于带有工具调用的流式响应:
1328
+
1329
+ 1. `response.created`
1330
+ 2. `response.output_item.added`
1331
+ 3. `response.function_call_arguments.delta`(多次)
1332
+ 4. `response.function_call_arguments.done`
1333
+ 5. `response.completed`
1334
+
1335
+ ---
1336
+
1337
+ ## 模型选择
1338
+
1339
+ ### 当前可用模型
1340
+
1341
+ ```typescript
1342
+ const models = {
1343
+ // Chat Completions API 推荐
1344
+ "gpt-4-turbo": {
1345
+ context: 128000,
1346
+ description: "最强能力,支持Vision",
1347
+ cost: "$10/1M输入,$30/1M输出",
1348
+ best_for: "复杂推理、代码分析"
1349
+ },
1350
+ "gpt-4o": {
1351
+ context: 128000,
1352
+ description: "新一代旗舰模型,更快更便宜",
1353
+ cost: "$5/1M输入,$15/1M输出",
1354
+ best_for: "大多数应用(推荐首选)"
1355
+ },
1356
+ "gpt-4o-mini": {
1357
+ context: 128000,
1358
+ description: "轻量级,成本最低",
1359
+ cost: "$0.15/1M输入,$0.6/1M输出",
1360
+ best_for: "简单任务、高吞吐"
1361
+ },
1362
+ "gpt-3.5-turbo": {
1363
+ context: 4096,
1364
+ description: "经典快速模型",
1365
+ cost: "$0.5/1M输入,$1.5/1M输出",
1366
+ best_for: "对话、翻译"
1367
+ },
1368
+
1369
+ // Realtime API
1370
+ "gpt-4o-realtime-preview": {
1371
+ context: 128000,
1372
+ description: "实时语音对话(预览版)",
1373
+ cost: "$5/1M输入,$20/1M输出",
1374
+ best_for: "实时语音交互"
1375
+ },
1376
+
1377
+ // Completions API(已弃用)
1378
+ "gpt-3.5-turbo-instruct": {
1379
+ context: 4096,
1380
+ description: "文本补全(维护模式)",
1381
+ best_for: "仅用于兼容性"
1382
+ }
1383
+ };
1384
+ ```
1385
+
1386
+ ### 模型对比表
1387
+
1388
+ | 特性 | GPT-4 Turbo | GPT-4o | GPT-4o-mini | 3.5-Turbo |
1389
+ |-----|-----------|--------|-----------|----------|
1390
+ | 上下文 | 128k | 128k | 128k | 4k |
1391
+ | Vision | ✅ | ✅ | ✅ | ❌ |
1392
+ | 函数调用 | ✅ | ✅ | ✅ | ✅ |
1393
+ | JSON 模式 | ✅ | ✅ | ✅ | ✅ |
1394
+ | 推理能力 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ |
1395
+ | 速度 | 中等 | 快 | 最快 | 快 |
1396
+ | 成本 | 高 | 中 | 低 | 最低 |
1397
+
1398
+ ### 选择决策树
1399
+
1400
+ ```
1401
+ 你的任务是什么?
1402
+ ├─ 实时语音交互 ──> gpt-4o-realtime-preview
1403
+ ├─ 图像识别和分析 ──> gpt-4-turbo 或 gpt-4o
1404
+ ├─ 代码生成/分析 ──> gpt-4o(推荐)或 gpt-4-turbo
1405
+ ├─ 简单对话/翻译 ──> gpt-3.5-turbo 或 gpt-4o-mini
1406
+ └─ 高吞吐量应用 ──> gpt-4o-mini
1407
+
1408
+ 预算和延迟考虑?
1409
+ ├─ 预算充足,性能优先 ──> gpt-4-turbo
1410
+ ├─ 平衡成本和性能 ──> gpt-4o(推荐首选)
1411
+ ├─ 成本敏感 ──> gpt-4o-mini
1412
+ └─ 低成本、快速 ──> gpt-3.5-turbo
1413
+ ```
1414
+
1415
+ ---
1416
+
1417
+ ## 流式事件
1418
+
1419
+ ### 流式响应启用
1420
+
1421
+ 在请求中添加:
1422
+ ```json
1423
+ {"stream": true}
1424
+ ```
1425
+
1426
+ ### Chat Completions 流式事件
1427
+
1428
+ #### 响应开始
1429
+
1430
+ ```
1431
+ data: {"id":"chatcmpl-8R8e...","object":"chat.completion.chunk","created":1699000000,"model":"gpt-4-turbo","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
1432
+ ```
1433
+
1434
+ #### 内容增量
1435
+
1436
+ ```
1437
+ data: {"choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
1438
+ data: {"choices":[{"index":0,"delta":{"content":" world"},"finish_reason":null}]}
1439
+ ```
1440
+
1441
+ #### 工具调用开始
1442
+
1443
+ ```
1444
+ data: {"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_abc","type":"function","function":{"name":"get_weather","arguments":""}}]},"finish_reason":null}]}
1445
+ ```
1446
+
1447
+ #### 工具调用参数增量
1448
+
1449
+ ```
1450
+ data: {"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"loca"}}]},"finish_reason":null}]}
1451
+ data: {"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"tion\":\"SF"}}]},"finish_reason":null}]}
1452
+ ```
1453
+
1454
+ #### 响应结束
1455
+
1456
+ ```
1457
+ data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
1458
+
1459
+ data: [DONE]
1460
+ ```
1461
+
1462
+ ### 流式请求示例
1463
+
1464
+ ```bash
1465
+ curl -X POST https://api.openai.com/v1/chat/completions \
1466
+ -H "Authorization: Bearer sk-xxx" \
1467
+ -H "Content-Type: application/json" \
1468
+ -d '{
1469
+ "model": "gpt-4-turbo",
1470
+ "messages": [
1471
+ {"role": "user", "content": "请写一首春天的诗"}
1472
+ ],
1473
+ "stream": true
1474
+ }'
1475
+ ```
1476
+
1477
+ ### Python 流式处理示例
1478
+
1479
+ ```python
1480
+ import openai
1481
+
1482
+ with openai.OpenAI(api_key="sk-xxx") as client:
1483
+ with client.chat.completions.stream(
1484
+ model="gpt-4-turbo",
1485
+ messages=[
1486
+ {"role": "user", "content": "写一首诗"}
1487
+ ]
1488
+ ) as stream:
1489
+ for text in stream.text_stream:
1490
+ print(text, end="", flush=True)
1491
+ ```
1492
+
1493
+ ---
1494
+
1495
+ ## 错误处理
1496
+
1497
+ ### 错误响应格式
1498
+
1499
+ ```json
1500
+ {
1501
+ "error": {
1502
+ "message": "错误描述信息",
1503
+ "type": "error_type",
1504
+ "param": "参数名称",
1505
+ "code": "invalid_api_key"
1506
+ }
1507
+ }
1508
+ ```
1509
+
1510
+ ### 常见错误
1511
+
1512
+ | HTTP 状态 | 错误类型 | 原因 | 处理方案 |
1513
+ |---------|--------|------|--------|
1514
+ | 400 | invalid_request_error | 参数错误或格式不合法 | 检查请求格式、参数值范围 |
1515
+ | 401 | authentication_error | API Key 无效或过期 | 更新有效的 API Key |
1516
+ | 403 | permission_error | 账户没有权限访问该模型 | 检查账户权限和模型可用性 |
1517
+ | 429 | rate_limit_error | 超过速率限制 | 实施指数退避重试 |
1518
+ | 500 | server_error | OpenAI 服务器错误 | 重试请求 |
1519
+ | 503 | service_unavailable_error | 服务暂时不可用 | 稍后重试 |
1520
+
1521
+ ### 错误响应示例
1522
+
1523
+ ```json
1524
+ {
1525
+ "error": {
1526
+ "message": "The model `gpt-4` does not exist",
1527
+ "type": "invalid_request_error",
1528
+ "param": "model",
1529
+ "code": "model_not_found"
1530
+ }
1531
+ }
1532
+ ```
1533
+
1534
+ ### 重试策略
1535
+
1536
+ ```typescript
1537
+ async function requestWithRetry(
1538
+ requestFn: () => Promise<any>,
1539
+ maxRetries: number = 3
1540
+ ): Promise<any> {
1541
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
1542
+ try {
1543
+ return await requestFn();
1544
+ } catch (error: any) {
1545
+ const status = error.status;
1546
+ const isRetryable = status === 429 || status >= 500;
1547
+
1548
+ if (!isRetryable || attempt === maxRetries) {
1549
+ throw error;
1550
+ }
1551
+
1552
+ // 指数退避:第一次等待1秒,第二次2秒,第三次4秒
1553
+ const delay = Math.pow(2, attempt - 1) * 1000;
1554
+ await new Promise(resolve => setTimeout(resolve, delay));
1555
+ }
1556
+ }
1557
+ }
1558
+ ```
1559
+
1560
+ ---
1561
+
1562
+ ## 快速参考
1563
+
1564
+ ### 常用参数速查
1565
+
1566
+ | 参数 | 范围 | 默认值 | 说明 |
1567
+ |-----|------|--------|------|
1568
+ | temperature | 0-2 | 1 | 采样温度,越低越稳定,越高越随机 |
1569
+ | top_p | 0-1 | 1 | 核采样,与temperature互斥或同用 |
1570
+ | max_tokens | 1-上限 | - | 最大输出长度 |
1571
+ | frequency_penalty | -2-2 | 0 | 频率惩罚,减少重复词汇 |
1572
+ | presence_penalty | -2-2 | 0 | 出现惩罚,鼓励新话题 |
1573
+ | stream | true/false | false | 是否流式响应 |
1574
+ | stop | 字符串数组 | - | 停止生成的序列 |
1575
+ | seed | 整数 | - | 随机种子,确保输出可复现 |
1576
+
1577
+ ### 三个 API 端点速查
1578
+
1579
+ ```bash
1580
+ # Responses API(推荐 - 新一代接口)
1581
+ POST https://api.openai.com/v1/responses
1582
+ GET https://api.openai.com/v1/responses/{response_id}
1583
+ DELETE https://api.openai.com/v1/responses/{response_id}
1584
+ POST https://api.openai.com/v1/responses/{response_id}/cancel
1585
+ POST https://api.openai.com/v1/responses/compact
1586
+ POST https://api.openai.com/v1/responses/input_tokens
1587
+
1588
+ # Chat Completions API(标准对话接口)
1589
+ POST https://api.openai.com/v1/chat/completions
1590
+
1591
+ # Completions API(已弃用,维护中)
1592
+ POST https://api.openai.com/v1/completions
1593
+ ```
1594
+
1595
+ ### 认证方式
1596
+
1597
+ ```bash
1598
+ # 方式1:Authorization头(推荐)
1599
+ Authorization: Bearer sk-xxx
1600
+
1601
+ # 方式2:API Key头
1602
+ x-api-key: sk-xxx
1603
+
1604
+ # Realtime API 还需要Beta头
1605
+ OpenAI-Beta: realtime=v1
1606
+ ```
1607
+
1608
+ ---
1609
+
1610
+ ## 最佳实践
1611
+
1612
+ ### 1. API 选择指南
1613
+
1614
+ ```typescript
1615
+ // ✅ 优先使用 Responses API(推荐首选)
1616
+ // - 新一代接口,功能最强
1617
+ // - 支持有状态对话、内置工具(网搜、文搜、计算机使用)
1618
+ // - 支持后台处理和长期运行任务
1619
+ // - 支持推理模型(gpt-5、o-series)
1620
+ // - 支持响应存储和检索
1621
+
1622
+ POST /v1/responses
1623
+
1624
+ // ✅ Chat Completions API(简单对话)
1625
+ // - 轻量级对话接口
1626
+ // - 支持函数调用和 Vision
1627
+ // - 当不需要内置工具和后台处理时使用
1628
+
1629
+ POST /v1/chat/completions
1630
+
1631
+ // ⚠️ 仅在兼容性需求时使用 Completions API
1632
+ // - 已进入维护模式
1633
+ // - 不推荐用于新项目
1634
+
1635
+ POST /v1/completions
1636
+ ```
1637
+
1638
+ ### 2. 模型选择原则
1639
+
1640
+ ```typescript
1641
+ // ✅ 首选 gpt-4o(新项目推荐)
1642
+ // - 最佳性能/成本比
1643
+ // - 支持所有高级功能
1644
+ // - 速度和质量平衡
1645
+
1646
+ // ✅ 成本敏感用 gpt-4o-mini
1647
+ // - 成本最低
1648
+ // - 足以处理大多数任务
1649
+ // - 吞吐量最高
1650
+
1651
+ // ✅ 复杂推理用 gpt-4-turbo
1652
+ // - 最强推理能力
1653
+ // - 上下文最大
1654
+
1655
+ // ⚠️ 避免使用 gpt-3.5-turbo
1656
+ // 除非有明确的遗留兼容性需求
1657
+ ```
1658
+
1659
+ ### 3. 成本优化
1660
+
1661
+ ```typescript
1662
+ // 估算请求成本
1663
+ function estimateCost(
1664
+ inputTokens: number,
1665
+ outputTokens: number,
1666
+ model: string
1667
+ ) {
1668
+ const prices = {
1669
+ 'gpt-4-turbo': { input: 0.01, output: 0.03 },
1670
+ 'gpt-4o': { input: 0.005, output: 0.015 },
1671
+ 'gpt-4o-mini': { input: 0.00015, output: 0.0006 },
1672
+ 'gpt-3.5-turbo': { input: 0.0005, output: 0.0015 }
1673
+ };
1674
+
1675
+ const price = prices[model];
1676
+ return (
1677
+ (inputTokens * price.input + outputTokens * price.output) / 1000000
1678
+ );
1679
+ }
1680
+
1681
+ // 优化策略
1682
+ // 1. 减少重复请求 - 缓存常见查询
1683
+ // 2. 使用 mini 模型处理简单任务
1684
+ // 3. 限制 max_tokens 避免冗长响应
1685
+ // 4. 批量处理而非逐个请求
1686
+ ```
1687
+
1688
+ ### 4. 错误处理核心清单
1689
+
1690
+ ```typescript
1691
+ ✅ 捕获所有异常,检查 error.status
1692
+ ✅ 区分可重试错误(429, 5xx)和不可重试(401, 403)
1693
+ ✅ 实施指数退避重试策略
1694
+ ✅ 为流式响应监听每个事件的错误
1695
+ ✅ 设置合理的请求超时(30-60秒)
1696
+ ✅ 记录错误详情便于调试
1697
+ ✅ 对于 429 应优先增加延迟而非重试
1698
+ ```
1699
+
1700
+ ### 5. 流式 vs 非流式
1701
+
1702
+ ```typescript
1703
+ // 使用流式响应:
1704
+ // ✅ 长响应(预期>500 tokens)
1705
+ // ✅ 需要实时反馈的交互式应用
1706
+ // ✅ 用户体验优先
1707
+
1708
+ if (expectedLength > 500 || needsRealtimeFeedback) {
1709
+ stream: true
1710
+ }
1711
+
1712
+ // 使用非流式响应:
1713
+ // ✅ 短响应(<100 tokens)
1714
+ // ✅ 需要完整响应后处理
1715
+ // ✅ 简单的后端任务
1716
+
1717
+ if (expectedLength < 100 && canWaitForComplete) {
1718
+ stream: false
1719
+ }
1720
+ ```
1721
+
1722
+ ### 6. 函数调用最佳实践
1723
+
1724
+ ```typescript
1725
+ // ✅ 定义清晰的函数描述
1726
+ function: {
1727
+ name: "calculate_distance",
1728
+ description: "计算两点间的直线距离(单位:公里)",
1729
+ parameters: {
1730
+ type: "object",
1731
+ properties: {
1732
+ lat1: { type: "number", description: "第一点纬度" },
1733
+ lon1: { type: "number", description: "第一点经度" },
1734
+ lat2: { type: "number", description: "第二点纬度" },
1735
+ lon2: { type: "number", description: "第二点经度" }
1736
+ },
1737
+ required: ["lat1", "lon1", "lat2", "lon2"]
1738
+ }
1739
+ }
1740
+
1741
+ // ✅ 处理连续调用
1742
+ for (let turn = 0; turn < maxTurns; turn++) {
1743
+ const response = await makeRequest();
1744
+
1745
+ if (response.finish_reason !== 'tool_calls') break;
1746
+
1747
+ // 执行所有函数调用
1748
+ const toolResults = await Promise.all(
1749
+ response.tool_calls.map(call => executeTool(call))
1750
+ );
1751
+
1752
+ // 添加结果到消息历史继续对话
1753
+ messages.push({
1754
+ role: 'assistant',
1755
+ content: response.content
1756
+ });
1757
+
1758
+ messages.push({
1759
+ role: 'user',
1760
+ content: toolResults.map(r => ({
1761
+ type: 'tool_result',
1762
+ tool_use_id: r.id,
1763
+ content: r.output
1764
+ }))
1765
+ });
1766
+ }
1767
+ ```
1768
+
1769
+ ### 7. Vision(图像识别)最佳实践
1770
+
1771
+ ```typescript
1772
+ // ✅ 使用 detail 参数控制分析粒度
1773
+ {
1774
+ type: "image_url",
1775
+ image_url: {
1776
+ url: "https://example.com/image.jpg",
1777
+ detail: "high" // low(快速)/auto(自适应)/high(详细)
1778
+ }
1779
+ }
1780
+
1781
+ // ✅ Base64 用于小图像或私密图像
1782
+ {
1783
+ type: "image_url",
1784
+ image_url: {
1785
+ url: "data:image/jpeg;base64,iVBORw0KGgo..."
1786
+ }
1787
+ }
1788
+
1789
+ // ✅ URL 用于公开网络图像
1790
+ {
1791
+ type: "image_url",
1792
+ image_url: {
1793
+ url: "https://example.com/large-image.jpg"
1794
+ }
1795
+ }
1796
+ ```
1797
+
1798
+ ### 8. 系统提示词工程
1799
+
1800
+ ```typescript
1801
+ // ✅ 清晰的角色定义和约束
1802
+ system: `你是一个专业的 Python 代码审查助手。
1803
+ 对于每个代码片段,你需要:
1804
+ 1. 识别潜在的性能问题
1805
+ 2. 指出不遵循 PEP8 的地方
1806
+ 3. 建议安全改进
1807
+ 4. 提供重构建议
1808
+
1809
+ 使用简洁、专业的语言。
1810
+ 每个问题都要提供具体代码示例。`
1811
+
1812
+ // ✅ 避免过度冗长或模糊的指令
1813
+ // ❌ system: "你是一个助手,帮助人们" // 太模糊
1814
+ // ✅ system: "你是一个...[具体职责]" // 清晰具体
1815
+ ```
1816
+
1817
+ ### 9. 速率限制处理
1818
+
1819
+ ```typescript
1820
+ // OpenAI API 速率限制:
1821
+ // - 同时连接数:有限制
1822
+ // - RPM(每分钟请求):有限制
1823
+ // - TPM(每分钟 token):有限制
1824
+
1825
+ // 应对策略
1826
+ function throttledRequest(delay: number = 100) {
1827
+ return new Promise(resolve => {
1828
+ setTimeout(resolve, delay);
1829
+ });
1830
+ }
1831
+
1832
+ async function smartRequest(requestFn) {
1833
+ while (true) {
1834
+ try {
1835
+ return await requestFn();
1836
+ } catch (error) {
1837
+ if (error.status === 429) {
1838
+ // 从响应头读取重试时间
1839
+ const retryAfter =
1840
+ error.headers?.['retry-after'] || '60';
1841
+ await throttledRequest(parseInt(retryAfter) * 1000);
1842
+ } else {
1843
+ throw error;
1844
+ }
1845
+ }
1846
+ }
1847
+ }
1848
+ ```
1849
+
1850
+ ### 10. 监控和日志
1851
+
1852
+ ```typescript
1853
+ interface RequestMetrics {
1854
+ model: string;
1855
+ inputTokens: number;
1856
+ outputTokens: number;
1857
+ totalTokens: number;
1858
+ responseTime: number; // 毫秒
1859
+ finishReason: string;
1860
+ status: 'success' | 'error';
1861
+ timestamp: Date;
1862
+ costEstimate: number; // 美元
1863
+ responseCount?: number; // Responses API 生成的响应数量
1864
+ }
1865
+
1866
+ // 定期分析
1867
+ const analytics = {
1868
+ avgTokensPerRequest: totalTokens / requestCount,
1869
+ avgResponseTime: totalTime / requestCount,
1870
+ successRate: successCount / totalCount,
1871
+ estimatedMonthlyCost: estimateCost(totalTokens)
1872
+ };
1873
+
1874
+ console.log('成本分析:', analytics);
1875
+ ```
1876
+
1877
+ ---
1878
+
1879
+ ## 代码示例库
1880
+
1881
+ ### Node.js / TypeScript 基础示例
1882
+
1883
+ ```typescript
1884
+ import OpenAI from "openai";
1885
+
1886
+ const openai = new OpenAI({
1887
+ apiKey: process.env.OPENAI_API_KEY,
1888
+ });
1889
+
1890
+ async function main() {
1891
+ const message = await openai.chat.completions.create({
1892
+ model: "gpt-4o",
1893
+ messages: [
1894
+ {
1895
+ role: "user",
1896
+ content: "What is the capital of France?",
1897
+ },
1898
+ ],
1899
+ });
1900
+
1901
+ console.log(message.choices[0].message.content);
1902
+ }
1903
+
1904
+ main();
1905
+ ```
1906
+
1907
+ ### Python 流式示例
1908
+
1909
+ ```python
1910
+ from openai import OpenAI
1911
+
1912
+ client = OpenAI(api_key="sk-xxx")
1913
+
1914
+ with client.chat.completions.stream(
1915
+ model="gpt-4o",
1916
+ messages=[
1917
+ {"role": "user", "content": "Write a poem about spring"}
1918
+ ],
1919
+ ) as stream:
1920
+ for text in stream.text_stream:
1921
+ print(text, end="", flush=True)
1922
+ ```
1923
+
1924
+ ### JavaScript 函数调用示例
1925
+
1926
+ ```javascript
1927
+ const OpenAI = require("openai").default;
1928
+
1929
+ const openai = new OpenAI({ apiKey: "sk-xxx" });
1930
+
1931
+ async function main() {
1932
+ const tools = [
1933
+ {
1934
+ type: "function",
1935
+ function: {
1936
+ name: "get_weather",
1937
+ description: "Get weather information for a location",
1938
+ parameters: {
1939
+ type: "object",
1940
+ properties: {
1941
+ location: {
1942
+ type: "string",
1943
+ description: "City name",
1944
+ },
1945
+ unit: {
1946
+ type: "string",
1947
+ enum: ["celsius", "fahrenheit"],
1948
+ default: "celsius",
1949
+ },
1950
+ },
1951
+ required: ["location"],
1952
+ },
1953
+ },
1954
+ },
1955
+ ];
1956
+
1957
+ const messages = [
1958
+ { role: "user", content: "What is the weather in Paris?" },
1959
+ ];
1960
+
1961
+ const response = await openai.chat.completions.create({
1962
+ model: "gpt-4o",
1963
+ messages: messages,
1964
+ tools: tools,
1965
+ tool_choice: "auto",
1966
+ });
1967
+
1968
+ const toolCall = response.choices[0].message.tool_calls?.[0];
1969
+ if (toolCall?.function.name === "get_weather") {
1970
+ console.log("Function called:", toolCall.function.name);
1971
+ console.log("Arguments:", toolCall.function.arguments);
1972
+ }
1973
+ }
1974
+
1975
+ main();
1976
+ ```
1977
+
1978
+ ### Responses API 使用示例 - TypeScript
1979
+
1980
+ ```typescript
1981
+ import OpenAI from "openai";
1982
+
1983
+ const openai = new OpenAI({
1984
+ apiKey: process.env.OPENAI_API_KEY,
1985
+ });
1986
+
1987
+ // 基础文本请求
1988
+ async function basicRequest() {
1989
+ const response = await openai.beta.responses.create({
1990
+ model: "gpt-4o",
1991
+ input: "Tell me a bedtime story about a unicorn."
1992
+ });
1993
+
1994
+ console.log(response.output[0].content[0].text);
1995
+ }
1996
+
1997
+ // 多轮对话
1998
+ async function multiTurnConversation() {
1999
+ // 第一个请求
2000
+ const response1 = await openai.beta.responses.create({
2001
+ model: "gpt-4o",
2002
+ input: "What is Python?"
2003
+ });
2004
+
2005
+ console.log("Assistant:", response1.output[0].content[0].text);
2006
+
2007
+ // 第二个请求,引用第一个响应
2008
+ const response2 = await openai.beta.responses.create({
2009
+ model: "gpt-4o",
2010
+ input: "Give me a practical example",
2011
+ previous_response_id: response1.id
2012
+ });
2013
+
2014
+ console.log("Assistant:", response2.output[0].content[0].text);
2015
+ }
2016
+
2017
+ // 使用网络搜索
2018
+ async function webSearch() {
2019
+ const response = await openai.beta.responses.create({
2020
+ model: "gpt-4o",
2021
+ input: "What is the latest news about AI?",
2022
+ tools: [
2023
+ {
2024
+ type: "web_search"
2025
+ }
2026
+ ]
2027
+ });
2028
+
2029
+ console.log(response.output[0].content[0].text);
2030
+ }
2031
+
2032
+ // 后台处理
2033
+ async function backgroundProcessing() {
2034
+ const response = await openai.beta.responses.create({
2035
+ model: "gpt-4o",
2036
+ input: "Process this large dataset",
2037
+ background: true,
2038
+ store: true
2039
+ });
2040
+
2041
+ console.log("Response ID:", response.id);
2042
+ console.log("Status:", response.status);
2043
+
2044
+ // 稍后获取结果
2045
+ await new Promise(r => setTimeout(r, 5000));
2046
+ const completed = await openai.beta.responses.retrieve(response.id);
2047
+ console.log("Completed:", completed.status);
2048
+ }
2049
+
2050
+ // 函数调用
2051
+ async function functionCalling() {
2052
+ const response = await openai.beta.responses.create({
2053
+ model: "gpt-4o",
2054
+ input: "What is the weather in Paris?",
2055
+ tools: [
2056
+ {
2057
+ type: "function",
2058
+ function: {
2059
+ name: "get_weather",
2060
+ description: "Get weather information",
2061
+ parameters: {
2062
+ type: "object",
2063
+ properties: {
2064
+ city: {
2065
+ type: "string",
2066
+ description: "City name"
2067
+ }
2068
+ },
2069
+ required: ["city"]
2070
+ }
2071
+ }
2072
+ }
2073
+ ]
2074
+ });
2075
+
2076
+ if (response.output[0].content[0].type === "tool_use") {
2077
+ console.log("Function called:", response.output[0].content[0].name);
2078
+ }
2079
+ }
2080
+
2081
+ basicRequest();
2082
+ ```
2083
+
2084
+ ### Responses API 流式示例
2085
+
2086
+ ```typescript
2087
+ import OpenAI from "openai";
2088
+
2089
+ const openai = new OpenAI({
2090
+ apiKey: process.env.OPENAI_API_KEY,
2091
+ });
2092
+
2093
+ async function streamingResponse() {
2094
+ const response = await openai.beta.responses.stream({
2095
+ model: "gpt-4o",
2096
+ input: "Write a poem about spring"
2097
+ });
2098
+
2099
+ for await (const event of response) {
2100
+ if (event.type === 'response.output_item.delta') {
2101
+ if (event.delta.content?.type === 'text') {
2102
+ process.stdout.write(event.delta.content.text);
2103
+ }
2104
+ }
2105
+ }
2106
+ }
2107
+
2108
+ streamingResponse();
2109
+ ```
2110
+
2111
+ ### Responses API 对话压缩
2112
+
2113
+ ```typescript
2114
+ // 当对话变得很长时,可以压缩以节省 token
2115
+ const compacted = await openai.beta.responses.compact({
2116
+ model: "gpt-5",
2117
+ input: [
2118
+ {
2119
+ role: "user",
2120
+ content: "Create a landing page"
2121
+ },
2122
+ {
2123
+ type: "message",
2124
+ role: "assistant",
2125
+ content: [
2126
+ {
2127
+ type: "output_text",
2128
+ text: "Here is a landing page..."
2129
+ }
2130
+ ]
2131
+ }
2132
+ ]
2133
+ });
2134
+
2135
+ console.log("Compacted response:", compacted.id);
2136
+ ```
2137
+
2138
+ ---
2139
+
2140
+ ## 版本信息和更新
2141
+
2142
+ - **文档版本**:2.3(添加完整流式事件文档)
2143
+ - **更新日期**:2026 年 2 月 3 日
2144
+ - **覆盖 API**:Responses、Chat Completions、Completions(已弃用)
2145
+ - **最新模型**:GPT-4o、GPT-4o-mini、GPT-5、O3
2146
+ - **API 基础 URL**:https://api.openai.com
2147
+
2148
+ ---
2149
+
2150
+ ## 常见问题解答
2151
+
2152
+ | 问题 | 答案 |
2153
+ |-----|------|
2154
+ | 应该使用哪个 API? | 优先使用 Responses API,它是 OpenAI 的新一代标准。Chat Completions API 仍可用但逐渐被替代。Completions API 已过时。 |
2155
+ | Responses API 和 Chat Completions API 有什么区别? | Responses API 支持有状态对话、内置工具(网搜、文搜等)、后台处理、推理模型。Chat Completions 更轻量,只支持基础对话和函数调用。 |
2156
+ | Responses API 支持哪些内置工具? | web_search(网络搜索)、file_search(文件搜索)、code_interpreter(代码执行)、computer(计算机使用)。 |
2157
+ | 如何在 Responses API 中进行多轮对话? | 使用 `previous_response_id` 参数引用之前的响应,或使用 `conversation` 参数。 |
2158
+ | 后台处理如何工作? | 设置 `"background": true`,响应会异步执行。可通过 `GET /v1/responses/{response_id}` 检查状态。 |
2159
+ | 哪个模型性价比最好? | gpt-4o 是目前的最佳选择,性能强、成本合理。 |
2160
+ | Responses API 支持流式吗? | 支持,设置 `"stream": true`。使用 Server-Sent Events 格式。 |
2161
+ | 如何处理速率限制? | 使用指数退避重试策略,从 Retry-After 头读取建议延迟。 |
2162
+