aicodeswitch 3.9.4 → 4.0.0

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.
@@ -175,7 +175,7 @@ interface Message {
175
175
  tool_calls?: ToolCall[]; // assistant消息中的工具调用
176
176
  }
177
177
 
178
- type ContentBlock =
178
+ type ContentBlock =
179
179
  | TextContent
180
180
  | ImageContent
181
181
  | ToolCallContent
@@ -399,1021 +399,6 @@ curl -X POST https://api.openai.com/v1/chat/completions \
399
399
  }'
400
400
  ```
401
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
402
  ## 流式事件
1418
403
 
1419
404
  ### 流式响应启用
@@ -1544,11 +529,11 @@ async function requestWithRetry(
1544
529
  } catch (error: any) {
1545
530
  const status = error.status;
1546
531
  const isRetryable = status === 429 || status >= 500;
1547
-
532
+
1548
533
  if (!isRetryable || attempt === maxRetries) {
1549
534
  throw error;
1550
535
  }
1551
-
536
+
1552
537
  // 指数退避:第一次等待1秒,第二次2秒,第三次4秒
1553
538
  const delay = Math.pow(2, attempt - 1) * 1000;
1554
539
  await new Promise(resolve => setTimeout(resolve, delay));
@@ -1574,24 +559,6 @@ async function requestWithRetry(
1574
559
  | stop | 字符串数组 | - | 停止生成的序列 |
1575
560
  | seed | 整数 | - | 随机种子,确保输出可复现 |
1576
561
 
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
562
  ### 认证方式
1596
563
 
1597
564
  ```bash
@@ -1612,27 +579,12 @@ OpenAI-Beta: realtime=v1
1612
579
  ### 1. API 选择指南
1613
580
 
1614
581
  ```typescript
1615
- // ✅ 优先使用 Responses API(推荐首选)
1616
- // - 新一代接口,功能最强
1617
- // - 支持有状态对话、内置工具(网搜、文搜、计算机使用)
1618
- // - 支持后台处理和长期运行任务
1619
- // - 支持推理模型(gpt-5、o-series)
1620
- // - 支持响应存储和检索
1621
-
1622
- POST /v1/responses
1623
-
1624
582
  // ✅ Chat Completions API(简单对话)
1625
583
  // - 轻量级对话接口
1626
584
  // - 支持函数调用和 Vision
1627
585
  // - 当不需要内置工具和后台处理时使用
1628
586
 
1629
587
  POST /v1/chat/completions
1630
-
1631
- // ⚠️ 仅在兼容性需求时使用 Completions API
1632
- // - 已进入维护模式
1633
- // - 不推荐用于新项目
1634
-
1635
- POST /v1/completions
1636
588
  ```
1637
589
 
1638
590
  ### 2. 模型选择原则
@@ -1671,7 +623,7 @@ function estimateCost(
1671
623
  'gpt-4o-mini': { input: 0.00015, output: 0.0006 },
1672
624
  'gpt-3.5-turbo': { input: 0.0005, output: 0.0015 }
1673
625
  };
1674
-
626
+
1675
627
  const price = prices[model];
1676
628
  return (
1677
629
  (inputTokens * price.input + outputTokens * price.output) / 1000000
@@ -1741,20 +693,20 @@ function: {
1741
693
  // ✅ 处理连续调用
1742
694
  for (let turn = 0; turn < maxTurns; turn++) {
1743
695
  const response = await makeRequest();
1744
-
696
+
1745
697
  if (response.finish_reason !== 'tool_calls') break;
1746
-
698
+
1747
699
  // 执行所有函数调用
1748
700
  const toolResults = await Promise.all(
1749
701
  response.tool_calls.map(call => executeTool(call))
1750
702
  );
1751
-
703
+
1752
704
  // 添加结果到消息历史继续对话
1753
705
  messages.push({
1754
706
  role: 'assistant',
1755
707
  content: response.content
1756
708
  });
1757
-
709
+
1758
710
  messages.push({
1759
711
  role: 'user',
1760
712
  content: toolResults.map(r => ({
@@ -1836,7 +788,7 @@ async function smartRequest(requestFn) {
1836
788
  } catch (error) {
1837
789
  if (error.status === 429) {
1838
790
  // 从响应头读取重试时间
1839
- const retryAfter =
791
+ const retryAfter =
1840
792
  error.headers?.['retry-after'] || '60';
1841
793
  await throttledRequest(parseInt(retryAfter) * 1000);
1842
794
  } else {
@@ -2134,29 +1086,3 @@ const compacted = await openai.beta.responses.compact({
2134
1086
 
2135
1087
  console.log("Compacted response:", compacted.id);
2136
1088
  ```
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
-