3. 返回參數(shù)

Request:

curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
// 這里要替換成你的API Key
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"}]
}'

Response:

{
// GPT響應(yīng)的唯一表示
"id": "chatcmpl-123",
// 表示接口類型
"object": "chat.completion",
// 創(chuàng)建的unix時(shí)間戳
"created": 1677652288,
// 模型生成的內(nèi)容
"choices": [{
// 序號(hào)
"index": 0,
// 生成內(nèi)容
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?",
},
/*
停止生成原因:
stop,正常結(jié)束或者遇到停止序列而停止
length,生成內(nèi)容達(dá)到了最大長(zhǎng)度限制
function_call,需要調(diào)用函數(shù)而停止
content_filter,內(nèi)容被過濾而停止
*/
"finish_reason": "stop"
}],
// 統(tǒng)計(jì)數(shù)據(jù)
"usage": {
// 傳入數(shù)據(jù)消耗的token數(shù)
"prompt_tokens": 9,
// GPT生成內(nèi)容的Token數(shù)
"completion_tokens": 12,
// 所有的Token數(shù)
"total_tokens": 21
}
}

4. 代碼示例

Python:

Demo 地址:python-gpt-course/course/openai_api/chat.py

運(yùn)行示例:python course openai_api chat

Golang:

Demo 地址:golang-gpt-course/internal/openai_api/chat.go

運(yùn)行示例:./start openai_api chat

Completions(單輪對(duì)話)

1. 請(qǐng)求地址

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

2. 請(qǐng)求參數(shù)

3. 返回參數(shù)

Request:

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
}'

Reponse:

{
"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
}
}

4. 代碼示例

Python:

Demo 地址:python-gpt-course/course/openai_api/completion.py

運(yùn)行示例:python course openai_api completion

Golang:

Demo 地址:golang-gpt-course/internal/openai_api/completion.go

運(yùn)行示例:./start openai_api completion

二、私有化模型構(gòu)建類

這類是用于構(gòu)建私有化模型的相關(guān)接口。私有化模型構(gòu)建分為兩種方式:Embeddings、Fine-tunes。兩個(gè)模型這里簡(jiǎn)單的介紹下,:

Embeddings,以改變上下文的方式來打造私有化模型,該方式不對(duì) GPT 本身的模型進(jìn)行調(diào)整。

Fine-tunes,基于 GPT 的模型再進(jìn)行額外的訓(xùn)練,會(huì)對(duì) GPT 模型本身的參數(shù)進(jìn)行微調(diào)。Fine-tunes 對(duì)模型訓(xùn)練的專業(yè)技能要求更高

PS:在后續(xù)的課程中還會(huì)詳細(xì)講解這兩種方式,所以你可以簡(jiǎn)單看下相關(guān)接口,也可以等到學(xué)習(xí)私有化模型構(gòu)建的時(shí)候再回過頭來看

Embeddings

1. 請(qǐng)求地址

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

2. 請(qǐng)求參數(shù)

3. 返回參數(shù)

Request

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"
}

Response

{
"object": "list",
"data": [
{
"object": "embedding",
// 向量化數(shù)據(jù)
"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
}
}

4. 代碼示例

Python:

Demo 地址:python-gpt-course/course/openai_api/embedding.py

運(yùn)行示例:python course openai_api embedding

Golang:

Demo 地址:golang-gpt-course/internal/openai_api/embedding.go

運(yùn)行示例:./start openai_api embedding

Fine-tunings

由于 Fine-tunings 的接口比較多,這里只會(huì)詳細(xì)介紹創(chuàng)建 Fine-tunning 的接口,其他會(huì)簡(jiǎn)要介紹。大多數(shù)場(chǎng)景下,都會(huì)“離線”完成模型訓(xùn)練,然后“在線”實(shí)時(shí)調(diào)用訓(xùn)練后的模型。而“離線”訓(xùn)練更多的是借助 OpenAI 的官方工具,具體的在后續(xù)課程中我們會(huì)進(jìn)一步講解。

另外,F(xiàn)ine-tunings 的使用需要有一定的模型訓(xùn)練的技術(shù)基礎(chǔ)以及經(jīng)驗(yàn)要求,比如你要懂:訓(xùn)練集、測(cè)試集、驗(yàn)證集、損失函數(shù)、梯度下降等等。其實(shí)對(duì)于大多數(shù)場(chǎng)景來說,Embeddings 的方式會(huì)更加容易落地。

1. 請(qǐng)求地址

POST https://api.openai.com/v1/fine-tunes

2. 請(qǐng)求參數(shù)

3. 返回參數(shù)

Request

curl "https://api.openai.com/v1/fine-tunes" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
// 訓(xùn)練集的FileID
"training_file": "file-XGinujblHPwGLSztz8cPS8XY"
}'

Response

{
// 訓(xùn)練模型的ID
"id": "ft-AF1WoRqd3aJAHsqc9NY7iL8F",
"object": "fine-tune",
"model": "curie",
"created_at": 1614807352,
// 模型訓(xùn)練過程中的一些事件
"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,
}

4. 其他接口

獲取 Fine-tunes 訓(xùn)練的模型列表

GET https://api.openai.com/v1/fine-tunes

獲取某一個(gè)模型的詳細(xì)信息

GET https://api.openai.com/v1/fine-tunes/{fine_tune_id}

取消正在訓(xùn)練的模型

POST https://api.openai.com/v1/fine-tunes/{fine_tune_id}/cancel

獲取某一個(gè)模型訓(xùn)練過程中的事件

GET https://api.openai.com/v1/fine-tunes/{fine_tune_id}/events

刪除一個(gè)模型

DELETE https://api.openai.com/v1/models/{model}

三、通用類

Models(獲取 GPT 可用模型列表)

1. 獲取 Models 列表

請(qǐng)求地址

GET https://api.openai.com/v1/models

Request

curl "https://api.openai.com/v1/models" \
-H "Authorization: Bearer $OPENAI_API_KEY"

Repsonse

{
"data": [
{
// GPT模型ID
"id": "model-id-0",
"object": "model",
"owned_by": "organization-owner",
// 權(quán)限
"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. 獲取 Models 詳情

請(qǐng)求地址

GET https://api.openai.com/v1/models/{model}

Request

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

Response

{
"id": "text-davinci-003",
"object": "model",
"owned_by": "openai",
"permission": [...]
}

Files(上傳文件到 GPT)

主要用于 Fine-tunings 的訓(xùn)練數(shù)據(jù)集、驗(yàn)證數(shù)據(jù)集的上傳

1. 上傳文件

Request

curl "https://api.openai.com/v1/files" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
// 文件用途
-F purpose="fine-tune" \
-F file="@mydata.jsonl"

Response

{
"id": "file-XjGxS3KTG0uNmNOK362iJua3",
"object": "file",
"bytes": 140,
"created_at": 1613779121,
"filename": "mydata.jsonl",
"purpose": "fine-tune"
}

2. 其他接口

獲取文件列表

GET https://api.openai.com/v1/files

獲取單個(gè)文件描述信息

GET https://api.openai.com/v1/files/{file_id}

獲取單個(gè)文件內(nèi)容

GET https://api.openai.com/v1/files/{file_id}/content

四、圖片 & 音頻 類

Images

1. 文生圖

文字描述圖,GPT 來生成

1. 請(qǐng)求地址

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

2. 請(qǐng)求參數(shù)

3. 返回參數(shù)

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": 2,
"size": "1024x1024"
}'

Response

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

2. 編輯圖

上傳圖片,并描述修改方式,GPT 生成新圖

1. 請(qǐng)求地址

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

2. 請(qǐng)求參數(shù)

3. 返回參數(shù)

Request

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"

Response

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

3. 圖變體

4. 請(qǐng)求地址

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

2. 請(qǐng)求參數(shù)

3. 返回參數(shù)

Request

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

Response

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

Audio(語音轉(zhuǎn)文字)

將語音轉(zhuǎn)為文字

1. 請(qǐng)求地址

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

2. 請(qǐng)求參數(shù)

3. 返回參數(shù)

Request

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"

Response

{
"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."
}

本文章轉(zhuǎn)載微信公眾號(hào)@AI老張

上一篇:

開放數(shù)據(jù)和API接口采用研究——基于美國(guó)銀行業(yè)的分析

下一篇:

接口大師v3.10.0 零代碼/低代碼,把內(nèi)部接口變成OpenAPI開放接口的妙招
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)