通用 OpenAI-API 文档

一、 Introduction 介绍

如果你想使用我们的API,你可以通过 HTTP 请求从任何语言与 API 进行交互,也可以使用我们的官方 Python 绑定、官方 Node.js库 或 社区维护的库。

若要安装官方 Python 绑定,请运行以下命令:

bash
运行复制
pip install openai

要安装官方的 Node.js 库,请在您的 Node.js 项目目录中运行以下命令:

bash
运行复制
npm install openai

二、 Authentication 认证

1. OpenAI-API-KEY

OpenAI API 使用 API密钥 进行身份验证。请访问您的 API密钥 页面以检索您在请求中使用的API密钥。

请记住,您的API密钥是机密的! 不要与他人分享它或在任何客户端代码(浏览器、应用程序)中公开它。生产请求必须通过您自己的后端服务器路由,其中您的 API密钥 可以从环境变量或密钥管理服务中安全加载。

所有API请求都应在 Authorization HTTP标头中包含您的API密钥,如下所示:

http
运行复制
# 注意Bearer OPENAI_API_KEY,Bearer的后面是有一个空格的 Authorization: Bearer OPENAI_API_KEY

2. OpenAI-Organization

Requesting organization 请求组织

对于属于多个组织的用户,您可以传递一个 表头 来指定用于 API请求 的组织。这些 API请求 的使用将计入指定组织的订阅配额。

示例 curl 命令:

bash
运行复制
curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "OpenAI-Organization: org-gth0C8mT2wnKealyCkSRrpQk"

使用 openai Python包 的示例:

python
运行复制
  • import os
  • import openai
  • openai.organization = "org-gth0C8mT2wnKealyCkSRrpQk"
  • openai.api_key = os.getenv("OPENAI_API_KEY")
  • openai.Model.list()

使用 openai Node.js包 的示例:

javascript
运行复制
  • import { Configuration, OpenAIApi } from "openai";
  • const configuration = new Configuration({
  • organization: "org-gth0C8mT2wnKealyCkSRrpQk",
  • apiKey: process.env.OPENAI_API_KEY,
  • });
  • const openai = new OpenAIApi(configuration);
  • const response = await openai.listEngines();

可以在 组织设置 页面上找到 组织ID。


三、 Making requests 提出请求

您可以将下面的命令粘贴到您的终端中,以运行您的第一个 API请求。请确保将 $OPENAI_API_KEY 替换为您的 API密钥。

bash
运行复制
  • 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": "Say this is a test!"}],
  • "temperature": 0.7
  • }'

此请求查询 gpt-3.5-turbo模型,以完成从提示“Say this is a test”开始的文本。您应该会收到类似以下内容的响应:

json
运行复制
  • {
  • "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返回了模型生成的完整完成。在上面的请求中,我们只生成了一条消息,但您可以将 n 参数设置为生成多个消息选项。在此示例中,gpt-3.5-turbo 被用于更传统的 文本完成任务。该模型也针对 聊天应用 进行了优化。


四、 Models 模型

列出并描述 API 中可用的各种模型。您可以参考 模型文档 以了解可用的模型以及它们之间的差异。

1. List models 列出模型

http
运行复制
GET https://api.openai.com/v1/models

列出当前可用的模型,并提供有关每个模型的基本信息,例如所有者和可用性。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY"

响应:

json
运行复制
  • {
  • "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"
  • }

2. Retrieve model 检索模型

http
运行复制
GET https://api.openai.com/v1/models/{model}

检索模型实例,提供有关模型的基本信息,例如所有者和权限。

其中,model 为必填的字符串类型,用于此请求的模型的 ID。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/models/text-davinci-003 \ -H "Authorization: Bearer $OPENAI_API_KEY"

响应:

json
运行复制
  • {
  • "id": "text-davinci-003",
  • "object": "model",
  • "owned_by": "openai",
  • "permission": [...]
  • }

五、 Completions 完成

给定一个提示,模型将返回一个或多个预测的完成,并且还可以在每个位置返回替代令牌的概率。

1. Create completion

http
运行复制
POST https://api.openai.com/v1/completions

为提供的提示和参数创建完成。

请求演示:

bash
运行复制
  • curl https://api.openai.com/v1/completions \
  • -H "Content-Type: application/json" \
  • -H "Authorization: Bearer $OPENAI_API_KEY" \
  • -d '{
  • "model": "text-davinci-003",
  • "prompt": "Say this is a test",
  • "max_tokens": 7,
  • "temperature": 0
  • }'

响应:

json
运行复制
  • {
  • "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
  • }
  • }

Request body(入参详解)


六、 Chat 聊天

给定一组描述对话的消息列表,模型将返回一个回复。

1. Create chat completion

http
运行复制
POST https://api.openai.com/v1/chat/completions

为给定的聊天对话创建模型响应。

请求演示:

bash
运行复制
  • 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!"}]
  • }'

响应:

json
运行复制
  • {
  • "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
  • }
  • }

Request body(入参详解)


七、 Edits 编辑

给定一个提示和一条指令,模型将返回提示的编辑版本。

1. Create edit

http
运行复制
POST https://api.openai.com/v1/edits

为提供的输入、指令和参数创建一个新的编辑。

请求演示:

bash
运行复制
  • curl https://api.openai.com/v1/edits \
  • -H "Content-Type: application/json" \
  • -H "Authorization: Bearer $OPENAI_API_KEY" \
  • -d '{
  • "model": "text-davinci-edit-001",
  • "input": "What day of the wek is it?",
  • "instruction": "Fix the spelling mistakes"
  • }'

响应:

json
运行复制
  • {
  • "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
  • }
  • }

Request body(入参详解)


八、 Images 图像

给定一个提示和/或输入图像,模型将生成一张新的图像。

相关指南:图像生成。

1. Create image

http
运行复制
POST https://api.openai.com/v1/images/generations

根据提示创建图像。

请求演示:

bash
运行复制
  • 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": 2,
  • "size": "1024x1024"
  • }'

响应:

json
运行复制
  • {
  • "created": 1589478378,
  • "data": [
  • {
  • "url": "https://..."
  • },
  • {
  • "url": "https://..."
  • }
  • ]
  • }

Request body(入参详解)

2. Create image edit

http
运行复制
POST https://api.openai.com/v1/images/edits

根据原始图像和提示创建编辑或扩展的图像。

请求演示:

bash
运行复制
  • curl https://api.openai.com/v1/images/edits \
  • -H "Authorization: Bearer $OPENAI_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"

响应:

json
运行复制
  • {
  • "created": 1589478378,
  • "data": [
  • {
  • "url": "https://..."
  • },
  • {
  • "url": "https://..."
  • }
  • ]
  • }

Request body(入参详解)

3. Create image variation

http
运行复制
POST https://api.openai.com/v1/images/variations

创建给定图像的变体。

请求演示:

bash
运行复制
  • curl https://api.openai.com/v1/images/variations \
  • -H "Authorization: Bearer $OPENAI_API_KEY" \
  • -F image="@otter.png" \
  • -F n=2 \
  • -F size="1024x1024"

响应:

json
运行复制
  • {
  • "created": 1589478378,
  • "data": [
  • {
  • "url": "https://..."
  • },
  • {
  • "url": "https://..."
  • }
  • ]
  • }

Request body(入参详解)


九、 Embeddings 嵌入

获得一个给定输入的向量表示,可以轻松地被机器学习模型和算法使用。

相关指南:嵌入

1. Create embeddings

http
运行复制
POST https://api.openai.com/v1/embeddings

创建一个嵌入向量,代表输入的文本。

请求演示:

bash
运行复制
  • curl https://api.openai.com/v1/embeddings \
  • -H "Authorization: Bearer $OPENAI_API_KEY" \
  • -H "Content-Type: application/json" \
  • -d '{
  • "input": "The food was delicious and the waiter...",
  • "model": "text-embedding-ada-002"
  • }'

响应:

json
运行复制
  • {
  • "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
  • }
  • }

Request body(入参详解)


十、 Audio 音频

了解如何将音频转换为文本。

相关指南:语音转文本

1. Create transcription

http
运行复制
POST https://api.openai.com/v1/audio/transcriptions

将音频转录为输入语言。

请求演示:

bash
运行复制
  • curl https://api.openai.com/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"

响应:

json
运行复制
{ "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." }

Request body(入参详解)

2. Create translation

http
运行复制
POST https://api.openai.com/v1/audio/translations

将音频翻译成英语。

请求演示:

bash
运行复制
  • curl https://api.openai.com/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"

响应:

json
运行复制
{ "text": "Hello, my name is Wolfgang and I come from Germany. Where are you heading today?" }

Request body(入参详解)


十一、 Files 文件

Files 用于上传文档,可与 Fine-tuning 等功能一起使用。

1. List files

http
运行复制
GET https://api.openai.com/v1/files

返回属于用户组织的文件列表。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/files \ -H "Authorization: Bearer $OPENAI_API_KEY"

响应:

json
运行复制
  • {
  • "data": [
  • {
  • "id": "file-ccdDZrC3iZVNiQVeEA6Z66wf",
  • "object": "file",
  • "bytes": 175,
  • "created_at": 1613677385,
  • "filename": "train.jsonl",
  • "purpose": "search"
  • },
  • {
  • "id": "file-XjGxS3KTG0uNmNOK362iJua3",
  • "object": "file",
  • "bytes": 140,
  • "created_at": 1613779121,
  • "filename": "puppy.jsonl",
  • "purpose": "search"
  • }
  • ],
  • "object": "list"
  • }

2. Upload file

http
运行复制
POST https://api.openai.com/v1/files

上传包含文档的文件以在各个端点/功能之间使用。目前,一个组织上传的所有文件的大小可以高达1 GB。如果您需要增加存储限制,请与我们联系。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/files \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -F purpose="fine-tune" \ -F file="@mydata.jsonl"

响应:

json
运行复制
  • {
  • "id": "file-XjGxS3KTG0uNmNOK362iJua3",
  • "object": "file",
  • "bytes": 140,
  • "created_at": 1613779121,
  • "filename": "mydata.jsonl",
  • "purpose": "fine-tune"
  • }

Request body(入参详解)

3. Delete file

http
运行复制
DELETE https://api.openai.com/v1/files/{file_id}

删除文件。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3 \ -X DELETE \ -H "Authorization: Bearer $OPENAI_API_KEY"

其中,{file_id} 为 string类型的必填项,用于此请求的文件的 ID。

响应:

json
运行复制
  • {
  • "id": "file-XjGxS3KTG0uNmNOK362iJua3",
  • "object": "file",
  • "deleted": true
  • }

4. Retrieve file

http
运行复制
GET https://api.openai.com/v1/files/{file_id}

返回有关特定文件的信息。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3 \ -H "Authorization: Bearer $OPENAI_API_KEY"

其中,{file_id} 为 string类型的必填项,用于此请求的文件的 ID。

响应:

json
运行复制
  • {
  • "id": "file-XjGxS3KTG0uNmNOK362iJua3",
  • "object": "file",
  • "bytes": 140,
  • "created_at": 1613779657,
  • "filename": "mydata.jsonl",
  • "purpose": "fine-tune"
  • }

5. Retrieve file content

http
运行复制
GET https://api.openai.com/v1/files/{file_id}/content

返回指定文件的内容。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3/content \ -H "Authorization: Bearer $OPENAI_API_KEY" > file.jsonl

其中,{file_id} 为 string类型的必填项,用于此请求的文件的 ID。


十二、 Fine-tunes 微调

管理微调作业以将模型定制为您的特定训练数据。

相关指南:Fine-tune models(微调模型)

1. Create fine-tune

http
运行复制
POST https://api.openai.com/v1/fine-tunes

创建一个工作,从给定的数据集中微调指定模型。

响应包括已入队的作业的详细信息,包括 作业状态 和 完成后微调模型的名称。

请求演示:

bash
运行复制
  • curl https://api.openai.com/v1/fine-tunes \
  • -H "Content-Type: application/json" \
  • -H "Authorization: Bearer $OPENAI_API_KEY" \
  • -d '{
  • "training_file": "file-XGinujblHPwGLSztz8cPS8XY"
  • }'

响应:

json
运行复制
  • {
  • "id": "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
  • "object": "fine-tune",
  • "model": "curie",
  • "created_at": 1614807352,
  • "events": [
  • {
  • "object": "fine-tune-event",
  • "created_at": 1614807352,
  • "level": "info",
  • "message": "Job enqueued. Waiting for jobs ahead to complete. Queue number: 0."
  • }
  • ],
  • "fine_tuned_model": null,
  • "hyperparams": {
  • "batch_size": 4,
  • "learning_rate_multiplier": 0.1,
  • "n_epochs": 4,
  • "prompt_loss_weight": 0.1
  • },
  • "organization_id": "org-...",
  • "result_files": [],
  • "status": "pending",
  • "validation_files": [],
  • "training_files": [
  • {
  • "id": "file-XGinujblHPwGLSztz8cPS8XY",
  • "object": "file",
  • "bytes": 1547276,
  • "created_at": 1610062281,
  • "filename": "my-data-train.jsonl",
  • "purpose": "fine-tune-train"
  • }
  • ],
  • "updated_at": 1614807352
  • }

Request body(入参详解)

2. List fine-tunes

http
运行复制
GET https://api.openai.com/v1/fine-tunes

列出组织的微调作业。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/fine-tunes \ -H "Authorization: Bearer $OPENAI_API_KEY"

响应:

json
运行复制
  • {
  • "object": "list",
  • "data": [
  • {
  • "id": "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
  • "object": "fine-tune",
  • "model": "curie",
  • "created_at": 1614807352,
  • "fine_tuned_model": null,
  • "hyperparams": { ... },
  • "organization_id": "org-...",
  • "result_files": [],
  • "status": "pending",
  • "validation_files": [],
  • "training_files": [ { ... } ],
  • "updated_at": 1614807352
  • }
  • ]
  • }

3. Retrieve fine-tune

http
运行复制
GET https://api.openai.com/v1/fine-tunes/{fine_tune_id}

获取有关微调作业的信息。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/fine-tunes/ft-AF1WoRqd3aJAHsqc9NY7iL8F \ -H "Authorization: Bearer $OPENAI_API_KEY"

其中,fine_tune_id 为 string类型 的字符串,必传;微调作业的 ID。

响应:略(结构同创建示例)

4. Cancel fine-tune

http
运行复制
POST https://api.openai.com/v1/fine-tunes/{fine_tune_id}/cancel

立即取消微调工作。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/fine-tunes/ft-AF1WoRqd3aJAHsqc9NY7iL8F/cancel \ -H "Authorization: Bearer $OPENAI_API_KEY"

5. List fine-tune events

http
运行复制
GET https://api.openai.com/v1/fine-tunes/{fine_tune_id}/events

获取微调作业的精细状态更新。

Query parameters

6. Delete fine-tune model

http
运行复制
DELETE https://api.openai.com/v1/models/{model}

删除微调的模型。您必须在组织中具有所有者的角色。

请求演示:

bash
运行复制
curl https://api.openai.com/v1/models/curie:ft-acmeco-2021-03-03-21-44-20 \ -X DELETE \ -H "Authorization: Bearer $OPENAI_API_KEY"

十三、 Moderations 审核

给定一个输入文本,输出模型是否将其分类为违反 OpenAI 内容政策。

相关指南:Moderations

1. Create moderation

http
运行复制
POST https://api.openai.com/v1/moderations

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

请求演示:

bash
运行复制
  • curl https://api.openai.com/v1/moderations \
  • -H "Content-Type: application/json" \
  • -H "Authorization: Bearer $OPENAI_API_KEY" \
  • -d '{
  • "input": "I want to kill them."
  • }'

响应:

json
运行复制
  • {
  • "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
  • }
  • ]
  • }

Request body(入参详解)


十四、 Engines 引擎

引擎端点已过时。
请使用它们的替代品模型。

1. List engines <荒废的>

http
运行复制
GET https://api.openai.com/v1/engines

2. Retrieve engine <荒废的>

http
运行复制
GET https://api.openai.com/v1/engines/{engine_id}

十五、 Parameter details 参数细节

频率和存在惩罚

在 Completions API 中发现的频率和存在惩罚可以用于减少采样重复令牌序列的可能性。它们通过直接向 logits(未归一化的对数概率)添加贡献来进行修改。

python
运行复制
mu[j] -> mu[j] - c[j] * alpha_frequency - float(c[j] > 0) * alpha_presence

Where:

正如我们所看到的,存在惩罚是一次性的加法贡献,适用于所有已经被采样至少一次的标记,并且频率惩罚是与特定标记已经被采样的频率成比例的贡献。

如果目的只是稍微减少重复样本,那么惩罚系数的合理值大约在 0.1 到 1 之间。如果目的是强烈抑制重复,则可以将系数增加到 2,但这可能会明显降低样本质量。负值可用于增加重复出现的可能性。