跳到主要内容
版本:Next

实战配方:向量 RAG 流水线

目标。 构建检索增强流水线的两半:把文档摄入向量库,再通过检索最近邻段落 并交给 LLM 生成来回答问题。

前置条件。

  • 一个 OpenAI 兼容的 embedding endpoint(api_base,例如本地 vLLM、Ollama 或 OpenAI),API key 通过 ${env:EMBEDDING_API_KEY} 提供。
  • 一个已运行、可达的向量库:
    • Qdrant——REST 端口 6333,
    • 或带 pgvector 的 PostgreSQL,
    • 或 Milvus。
  • 集合/表必须已存在,且向量维度与 embedding 模型一致(例如 text-embedding-3-small 为 1536)。

前半:摄入(文档 → 向量)​

对文本列做 embedding,并把向量加 payload upsert 到 Qdrant:

- type: json_to_arrow
- type: embedding
api_base: "http://localhost:9997/v1"
model: "text-embedding-3-small"
api_key: "${env:EMBEDDING_API_KEY:-}"
field: "text"
batch_size: 32
output:
type: qdrant
url: "http://localhost:6333"
collection: documents
vector_field: embedding
id_field: doc_id

Point 语义:

  • 设置 id_field 后,重投会覆盖同一个 point——至少一次的流水线在重跑时 是幂等的。
  • 不设置 id_field 时,每行都会生成新的 UUID;每次重投都会插入重复。 流式摄入请务必设置 id 列。
  • 每个 PUT 切片最多 1,000 个 point;更大的批次按序拆分。
  • 向量维度必须与集合一致;不匹配会以 4xx 失败并进入 error_output。

pgvector 使用 pgvector 输出, 写入 (id, embedding vector(n), payload jsonb)——与 pgvector_search processor 读取的表形状一致,因此摄入与检索共用一张表。Milvus 使用 Milvus 输出。

完整可运行的摄入配置见 examples/embedding_qdrant_pipeline.yaml (登记为 embedding-qdrant-pipeline;pgvector 与 Milvus 变体也已登记)。

后半:查询(问题 → 检索 → 回答)​

对问题做 embedding,检索 top-k,组装提示词并生成:

- type: json_to_arrow
- type: embedding
api_base: "http://localhost:9997/v1"
model: "text-embedding-3-small"
api_key: "${env:EMBEDDING_API_KEY:-}"
field: "text"
- type: vector_search
url: "http://localhost:6333"
collection: documents
vector_field: embedding
top_k: 5
target_field: matches
- type: sql
query: "SELECT *, concat('Question: ', text, ' | Passages: ', matches) AS rag_prompt FROM flow"
- type: llm
api_base: "http://localhost:9997/v1"
model: "gpt-4o-mini"
api_key: "${env:OPENAI_API_KEY:-}"
field: "rag_prompt"
system_prompt: "Answer the user's question using only the provided passages."
target_field: "answer"
- type: arrow_to_json

预期结果:每个输入行新增 matches(JSON 数组,元素为 {id, score, payload}, 按相似度降序)与 answer(生成的文本)。完整可运行的查询配置见 examples/rag_query_pipeline.yaml (登记为 rag-query-pipeline)。

验证​

  1. 启动 Qdrant 与 embedding endpoint;按正确维度创建集合。
  2. 用小文档集跑摄入配置,然后检查 GET /collections/documents 的 point 数。
  3. 用一个问题跑查询配置;确认 stdout 批次带非空 matches、分数合理,且 answer 基于检索内容。

失败模式​

症状原因修复
维度不匹配 4xx模型与集合维度不同重建集合或更换模型。
matches 为空集合为空或 vector_field 错误先跑摄入;检查列名。
llm/embedding 429速率限制提高 retry_count、降低 batch_size,或在检索 processor 上调 concurrency。
批次进入 error_output重试耗尽 / 输入文本为空检查 DLQ;参见死信队列。

相关页面​