← 返回列表

RAG 检索增强生成最佳实践

整体架构

  1. 用 openai/text-embedding-3-large 计算文档与 query 向量
  2. 向量库 (FAISS / Milvus / pgvector) 召回 Top-K
  3. 用 cohere/rerank-english-v3 进行精排
  4. 把 Top-N chunk 拼到 prompt,交给 cohere/command-r-plus 或 Claude 生成答案

Embedding

python
resp = client.embeddings.create(
    model="openai/text-embedding-3-large",
    input=[chunk.text for chunk in chunks],
)
vectors = [d.embedding for d in resp.data]

Prompt 模板

text
You are an expert assistant. Answer based ONLY on context.
If the answer is not in the context, say "I don't know".

[Context]
{retrieved_chunks}

[Question]
{user_question}