Skip to content

About

RAG — retrieval-augmented generation: tools, patterns, papers

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

English | Francais | Espanol | Chinese | Japanese | Russian

RAG Resources

A practical guide to Retrieval-Augmented Generation: architecture, the decisions that actually move quality, how to evaluate, and why systems fail. Includes a runnable demo with zero dependencies.

Model and product names move quickly. Treat named products as a dated snapshot (late 2026) and verify before adopting.

Try it in 10 seconds

python3 rag_demo.py

rag_demo.py chunks documents, builds TF-IDF vectors, retrieves with cosine similarity and assembles a grounded prompt. Replace embed() with a real embedding model and generate() with an LLM call to go to production. The structure does not change.

The pipeline in one picture

documents -> clean -> chunk -> embed -> index
query -> (rewrite) -> embed -> retrieve top-k -> (rerank) -> build prompt -> LLM -> answer + citations

Most quality problems are in the left half (parsing, chunking) and in retrieval, not in the LLM.

Architecture patterns

Pattern How it works Use when Cost
Naive RAG embed query, top-k, stuff into prompt prototypes, clean small corpora lowest
Hybrid search combine BM25 keyword + vector, fuse ranks (RRF) identifiers, names, exact terms matter low
Rerank retrieve 50, cross-encoder reranks to 5 precision matters, noisy corpora medium latency
Query rewriting LLM expands or splits the question vague or multi-part queries extra LLM call
Parent-child chunks match small chunks, return the larger parent need precise match with full context more storage
Agentic RAG the model decides when and what to search, loops multi-hop questions highest, least predictable
GraphRAG entity/relation graph over the corpus corpus-wide "themes" questions expensive indexing

Start with hybrid + rerank. It beats most exotic setups.

Chunking: the highest-leverage decision

Strategy Strength Weakness
Fixed size with overlap (e.g. 300-500 tokens, 10-15 percent overlap) simple, predictable cuts mid-thought
Structure-aware (headings, paragraphs, code blocks) respects meaning needs good parsing
Semantic (split on embedding drift) coherent chunks slower, tune thresholds
Hierarchical (summary + details) multi-level retrieval complexity

Rules of thumb: put the document title and section path in every chunk (context for the embedder), keep tables and code blocks intact, and tune size with an evaluation set, not intuition.

Embedding models (snapshot)

Family Type Notes
OpenAI text-embedding-3 small/large API strong default, adjustable dimensions
Cohere Embed, Voyage API strong multilingual/retrieval quality, domain variants
BGE, E5, GTE, Nomic Embed, Qwen embedding models open weights self-host for privacy and cost; check the license

Choose by: language coverage, max input length, dimension (cost and latency), and your own retrieval eval. Public leaderboards (MTEB) are a start, not a verdict.

Vector stores

Option Good for Watch out
pgvector (Postgres) you already run Postgres, under tens of millions of vectors tune index (HNSW) and memory
Qdrant, Weaviate, Milvus dedicated scale, filtering, hybrid another service to operate
Chroma, LanceDB local/embedded, prototypes verify scale needs early
FAISS in-process library, research not a database (no persistence, filters by default)

Metadata filtering (tenant, date, ACL) matters more than raw speed. Enforce access control at retrieval time, never after the LLM has seen the text.

Evaluation (do this or you are guessing)

  1. Build 50-200 real questions with the expected source passages.
  2. Measure retrieval separately: recall@k, MRR, hit rate. If the right chunk is not retrieved, no LLM can save you.
  3. Measure generation: faithfulness (answer supported by context), answer relevance, citation correctness. Frameworks such as RAGAS help, but spot-check by hand.
  4. Track regressions in CI whenever chunking, embeddings or prompts change.

Common failure modes

Symptom Likely cause Fix
Confident wrong answers irrelevant chunks retrieved add rerank, raise threshold, allow "I do not know"
Misses exact terms (IDs, names) vector-only search add BM25 hybrid
Answer ignores key passage lost in the middle, too many chunks fewer, better chunks; reorder
Stale answers no re-indexing incremental ingestion with timestamps
Table or PDF garbage bad parsing better parser, table-aware chunks
Data leak across users no ACL filter filter by permission at retrieval
Prompt injection from documents retrieved text treated as instructions delimit context, never grant tools from retrieved text

Security notes

Retrieved content is untrusted input. A poisoned document can carry instructions. Separate context from instructions, limit tool permissions, log what was retrieved, and monitor outbound traffic of your pipeline (for example with Wiretap) to see which hosts your tools actually call.

Related from aiXis Studio

awesome-ai-agents | awesome-mcp-servers | rag-resources | awesome-local-llm | llm-api-examples | Wiretap - audit what your AI tools send out.

About

RAG — retrieval-augmented generation: tools, patterns, papers

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages