Embeddings文本向量化API
更新時間 2025-09-09 11:44:03
最近更新時間: 2025-09-09 11:44:03
分享文章
本文是關于Embeddings文本向量化API的詳情描述。
接口描述
| - | 描述 |
|---|---|
| 接口名稱 | 嵌入 |
| 請求路徑 | //wishub-x5.daliqc.cn/api/v1/c760bd1e83/d1a696ae54419d34987d95d52ac337f8(僅示例,以服務詳情頁的接口地址為準) |
| 功能描述 | 創建表示輸入文本的嵌入向量 |
請求參數
請求頭參數
| 參數 | 示例值 | 描述 |
|---|---|---|
| Authorization | Bearer 123456789 | 鑒權信息填入AppKey |
| Content-Type | application/json | - |
請求參數
| 參數名稱 | 參數類型 | 必選 | 描述 |
|---|---|---|---|
| model | string | 是 | 模型ID。 |
| input | object | 是 | 字符串或數組類型,表示輸入的文本,編碼為字符串或令牌數組(整型數組、字符串數組、整型數組的數組)。要在單個請求中為多個輸入獲取嵌入,請傳遞字符串數組或令牌數組的數組。每個輸入的長度不得超過 8192 個令牌。 |
| encoding_format | string | 否 | 編碼格式,float 或 base64,默認為float。 |
| dimensions | int | 否 | 輸出的embeddings維度,僅支持text-embedding-3及其高版本。 |
| user | string | 否 | 用戶唯一身份ID。 |
請求返回
請求正常返回
| 字段名稱 | 二級字段 | 三級字段 | 字段類型 | 描述 |
|---|---|---|---|---|
| model | string | 調用的模型名稱。 | ||
| object | string | 返回的對象類型,默認為list | ||
| data | array | embedding 列表 | ||
| - | index | int | index索引 | |
| - | embeddings | array/string | embedding嵌入向量的float數組 或 base64 字符串 | |
| - | object | string | 默認為embedding | |
| usage | object | 請求使用情況的統計信息。 | ||
| - | prompt_tokens | int | 輸入token數。 | |
| - | total_tokens | int | 使用的token總數。 |
異常返回
異常返回時:
http code 返回非200。
http body 中返回 error 結構,error結構中包含code、type、message、param等信息,具體可見OpenAPI接口文檔中的error結構描述及錯誤碼部分介紹。
錯誤結果示例
{
"error" : {
"code" : "500001",
"type" : "INVOKE_MODEL_ERROR",
"message" : "服務接口異常,請聯系管理員"
}
}請求示例代碼
假設慧聚平臺用戶組AppKey=884c8fc4054548a7b1ca1123592f5b7,模型ID=96dcaaaaaaaaaaaa5ff55ea377831a,以此為例進行說明。curl方式請求
curl --request POST \
--url //wishub-x5.daliqc.cn/api/v1/c760bd1e83/d1a696ae54419d34987d95d52ac337f8/v1/embeddings \
--header 'Accept: */*' \
--header 'Accept-Encoding: gzip, deflate, br' \
--header 'Authorization: Bearer 884c8fc4054548a7b1ca1123592f5b7' \
--header 'Content-Type: application/json' \
--data '{
"model": "4b38a36cabe247c8a996206984c9e2a61",
"input" : "A cute baby sea otter"
}'python方式請求
import json
import requests
URL = "//wishub-x5.daliqc.cn/api/v1/c760bd1e83/d1a696ae54419d34987d95d52ac337f8/v1/embeddings"
headers = {
"Authorization": "Bearer 884c8fc4054548a7b1ca1123592f5b7",
"Content-Type": "application/json"
}
data = {
"model": "4b38a36cabe247c8a996206984c9e2a61",
"input" : "A cute baby sea otter"
}
try:
response = requests.post(URL, headers=headers, json=data)
if response.status_code != 200:
print(response.json())
else:
embeddings = response.json()["data"][0]["embeddings"]
except Exception as e:
print(f"Exception: {e}")openai 客戶端示例代碼
import openai
from openai import OpenAI
client = OpenAI(base_url="//wishub-x5.daliqc.cn/api/v1/c760bd1e83/d1a696ae54419d34987d95d52ac337f8", api_key="884c8fc4054548a7b1ca1123592f5b7")
try:
response = client.embeddings(
model="4b38a36cabe247c8a996206984c9e2a61",
input="A cute baby sea otter",
)
print(f"{response}")
except openai.APIStatusError as e:
print(f"APIStatusError: {e.status_code}, {e.message}, {e.body}")
except openai.APIError as e:
print(f"APIError: {e.body}")
except Exception as e:
print(f"Exception: {e}")