OpenAI API使用用户API keys来鉴权。联系管理员获取API Keys。

请注意保护API keys! 不要分享或暴露在任何客户端(浏览器, apps)。 业务访问的时候一定在服务器端进行,不要直接通过客户端使用API keys访问接口。

所有的接口访问都需要在HTTP头加入Authorization进行鉴权。例如:

Authorization: Bearer API_KEY

联系我们获取API Endpoint。

在终端使用如下代码可以发起你的第一次API请求。确保使用你的API key替换了 $API_KEY

1
2
3
4
5
6
7
8
curl https://API_ENDPOINT/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
     "model": "gpt-3.5-turbo",
     "messages": [{"role": "user", "content": "Say this is a test!"}],
     "temperature": 0.7
   }'

这个请求使用 gpt-3.5-turbo model 针对 "Say this is a test"进行回复。收到的返回如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
   "id":"chatcmpl-abc123",
   "object":"chat.completion",
   "created":1677858242,
   "model":"gpt-3.5-turbo-0301",
   "usage":{
      "prompt_tokens":13,
      "completion_tokens":7,
      "total_tokens":20
   },
   "choices":[
      {
         "message":{
            "role":"assistant",
            "content":"\n\nThis is a test!"
         },
         "finish_reason":"stop",
         "index":0
      }
   ]
}

你已完成首次对话。反馈中的 finish_reasonstop , 这表示 API 返回了model生成的完整内容。如下的请求中,我们只生成唯一的结果,但是可以通过 设定参数 n 来生成多种结果。另外下列示例中使用 gpt-3.5-turbo model, 此model多用在 text completion task。 同时也多用于 chat applications

列出所有的Models,并可以查询具体某个model的详情。 参考 Models 查看models之间的差异。

get https://API_ENDPOINT/v1/models

列出可用 models,并给出基本信息,例如 owner 和 availability。

Example request
1
2
curl https://API_ENDPOINT/v1/models \
  -H "Authorization: Bearer $API_KEY"
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "data": [
    {
      "id": "model-id-0",
      "object": "model",
      "owned_by": "organization-owner",
      "permission": [...]
    },
    {
      "id": "model-id-1",
      "object": "model",
      "owned_by": "organization-owner",
      "permission": [...]
    },
    {
      "id": "model-id-2",
      "object": "model",
      "owned_by": "openai",
      "permission": [...]
    },
  ],
  "object": "list"
}

get https://API_ENDPOINT/v1/models/{model}

查询某个model的基本信息,例如 owner 和 permissioning.

传参

Example request
1
2
curl https://API_ENDPOINT/v1/models/text-davinci-003 \
  -H "Authorization: Bearer $API_KEY"
Response
1
2
3
4
5
6
{
  "id": "text-davinci-003",
  "object": "model",
  "owned_by": "openai",
  "permission": [...]
}

根据给定的prompt(引导语), model 将给出一个或多个completions(预言结果), 并将给出每个结果的相关属性。

post https://API_ENDPOINT/v1/completions

根据给定的 prompt (引导语)和参数创建文本处理任务。

Request body

Example request
1
2
3
4
5
6
7
8
9
curl https://API_ENDPOINT/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "text-davinci-003",
    "prompt": "Say this is a test",
    "max_tokens": 7,
    "temperature": 0
  }'
Parameters
1
2
3
4
5
6
7
8
9
10
11
{
  "model": "text-davinci-003",
  "prompt": "Say this is a test",
  "max_tokens": 7,
  "temperature": 0,
  "top_p": 1,
  "n": 1,
  "stream": false,
  "logprobs": null,
  "stop": "\n"
}
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "\n\nThis is indeed a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}

对话交谈场景。

OpenAI的ChatCompletion和Completion都是自然语言生成模型的接口,但它们的用途和应用场景略有不同。

ChatCompletion Completion
区别 专为生成对话和聊天场景而设计。 是一个通用的自然语言生成接口,支持生成各种类型的文本,包括段落、摘要、建议、答案等等。
适用场景 ChatCompletion接口生成的文本通常会更具有人类对话的风格和语调,可以用于智能客服、聊天机器人等场景,以及在日常聊天中帮助用户自动生成回复。 Completion接口的输出更为多样化,可能会更加严谨和专业,适用于各种文本生成场景,例如文章创作、信息提取、机器翻译、自然语言问题回答等等。

post https://API_ENDPOINT/v1/chat/completions

根据消息获取对话

Request body

Example request
1
2
3
4
5
6
7
curl https://API_ENDPOINT/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'
Parameters
1
2
3
4
{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
  "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
  }
}

根据指示 instruction 对 导引语 prompt进行修改。

post https://API_ENDPOINT/v1/edits

根据输入 input 、指示 instruction 和参数 parameters,创建一个新的文本修改。

Request body

Example request
1
2
3
4
5
6
7
8
curl https://API_ENDPOINT/v1/edits \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "text-davinci-edit-001",
    "input": "What day of the wek is it?",
    "instruction": "Fix the spelling mistakes"
  }'
Parameters
1
2
3
4
5
{
  "model": "text-davinci-edit-001",
  "input": "What day of the wek is it?",
  "instruction": "Fix the spelling mistakes",
}
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "object": "edit",
  "created": 1589478378,
  "choices": [
    {
      "text": "What day of the week is it?",
      "index": 0,
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 32,
    "total_tokens": 57
  }
}

根据导引词 prompt 生成图片,可以输入图像作为参考。

post https://API_ENDPOINT/v1/images/generations

根据给定的 prompt,创建图片。

Request body

Example request
1
2
3
4
5
6
7
8
curl https://API_ENDPOINT/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "prompt": "A cute baby sea otter",
    "n": 2,
    "size": "1024x1024"
  }'
Parameters
1
2
3
4
5
{
  "prompt": "A cute baby sea otter",
  "n": 2,
  "size": "1024x1024"
}
Response
1
2
3
4
5
6
7
8
9
10
11
{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}

post https://API_ENDPOINT/v1/images/edits

根据引导词对给定的图像进行扩展和修正。

Request body

Example request
1
2
3
4
5
6
7
curl https://API_ENDPOINT/v1/images/edits \
  -H "Authorization: Bearer $API_KEY" \
  -F image="@otter.png" \
  -F mask="@mask.png" \
  -F prompt="A cute baby sea otter wearing a beret" \
  -F n=2 \
  -F size="1024x1024"
Response
1
2
3
4
5
6
7
8
9
10
11
{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}

post https://API_ENDPOINT/v1/images/variations

异化一张图?

Request body

Example request
1
2
3
4
5
curl https://API_ENDPOINT/v1/images/variations \
  -H "Authorization: Bearer $API_KEY" \
  -F image="@otter.png" \
  -F n=2 \
  -F size="1024x1024"
Response
1
2
3
4
5
6
7
8
9
10
11
{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}

获取输入文本的自然语言向量 vector

post https://API_ENDPOINT/v1/embeddings

获取代表输入字符串的自然语言向量。

Request body

Example request
1
2
3
4
5
6
7
curl https://API_ENDPOINT/v1/embeddings \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-ada-002"
  }'
Parameters
1
2
3
4
{
  "model": "text-embedding-ada-002",
  "input": "The food was delicious and the waiter..."
}
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [
        0.0023064255,
        -0.009327292,
        .... (1536 floats total for ada-002)
        -0.0028842222,
      ],
      "index": 0
    }
  ],
  "model": "text-embedding-ada-002",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}

给定输入文本,如果模型将其分类为违反 OpenAI 的内容策略,则输出。

Related guide: Moderations

post https://API_ENDPOINT/v1/moderations

分类文本是否违反 OpenAI 的内容政策

Request body

Example request
1
2
3
4
5
6
curl https://API_ENDPOINT/v1/moderations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "input": "I want to kill them."
  }'
Parameters
1
2
3
{
  "input": "I want to kill them."
}
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
  "id": "modr-5MWoLO",
  "model": "text-moderation-001",
  "results": [
    {
      "categories": {
        "hate": false,
        "hate/threatening": true,
        "self-harm": false,
        "sexual": false,
        "sexual/minors": false,
        "violence": true,
        "violence/graphic": false
      },
      "category_scores": {
        "hate": 0.22714105248451233,
        "hate/threatening": 0.4132447838783264,
        "self-harm": 0.005232391878962517,
        "sexual": 0.01407341007143259,
        "sexual/minors": 0.0038522258400917053,
        "violence": 0.9223177433013916,
        "violence/graphic": 0.036865197122097015
      },
      "flagged": true
    }
  ]
}

将音频转化为文本

post https://API_ENDPOINT/v1/audio/transcriptions

转换语音成文本,以输入语音的语言为准

Request body

需要转换的音频,支持如下格式: mp3, mp4, mpeg, mpga, m4a, wav, or webm.

模型ID。只支持 whisper-1

导引词,需要和原语音相同语种。可以对输出内容细调,比如简体,繁体。

返回格式: json, text, srt, verbose_json, or vtt.

采样 temperature,介于 0 和 1 之间。 较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加集中和确定。 如果设置为 0,模型将使用对数概率自动升高 temperature,直到达到特定阈值。

输入音频的语言。以 ISO-639-1 格式提供输入语言将提高准确性。

Example request
1
2
3
4
5
curl https://API_ENDPOINT/v1/audio/transcriptions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F file="@/path/to/file/audio.mp3" \
  -F model="whisper-1"
Parameters
1
2
3
4
{
  "file": "audio.mp3",
  "model": "whisper-1"
}
Response
1
2
3
{
  "text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger. This is a place where you can get to do that."
}

post https://API_ENDPOINT/v1/audio/translations

语音转换为英语

Request body

支持格式: mp3, mp4, mpeg, mpga, m4a, wav, or webm.

模型ID。只支持 whisper-1 .

导模型的风格或继续之前的音频片段。提示应为英文,英语。

返回格式: json, text, srt, verbose_json, or vtt.

采样 temperature,介于 0 和 1 之间。 较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使输出更加集中和确定。 如果设置为 0,模型将使用对数概率自动升高 temperature,直到达到特定阈值。

Example request
1
2
3
4
5
curl https://API_ENDPOINT/v1/audio/translations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F file="@/path/to/file/german.m4a" \
  -F model="whisper-1"
Parameters
1
2
3
4
{
  "file": "german.m4a",
  "model": "whisper-1"
}
Response
1
2
3
{
  "text": "Hello, my name is Wolfgang and I come from Germany. Where are you heading today?"
}