DSPy Tutorial 2026: Automatic LLM Prompt Optimization

Replace manual prompt engineering with DSPy automatic optimization

返回教程列表
高级40 分钟

DSPy Tutorial 2026: Automatic LLM Prompt Optimization

Replace manual prompt engineering with DSPy automatic optimization

Complete DSPy tutorial. Covers typed signatures, chain-of-thought reasoning, building RAG pipelines, and automatic optimization with MIPROv2 using training examples and metrics.

dspyprompt engineeringllmoptimizationrag

DSPy Tutorial 2026: Optimize LLM Prompts Automatically

DSPy is a framework for programming LLMs with automatic prompt optimization, eliminating the need for manual prompt engineering.

What Makes DSPy Different?

Traditional prompt engineering: manually tweak prompts until they work.

DSPy approach:

  • Define your task as a program with typed inputs/outputs
  • Provide a few examples (training data)
  • Let DSPy's optimizer automatically find the best prompts and few-shot examples
  • Installation

    bash
    pip install dspy-ai
    

    Basic Signature and Module

    python
    import dspy
    from dspy import OpenAI

    Configure LLM

    gpt4 = dspy.OpenAI(model='gpt-4o-mini', max_tokens=500) dspy.settings.configure(lm=gpt4)

    Define task with typed signature

    class SentimentAnalysis(dspy.Signature): 'Analyze the sentiment of the given text.' text: str = dspy.InputField(desc='Text to analyze') sentiment: str = dspy.OutputField(desc='Sentiment: positive, negative, or neutral') confidence: float = dspy.OutputField(desc='Confidence score between 0 and 1') reasoning: str = dspy.OutputField(desc='Brief explanation of the sentiment')

    Use the signature

    predictor = dspy.Predict(SentimentAnalysis) result = predictor(text='The new MacBook Pro is absolutely incredible!') print(result.sentiment) # positive print(result.confidence) # 0.95 print(result.reasoning)

    Chain-of-Thought Reasoning

    python
    

    ChainOfThought adds step-by-step reasoning automatically

    cot_predictor = dspy.ChainOfThought(SentimentAnalysis) result = cot_predictor(text='Despite the high price, the product quality exceeded my expectations.') print(result.rationale) # Intermediate reasoning steps print(result.sentiment)

    Building a RAG Pipeline

    python
    class RAGSignature(dspy.Signature):
        'Answer a question using retrieved context.'
        context: list[str] = dspy.InputField(desc='Retrieved documents')
        question: str = dspy.InputField()
        answer: str = dspy.OutputField(desc='Concise answer based on context')
        citations: list[str] = dspy.OutputField(desc='Which context pieces were used')

    class RAGProgram(dspy.Module): def __init__(self, retriever, k=5): self.retriever = retriever self.k = k self.generate_answer = dspy.ChainOfThought(RAGSignature) def forward(self, question: str): context = self.retriever.top_k(question, k=self.k) return self.generate_answer(context=context, question=question)

    rag = RAGProgram(retriever=my_retriever) result = rag(question='What is the difference between HNSW and IVFFlat indexes?') print(result.answer)

    Automatic Optimization with MIPROv2

    python
    from dspy.teleprompt import MIPROv2

    Training examples

    trainset = [ dspy.Example( text='Great product! Fast shipping!', sentiment='positive', confidence=0.9 ).with_inputs('text'), dspy.Example( text='Product broke after one week. Terrible quality.', sentiment='negative', confidence=0.95 ).with_inputs('text'), # Add 20-50 examples for best results ]

    Metric to optimize

    def sentiment_accuracy(example, prediction, trace=None): return example.sentiment == prediction.sentiment

    Optimize with MIPROv2 (state-of-the-art optimizer)

    optimizer = MIPROv2(metric=sentiment_accuracy, num_candidates=10) optimized_program = optimizer.compile( predictor, trainset=trainset, num_trials=20 )

    The optimized program now has better prompts auto-generated

    result = optimized_program(text='Amazing performance and great value!') print(result.sentiment) # More accurate after optimization

    Save and load optimized program

    optimized_program.save('sentiment_optimized.json') loaded = predictor.__class__.load('sentiment_optimized.json')

    Conclusion

    DSPy transforms prompt engineering from an art to a science. Define your task, provide examples, and let DSPy find optimal prompts automatically. Particularly powerful for RAG, classification, and multi-step reasoning pipelines.

    相关工具

    dspyopenaipython