OpenAI API 文档

Create Chat Completion

POST https://api.openai.com/v1/chat/completions

输入参数

参数名 类型 必填 描述
model string 要使用的模型ID。推荐使用 gpt-3.5-turbogpt-4
messages array 对话消息列表。每条消息应包含 role(角色)和 content(内容)。角色可以是 system、user 或 assistant
temperature number 采样温度,介于0和2之间。较高的值会使输出更随机,较低的值会使输出更确定。默认值为1
top_p number 核采样概率,介于0和1之间。是temperature的替代方案。建议只修改其中一个。默认值为1
n integer 每个输入消息要生成的聊天完成选项数量。默认值为1
stream boolean 是否启用流式响应。如果设置为true,将分部分返回结果。默认为 false
stop string/array 最多4个序列,当API遇到这些序列时将停止生成。可以是字符串或字符串数组
max_tokens integer 生成的最大令牌数。模型的上下文长度限制减去输入令牌数就是可用的最大值
presence_penalty number 介于-2.0和2.0之间的数字。正值会根据新标记在文本中是否出现来惩罚它们,增加模型谈论新主题的可能性。默认值为0
frequency_penalty number 介于-2.0和2.0之间的数字。正值会根据新标记在现有文本中的频率来惩罚它们,降低模型逐字重复同样内容的可能性。默认值为0
logit_bias map 修改指定标记出现的可能性。接受一个token和关联偏差值的JSON对象
user string 代表终端用户的唯一标识符,帮助OpenAI监控和检测滥用行为
seed integer 用于随机采样的种子。指定种子可以帮助生成确定性的响应
tools array 模型可能调用的工具列表。目前支持 function 类型的工具
tool_choice string/object 控制模型如何选择调用工具。可以是 "auto""none" 或强制使用特定函数的对象
response_format object 指定模型的输出格式。可以设置 type"text""json_object"

输出参数

参数名 类型 描述
id string 响应的唯一标识符
choices array 包含生成的回复。每个选项包含 messagefinish_reason
usage object 包含令牌使用统计:prompt_tokens(提示令牌数)、completion_tokens(完成令牌数)和total_tokens(总令牌数)

Request Body

{
  "model": "gpt-3.5-turbo",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ],
  "temperature": 0.7
}

Response Body

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Hello! How can I help you today?"
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

Example Request

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!"}]
  }'

Create Embeddings

POST https://api.openai.com/v1/embeddings

输入参数

参数名 类型 必填 描述
model string 要使用的模型ID。推荐使用 text-embedding-ada-002
input string/array 要嵌入的输入文本。可以是字符串或字符串数组
user string 用于标识最终用户的唯一标识符

输出参数

参数名 类型 描述
data array 嵌入向量列表。每个对象包含 embedding(向量数组)和 index(索引号)
usage object 包含令牌使用统计:prompt_tokens(输入令牌数)和 total_tokens(总令牌数)
model string 使用的模型名称

Request Body

{
  "model": "text-embedding-ada-002",
  "input": "The food was delicious and the waiter..."
}

Response Body

{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [0.0023064255, -0.009327292, ...],
      "index": 0
    }
  ],
  "model": "text-embedding-ada-002",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}

Example Request

curl https://api.openai.com/v1/embeddings \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "Your text string goes here",
    "model": "text-embedding-ada-002"
  }'

Create Transcription

POST https://api.openai.com/v1/audio/transcriptions

输入参数

参数名 类型 必填 描述
file file 要转录的音频文件。支持格式:mp3, mp4, mpeg, mpga, m4a, wav, webm
model string 要使用的模型ID。目前支持 whisper-1
language string 音频语言的 ISO-639-1 代码。如果未提供,将自动检测语言
prompt string 可选的文本,用于引导模型的风格或继续前一个音频片段的转录
response_format string 输出格式,支持:json, text, srt, verbose_json, vtt。默认为 json

输出参数

参数名 类型 描述
text string 转录的文本内容

Request Body

{
  "file": "audio.mp3",
  "model": "whisper-1",
  "language": "en"
}

Response Body

{
  "text": "Hello, how are you today?"
}

Example Request

curl https://api.openai.com/v1/audio/transcriptions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F file="@/path/to/file/audio.mp3" \
  -F model="whisper-1"

Create Speech

POST https://api.openai.com/v1/audio/speech

输入参数

参数名 类型 必填 描述
model string 要使用的TTS模型。当前支持 tts-1tts-1-hd
input string 要转换为语音的输入文本。最大支持4096个字符
voice string 要使用的语音。可选值:alloy, echo, fable, onyx, nova, shimmer
speed number 语音速度,范围 0.25 到 4.0。默认为 1.0
response_format string 音频格式。支持 mp3, opus, aac, flac。默认为 mp3

输出参数

参数名 类型 描述
content binary 生成的音频文件的二进制数据

Request Body

{
  "model": "tts-1",
  "input": "Hello world!",
  "voice": "alloy"
}

Response Body

Binary audio file (mp3)

Example Request

curl https://api.openai.com/v1/audio/speech \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "tts-1",
    "input": "Hello world!",
    "voice": "alloy"
  }' \
  --output speech.mp3

Create Image

POST https://api.openai.com/v1/images/generations

输入参数

参数名 类型 必填 描述
prompt string 生成图像的文本描述。最大长度为1000个字符
model string 要使用的模型。支持 dall-e-2dall-e-3
n integer 要生成的图像数量。DALL·E 3仅支持 n=1,DALL·E 2支持1-10之间的值
size string 图像尺寸。DALL·E 3支持:1024x1024, 1792x1024, 1024x1792;DALL·E 2支持:256x256, 512x512, 1024x1024
quality string 图像质量。可选 standard(默认) 或 hd,仅适用于 DALL·E 3

输出参数

参数名 类型 描述
created integer 创建时间戳
data array 生成的图像数组。每个对象包含 urlb64_json

Request Body

{
  "prompt": "A cute baby sea otter",
  "n": 1,
  "size": "1024x1024"
}

Response Body

{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    }
  ]
}

Example Request

curl https://api.openai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "prompt": "A cute baby sea otter",
    "n": 1,
    "size": "1024x1024"
  }'