agentleFS
BULDEEBULDEE/ai-craftsman-superpowers/skills/rag/SKILL.md

rag

Skill35 starsChanged 53 days agoApache-2.0View on GitHub

Design RAG (Retrieval-Augmented Generation) pipelines. Use when building knowledge bases, document Q&A, or semantic search systems.

7 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

/craftsman:rag - Retrieval-Augmented Generation Design

Outcome Contract

  • Outcome: a RAG pipeline design with explicit chunking, embedding, and retrieval decisions.
  • Done when: chunk strategy, embedding model, and retrieval method are each chosen with a stated reason, and the evaluation method for retrieval quality is defined.
  • Evidence: the design decisions listed with their trade-offs, and the evaluation plan.

Design production-ready RAG pipelines following best practices.

RAG Architecture Overview

┌─────────────────────────────────────────────────────────────────┐
│                        RAG PIPELINE                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐       │
│  │   INGESTION  │───▶│   RETRIEVAL  │───▶│  GENERATION  │       │
│  └──────────────┘    └──────────────┘    └──────────────┘       │
│                                                                  │
│  Documents → Chunks   Query → Vectors    Context → LLM → Answer │
│  → Embeddings →       → Similar Chunks   → Response             │
│  Vector Store                                                    │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

Three Pipelines

1. Ingestion Pipeline

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import PGVector

# Chunking Strategy
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,        # Characters per chunk
    chunk_overlap=200,      # Overlap for context
    separators=["\n\n", "\n", ". ", " ", ""],
)

# Embedding Model
embeddings = OpenAIEmbeddings(
    model="text-embedding-3-small",  # Cost-effective
    # model="text-embedding-3-large", # Higher quality
)

# Vector Store
vectorstore = PGVector.from_documents(
    documents=chunks,
    embedding=embeddings,
    collection_name="knowledge_base",
    connection_string=DATABASE_URL,
)

2. Retrieval Pipeline

from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor

# Base Retriever
retriever = vectorstore.as_retriever(
    search_type="similarity",  # or "mmr" for diversity
    search_kwargs={"k": 5},    # Top K results
)

# Optional: Contextual Compression
compressor = LLMChainExtractor.from_llm(llm)
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor,
    base_retriever=retriever,
)

3. Generation Pipeline

from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4-turbo-preview",
    temperature=0,  # Deterministic for factual answers
)

qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",  # or "map_reduce" for long contexts
    retriever=retriever,
    return_source_documents=True,
)

Chunking Strategies

StrategyUse CaseChunk Size
FixedGeneral text500-1000 chars
SentencePrecise retrieval1-3 sentences
ParagraphStructured docsNatural breaks
SemanticTechnical docsTopic boundaries
RecursiveMixed contentAdaptive

Vector Database Comparison

DatabaseProsConsBest For
pgvectorPostgreSQL native, ACIDScale limitsSmall-medium, existing PG
PineconeManaged, fastCost, vendor lockProduction, scale
WeaviateHybrid searchComplexityComplex queries
ChromaSimple, localNot for prodDevelopment, prototypes
QdrantFast, filteringSelf-hostPerformance critical

Design Checklist

## RAG Design: [Project Name]

### 1. Data Source Analysis
- [ ] Document types: [PDF, Markdown, HTML, etc.]
- [ ] Total volume: [X documents, Y MB]
- [ ] Update frequency: [Real-time, Daily, Weekly]
- [ ] Quality: [Clean, Needs preprocessing]

### 2. Chunking Strategy
- [ ] Strategy: [Recursive, Semantic, etc.]
- [ ] Chunk size: [X characters]
- [ ] Overlap: [Y characters]
- [ ] Metadata: [Title, Source, Date, etc.]

### 3. Embedding Choice
- [ ] Model: [text-embedding-3-small, etc.]
- [ ] Dimensions: [1536, 3072, etc.]
- [ ] Cost estimate: [$X per 1M tokens]

### 4. Vector Store
- [ ] Database: [pgvector, Pinecone, etc.]
- [ ] Index type: [HNSW, IVF, etc.]
- [ ] Hosting: [Self-hosted, Managed]

### 5. Retrieval Strategy
- [ ] Search type: [Similarity, MMR, Hybrid]
- [ ] Top K: [3-10]
- [ ] Reranking: [Yes/No]
- [ ] Filtering: [Metadata filters]

### 6. Generation
- [ ] Model: [GPT-4, Claude, etc.]
- [ ] Temperature: [0 for factual, 0.7 for creative]
- [ ] Context window: [Token limit]
- [ ] Fallback: [When no relevant docs found]

Common Pitfalls

PitfallSolution
Chunks too smallIncrease size, lose context
Chunks too largeDecrease, add overlap
Wrong embedding modelMatch to domain (code, medical, etc.)
No metadataAdd source, date, section
No evaluationImplement retrieval metrics

Evaluation Metrics

# Retrieval Quality
- Recall@K: % of relevant docs in top K
- Precision@K: % of top K that are relevant
- MRR: Mean Reciprocal Rank

# Generation Quality
- Faithfulness: Does answer match sources?
- Relevance: Does answer address query?
- Groundedness: Is answer supported by context?

Process

  1. Understand the use case (Q&A, search, chatbot)
  2. Analyze data sources
  3. Choose chunking strategy
  4. Select embedding model
  5. Design vector store schema
  6. Implement retrieval pipeline
  7. Configure generation
  8. Set up evaluation
  9. Deploy and monitor

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.