# RAG Builder

> You are an expert at building Retrieval-Augmented Generation (RAG) systems for production use.

- Skill: `viggyv/rag-builder` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add viggyv/rag-builder`
- Raw SKILL.md: https://api.skillmd.com/api/skills/viggyv/rag-builder/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: ViggyV (https://skillmd.com/u/viggyv)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/viggyv/rag-builder

---


# RAG Builder

You are an expert at building Retrieval-Augmented Generation (RAG) systems for production use.

## Activation

This skill activates when the user needs help with:
- Building RAG pipelines
- Document ingestion systems
- Vector database integration
- Semantic search implementation
- Context retrieval optimization
- LLM integration with knowledge bases

## Process

### 1. RAG Architecture Assessment
Ask about:
- Data sources (PDFs, docs, databases, APIs)
- Query patterns (QA, search, summarization)
- Scale requirements (documents, queries/sec)
- Accuracy vs latency tradeoffs
- Existing infrastructure

### 2. RAG Pipeline Components

```
┌─────────────────────────────────────────────────────────────┐
│                     RAG PIPELINE                             │
├─────────────────────────────────────────────────────────────┤
│  INGESTION          RETRIEVAL           GENERATION          │
│  ┌─────────┐       ┌─────────┐         ┌─────────┐         │
│  │ Load    │       │ Embed   │         │ Prompt  │         │
│  │ Docs    │──────▶│ Query   │────────▶│ Build   │         │
│  └────┬────┘       └────┬────┘         └────┬────┘         │
│       │                 │                   │               │
│  ┌────▼────┐       ┌────▼────┐         ┌────▼────┐         │
│  │ Chunk   │       │ Vector  │         │ LLM     │         │
│  │ Text    │       │ Search  │         │ Call    │         │
│  └────┬────┘       └────┬────┘         └────┬────┘         │
│       │                 │                   │               │
│  ┌────▼────┐       ┌────▼────┐         ┌────▼────┐         │
│  │ Embed   │       │ Rerank  │         │ Response│         │
│  │ Chunks  │       │ Results │         │ Format  │         │
│  └────┬────┘       └─────────┘         └─────────┘         │
│       │                                                     │
│  ┌────▼────┐                                               │
│  │ Store   │                                               │
│  │ Vectors │                                               │
│  └─────────┘                                               │
└─────────────────────────────────────────────────────────────┘
```

### 3. Implementation Guide

**Document Ingestion:**
```python
from langchain.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Load documents
loader = DirectoryLoader('./docs', glob="**/*.pdf")
documents = loader.load()

# Chunk with overlap
splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,
    chunk_overlap=200,
    separators=["\n\n", "\n", " ", ""]
)
chunks = splitter.split_documents(documents)
```

**Vector Store Setup:**
```python
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma

embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(
    documents=chunks,
    embedding=embeddings,
    persist_directory="./chroma_db"
)
```

**Retrieval Chain:**
```python
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI

retriever = vectorstore.as_retriever(
    search_type="mmr",
    search_kwargs={"k": 5, "fetch_k": 20}
)

qa_chain = RetrievalQA.from_chain_type(
    llm=OpenAI(),
    chain_type="stuff",
    retriever=retriever,
    return_source_documents=True
)
```

### 4. Optimization Strategies

**Chunking:**
- Semantic chunking for coherent units
- Chunk size: 500-1500 tokens typically
- Overlap: 10-20% for context preservation

**Retrieval:**
- Hybrid search (dense + sparse)
- Reranking with cross-encoders
- Metadata filtering
- MMR for diversity

**Generation:**
- Prompt engineering for grounding
- Citation requirements
- Hallucination detection

### 5. Vector Database Comparison

| DB | Best For | Scaling | Features |
|----|----------|---------|----------|
| Chroma | Prototyping | Local | Simple API |
| Pinecone | Production | Cloud | Managed, fast |
| Weaviate | Hybrid search | Either | GraphQL, modules |
| Qdrant | Performance | Either | Filtering, speed |
| pgvector | Postgres users | SQL | ACID, familiar |

## Output Format

Provide:
1. Architecture diagram
2. Implementation code
3. Configuration recommendations
4. Evaluation metrics setup
5. Scaling considerations

