
深度解析:臨床試驗數據庫CT.gov與API接口指南
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響應的唯一表示
"id": "chatcmpl-123",
// 表示接口類型
"object": "chat.completion",
// 創建的unix時間戳
"created": 1677652288,
// 模型生成的內容
"choices": [{
// 序號
"index": 0,
// 生成內容
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?",
},
/*
停止生成原因:
stop,正常結束或者遇到停止序列而停止
length,生成內容達到了最大長度限制
function_call,需要調用函數而停止
content_filter,內容被過濾而停止
*/
"finish_reason": "stop"
}],
// 統計數據
"usage": {
// 傳入數據消耗的token數
"prompt_tokens": 9,
// GPT生成內容的Token數
"completion_tokens": 12,
// 所有的Token數
"total_tokens": 21
}
}
Python:
Demo 地址:python-gpt-course/course/openai_api/chat.py
運行示例:python course openai_api chat
Golang:
Demo 地址:golang-gpt-course/internal/openai_api/chat.go
運行示例:./start openai_api chat
POST
https://api.openai.com/v1/completions
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
}
}
Python:
Demo 地址:python-gpt-course/course/openai_api/completion.py
運行示例:python course openai_api completion
Golang:
Demo 地址:golang-gpt-course/internal/openai_api/completion.go
運行示例:./start openai_api completion
這類是用于構建私有化模型的相關接口。私有化模型構建分為兩種方式:Embeddings、Fine-tunes。兩個模型這里簡單的介紹下,:
Embeddings,以改變上下文的方式來打造私有化模型,該方式不對 GPT 本身的模型進行調整。
Fine-tunes,基于 GPT 的模型再進行額外的訓練,會對 GPT 模型本身的參數進行微調。Fine-tunes 對模型訓練的專業技能要求更高
PS:在后續的課程中還會詳細講解這兩種方式,所以你可以簡單看下相關接口,也可以等到學習私有化模型構建的時候再回過頭來看
POST
https://api.openai.com/v1/embeddings
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",
// 向量化數據
"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
}
}
Python:
Demo 地址:python-gpt-course/course/openai_api/embedding.py
運行示例:python course openai_api embedding
Golang:
Demo 地址:golang-gpt-course/internal/openai_api/embedding.go
運行示例:./start openai_api embedding
由于 Fine-tunings 的接口比較多,這里只會詳細介紹創建 Fine-tunning 的接口,其他會簡要介紹。大多數場景下,都會“離線”完成模型訓練,然后“在線”實時調用訓練后的模型。而“離線”訓練更多的是借助 OpenAI 的官方工具,具體的在后續課程中我們會進一步講解。
另外,Fine-tunings 的使用需要有一定的模型訓練的技術基礎以及經驗要求,比如你要懂:訓練集、測試集、驗證集、損失函數、梯度下降等等。其實對于大多數場景來說,Embeddings 的方式會更加容易落地。
POST
https://api.openai.com/v1/fine-tunes
Request
curl "https://api.openai.com/v1/fine-tunes" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
// 訓練集的FileID
"training_file": "file-XGinujblHPwGLSztz8cPS8XY"
}'
Response
{
// 訓練模型的ID
"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,
}
獲取 Fine-tunes 訓練的模型列表
GET
https://api.openai.com/v1/fine-tunes
獲取某一個模型的詳細信息
GET
https://api.openai.com/v1/fine-tunes/{fine_tune_id}
取消正在訓練的模型
POST
https://api.openai.com/v1/fine-tunes/{fine_tune_id}/cancel
獲取某一個模型訓練過程中的事件
GET
https://api.openai.com/v1/fine-tunes/{fine_tune_id}/events
刪除一個模型
DELETE
https://api.openai.com/v1/models/{model}
Models(獲取 GPT 可用模型列表)
請求地址
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",
// 權限
"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.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": [...]
}
主要用于 Fine-tunings 的訓練數據集、驗證數據集的上傳
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"
}
獲取文件列表
GET
https://api.openai.com/v1/files
獲取單個文件描述信息
GET
https://api.openai.com/v1/files/{file_id}
獲取單個文件內容
GET
https://api.openai.com/v1/files/{file_id}/content
文字描述圖,GPT 來生成
POST
https://api.openai.com/v1/images/generations
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://..."
}
]
}
上傳圖片,并描述修改方式,GPT 生成新圖
POST
https://api.openai.com/v1/images/edits
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://..."
}
]
}
POST
https://api.openai.com/v1/images/variations
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://..."
}
]
}
將語音轉為文字
POST
https://api.openai.com/v1/audio/transcriptions
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."
}
本文章轉載微信公眾號@AI老張