OpenAI API 接口文档 表格形式

32 min read

请求体

参数/属性 类型 是否必须 默认值 描述
model string 使用的模型ID。参考模型端点兼容性表以了解哪些模型与Chat API兼容。
messages array 描述到目前为止的对话列表。
- role string 该消息的作者角色。可选系统(system)、用户(user)或助手(assistant)。
- content string 消息内容。
- name string 该消息的作者名称。最多可包含64个字符,可能包含a-z、A-Z、0-9和下划线。
temperature number 1 使用的采样温度,介于0和2之间。更高的值如0.8会使输出更随机,而更低的值如0.2会使其更集中和确定。
top_p number 1 与温度采样相似的替代方案,称为核采样,模型考虑概率质量最高的top_p结果。例如0.1表示仅考虑概率质量最高的10%的标记。
n integer 1 为每个输入消息生成多少个聊天完成选项。
stream boolean false 如果设置,将发送部分消息增量,就像在ChatGPT中一样。在可用时,将发送数据仅作为服务器发送的事件,流以数据:[完成]消息终止。
stop string/array null 最多可设置4个序列,API将在生成更多标记后停止。
max_tokens integer 无限制 在聊天完成中生成的最大标记数。输入标记和生成标记的总长度受模型的上下文长度限制。
presence_penalty number 0 在-2.0和2.0之间的数字。正值会根据新标记在文本中迄今为止的出现情况对其进行惩罚,增加模型谈论新主题的可能性。
frequency_penalty number 0 在-2.0和2.0之间的数字。正值会根据新标记在文本中迄今为止的存在频率对其进行惩罚,降低模型逐字重复相同内容的可能性。
logit_bias map null 修改指定标记在完成中出现的可能性。接受一个将标记(由分词器中的标

响应结果

字段 类型 描述
id string 聊天完成的ID。
object string 对象类型,此处为"chat.completion"。
created integer 创建时间戳。
choices array 包含生成的回复选项的数组。
- index integer 选项的索引。
- message object 包含生成的回复信息的对象。
-- role string 生成的回复的角色,此处为"assistant"。
-- content string 生成的回复内容。
finish_reason string 完成生成的原因。
usage object 描述API使用情况的对象。
- prompt_tokens integer 用于提示的标记数量。
- completion_tokens integer 生成的完成标记数量。
- total_tokens integer 总标记数量(包括提示和完成标记)。

测试

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}