{"id":"ad6e8c45-c598-4a6f-9bab-ffcd5de6c775","shortId":"sCPcf2","kind":"skill","title":"dspy-rag-pipeline","tagline":"This skill should be used when the user asks to \"build a RAG pipeline\", \"create retrieval augmented generation\", \"use ColBERTv2 in DSPy\", \"set up a retriever in DSPy\", mentions \"RAG with DSPy\", \"context retrieval\", \"multi-hop RAG\", or needs to build a DSPy system that retrieves e","description":"# DSPy RAG Pipeline\n\n## Goal\n\nBuild retrieval-augmented generation pipelines with ColBERTv2 that can be systematically optimized.\n\n## When to Use\n\n- Questions require external knowledge\n- You have a document corpus to search\n- Need grounded, factual responses\n- Want to optimize retrieval + generation jointly\n\n## Related Skills\n\n- Optimize this pipeline: [dspy-miprov2-optimizer](../dspy-miprov2-optimizer/SKILL.md), [dspy-bootstrap-fewshot](../dspy-bootstrap-fewshot/SKILL.md)\n- Evaluate results: [dspy-evaluation-suite](../dspy-evaluation-suite/SKILL.md)\n- Design signatures: [dspy-signature-designer](../dspy-signature-designer/SKILL.md)\n\n## Inputs\n\n| Input | Type | Description |\n|-------|------|-------------|\n| `question` | `str` | User query |\n| `k` | `int` | Number of passages to retrieve |\n| `rm` | `dspy.Retrieve` | Retrieval model (ColBERTv2) |\n\n## Outputs\n\n| Output | Type | Description |\n|--------|------|-------------|\n| `context` | `list[str]` | Retrieved passages |\n| `answer` | `str` | Generated response |\n\n## Workflow\n\n### Phase 1: Configure Retrieval\n\n```python\nimport dspy\n\n# Configure LM and retriever\ncolbert = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')\ndspy.configure(\n    lm=dspy.LM(\"openai/gpt-4o-mini\"),\n    rm=colbert\n)\n```\n\n### Phase 2: Define Signature\n\n```python\nclass GenerateAnswer(dspy.Signature):\n    \"\"\"Answer questions with short factoid answers.\"\"\"\n    context: str = dspy.InputField(desc=\"May contain relevant facts\")\n    question: str = dspy.InputField()\n    answer: str = dspy.OutputField(desc=\"Often between 1 and 5 words\")\n```\n\n### Phase 3: Build RAG Module\n\n```python\nclass RAG(dspy.Module):\n    def __init__(self, num_passages=3):\n        super().__init__()\n        self.retrieve = dspy.Retrieve(k=num_passages)\n        self.generate = dspy.ChainOfThought(GenerateAnswer)\n    \n    def forward(self, question):\n        context = self.retrieve(question).passages\n        pred = self.generate(context=context, question=question)\n        return dspy.Prediction(context=context, answer=pred.answer)\n```\n\n### Phase 4: Use\n\n```python\nrag = RAG(num_passages=3)\nresult = rag(question=\"What is the capital of France?\")\nprint(result.answer)  # Paris\n```\n\n## Production Example\n\n```python\nimport dspy\nfrom dspy.teleprompt import BootstrapFewShot\nfrom dspy.evaluate import Evaluate\nimport logging\n\nlogger = logging.getLogger(__name__)\n\nclass GenerateAnswer(dspy.Signature):\n    \"\"\"Answer questions using the provided context.\"\"\"\n    context: list[str] = dspy.InputField(desc=\"Retrieved passages\")\n    question: str = dspy.InputField()\n    answer: str = dspy.OutputField(desc=\"Concise factual answer\")\n\nclass ProductionRAG(dspy.Module):\n    def __init__(self, num_passages=5):\n        super().__init__()\n        self.num_passages = num_passages\n        self.retrieve = dspy.Retrieve(k=num_passages)\n        self.generate = dspy.ChainOfThought(GenerateAnswer)\n    \n    def forward(self, question: str):\n        try:\n            # Retrieve\n            retrieval_result = self.retrieve(question)\n            context = retrieval_result.passages\n            \n            if not context:\n                logger.warning(f\"No passages retrieved for: {question}\")\n                return dspy.Prediction(\n                    context=[],\n                    answer=\"I couldn't find relevant information.\"\n                )\n            \n            # Generate\n            pred = self.generate(context=context, question=question)\n            \n            return dspy.Prediction(\n                context=context,\n                answer=pred.answer,\n                reasoning=getattr(pred, 'reasoning', None)\n            )\n            \n        except Exception as e:\n            logger.error(f\"RAG failed: {e}\")\n            return dspy.Prediction(\n                context=[],\n                answer=\"An error occurred while processing your question.\"\n            )\n\ndef validate_answer(example, pred, trace=None):\n    \"\"\"Check if answer is grounded and correct.\"\"\"\n    if not pred.answer or not pred.context:\n        return 0.0\n    \n    # Check correctness\n    correct = example.answer.lower() in pred.answer.lower()\n    \n    # Check grounding (answer should relate to context)\n    context_text = \" \".join(pred.context).lower()\n    grounded = any(word in context_text for word in pred.answer.lower().split())\n    \n    return float(correct and grounded)\n\ndef build_optimized_rag(trainset, devset):\n    \"\"\"Build and optimize a RAG pipeline.\"\"\"\n    \n    # Configure\n    colbert = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')\n    dspy.configure(\n        lm=dspy.LM(\"openai/gpt-4o-mini\"),\n        rm=colbert\n    )\n    \n    # Build\n    rag = ProductionRAG(num_passages=5)\n    \n    # Evaluate baseline\n    evaluator = Evaluate(devset=devset, metric=validate_answer, num_threads=8)\n    baseline = evaluator(rag)\n    logger.info(f\"Baseline: {baseline:.2%}\")\n    \n    # Optimize\n    optimizer = BootstrapFewShot(\n        metric=validate_answer,\n        max_bootstrapped_demos=4,\n        max_labeled_demos=4\n    )\n    compiled = optimizer.compile(rag, trainset=trainset)\n    \n    optimized = evaluator(compiled)\n    logger.info(f\"Optimized: {optimized:.2%}\")\n    \n    compiled.save(\"rag_optimized.json\")\n    return compiled\n```\n\n## Multi-Hop RAG\n\n```python\nclass MultiHopRAG(dspy.Module):\n    \"\"\"RAG with iterative retrieval for complex questions.\"\"\"\n    \n    def __init__(self, num_hops=2, passages_per_hop=3):\n        super().__init__()\n        self.num_hops = num_hops\n        self.retrieve = dspy.Retrieve(k=passages_per_hop)\n        self.generate_query = dspy.ChainOfThought(\"context, question -> search_query\")\n        self.generate_answer = dspy.ChainOfThought(GenerateAnswer)\n    \n    def forward(self, question):\n        context = []\n        \n        for hop in range(self.num_hops):\n            # First hop: use original question\n            # Later hops: generate refined query\n            if hop == 0:\n                query = question\n            else:\n                query = self.generate_query(\n                    context=context,\n                    question=question\n                ).search_query\n            \n            # Retrieve and accumulate\n            new_passages = self.retrieve(query).passages\n            context.extend(new_passages)\n        \n        # Generate final answer\n        pred = self.generate_answer(context=context, question=question)\n        return dspy.Prediction(context=context, answer=pred.answer)\n```\n\n## Best Practices\n\n1. **Tune k carefully** - More passages = more context but also noise\n2. **Signature descriptions matter** - Guide the model with field descriptions\n3. **Validate grounding** - Ensure answers come from retrieved context\n4. **Consider multi-hop** - Complex questions may need iterative retrieval\n\n## Limitations\n\n- Retrieval quality bounds generation quality\n- ColBERTv2 requires hosted index\n- Context length limits affect passage count\n- Latency increases with more passages\n\n## Official Documentation\n\n- **DSPy Documentation**: https://dspy.ai/\n- **DSPy GitHub**: https://github.com/stanfordnlp/dspy\n- **RAG Tutorial**: https://dspy.ai/tutorials/rag/\n- **ColBERTv2 API**: https://dspy.ai/api/tools/ColBERTv2/","tags":["dspy","rag","pipeline","skills","omidzamani","agent-skills","claude-code","claude-skills","llm","prompt-optimization"],"capabilities":["skill","source-omidzamani","skill-dspy-rag-pipeline","topic-agent-skills","topic-claude-code","topic-claude-skills","topic-dspy","topic-llm","topic-prompt-optimization","topic-rag"],"categories":["dspy-skills"],"synonyms":[],"warnings":[],"endpointUrl":"https://skills.sh/OmidZamani/dspy-skills/dspy-rag-pipeline","protocol":"skill","transport":"skills-sh","auth":{"type":"none","details":{"cli":"npx skills add OmidZamani/dspy-skills","source_repo":"https://github.com/OmidZamani/dspy-skills","install_from":"skills.sh"}},"qualityScore":"0.487","qualityRationale":"deterministic score 0.49 from registry signals: · indexed on github topic:agent-skills · 74 github stars · SKILL.md body (7,227 chars)","verified":false,"liveness":"unknown","lastLivenessCheck":null,"agentReviews":{"count":0,"score_avg":null,"cost_usd_avg":null,"success_rate":null,"latency_p50_ms":null,"narrative_summary":null,"summary_updated_at":null},"enrichmentModel":"deterministic:skill-github:v1","enrichmentVersion":1,"enrichedAt":"2026-05-02T06:55:44.720Z","embedding":null,"createdAt":"2026-04-18T22:14:16.485Z","updatedAt":"2026-05-02T06:55:44.720Z","lastSeenAt":"2026-05-02T06:55:44.720Z","tsv":"'/api/tools/colbertv2/':749 '/dspy-bootstrap-fewshot/skill.md':108 '/dspy-evaluation-suite/skill.md':115 '/dspy-miprov2-optimizer/skill.md':103 '/dspy-signature-designer/skill.md':122 '/stanfordnlp/dspy':739 '/tutorials/rag/':744 '0':626 '0.0':439 '1':158,210,668 '2':180,523,550,575,679 '20.102.90.50':171,490 '2017/wiki17_abstracts':172,491 '3':215,228,267,579,689 '4':260,533,537,698 '5':212,332,503 '8':515 'accumul':641 'affect':722 'also':677 'answer':152,187,192,204,257,301,317,323,373,391,410,420,427,448,512,529,600,652,655,664,693 'api':746 'ask':13 'augment':21,60 'baselin':505,516,521,522 'best':666 'bootstrap':106,531 'bootstrapfewshot':288,526 'bound':712 'build':15,46,57,216,475,480,498 'capit':274 'care':671 'check':425,440,446 'class':184,220,298,324,560 'colbert':168,178,487,497 'colbertv2':24,64,142,715,745 'come':694 'compil':538,545,554 'compiled.save':551 'complex':568,703 'concis':321 'configur':159,164,486 'consid':699 'contain':198 'context':37,147,193,243,249,250,255,256,306,307,358,362,372,383,384,389,390,409,452,453,462,595,607,633,634,656,657,662,663,675,697,719 'context.extend':647 'corpus':81 'correct':431,441,442,471 'couldn':375 'count':724 'creat':19 'def':223,239,327,347,418,474,570,603 'defin':181 'demo':532,536 'desc':196,207,311,320 'descript':126,146,681,688 'design':116,121 'devset':479,508,509 'document':80,731,733 'dspi':2,26,32,36,48,53,100,105,112,119,163,284,732,735 'dspy-bootstrap-fewshot':104 'dspy-evaluation-suit':111 'dspy-miprov2-optimizer':99 'dspy-rag-pipelin':1 'dspy-signature-design':118 'dspy.ai':734,743,748 'dspy.ai/api/tools/colbertv2/':747 'dspy.ai/tutorials/rag/':742 'dspy.chainofthought':237,345,594,601 'dspy.colbertv2':169,488 'dspy.configure':173,492 'dspy.evaluate':290 'dspy.inputfield':195,203,310,316 'dspy.lm':175,494 'dspy.module':222,326,562 'dspy.outputfield':206,319 'dspy.prediction':254,371,388,408,661 'dspy.retrieve':139,232,340,587 'dspy.signature':186,300 'dspy.teleprompt':286 'e':52,401,406 'els':629 'ensur':692 'error':412 'evalu':109,113,292,504,506,507,517,544 'exampl':281,421 'example.answer.lower':443 'except':398,399 'extern':75 'f':364,403,520,547 'fact':200 'factoid':191 'factual':86,322 'fail':405 'fewshot':107 'field':687 'final':651 'find':377 'first':614 'float':470 'forward':240,348,604 'franc':276 'generat':22,61,92,154,380,621,650,713 'generateansw':185,238,299,346,602 'getattr':394 'github':736 'github.com':738 'github.com/stanfordnlp/dspy':737 'goal':56 'ground':85,429,447,458,473,691 'guid':683 'hop':41,557,574,578,583,585,591,609,613,615,620,625,702 'host':717 'import':162,283,287,291,293 'increas':726 'index':718 'inform':379 'init':224,230,328,334,571,581 'input':123,124 'int':132 'iter':565,707 'join':455 'joint':93 'k':131,233,341,588,670 'knowledg':76 'label':535 'latenc':725 'later':619 'length':720 'limit':709,721 'list':148,308 'lm':165,174,493 'log':294 'logger':295 'logger.error':402 'logger.info':519,546 'logger.warning':363 'logging.getlogger':296 'lower':457 'matter':682 'max':530,534 'may':197,705 'mention':33 'metric':510,527 'miprov2':101 'model':141,685 'modul':218 'multi':40,556,701 'multi-hop':39,555,700 'multihoprag':561 'name':297 'need':44,84,706 'new':642,648 'nois':678 'none':397,424 'num':226,234,265,330,337,342,501,513,573,584 'number':133 'occur':413 'offici':730 'often':208 'openai/gpt-4o-mini':176,495 'optim':69,90,96,102,476,482,524,525,543,548,549 'optimizer.compile':539 'origin':617 'output':143,144 'pari':279 'passag':135,151,227,235,246,266,313,331,336,338,343,366,502,576,589,643,646,649,673,723,729 'per':577,590 'phase':157,179,214,259 'pipelin':4,18,55,62,98,485 'practic':667 'pred':247,381,395,422,653 'pred.answer':258,392,434,665 'pred.answer.lower':445,467 'pred.context':437,456 'print':277 'process':415 'product':280 'productionrag':325,500 'provid':305 'python':161,183,219,262,282,559 'qualiti':711,714 'queri':130,593,598,623,627,630,632,638,645 'question':73,127,188,201,242,245,251,252,270,302,314,350,357,369,385,386,417,569,596,606,618,628,635,636,658,659,704 'rag':3,17,34,42,54,217,221,263,264,269,404,477,484,499,518,540,558,563,740 'rag_optimized.json':552 'rang':611 'reason':393,396 'refin':622 'relat':94,450 'relev':199,378 'requir':74,716 'respons':87,155 'result':110,268,355 'result.answer':278 'retriev':20,30,38,51,59,91,137,140,150,160,167,312,353,354,367,566,639,696,708,710 'retrieval-aug':58 'retrieval_result.passages':359 'return':253,370,387,407,438,469,553,660 'rm':138,177,496 'search':83,597,637 'self':225,241,329,349,572,605 'self.generate':236,248,344,382,592,599,631,654 'self.num':335,582,612 'self.retrieve':231,244,339,356,586,644 'set':27 'short':190 'signatur':117,120,182,680 'skill':6,95 'skill-dspy-rag-pipeline' 'source-omidzamani' 'split':468 'str':128,149,153,194,202,205,309,315,318,351 'suit':114 'super':229,333,580 'system':49 'systemat':68 'text':454,463 'thread':514 'topic-agent-skills' 'topic-claude-code' 'topic-claude-skills' 'topic-dspy' 'topic-llm' 'topic-prompt-optimization' 'topic-rag' 'trace':423 'trainset':478,541,542 'tri':352 'tune':669 'tutori':741 'type':125,145 'url':170,489 'use':9,23,72,261,303,616 'user':12,129 'valid':419,511,528,690 'want':88 'word':213,460,465 'workflow':156","prices":[{"id":"8a2de7b6-af57-4cbf-9bfe-529f214414ad","listingId":"ad6e8c45-c598-4a6f-9bab-ffcd5de6c775","amountUsd":"0","unit":"free","nativeCurrency":null,"nativeAmount":null,"chain":null,"payTo":null,"paymentMethod":"skill-free","isPrimary":true,"details":{"org":"OmidZamani","category":"dspy-skills","install_from":"skills.sh"},"createdAt":"2026-04-18T22:14:16.485Z"}],"sources":[{"listingId":"ad6e8c45-c598-4a6f-9bab-ffcd5de6c775","source":"github","sourceId":"OmidZamani/dspy-skills/dspy-rag-pipeline","sourceUrl":"https://github.com/OmidZamani/dspy-skills/tree/master/skills/dspy-rag-pipeline","isPrimary":false,"firstSeenAt":"2026-04-18T22:14:16.485Z","lastSeenAt":"2026-05-02T06:55:44.720Z"}],"details":{"listingId":"ad6e8c45-c598-4a6f-9bab-ffcd5de6c775","quickStartSnippet":null,"exampleRequest":null,"exampleResponse":null,"schema":null,"openapiUrl":null,"agentsTxtUrl":null,"citations":[],"useCases":[],"bestFor":[],"notFor":[],"kindDetails":{"org":"OmidZamani","slug":"dspy-rag-pipeline","github":{"repo":"OmidZamani/dspy-skills","stars":74,"topics":["agent-skills","claude-code","claude-skills","dspy","llm","prompt-optimization","rag"],"license":"mit","html_url":"https://github.com/OmidZamani/dspy-skills","pushed_at":"2026-02-21T12:49:43Z","description":"Collection of Claude Skills for DSPy framework - program language models, optimize prompts, and build RAG pipelines systematically","skill_md_sha":"8f397288325a5fa0cf49c305c4778cf73ff655ea","skill_md_path":"skills/dspy-rag-pipeline/SKILL.md","default_branch":"master","skill_tree_url":"https://github.com/OmidZamani/dspy-skills/tree/master/skills/dspy-rag-pipeline"},"layout":"multi","source":"github","category":"dspy-skills","frontmatter":{"name":"dspy-rag-pipeline","description":"This skill should be used when the user asks to \"build a RAG pipeline\", \"create retrieval augmented generation\", \"use ColBERTv2 in DSPy\", \"set up a retriever in DSPy\", mentions \"RAG with DSPy\", \"context retrieval\", \"multi-hop RAG\", or needs to build a DSPy system that retrieves external knowledge to answer questions with grounded, factual responses."},"skills_sh_url":"https://skills.sh/OmidZamani/dspy-skills/dspy-rag-pipeline"},"updatedAt":"2026-05-02T06:55:44.720Z"}}