
深度解析:臨床試驗(yàn)數(shù)據(jù)庫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響應(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
}
}
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
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
運(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)建私有化模型的相關(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í)候再回過頭來看
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",
// 向量化數(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
}
}
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 的接口比較多,這里只會(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ì)更加容易落地。
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 '{
// 訓(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,
}
獲取 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 可用模型列表)
請(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"
}
請(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": [...]
}
主要用于 Fine-tunings 的訓(xùn)練數(shù)據(jù)集、驗(yàn)證數(shù)據(jù)集的上傳
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
獲取單個(gè)文件描述信息
GET
https://api.openai.com/v1/files/{file_id}
獲取單個(gè)文件內(nèi)容
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://..."
}
]
}
將語音轉(zhuǎn)為文字
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."
}
本文章轉(zhuǎn)載微信公眾號(hào)@AI老張
深度解析:臨床試驗(yàn)數(shù)據(jù)庫CT.gov與API接口指南
2024年您產(chǎn)品必備的10大AI API推薦
GraphRAG:基于PolarDB+通義千問api+LangChain的知識(shí)圖譜定制實(shí)踐
使用Node.js、Express和MySQL構(gòu)建REST API
天氣API推薦:精準(zhǔn)獲取氣象數(shù)據(jù)的首選
基于自定義數(shù)據(jù)集的微調(diào):Alpaca與LLaMA模型的訓(xùn)練
OAuth和OpenID Connect圖解指南
有哪些新聞媒體提供Open API?
現(xiàn)在做大模型,還有靠譜且免費(fèi)的API接口嗎?
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)