ai-mlMIT License Official

RAG Pipeline Architect

Vector chunking strategies, hybrid dense/sparse search (BM25 + vector), re-ranking models, context window compression, and citation grounding.

#RAG#Vector Search#Embeddings#Re-Ranking#Chunking#LLM
Install for:
npx domoskills add rag-pipeline-architect
GitHub
Security verified • Score: 100/100
Installs into: .agent/skills/rag-pipeline-architect
SKILL.md Prompt Instructions
Read by AI agent on demand
---
name: rag-pipeline-architect
description: Production-grade RAG pipeline design with chunking strategies, vector stores, hybrid search, reranking, and RAGAS evaluation.
license: MIT
version: 2.3.0
---

# RAG Pipeline Architect

## Overview
Retrieval-Augmented Generation (RAG) grounds LLM responses in external knowledge bases, reducing hallucinations and enabling up-to-date answers. A production RAG pipeline involves ingestion, indexing, retrieval, reranking, augmentation, and generation.

## 1. Document Ingestion & Chunking
```python
from langchain_community.document_loaders import PDFMinerLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

loader = PDFMinerLoader("technical-docs.pdf")
docs = loader.load()

splitter = RecursiveCharacterTextSplitter(
    chunk_size=512,
    chunk_overlap=64,
    separators=["\n\n", "\n", ".", " ", ""],
)
chunks = splitter.split_documents(docs)
```

Chunking strategy selection:
| Strategy | Best For |
|----------|----------|
| Fixed token size | General text, PDFs |
| Semantic chunking | Narrative documents, blog posts |
| Recursive character | Code, markdown with headers |
| Parent-child (hierarchical) | Multi-level documents |
| Sliding window | Dense technical content |

## 2. Embedding
```python
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-large", dimensions=1024)

# Open-source alternative (self-hostable)
from langchain_community.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(
    model_name="BAAI/bge-large-en-v1.5",
    encode_kwargs={"normalize_embeddings": True},
)
```

Always normalize embeddings before storing — cosine similarity of normalized vectors equals dot product (faster).

## 3. Vector Store Indexing
```python
# pgvector (Postgres-based)
from langchain_postgres import PGVector
vectorstore = PGVector.from_documents(chunks, embeddings, connection=DATABASE_URL)

# Qdrant (production-grade, filtering-first)
from langchain_qdrant import QdrantVectorStore
vectorstore = QdrantVectorStore.from_documents(chunks, embeddings, url=QDRANT_URL, collection_name="docs")
```

## 4. Retrieval Strategies

Dense retrieval (baseline):
```python
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 6})
```

Hybrid search (BM25 + Dense — better recall):
```python
from langchain.retrievers import EnsembleRetriever
from langchain_community.retrievers import BM25Retriever

bm25_retriever = BM25Retriever.from_documents(chunks, k=4)
dense_retriever = vectorstore.as_retriever(search_kwargs={"k": 4})

ensemble = EnsembleRetriever(
    retrievers=[bm25_retriever, dense_retriever],
    weights=[0.4, 0.6],
)
```

MMR for deduplication:
```python
retriever = vectorstore.as_retriever(
    search_type="mmr",
    search_kwargs={"k": 6, "fetch_k": 20, "lambda_mult": 0.7}
)
```

## 5. Reranking (Highest Leverage Optimization)
```python
from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
from langchain_cohere import CohereRerank

reranker = CohereRerank(model="rerank-english-v3.0", top_n=3)
compressed_retriever = ContextualCompressionRetriever(
    base_compressor=reranker,
    base_retriever=retriever,
)
```

## 6. Prompt with Citation
```python
SYSTEM = """Answer ONLY based on the provided context.
If the context does not contain the answer, say "I do not have information on that."
Always cite the source document name at the end of your answer.

Context: {context}"""
```

## 7. Evaluation with RAGAS
```python
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy, context_precision, context_recall

results = evaluate(dataset=test_dataset, metrics=[faithfulness, answer_relevancy, context_precision, context_recall])
# Faithfulness > 0.8 is production-ready; context_precision > 0.7 is acceptable
```

## 8. Advanced Techniques
- HyDE: Generate hypothetical answer, embed it, use to retrieve real docs.
- RAG-Fusion: Multiple queries per question, RRF-merge results.
- Self-RAG: LLM decides when to retrieve and grades its own answers.
- CRAG: Evaluates retrieved documents, falls back to web search if quality is low.

## 9. Anti-Patterns
- Chunk size >1024 tokens — exceeds embedding model context window.
- No metadata filtering — every query searches entire corpus.
- No reranking — dense retrieval recall is high but precision is low.
- Not evaluating RAG systematically — gut feeling is not sufficient.

Ecosystem Radar & Recommended Companions

Dynamic Capability Matrix
Standard Connectors
Antigravity (.agent)Claude Code (.claude)Cursor (.cursor)
RAG Pipeline ArchitectActive Capability