Reference · API
RAG retrieval pattern
Last updated
Standard **embed → search → pack → generate** loop for question answering over your docs.
Standard **embed → search → pack → generate** loop for question answering over your docs.
Pipeline
Documents → chunk → embed → vector store
Question → embed → top-k similarity → prompt context → LLM → answer + citesIndex time (once per doc update)
| Step | Action |
|------|--------|
| Chunk | Split text (~300–800 tokens, overlap ~10–20%) |
| Embed | Same model as query time |
| Store | vector + raw text + metadata (source, id) |
Query time
| Step | Action |
|------|--------|
| Embed query | Same model/dimensions as index |
| Retrieve | Cosine similarity or vector DB search, k = 3–8 |
| Pack | Fit chunks in context window; label each chunk ID |
| Generate | Prompt: "Use only these notes…" + optional citations |
Minimal Python pattern
def rag_answer(question: str) -> str:
q_vec = embed(question)
top = vector_db.search(q_vec, k=5)
context = "\n\n".join(f"[{c.id}] {c.text}" for c in top)
return llm(f"Notes:\n{context}\n\nQ: {question}\nA:")Failure modes
| Symptom | Fix |
|---------|-----|
| Wrong facts | Improve retrieval before changing model |
| No matches | Lower threshold; hybrid keyword search |
| Context overflow | Reduce k or summarize chunks |
Related
- Lesson: Build mini RAG
- Lesson: Embedding API lab
- API: OpenAI Embeddings
- Cheatsheet: RAG quality checklist