Overview
Builds a complete, production-oriented Retrieval-Augmented Generation (RAG) pipeline. Covers document ingestion, intelligent chunking strategies (fixed, recursive, semantic), embedding model selection, vector database choice and setup, retrieval (top-k, hybrid, reranking), context assembly, generation with hallucination mitigation, and evaluation of the full pipeline.
When to Use This Skill
- Building a chatbot or Q&A system over company docs, knowledge base, code, or private data.
- The user says "RAG", "chat with my PDFs", "question answering over documents", or provides a corpus.
- Existing simple RAG is producing poor or hallucinated answers.
Prerequisites
- A collection of documents (PDF, Markdown, HTML, Notion export, etc.).
- Embedding model access (OpenAI, Cohere, Voyage, or local via sentence-transformers).
- Vector database (Chroma for dev, Pinecone/Qdrant/Weaviate/pgvector for prod).
- LLM for generation.
Steps
Ingestion & preprocessing:
- Load documents (LangChain loaders, LlamaIndex readers, or custom).
- Clean (remove boilerplate, normalize whitespace, extract metadata: source, page, date).
Chunking (critical for quality):
- Fixed size (512-1024 tokens) with overlap.
- Recursive (by paragraph → sentence → token) — best starting point.
- Semantic chunking (embed sentences, cluster) for higher quality at higher cost.
- Document-aware (respect headings, tables, code blocks).
Embedding:
- Choose model (tradeoff: quality vs cost vs latency vs context length).
- Batch embed and normalize if required by the vector DB.
- Store rich metadata with each chunk.
Vector store:
- Create collection/index with appropriate distance metric (cosine usually).
- Upsert with metadata filtering support.
Retrieval:
- Top-k (start with 5-10).
- Hybrid (vector + keyword / BM25) when available.
- Metadata filters (date range, source, user permissions).
- Reranking (Cohere Rerank, cross-encoder, or LLM reranker) for better precision.
Context assembly & generation:
- Assemble prompt: system instructions + retrieved chunks (with citations) + user question.
- Instruct the model to only use provided context and to cite sources.
- Use "If you don't know, say so" guardrails.
Evaluation:
- Golden Q&A set (question, ground-truth answer, source docs).
- Metrics: faithfulness (LLM judge), answer relevance, context relevance, citation accuracy.
- RAGAS, ARES, or custom eval harness.
Output:
- Full pipeline code (Python, using LangChain or LlamaIndex or custom).
- Chunking + embedding + retrieval + generation functions.
- Example ingestion script.
- Evaluation script + sample golden set.
- Deployment notes (indexing job, query service, caching).
Examples
A complete end-to-end RAG pipeline over a set of company policy PDFs using recursive chunking, OpenAI embeddings, Chroma (dev) / Qdrant (prod), hybrid retrieval, Cohere rerank, and Claude 3 / GPT-4 generation with source citation and hallucination guardrails is included, along with a RAGAS-style evaluation.
Edge Cases & Error Handling
- No relevant context: Model should say "I don't have information on that in the provided documents."
- Conflicting sources: Instruct the model to present both sides or note the conflict.
- Large corpus / many updates: Incremental indexing, versioning of chunks, metadata for "as of" date.
- Permissions: Filter chunks by user ACL at retrieval time.
Verification
- Ingest the documents — chunks appear in the vector DB with correct metadata.
- Run a set of test questions — retrieved chunks are relevant (manual review).
- Generated answers are grounded in the retrieved context (no obvious hallucinations) and cite sources.
- Evaluation scores on the golden set meet targets (e.g., faithfulness > 0.85).
- Re-indexing with updated documents works without duplicating old chunks.
- Success: The RAG system answers questions accurately from the corpus with good citation and low hallucination rate.
References
1---2name: rag-pipeline-builder3description: Builds a Retrieval-Augmented Generation pipeline with document ingestion, chunking, embedding, vector search, and generation. Use when building a Q&A system over custom documents.4license: Apache-2.05---67## Overview89Builds a complete, production-oriented Retrieval-Augmented Generation (RAG) pipeline. Covers document ingestion, intelligent chunking strategies (fixed, recursive, semantic), embedding model selection, vector database choice and setup, retrieval (top-k, hybrid, reranking), context assembly, generation with hallucination mitigation, and evaluation of the full pipeline.1011## When to Use This Skill1213- Building a chatbot or Q&A system over company docs, knowledge base, code, or private data.14- The user says "RAG", "chat with my PDFs", "question answering over documents", or provides a corpus.15- Existing simple RAG is producing poor or hallucinated answers.1617## Prerequisites1819- A collection of documents (PDF, Markdown, HTML, Notion export, etc.).20- Embedding model access (OpenAI, Cohere, Voyage, or local via sentence-transformers).21- Vector database (Chroma for dev, Pinecone/Qdrant/Weaviate/pgvector for prod).22- LLM for generation.2324## Steps25261. **Ingestion & preprocessing**:27 - Load documents (LangChain loaders, LlamaIndex readers, or custom).28 - Clean (remove boilerplate, normalize whitespace, extract metadata: source, page, date).29302. **Chunking** (critical for quality):31 - Fixed size (512-1024 tokens) with overlap.32 - Recursive (by paragraph → sentence → token) — best starting point.33 - Semantic chunking (embed sentences, cluster) for higher quality at higher cost.34 - Document-aware (respect headings, tables, code blocks).35363. **Embedding**:37 - Choose model (tradeoff: quality vs cost vs latency vs context length).38 - Batch embed and normalize if required by the vector DB.39 - Store rich metadata with each chunk.40414. **Vector store**:42 - Create collection/index with appropriate distance metric (cosine usually).43 - Upsert with metadata filtering support.44455. **Retrieval**:46 - Top-k (start with 5-10).47 - Hybrid (vector + keyword / BM25) when available.48 - Metadata filters (date range, source, user permissions).49 - Reranking (Cohere Rerank, cross-encoder, or LLM reranker) for better precision.50516. **Context assembly & generation**:52 - Assemble prompt: system instructions + retrieved chunks (with citations) + user question.53 - Instruct the model to only use provided context and to cite sources.54 - Use "If you don't know, say so" guardrails.55567. **Evaluation**:57 - Golden Q&A set (question, ground-truth answer, source docs).58 - Metrics: faithfulness (LLM judge), answer relevance, context relevance, citation accuracy.59 - RAGAS, ARES, or custom eval harness.60618. **Output**:62 - Full pipeline code (Python, using LangChain or LlamaIndex or custom).63 - Chunking + embedding + retrieval + generation functions.64 - Example ingestion script.65 - Evaluation script + sample golden set.66 - Deployment notes (indexing job, query service, caching).6768## Examples6970A complete end-to-end RAG pipeline over a set of company policy PDFs using recursive chunking, OpenAI embeddings, Chroma (dev) / Qdrant (prod), hybrid retrieval, Cohere rerank, and Claude 3 / GPT-4 generation with source citation and hallucination guardrails is included, along with a RAGAS-style evaluation.7172## Edge Cases & Error Handling7374- **No relevant context**: Model should say "I don't have information on that in the provided documents."75- **Conflicting sources**: Instruct the model to present both sides or note the conflict.76- **Large corpus / many updates**: Incremental indexing, versioning of chunks, metadata for "as of" date.77- **Permissions**: Filter chunks by user ACL at retrieval time.7879## Verification80811. Ingest the documents — chunks appear in the vector DB with correct metadata.822. Run a set of test questions — retrieved chunks are relevant (manual review).833. Generated answers are grounded in the retrieved context (no obvious hallucinations) and cite sources.844. Evaluation scores on the golden set meet targets (e.g., faithfulness > 0.85).855. Re-indexing with updated documents works without duplicating old chunks.866. Success: The RAG system answers questions accurately from the corpus with good citation and low hallucination rate.8788## References8990- [LangChain RAG](https://python.langchain.com/docs/use_cases/question_answering/)91- [LlamaIndex](https://docs.llamaindex.ai/)92- [RAGAS](https://github.com/explodinggradients/ragas)93- [Chunking strategies for RAG](https://www.pinecone.io/learn/chunking-strategies/)94- [Hybrid Search](https://www.pinecone.io/learn/hybrid-search-intro/)