接口描述
- | 描述 |
接口名称 | 嵌入 |
请求路径 | https://wishub-x1.ctyun.cn/v1/embeddings |
功能描述 | 创建表示输入文本的嵌入向量 |
请求参数
请求头参数
参数 | 示例值 | 描述 |
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总数。 |
异常返回
异常返回时:
错误结果示例
{
"error" : {
"code" : "500001",
"type" : "INVOKE_MODEL_ERROR",
"message" : "服务接口异常,请联系管理员"
}
}
请求示例代码
假设慧聚平台用户组AppKey=884c8fc4054548a7b1ca1123592f5b7,模型ID=96dcaaaaaaaaaaaa5ff55ea377831a,以此为例进行说明。
curl方式请求
curl --request POST \
--url https://wishub-x1.ctyun.cn/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 = "https://wishub-x1.ctyun.cn/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="https://wishub-x1.ctyun.cn/v1", 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}")