Chapter 6 · Section 8

Exercises

Apply your knowledge to build comprehensive real-world applications with DSPy.

~30 min read

Overview

These exercises provide hands-on practice building complete, production-ready applications with DSPy. You'll work with all the concepts learned in previous chapters—signatures, modules, evaluation, and optimization—to solve real-world problems.

Exercise 1: Customer Support RAG System

Objective: Create a complete RAG system for customer support that can answer questions about product documentation and policies.

Tasks

  • Complete the CustomerSupportRAG class implementation.
  • Add modules for question understanding and answer generation.
  • Implement the create_support_rag function.
  • Create training data and optimize with BootstrapFewShot.
  • Test with support-related questions.
Python
import dspy

class CustomerSupportRAG(dspy.Module):
    def __init__(self):
        super().__init__()
        self.retrieve = dspy.Retrieve(k=5)
        # TODO: Add modules

    def forward(self, question):
        # TODO: Implement RAG pipeline
        pass

Exercise 2: Multi-hop Research Assistant

Objective: Build a system that can answer complex research questions by gathering information from multiple sources.

Tasks

  • Implement multi-hop search logic.
  • Add modules for connecting information across documents.
  • Create a comprehensive evaluation metric.
  • Optimize with MIPRO for complex reasoning.
Python
class ResearchAssistant(dspy.Module):
    def __init__(self):
        super().__init__()
        self.retrieve = dspy.Retrieve(k=5)
        # TODO: Add modules for multi-hop reasoning

    def forward(self, research_question):
        # TODO: Implement multi-hop search and synthesis
        pass

Exercise 3: Multi-label Document Classifier

Objective: Build a sophisticated classifier that can assign multiple labels to documents based on their content.

Tasks

  • Implement multi-label classification logic.
  • Handle label dependencies (some labels co-occur).
  • Create appropriate training data and evaluation metrics.
  • Optimize with appropriate DSPy optimizer.

Exercise 4: Contract Information Extractor

Objective: Build an entity extraction system specifically designed for legal contracts.

Tasks

  • Identify contract-specific entity types.
  • Implement extraction for parties, dates, amounts, obligations.
  • Add validation to ensure extracted info is accurate.
  • Create a relationship extractor for contract clauses.

Exercise 5: Autonomous Customer Service Agent

Objective: Create an intelligent agent that can handle customer service interactions from start to finish.

Tasks

  • Implement intent classification for customer messages.
  • Add modules for handling different types of requests and escalation logic.
  • Add memory to maintain conversation context.
  • Optimize with real conversation data.

Exercise 6: Code Review Assistant

Objective: Build an automated code review assistant that can analyze code for bugs, security issues, and style violations.

Tasks

  • Implement analysis for different code quality aspects.
  • Detect common bugs, anti-patterns, and security vulnerabilities.
  • Ensure adherence to coding standards.
  • Generate a comprehensive review report.