API 參考
OpenAI Embeddings
透過 RunAPI 相容 OpenAI 的 Embeddings 操作傳送請求。
01
概覽
使用 RunAPI 的 OpenAI 相容「Embeddings」操作,搭配相容的模型及該協定的請求與回應格式。
快速入門
- 建立 API 金鑰並將其設定為 RUNAPI_API_KEY。
- 選擇相容的模型,然後傳送文件中指定的路徑參數與 JSON 本文。
- 讀取此操作顯示的 JSON 回應或伺服器傳送事件,並處理協定錯誤。
端點
- 基礎 URL
https://runapi.ai- 合約版本
v1- 建議的身分驗證方式
Authorization: Bearer YOUR_API_TOKEN
02
身分驗證載體
RunAPI 接受以下列出的每種傳輸協定。產生的範例使用該協定偏好的標頭。
- header - 建議
Authorization: Bearer YOUR_API_TOKEN- header
x-api-key: YOUR_API_TOKEN- header
x-goog-api-key: YOUR_API_TOKEN- query
?key=YOUR_API_TOKEN
03
相容模型
開啟模型頁面以查看目前的定價、速率限制及商業用途詳情。
04
JSON 請求內文請求合約
僅列出同時具備公開協定與 RunAPI 支援依據的欄位。其他 JSON 本文欄位將直接傳遞。
JSON 主體4 個欄位
dimensionsinteger
選填
支援此功能之模型所請求的輸出向量維度。
encoding_formatstring
選填
回傳的嵌入向量所使用的格式。
input["string", "array"]
必填
要嵌入的文字或 token 陣列。
modelstring
必填
RunAPI 嵌入模型識別碼。
請求範例
JSON
{
"input": "The quick brown fox.",
"model": "text-embedding-3-small"
}
05
HTTP 200
同步回應
結構描述
JSON
{
"additionalProperties": true,
"properties": {
"data": {
"type": "array"
},
"model": {
"type": "string"
},
"object": {
"const": "list",
"type": "string"
},
"usage": {
"additionalProperties": true,
"properties": {
"prompt_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
},
"required": [
"prompt_tokens",
"total_tokens"
],
"type": "object"
}
},
"required": [
"object",
"data",
"model",
"usage"
],
"type": "object"
}
範例
JSON
{
"data": [
{
"embedding": [
0.0023,
-0.0093,
0.0151
],
"index": 0,
"object": "embedding"
}
],
"model": "text-embedding-3-small",
"object": "list",
"usage": {
"prompt_tokens": 5,
"total_tokens": 5
}
}
06
HTTP 400
協定錯誤:invalid_request
結構描述
JSON
{
"additionalProperties": true,
"properties": {
"error": {
"additionalProperties": true,
"properties": {
"code": {
"type": [
"string",
"null"
]
},
"message": {
"type": "string"
},
"param": {
"type": [
"string",
"null"
]
},
"type": {
"type": "string"
}
},
"required": [
"message",
"type"
],
"type": "object"
}
},
"required": [
"error"
],
"type": "object"
}
範例
JSON
{
"error": {
"code": null,
"message": "input is required",
"param": "input",
"type": "invalid_request_error"
}
}
07
用量
結構描述
JSON
{
"additionalProperties": true,
"properties": {
"prompt_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
},
"required": [
"prompt_tokens",
"total_tokens"
],
"type": "object"
}
範例
JSON
{
"prompt_tokens": 5,
"total_tokens": 5
}
08
產生的程式碼範例
每個 cURL、JavaScript 和 Python 範例均會從此契約傳送經驗證的請求,無需特定協定的 SDK。
安裝
Shell
pip install requests
PYTHON
import requests
response = requests.post(
"https://runapi.ai/v1/embeddings",
headers={"Authorization": "Bearer YOUR_API_TOKEN", "Content-Type": "application/json"},
json={"model": "text-embedding-3-small", "input": "The quick brown fox."}
)
response.raise_for_status()
print(response.json())