Docs

模型 API

Embedding 与 Rerank 检索

使用向量嵌入和文档重排构建 RAG,理解维度、输入、排名和模型能力要求。

更新于

POST/v1/embeddings
POST/v1/rerank

生成文本向量

POST /v1/embeddings 接受 model 和 input,input 可以是字符串或字符串数组。返回的 data[].embedding 是向量,data[].index 对应输入顺序。需要可用的嵌入模型和 embeddings.create 权限;聊天模型不能自动作为嵌入模型使用。

cURL
curl --fail-with-body https://api.token4ai.cloud/v1/embeddings \
  -H "Authorization: Bearer $TOKEN4AI_API_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "model": "YOUR_MODEL_ID",
  "input": [
    "A unified model gateway",
    "Cloud agent task execution"
  ]
}'

维度与索引一致性

同一个向量索引应保持相同嵌入模型、维度和归一化约定。切换模型后通常需要重建文档向量,不能直接把新查询向量与旧索引混用。对长文档先切分,保存段落与原文来源,避免向量结果失去引用依据。

候选文档重排

POST /v1/rerank 对已有候选文档排序,不代替文档检索或向量存储。query 必须非空,documents 必须是非空数组;文档可以是字符串或包含 text 的对象。top_n 是正整数,return_documents 可请求返回文档文本。

cURL
curl --fail-with-body https://api.token4ai.cloud/v1/rerank \
  -H "Authorization: Bearer $TOKEN4AI_API_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "model": "YOUR_MODEL_ID",
  "query": "How can I run an agent in the cloud?",
  "documents": [
    "Configure a cloud agent in Vega.",
    "Embedding vectors support semantic search."
  ],
  "top_n": 1,
  "return_documents": true
}'
重排需要可用的 rerank 模型。把返回索引映射回候选文档,并保留来源后再传给生成模型。

继续阅读