创建文本嵌入(OpenAI)
POST v1/embeddings获取给定输入的矢量表示,机器学习模型和算法可以轻松使用该表示。
相关指南:嵌入
创建表示输入文本的嵌入向量。
请求参数
Authorization
在 Header 添加参数 Authorization,其值为在 Bearer 之后拼接 Token。
Authorization: Bearer ******************Body 参数
Content-Type: application/json
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| input | string | 必需 | 输入内容。这是需要被处理的核心文本数据。在 Embedding 接口中,这里填入需要向量化的句子或段落。 |
| model | string | 必需 | 模型标识。指定使用哪个具体的模型版本来处理请求(例如 text-embedding-3-small 或 cl100k_base)。 |
| encoding_format | string | 必需 | 编码/输出格式。指定返回数据的格式。常见的取值通常是 float(返回浮点数数组)或 base64(返回压缩后的字符串),用于平衡精度与传输体积。 |
请求示例 (JSON)
{
"input": "The food was delicious and the waiter...",
"model": "text-embedding-3-large",
"encoding_format": "float"
}返回响应
200 成功
Content-Type: application/json
响应参数
| 参数路径 | 类型 | 必填 | 说明 |
|---|---|---|---|
| object | string | 必需 | 根对象类型。通常用于标识整个响应体的类型,例如 "list" 或 "response"。 |
| data | array[object] | 必需 | 核心数据列表。包含具体的处理结果(如向量数据),因为一次请求可能包含多个输入,所以是数组。 |
| └─ object | string | 可选 | 数据对象类型。标识该具体条目的类型,通常为 "embedding"。 |
| └─ embedding | array[integer] | 可选 | 向量/编码数据。这是核心内容,即文本转换后的数字数组(向量)。注:此处为整数数组,可能是量化后的向量或 Token IDs。 |
| └─ index | integer | 可选 | 索引。对应请求中 input 数组的下标,用于区分哪个向量对应哪段输入文本。 |
| model | string | 必需 | 模型名称。实际用于处理任务的模型版本(例如 "text-embedding-3-small")。 |
| usage | object | 必需 | 用量统计。包含 Token 消耗信息,用于计费或监控。 |
| └─ prompt_tokens | integer | 必需 | 提示词 Token 数。输入文本所消耗的 Token 数量。 |
| └─ total_tokens | integer | 必需 | 总 Token 数。本次调用消耗的总 Token 数量。 |
响应示例
{
"object": "string",
"data": [
{
"object": "string",
"embedding": [
0
],
"index": 0
}
],
"model": "string",
"usage": {
"prompt_tokens": 0,
"total_tokens": 0
}
}示例代码
python
import http.client
import json
conn = http.client.HTTPSConnection("nxaiapp.com")
payload = json.dumps({
"input": "The food was delicious and the waiter...",
"model": "text-embedding-3-large",
"encoding_format": "float"
})
headers = {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
}
conn.request("POST", "/v1/embeddings", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))