[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M71HD34M85WRH0CXB333SW23":3,"skill-same:01M71HD34M85WRH0CXB333SW23":98},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":27,"publishedAt":47,"linkOnly":48,"markdownContent":49,"markdownBody":50,"frontMatter":51,"refName":63,"commitSha":64,"skillDirectory":65,"forksCount":66,"repositoryHtmlUrl":67,"repositoryHomepageUrl":6,"ownerHtmlUrl":68,"ownerBlogUrl":6,"ownerDescription":6,"updatedAt":69,"install":70,"resources":83},"01M71HD34M85WRH0CXB333SW23","dspy",null,"Build complex AI systems with declarative programming, optimize prompts automatically, create modular RAG systems and agents with DSPy - Stanford NLP's framework for systematic LM programming","0xzgbot\u002Fforge-nps-v01","0xzgbot","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F271900281?v=4","01M71GXWAFRXXJSJE3HBCVDKZY","01M71GXWBFQYRJK7M8RM6BDP0X",6,"MIT","https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fprofiles\u002Flive\u002Fskills\u002Fmlops\u002Fresearch\u002Fdspy\u002FSKILL.md",[17,22],{"categoryNo":18,"categoryName":19,"taxonomyType":20,"taxonomyTypeDesc":21},"01JBSEEDCAT000000000000023","Skill与智能体开发","SCENARIO","使用场景",{"categoryNo":23,"categoryName":24,"taxonomyType":25,"taxonomyTypeDesc":26},"01JBSEEDCAT000000000000030","开发工程师","PROFESSION","适用职业",[5,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46],"rag","nlp","Optimization","for","orchestra","declarative","framework","openai","automatic","systematic","modular","optimize","create","Modular AI","Automatic Optimization","Stanford NLP","LM Programming","Prompt Optimization","Declarative Programming","2026-11-10 10:12:30",false,"---\nname: dspy\ndescription: Build complex AI systems with declarative programming, optimize prompts automatically, create modular RAG systems and agents with DSPy - Stanford NLP's framework for systematic LM programming\nversion: 1.0.0\nauthor: Orchestra Research\nlicense: MIT\ndependencies: [dspy, openai, anthropic]\nmetadata:\n  hermes:\n    tags: [Prompt Engineering, DSPy, Declarative Programming, RAG, Agents, Prompt Optimization, LM Programming, Stanford NLP, Automatic Optimization, Modular AI]\n\n---\n\n# DSPy: Declarative Language Model Programming\n\n## When to Use This Skill\n\nUse DSPy when you need to:\n- **Build complex AI systems** with multiple components and workflows\n- **Program LMs declaratively** instead of manual prompt engineering\n- **Optimize prompts automatically** using data-driven methods\n- **Create modular AI pipelines** that are maintainable and portable\n- **Improve model outputs systematically** with optimizers\n- **Build RAG systems, agents, or classifiers** with better reliability\n\n**GitHub Stars**: 22,000+ | **Created By**: Stanford NLP\n\n## Installation\n\n```bash\n# Stable release\npip install dspy\n\n# Latest development version\npip install git+https:\u002F\u002Fgithub.com\u002Fstanfordnlp\u002Fdspy.git\n\n# With specific LM providers\npip install dspy[openai]        # OpenAI\npip install dspy[anthropic]     # Anthropic Claude\npip install dspy[all]           # All providers\n```\n\n## Quick Start\n\n### Basic Example: Question Answering\n\n```python\nimport dspy\n\n# Configure your language model\nlm = dspy.Claude(model=\"claude-sonnet-4-5-20250929\")\ndspy.settings.configure(lm=lm)\n\n# Define a signature (input → output)\nclass QA(dspy.Signature):\n    \"\"\"Answer questions with short factual answers.\"\"\"\n    question = dspy.InputField()\n    answer = dspy.OutputField(desc=\"often between 1 and 5 words\")\n\n# Create a module\nqa = dspy.Predict(QA)\n\n# Use it\nresponse = qa(question=\"What is the capital of France?\")\nprint(response.answer)  # \"Paris\"\n```\n\n### Chain of Thought Reasoning\n\n```python\nimport dspy\n\nlm = dspy.Claude(model=\"claude-sonnet-4-5-20250929\")\ndspy.settings.configure(lm=lm)\n\n# Use ChainOfThought for better reasoning\nclass MathProblem(dspy.Signature):\n    \"\"\"Solve math word problems.\"\"\"\n    problem = dspy.InputField()\n    answer = dspy.OutputField(desc=\"numerical answer\")\n\n# ChainOfThought generates reasoning steps automatically\ncot = dspy.ChainOfThought(MathProblem)\n\nresponse = cot(problem=\"If John has 5 apples and gives 2 to Mary, how many does he have?\")\nprint(response.rationale)  # Shows reasoning steps\nprint(response.answer)     # \"3\"\n```\n\n## Core Concepts\n\n### 1. Signatures\n\nSignatures define the structure of your AI task (inputs → outputs):\n\n```python\n# Inline signature (simple)\nqa = dspy.Predict(\"question -> answer\")\n\n# Class signature (detailed)\nclass Summarize(dspy.Signature):\n    \"\"\"Summarize text into key points.\"\"\"\n    text = dspy.InputField()\n    summary = dspy.OutputField(desc=\"bullet points, 3-5 items\")\n\nsummarizer = dspy.ChainOfThought(Summarize)\n```\n\n**When to use each:**\n- **Inline**: Quick prototyping, simple tasks\n- **Class**: Complex tasks, type hints, better documentation\n\n### 2. Modules\n\nModules are reusable components that transform inputs to outputs:\n\n#### dspy.Predict\nBasic prediction module:\n\n```python\npredictor = dspy.Predict(\"context, question -> answer\")\nresult = predictor(context=\"Paris is the capital of France\",\n                   question=\"What is the capital?\")\n```\n\n#### dspy.ChainOfThought\nGenerates reasoning steps before answering:\n\n```python\ncot = dspy.ChainOfThought(\"question -> answer\")\nresult = cot(question=\"Why is the sky blue?\")\nprint(result.rationale)  # Reasoning steps\nprint(result.answer)     # Final answer\n```\n\n#### dspy.ReAct\nAgent-like reasoning with tools:\n\n```python\nfrom dspy.predict import ReAct\n\nclass SearchQA(dspy.Signature):\n    \"\"\"Answer questions using search.\"\"\"\n    question = dspy.InputField()\n    answer = dspy.OutputField()\n\ndef search_tool(query: str) -> str:\n    \"\"\"Search Wikipedia.\"\"\"\n    # Your search implementation\n    return results\n\nreact = ReAct(SearchQA, tools=[search_tool])\nresult = react(question=\"When was Python created?\")\n```\n\n#### dspy.ProgramOfThought\nGenerates and executes code for reasoning:\n\n```python\npot = dspy.ProgramOfThought(\"question -> answer\")\nresult = pot(question=\"What is 15% of 240?\")\n# Generates: answer = 240 * 0.15\n```\n\n### 3. Optimizers\n\nOptimizers improve your modules automatically using training data:\n\n#### BootstrapFewShot\nLearns from examples:\n\n```python\nfrom dspy.teleprompt import BootstrapFewShot\n\n# Training data\ntrainset = [\n    dspy.Example(question=\"What is 2+2?\", answer=\"4\").with_inputs(\"question\"),\n    dspy.Example(question=\"What is 3+5?\", answer=\"8\").with_inputs(\"question\"),\n]\n\n# Define metric\ndef validate_answer(example, pred, trace=None):\n    return example.answer == pred.answer\n\n# Optimize\noptimizer = BootstrapFewShot(metric=validate_answer, max_bootstrapped_demos=3)\noptimized_qa = optimizer.compile(qa, trainset=trainset)\n\n# Now optimized_qa performs better!\n```\n\n#### MIPRO (Most Important Prompt Optimization)\nIteratively improves prompts:\n\n```python\nfrom dspy.teleprompt import MIPRO\n\noptimizer = MIPRO(\n    metric=validate_answer,\n    num_candidates=10,\n    init_temperature=1.0\n)\n\noptimized_cot = optimizer.compile(\n    cot,\n    trainset=trainset,\n    num_trials=100\n)\n```\n\n#### BootstrapFinetune\nCreates datasets for model fine-tuning:\n\n```python\nfrom dspy.teleprompt import BootstrapFinetune\n\noptimizer = BootstrapFinetune(metric=validate_answer)\noptimized_module = optimizer.compile(qa, trainset=trainset)\n\n# Exports training data for fine-tuning\n```\n\n### 4. Building Complex Systems\n\n#### Multi-Stage Pipeline\n\n```python\nimport dspy\n\nclass MultiHopQA(dspy.Module):\n    def __init__(self):\n        super().__init__()\n        self.retrieve = dspy.Retrieve(k=3)\n        self.generate_query = dspy.ChainOfThought(\"question -> search_query\")\n        self.generate_answer = dspy.ChainOfThought(\"context, question -> answer\")\n\n    def forward(self, question):\n        # Stage 1: Generate search query\n        search_query = self.generate_query(question=question).search_query\n\n        # Stage 2: Retrieve context\n        passages = self.retrieve(search_query).passages\n        context = \"\\n\".join(passages)\n\n        # Stage 3: Generate answer\n        answer = self.generate_answer(context=context, question=question).answer\n        return dspy.Prediction(answer=answer, context=context)\n\n# Use the pipeline\nqa_system = MultiHopQA()\nresult = qa_system(question=\"Who wrote the book that inspired the movie Blade Runner?\")\n```\n\n#### RAG System with Optimization\n\n```python\nimport dspy\nfrom dspy.retrieve.chromadb_rm import ChromadbRM\n\n# Configure retriever\nretriever = ChromadbRM(\n    collection_name=\"documents\",\n    persist_directory=\".\u002Fchroma_db\"\n)\n\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(\"context, question -> answer\")\n\n    def forward(self, question):\n        context = self.retrieve(question).passages\n        return self.generate(context=context, question=question)\n\n# Create and optimize\nrag = RAG()\n\n# Optimize with training data\nfrom dspy.teleprompt import BootstrapFewShot\n\noptimizer = BootstrapFewShot(metric=validate_answer)\noptimized_rag = optimizer.compile(rag, trainset=trainset)\n```\n\n## LM Provider Configuration\n\n### Anthropic Claude\n\n```python\nimport dspy\n\nlm = dspy.Claude(\n    model=\"claude-sonnet-4-5-20250929\",\n    api_key=\"your-api-key\",  # Or set ANTHROPIC_API_KEY env var\n    max_tokens=1000,\n    temperature=0.7\n)\ndspy.settings.configure(lm=lm)\n```\n\n### OpenAI\n\n```python\nlm = dspy.OpenAI(\n    model=\"gpt-4\",\n    api_key=\"your-api-key\",\n    max_tokens=1000\n)\ndspy.settings.configure(lm=lm)\n```\n\n### Local Models (Ollama)\n\n```python\nlm = dspy.OllamaLocal(\n    model=\"llama3.1\",\n    base_url=\"http:\u002F\u002Flocalhost:11434\"\n)\ndspy.settings.configure(lm=lm)\n```\n\n### Multiple Models\n\n```python\n# Different models for different tasks\ncheap_lm = dspy.OpenAI(model=\"gpt-3.5-turbo\")\nstrong_lm = dspy.Claude(model=\"claude-sonnet-4-5-20250929\")\n\n# Use cheap model for retrieval, strong model for reasoning\nwith dspy.settings.context(lm=cheap_lm):\n    context = retriever(question)\n\nwith dspy.settings.context(lm=strong_lm):\n    answer = generator(context=context, question=question)\n```\n\n## Common Patterns\n\n### Pattern 1: Structured Output\n\n```python\nfrom pydantic import BaseModel, Field\n\nclass PersonInfo(BaseModel):\n    name: str = Field(description=\"Full name\")\n    age: int = Field(description=\"Age in years\")\n    occupation: str = Field(description=\"Current job\")\n\nclass ExtractPerson(dspy.Signature):\n    \"\"\"Extract person information from text.\"\"\"\n    text = dspy.InputField()\n    person: PersonInfo = dspy.OutputField()\n\nextractor = dspy.TypedPredictor(ExtractPerson)\nresult = extractor(text=\"John Doe is a 35-year-old software engineer.\")\nprint(result.person.name)  # \"John Doe\"\nprint(result.person.age)   # 35\n```\n\n### Pattern 2: Assertion-Driven Optimization\n\n```python\nimport dspy\nfrom dspy.primitives.assertions import assert_transform_module, backtrack_handler\n\nclass MathQA(dspy.Module):\n    def __init__(self):\n        super().__init__()\n        self.solve = dspy.ChainOfThought(\"problem -> solution: float\")\n\n    def forward(self, problem):\n        solution = self.solve(problem=problem).solution\n\n        # Assert solution is numeric\n        dspy.Assert(\n            isinstance(float(solution), float),\n            \"Solution must be a number\",\n            backtrack=backtrack_handler\n        )\n\n        return dspy.Prediction(solution=solution)\n```\n\n### Pattern 3: Self-Consistency\n\n```python\nimport dspy\nfrom collections import Counter\n\nclass ConsistentQA(dspy.Module):\n    def __init__(self, num_samples=5):\n        super().__init__()\n        self.qa = dspy.ChainOfThought(\"question -> answer\")\n        self.num_samples = num_samples\n\n    def forward(self, question):\n        # Generate multiple answers\n        answers = []\n        for _ in range(self.num_samples):\n            result = self.qa(question=question)\n            answers.append(result.answer)\n\n        # Return most common answer\n        most_common = Counter(answers).most_common(1)[0][0]\n        return dspy.Prediction(answer=most_common)\n```\n\n### Pattern 4: Retrieval with Reranking\n\n```python\nclass RerankedRAG(dspy.Module):\n    def __init__(self):\n        super().__init__()\n        self.retrieve = dspy.Retrieve(k=10)\n        self.rerank = dspy.Predict(\"question, passage -> relevance_score: float\")\n        self.answer = dspy.ChainOfThought(\"context, question -> answer\")\n\n    def forward(self, question):\n        # Retrieve candidates\n        passages = self.retrieve(question).passages\n\n        # Rerank passages\n        scored = []\n        for passage in passages:\n            score = float(self.rerank(question=question, passage=passage).relevance_score)\n            scored.append((score, passage))\n\n        # Take top 3\n        top_passages = [p for _, p in sorted(scored, reverse=True)[:3]]\n        context = \"\\n\\n\".join(top_passages)\n\n        # Generate answer\n        return self.answer(context=context, question=question)\n```\n\n## Evaluation and Metrics\n\n### Custom Metrics\n\n```python\ndef exact_match(example, pred, trace=None):\n    \"\"\"Exact match metric.\"\"\"\n    return example.answer.lower() == pred.answer.lower()\n\ndef f1_score(example, pred, trace=None):\n    \"\"\"F1 score for text overlap.\"\"\"\n    pred_tokens = set(pred.answer.lower().split())\n    gold_tokens = set(example.answer.lower().split())\n\n    if not pred_tokens:\n        return 0.0\n\n    precision = len(pred_tokens & gold_tokens) \u002F len(pred_tokens)\n    recall = len(pred_tokens & gold_tokens) \u002F len(gold_tokens)\n\n    if precision + recall == 0:\n        return 0.0\n\n    return 2 * (precision * recall) \u002F (precision + recall)\n```\n\n### Evaluation\n\n```python\nfrom dspy.evaluate import Evaluate\n\n# Create evaluator\nevaluator = Evaluate(\n    devset=testset,\n    metric=exact_match,\n    num_threads=4,\n    display_progress=True\n)\n\n# Evaluate model\nscore = evaluator(qa_system)\nprint(f\"Accuracy: {score}\")\n\n# Compare optimized vs unoptimized\nscore_before = evaluator(qa)\nscore_after = evaluator(optimized_qa)\nprint(f\"Improvement: {score_after - score_before:.2%}\")\n```\n\n## Best Practices\n\n### 1. Start Simple, Iterate\n\n```python\n# Start with Predict\nqa = dspy.Predict(\"question -> answer\")\n\n# Add reasoning if needed\nqa = dspy.ChainOfThought(\"question -> answer\")\n\n# Add optimization when you have data\noptimized_qa = optimizer.compile(qa, trainset=data)\n```\n\n### 2. Use Descriptive Signatures\n\n```python\n# ❌ Bad: Vague\nclass Task(dspy.Signature):\n    input = dspy.InputField()\n    output = dspy.OutputField()\n\n# ✅ Good: Descriptive\nclass SummarizeArticle(dspy.Signature):\n    \"\"\"Summarize news articles into 3-5 key points.\"\"\"\n    article = dspy.InputField(desc=\"full article text\")\n    summary = dspy.OutputField(desc=\"bullet points, 3-5 items\")\n```\n\n### 3. Optimize with Representative Data\n\n```python\n# Create diverse training examples\ntrainset = [\n    dspy.Example(question=\"factual\", answer=\"...).with_inputs(\"question\"),\n    dspy.Example(question=\"reasoning\", answer=\"...\").with_inputs(\"question\"),\n    dspy.Example(question=\"calculation\", answer=\"...\").with_inputs(\"question\"),\n]\n\n# Use validation set for metric\ndef metric(example, pred, trace=None):\n    return example.answer in pred.answer\n```\n\n### 4. Save and Load Optimized Models\n\n```python\n# Save\noptimized_qa.save(\"models\u002Fqa_v1.json\")\n\n# Load\nloaded_qa = dspy.ChainOfThought(\"question -> answer\")\nloaded_qa.load(\"models\u002Fqa_v1.json\")\n```\n\n### 5. Monitor and Debug\n\n```python\n# Enable tracing\ndspy.settings.configure(lm=lm, trace=[])\n\n# Run prediction\nresult = qa(question=\"...\")\n\n# Inspect trace\nfor call in dspy.settings.trace:\n    print(f\"Prompt: {call['prompt']}\")\n    print(f\"Response: {call['response']}\")\n```\n\n## Comparison to Other Approaches\n\n| Feature | Manual Prompting | LangChain | DSPy |\n|---------|-----------------|-----------|------|\n| Prompt Engineering | Manual | Manual | Automatic |\n| Optimization | Trial & error | None | Data-driven |\n| Modularity | Low | Medium | High |\n| Type Safety | No | Limited | Yes (Signatures) |\n| Portability | Low | Medium | High |\n| Learning Curve | Low | Medium | Medium-High |\n\n**When to choose DSPy:**\n- You have training data or can generate it\n- You need systematic prompt improvement\n- You're building complex multi-stage systems\n- You want to optimize across different LMs\n\n**When to choose alternatives:**\n- Quick prototypes (manual prompting)\n- Simple chains with existing tools (LangChain)\n- Custom optimization logic needed\n\n## Resources\n\n- **Documentation**: https:\u002F\u002Fdspy.ai\n- **GitHub**: https:\u002F\u002Fgithub.com\u002Fstanfordnlp\u002Fdspy (22k+ stars)\n- **Discord**: https:\u002F\u002Fdiscord.gg\u002FXCGy2WDCQB\n- **Twitter**: @DSPyOSS\n- **Paper**: \"DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines\"\n\n## See Also\n\n- `references\u002Fmodules.md` - Detailed module guide (Predict, ChainOfThought, ReAct, ProgramOfThought)\n- `references\u002Foptimizers.md` - Optimization algorithms (BootstrapFewShot, MIPRO, BootstrapFinetune)\n- `references\u002Fexamples.md` - Real-world examples (RAG, agents, classifiers)\n\n\n","# DSPy: Declarative Language Model Programming\n\n## When to Use This Skill\n\nUse DSPy when you need to:\n- **Build complex AI systems** with multiple components and workflows\n- **Program LMs declaratively** instead of manual prompt engineering\n- **Optimize prompts automatically** using data-driven methods\n- **Create modular AI pipelines** that are maintainable and portable\n- **Improve model outputs systematically** with optimizers\n- **Build RAG systems, agents, or classifiers** with better reliability\n\n**GitHub Stars**: 22,000+ | **Created By**: Stanford NLP\n\n## Installation\n\n```bash\n# Stable release\npip install dspy\n\n# Latest development version\npip install git+https:\u002F\u002Fgithub.com\u002Fstanfordnlp\u002Fdspy.git\n\n# With specific LM providers\npip install dspy[openai]        # OpenAI\npip install dspy[anthropic]     # Anthropic Claude\npip install dspy[all]           # All providers\n```\n\n## Quick Start\n\n### Basic Example: Question Answering\n\n```python\nimport dspy\n\n# Configure your language model\nlm = dspy.Claude(model=\"claude-sonnet-4-5-20250929\")\ndspy.settings.configure(lm=lm)\n\n# Define a signature (input → output)\nclass QA(dspy.Signature):\n    \"\"\"Answer questions with short factual answers.\"\"\"\n    question = dspy.InputField()\n    answer = dspy.OutputField(desc=\"often between 1 and 5 words\")\n\n# Create a module\nqa = dspy.Predict(QA)\n\n# Use it\nresponse = qa(question=\"What is the capital of France?\")\nprint(response.answer)  # \"Paris\"\n```\n\n### Chain of Thought Reasoning\n\n```python\nimport dspy\n\nlm = dspy.Claude(model=\"claude-sonnet-4-5-20250929\")\ndspy.settings.configure(lm=lm)\n\n# Use ChainOfThought for better reasoning\nclass MathProblem(dspy.Signature):\n    \"\"\"Solve math word problems.\"\"\"\n    problem = dspy.InputField()\n    answer = dspy.OutputField(desc=\"numerical answer\")\n\n# ChainOfThought generates reasoning steps automatically\ncot = dspy.ChainOfThought(MathProblem)\n\nresponse = cot(problem=\"If John has 5 apples and gives 2 to Mary, how many does he have?\")\nprint(response.rationale)  # Shows reasoning steps\nprint(response.answer)     # \"3\"\n```\n\n## Core Concepts\n\n### 1. Signatures\n\nSignatures define the structure of your AI task (inputs → outputs):\n\n```python\n# Inline signature (simple)\nqa = dspy.Predict(\"question -> answer\")\n\n# Class signature (detailed)\nclass Summarize(dspy.Signature):\n    \"\"\"Summarize text into key points.\"\"\"\n    text = dspy.InputField()\n    summary = dspy.OutputField(desc=\"bullet points, 3-5 items\")\n\nsummarizer = dspy.ChainOfThought(Summarize)\n```\n\n**When to use each:**\n- **Inline**: Quick prototyping, simple tasks\n- **Class**: Complex tasks, type hints, better documentation\n\n### 2. Modules\n\nModules are reusable components that transform inputs to outputs:\n\n#### dspy.Predict\nBasic prediction module:\n\n```python\npredictor = dspy.Predict(\"context, question -> answer\")\nresult = predictor(context=\"Paris is the capital of France\",\n                   question=\"What is the capital?\")\n```\n\n#### dspy.ChainOfThought\nGenerates reasoning steps before answering:\n\n```python\ncot = dspy.ChainOfThought(\"question -> answer\")\nresult = cot(question=\"Why is the sky blue?\")\nprint(result.rationale)  # Reasoning steps\nprint(result.answer)     # Final answer\n```\n\n#### dspy.ReAct\nAgent-like reasoning with tools:\n\n```python\nfrom dspy.predict import ReAct\n\nclass SearchQA(dspy.Signature):\n    \"\"\"Answer questions using search.\"\"\"\n    question = dspy.InputField()\n    answer = dspy.OutputField()\n\ndef search_tool(query: str) -> str:\n    \"\"\"Search Wikipedia.\"\"\"\n    # Your search implementation\n    return results\n\nreact = ReAct(SearchQA, tools=[search_tool])\nresult = react(question=\"When was Python created?\")\n```\n\n#### dspy.ProgramOfThought\nGenerates and executes code for reasoning:\n\n```python\npot = dspy.ProgramOfThought(\"question -> answer\")\nresult = pot(question=\"What is 15% of 240?\")\n# Generates: answer = 240 * 0.15\n```\n\n### 3. Optimizers\n\nOptimizers improve your modules automatically using training data:\n\n#### BootstrapFewShot\nLearns from examples:\n\n```python\nfrom dspy.teleprompt import BootstrapFewShot\n\n# Training data\ntrainset = [\n    dspy.Example(question=\"What is 2+2?\", answer=\"4\").with_inputs(\"question\"),\n    dspy.Example(question=\"What is 3+5?\", answer=\"8\").with_inputs(\"question\"),\n]\n\n# Define metric\ndef validate_answer(example, pred, trace=None):\n    return example.answer == pred.answer\n\n# Optimize\noptimizer = BootstrapFewShot(metric=validate_answer, max_bootstrapped_demos=3)\noptimized_qa = optimizer.compile(qa, trainset=trainset)\n\n# Now optimized_qa performs better!\n```\n\n#### MIPRO (Most Important Prompt Optimization)\nIteratively improves prompts:\n\n```python\nfrom dspy.teleprompt import MIPRO\n\noptimizer = MIPRO(\n    metric=validate_answer,\n    num_candidates=10,\n    init_temperature=1.0\n)\n\noptimized_cot = optimizer.compile(\n    cot,\n    trainset=trainset,\n    num_trials=100\n)\n```\n\n#### BootstrapFinetune\nCreates datasets for model fine-tuning:\n\n```python\nfrom dspy.teleprompt import BootstrapFinetune\n\noptimizer = BootstrapFinetune(metric=validate_answer)\noptimized_module = optimizer.compile(qa, trainset=trainset)\n\n# Exports training data for fine-tuning\n```\n\n### 4. Building Complex Systems\n\n#### Multi-Stage Pipeline\n\n```python\nimport dspy\n\nclass MultiHopQA(dspy.Module):\n    def __init__(self):\n        super().__init__()\n        self.retrieve = dspy.Retrieve(k=3)\n        self.generate_query = dspy.ChainOfThought(\"question -> search_query\")\n        self.generate_answer = dspy.ChainOfThought(\"context, question -> answer\")\n\n    def forward(self, question):\n        # Stage 1: Generate search query\n        search_query = self.generate_query(question=question).search_query\n\n        # Stage 2: Retrieve context\n        passages = self.retrieve(search_query).passages\n        context = \"\\n\".join(passages)\n\n        # Stage 3: Generate answer\n        answer = self.generate_answer(context=context, question=question).answer\n        return dspy.Prediction(answer=answer, context=context)\n\n# Use the pipeline\nqa_system = MultiHopQA()\nresult = qa_system(question=\"Who wrote the book that inspired the movie Blade Runner?\")\n```\n\n#### RAG System with Optimization\n\n```python\nimport dspy\nfrom dspy.retrieve.chromadb_rm import ChromadbRM\n\n# Configure retriever\nretriever = ChromadbRM(\n    collection_name=\"documents\",\n    persist_directory=\".\u002Fchroma_db\"\n)\n\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(\"context, question -> answer\")\n\n    def forward(self, question):\n        context = self.retrieve(question).passages\n        return self.generate(context=context, question=question)\n\n# Create and optimize\nrag = RAG()\n\n# Optimize with training data\nfrom dspy.teleprompt import BootstrapFewShot\n\noptimizer = BootstrapFewShot(metric=validate_answer)\noptimized_rag = optimizer.compile(rag, trainset=trainset)\n```\n\n## LM Provider Configuration\n\n### Anthropic Claude\n\n```python\nimport dspy\n\nlm = dspy.Claude(\n    model=\"claude-sonnet-4-5-20250929\",\n    api_key=\"your-api-key\",  # Or set ANTHROPIC_API_KEY env var\n    max_tokens=1000,\n    temperature=0.7\n)\ndspy.settings.configure(lm=lm)\n```\n\n### OpenAI\n\n```python\nlm = dspy.OpenAI(\n    model=\"gpt-4\",\n    api_key=\"your-api-key\",\n    max_tokens=1000\n)\ndspy.settings.configure(lm=lm)\n```\n\n### Local Models (Ollama)\n\n```python\nlm = dspy.OllamaLocal(\n    model=\"llama3.1\",\n    base_url=\"http:\u002F\u002Flocalhost:11434\"\n)\ndspy.settings.configure(lm=lm)\n```\n\n### Multiple Models\n\n```python\n# Different models for different tasks\ncheap_lm = dspy.OpenAI(model=\"gpt-3.5-turbo\")\nstrong_lm = dspy.Claude(model=\"claude-sonnet-4-5-20250929\")\n\n# Use cheap model for retrieval, strong model for reasoning\nwith dspy.settings.context(lm=cheap_lm):\n    context = retriever(question)\n\nwith dspy.settings.context(lm=strong_lm):\n    answer = generator(context=context, question=question)\n```\n\n## Common Patterns\n\n### Pattern 1: Structured Output\n\n```python\nfrom pydantic import BaseModel, Field\n\nclass PersonInfo(BaseModel):\n    name: str = Field(description=\"Full name\")\n    age: int = Field(description=\"Age in years\")\n    occupation: str = Field(description=\"Current job\")\n\nclass ExtractPerson(dspy.Signature):\n    \"\"\"Extract person information from text.\"\"\"\n    text = dspy.InputField()\n    person: PersonInfo = dspy.OutputField()\n\nextractor = dspy.TypedPredictor(ExtractPerson)\nresult = extractor(text=\"John Doe is a 35-year-old software engineer.\")\nprint(result.person.name)  # \"John Doe\"\nprint(result.person.age)   # 35\n```\n\n### Pattern 2: Assertion-Driven Optimization\n\n```python\nimport dspy\nfrom dspy.primitives.assertions import assert_transform_module, backtrack_handler\n\nclass MathQA(dspy.Module):\n    def __init__(self):\n        super().__init__()\n        self.solve = dspy.ChainOfThought(\"problem -> solution: float\")\n\n    def forward(self, problem):\n        solution = self.solve(problem=problem).solution\n\n        # Assert solution is numeric\n        dspy.Assert(\n            isinstance(float(solution), float),\n            \"Solution must be a number\",\n            backtrack=backtrack_handler\n        )\n\n        return dspy.Prediction(solution=solution)\n```\n\n### Pattern 3: Self-Consistency\n\n```python\nimport dspy\nfrom collections import Counter\n\nclass ConsistentQA(dspy.Module):\n    def __init__(self, num_samples=5):\n        super().__init__()\n        self.qa = dspy.ChainOfThought(\"question -> answer\")\n        self.num_samples = num_samples\n\n    def forward(self, question):\n        # Generate multiple answers\n        answers = []\n        for _ in range(self.num_samples):\n            result = self.qa(question=question)\n            answers.append(result.answer)\n\n        # Return most common answer\n        most_common = Counter(answers).most_common(1)[0][0]\n        return dspy.Prediction(answer=most_common)\n```\n\n### Pattern 4: Retrieval with Reranking\n\n```python\nclass RerankedRAG(dspy.Module):\n    def __init__(self):\n        super().__init__()\n        self.retrieve = dspy.Retrieve(k=10)\n        self.rerank = dspy.Predict(\"question, passage -> relevance_score: float\")\n        self.answer = dspy.ChainOfThought(\"context, question -> answer\")\n\n    def forward(self, question):\n        # Retrieve candidates\n        passages = self.retrieve(question).passages\n\n        # Rerank passages\n        scored = []\n        for passage in passages:\n            score = float(self.rerank(question=question, passage=passage).relevance_score)\n            scored.append((score, passage))\n\n        # Take top 3\n        top_passages = [p for _, p in sorted(scored, reverse=True)[:3]]\n        context = \"\\n\\n\".join(top_passages)\n\n        # Generate answer\n        return self.answer(context=context, question=question)\n```\n\n## Evaluation and Metrics\n\n### Custom Metrics\n\n```python\ndef exact_match(example, pred, trace=None):\n    \"\"\"Exact match metric.\"\"\"\n    return example.answer.lower() == pred.answer.lower()\n\ndef f1_score(example, pred, trace=None):\n    \"\"\"F1 score for text overlap.\"\"\"\n    pred_tokens = set(pred.answer.lower().split())\n    gold_tokens = set(example.answer.lower().split())\n\n    if not pred_tokens:\n        return 0.0\n\n    precision = len(pred_tokens & gold_tokens) \u002F len(pred_tokens)\n    recall = len(pred_tokens & gold_tokens) \u002F len(gold_tokens)\n\n    if precision + recall == 0:\n        return 0.0\n\n    return 2 * (precision * recall) \u002F (precision + recall)\n```\n\n### Evaluation\n\n```python\nfrom dspy.evaluate import Evaluate\n\n# Create evaluator\nevaluator = Evaluate(\n    devset=testset,\n    metric=exact_match,\n    num_threads=4,\n    display_progress=True\n)\n\n# Evaluate model\nscore = evaluator(qa_system)\nprint(f\"Accuracy: {score}\")\n\n# Compare optimized vs unoptimized\nscore_before = evaluator(qa)\nscore_after = evaluator(optimized_qa)\nprint(f\"Improvement: {score_after - score_before:.2%}\")\n```\n\n## Best Practices\n\n### 1. Start Simple, Iterate\n\n```python\n# Start with Predict\nqa = dspy.Predict(\"question -> answer\")\n\n# Add reasoning if needed\nqa = dspy.ChainOfThought(\"question -> answer\")\n\n# Add optimization when you have data\noptimized_qa = optimizer.compile(qa, trainset=data)\n```\n\n### 2. Use Descriptive Signatures\n\n```python\n# ❌ Bad: Vague\nclass Task(dspy.Signature):\n    input = dspy.InputField()\n    output = dspy.OutputField()\n\n# ✅ Good: Descriptive\nclass SummarizeArticle(dspy.Signature):\n    \"\"\"Summarize news articles into 3-5 key points.\"\"\"\n    article = dspy.InputField(desc=\"full article text\")\n    summary = dspy.OutputField(desc=\"bullet points, 3-5 items\")\n```\n\n### 3. Optimize with Representative Data\n\n```python\n# Create diverse training examples\ntrainset = [\n    dspy.Example(question=\"factual\", answer=\"...).with_inputs(\"question\"),\n    dspy.Example(question=\"reasoning\", answer=\"...\").with_inputs(\"question\"),\n    dspy.Example(question=\"calculation\", answer=\"...\").with_inputs(\"question\"),\n]\n\n# Use validation set for metric\ndef metric(example, pred, trace=None):\n    return example.answer in pred.answer\n```\n\n### 4. Save and Load Optimized Models\n\n```python\n# Save\noptimized_qa.save(\"models\u002Fqa_v1.json\")\n\n# Load\nloaded_qa = dspy.ChainOfThought(\"question -> answer\")\nloaded_qa.load(\"models\u002Fqa_v1.json\")\n```\n\n### 5. Monitor and Debug\n\n```python\n# Enable tracing\ndspy.settings.configure(lm=lm, trace=[])\n\n# Run prediction\nresult = qa(question=\"...\")\n\n# Inspect trace\nfor call in dspy.settings.trace:\n    print(f\"Prompt: {call['prompt']}\")\n    print(f\"Response: {call['response']}\")\n```\n\n## Comparison to Other Approaches\n\n| Feature | Manual Prompting | LangChain | DSPy |\n|---------|-----------------|-----------|------|\n| Prompt Engineering | Manual | Manual | Automatic |\n| Optimization | Trial & error | None | Data-driven |\n| Modularity | Low | Medium | High |\n| Type Safety | No | Limited | Yes (Signatures) |\n| Portability | Low | Medium | High |\n| Learning Curve | Low | Medium | Medium-High |\n\n**When to choose DSPy:**\n- You have training data or can generate it\n- You need systematic prompt improvement\n- You're building complex multi-stage systems\n- You want to optimize across different LMs\n\n**When to choose alternatives:**\n- Quick prototypes (manual prompting)\n- Simple chains with existing tools (LangChain)\n- Custom optimization logic needed\n\n## Resources\n\n- **Documentation**: https:\u002F\u002Fdspy.ai\n- **GitHub**: https:\u002F\u002Fgithub.com\u002Fstanfordnlp\u002Fdspy (22k+ stars)\n- **Discord**: https:\u002F\u002Fdiscord.gg\u002FXCGy2WDCQB\n- **Twitter**: @DSPyOSS\n- **Paper**: \"DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines\"\n\n## See Also\n\n- `references\u002Fmodules.md` - Detailed module guide (Predict, ChainOfThought, ReAct, ProgramOfThought)\n- `references\u002Foptimizers.md` - Optimization algorithms (BootstrapFewShot, MIPRO, BootstrapFinetune)\n- `references\u002Fexamples.md` - Real-world examples (RAG, agents, classifiers)\n\n\n",{"name":5,"author":52,"license":14,"version":53,"metadata":54,"description":7,"dependencies":61},"Orchestra Research","1.0.0",{"hermes":55},{"tags":56},[57,58,46,59,60,45,44,43,42,41],"Prompt Engineering","DSPy","RAG","Agents",[5,35,62],"anthropic","master","a76a61826887171cb38ce217ae85643d5d7685d1","hermes_home\u002Fprofiles\u002Flive\u002Fskills\u002Fmlops\u002Fresearch\u002Fdspy",2,"https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01","https:\u002F\u002Fgithub.com\u002F0xzgbot","2026-08-31 09:13:28",{"source":8,"skillName":5,"commands":71,"prompt":81,"zipAvailable":82},[72,75,78],{"tool":73,"command":74},"npx","npx skills add 0xzgbot\u002Fforge-nps-v01 --skill dspy",{"tool":76,"command":77},"bunx","bunx skills add 0xzgbot\u002Fforge-nps-v01 --skill dspy",{"tool":79,"command":80},"pnpm","pnpm dlx skills add 0xzgbot\u002Fforge-nps-v01 --skill dspy","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fprofiles\u002Flive\u002Fskills\u002Fmlops\u002Fresearch\u002Fdspy\u002FSKILL.md",true,[84,90,94],{"path":85,"resourceType":86,"resourceTypeDesc":87,"sizeBytes":88,"sourceHtmlUrl":89},"references\u002Fexamples.md","REFERENCE","参考文档",18983,"https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fprofiles\u002Flive\u002Fskills\u002Fmlops\u002Fresearch\u002Fdspy\u002Freferences\u002Fexamples.md",{"path":91,"resourceType":86,"resourceTypeDesc":87,"sizeBytes":92,"sourceHtmlUrl":93},"references\u002Fmodules.md",12363,"https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fprofiles\u002Flive\u002Fskills\u002Fmlops\u002Fresearch\u002Fdspy\u002Freferences\u002Fmodules.md",{"path":95,"resourceType":86,"resourceTypeDesc":87,"sizeBytes":96,"sourceHtmlUrl":97},"references\u002Foptimizers.md",14933,"https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fprofiles\u002Flive\u002Fskills\u002Fmlops\u002Fresearch\u002Fdspy\u002Freferences\u002Foptimizers.md",[99,112,130,144,155,167],{"skillNo":100,"name":101,"nameZh":6,"description":102,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":103,"categories":104,"keywords":105,"publishedAt":47},"01M71J5TFCV7K97773JYZE7ET8","yuanbao","Yuanbao (元宝) groups: @mention users, query info\u002Fmembers.","https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fskills\u002Fyuanbao\u002FSKILL.md",[],[101,106,107,108,109,110,111],"groups","at","info","users","query","group",{"skillNo":113,"name":114,"nameZh":6,"description":115,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":116,"categories":117,"keywords":122,"publishedAt":47},"01M71J5QR69MCT5J8P3B4Y9ZHA","workflow-ltx-i2v","Use when the task involves workflow ltx i2v. DESCRIPTION.","https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fskills\u002Fworkflow_ltx_i2v\u002FSKILL.md",[118,121],{"categoryNo":119,"categoryName":120,"taxonomyType":20,"taxonomyTypeDesc":21},"01JBSEEDCAT000000000000016","图像与多媒体",{"categoryNo":18,"categoryName":19,"taxonomyType":20,"taxonomyTypeDesc":21},[123,124,125,126,127,128,129],"ltx","workflow ltx i2v","workflow","Production","task","when","use",{"skillNo":131,"name":132,"nameZh":6,"description":133,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":134,"categories":135,"keywords":138,"publishedAt":47},"01M71J5NYW853AAPB17VR0F7ZX","workflow-flux2-text-to-image","Flux2 Text-to-Image Workflow Reference.","https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fskills\u002Fworkflow_flux2_text_to_image\u002FSKILL.md",[136,137],{"categoryNo":119,"categoryName":120,"taxonomyType":20,"taxonomyTypeDesc":21},{"categoryNo":18,"categoryName":19,"taxonomyType":20,"taxonomyTypeDesc":21},[139,140,125,141,142,126,143],"workflow flux2 text to image","image","text","reference","Text-to-Image",{"skillNo":145,"name":146,"nameZh":6,"description":147,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":148,"categories":149,"keywords":151,"publishedAt":47},"01M71J5KWY96MD6B6YC2XP6NXB","wide-angle-environmental","Use when the task involves wide angle environmental. DESCRIPTION.","https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fskills\u002Fwide_angle_environmental\u002FSKILL.md",[150],{"categoryNo":18,"categoryName":19,"taxonomyType":20,"taxonomyTypeDesc":21},[152,153,154,126,127,128,129],"environmental","wide","wide angle environmental",{"skillNo":156,"name":157,"nameZh":6,"description":158,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":159,"categories":160,"keywords":162,"publishedAt":47},"01M71J5DXHECTDVDF8HRP81YFY","wes-anderson-specialist","Use when the task involves wes anderson specialist. 1. EXECUTIVE SUMMARY.","https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fskills\u002Fwes_anderson_specialist\u002FSKILL.md",[161],{"categoryNo":18,"categoryName":19,"taxonomyType":20,"taxonomyTypeDesc":21},[163,164,165,166,128,129,127,126],"specialist","wes anderson specialist","summary","executive summary",{"skillNo":168,"name":169,"nameZh":6,"description":170,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":171,"categories":172,"keywords":174,"publishedAt":47},"01M71J5BRHX2N4BRG2VQMWJK3E","volumetric-god-rays-lighting","Use when the task involves volumetric god rays lighting. DESCRIPTION.","https:\u002F\u002Fgithub.com\u002F0xzgbot\u002Fforge-nps-v01\u002Fblob\u002Fmaster\u002Fhermes_home\u002Fskills\u002Fvolumetric_god_rays_lighting\u002FSKILL.md",[173],{"categoryNo":18,"categoryName":19,"taxonomyType":20,"taxonomyTypeDesc":21},[175,176,129,128,127,126],"volumetric god rays lighting","lighting"]