Skip to content
开发指南

向量化与重排 (RAG) ​

将稠密文本向量化(/v1/embeddings)与二阶段交叉编码器重排(/v1/rerank)组合使用,在保持低延迟的同时大幅提升检索增强生成(RAG)的召回精准度。

稠密文本向量化(Embeddings) ​

POSThttps://api.vanix.io/v1/embeddings
bash
curl https://api.vanix.io/v1/embeddings \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VANIX_API_KEY" \
  -d '{
    "model": "text-embedding-3-small",
    "input": [
      "Vanix AI supports multi-AZ auto-scaling on AWS ECS Fargate.",
      "Prompt caching cuts repeated system prompt costs by up to 90%."
    ]
  }'
python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["VANIX_API_KEY"],
    base_url="https://api.vanix.io/v1",
)

resp = client.embeddings.create(
    model="text-embedding-3-small",
    input=["Vanix AI gateway", "Unified LLM billing"],
    dimensions=1024,
)
print("Vector length:", len(resp.data[0].embedding))

二阶段语义重排(Rerank) ​

POSThttps://api.vanix.io/v1/rerank

先从向量数据库(pgvector、Qdrant、Pinecone、OpenSearch)初筛召回 Top 30–50 候选片段,再通过重排接口精选出相关性最高的 Top 5–8 片段送入大语言模型:

bash
curl https://api.vanix.io/v1/rerank \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VANIX_API_KEY" \
  -d '{
    "model": "rerank-v3.5",
    "query": "How do I enable prompt caching for Claude?",
    "top_n": 3,
    "documents": [
      "Add cache_control ephemeral breakpoints to Anthropic Messages system blocks.",
      "Configure ECS Fargate tasks with ARM64 Graviton images.",
      "Use GET /v1/models to list enabled models for your API token."
    ]
  }'

RAG 精度一致性原则

文档建库与在线查询必须使用完全相同的向量模型与 dimensions 维度配置。混用不同模型或维度会导致余弦相似度计算失真。

Vanix AI 统一模型网关 · https://api.vanix.io