### Install and Run DSPy Examples Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Installs DSPy, pulls a language model, and runs basic and persona examples. ```bash make install ollama pull llama3 python examples/basic/hello_world.py python examples/personas/support_sam.py ``` -------------------------------- ### Clone and Setup DSPy Project Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/README.md Clone the DSPy 0-to-1 guide repository and set up the development environment using make commands. ```bash git clone cd dspy-0to1-guide make dev-setup ``` -------------------------------- ### Clone Repository and Set Up Development Environment Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Use this option to clone the DSPy repository and set up a local development environment, including activating a virtual environment and running a quick start example. ```bash git clone cd dspy-0to1-guide make dev-setup source .venv/bin/activate make quick-start ``` -------------------------------- ### Run First DSPy Example Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/README.md Execute the initial quick-start example after setting up the DSPy environment. ```bash make quick-start ``` -------------------------------- ### Run DSPy Repository Examples Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Execute pre-built examples to test DSPy's capabilities. Use these commands to run basic, advanced, or all available examples. ```bash # Basic examples make run-basic # Advanced examples make run-advanced # All examples make run-all-examples ``` -------------------------------- ### Install Ollama on Linux Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Install Ollama on Linux by downloading and executing the official installation script. ```bash curl -fsSL https://ollama.ai/install.sh | sh ``` -------------------------------- ### DSPy Project Repository Structure Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/README.md Overview of the directory structure for the DSPy 0-to-1 guide project, including documentation, examples, datasets, and source code. ```bash ├── docs/ # Comprehensive documentation ├── examples/ │ ├── basic/ # Hello world, basic patterns │ ├── personas/ # Real-world use cases │ ├── advanced/ # GEPA, Pydantic, async │ └── infrastructure/ # Monitoring, deployment ├── datasets/ # Sample data and metrics ├── scripts/ # Automation and utilities ├── tests/ # Comprehensive test suite └── src/ # Reusable components ``` -------------------------------- ### Copy Example Environment File Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Copy the example environment file to create a new .env file for your configuration. ```bash cp .env.example .env ``` -------------------------------- ### Available DSPy Project Commands Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/README.md List of make commands available for managing the DSPy project, including installation, testing, running examples, evaluation, and Docker builds. ```bash make help # Show all available commands make install # Install dependencies make test # Run tests make run-examples # Run all examples make evaluate # Run evaluation pipeline make docker-build # Build Docker image ``` -------------------------------- ### Basic DSPy Installation Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Install the core DSPy package using pip. ```bash pip install dspy-ai ``` -------------------------------- ### Install and Run Ollama Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Install Ollama for local LLM execution and pull a model. This is an optional step for privacy or cost reasons. ```bash brew install ollama # MacOS example ``` ```bash ollama pull llama3 ``` ```bash ollama serve ``` -------------------------------- ### DSPy Development Installation Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Install DSPy with specific dependencies suitable for development, including transformers, sentence-transformers, and faiss-cpu. ```bash pip install dspy-ai transformers sentence-transformers faiss-cpu ``` -------------------------------- ### Check DSPy Installation and Version Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Imports the DSPy library to confirm its installation and prints the installed version and file location, useful for troubleshooting module not found errors. ```python # Check DSPy installation import dspy print(f"DSPy version: {dspy.__version__}") print(f"DSPy location: {dspy.__file__}") ``` -------------------------------- ### Install Ollama on macOS Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Install Ollama on macOS using Homebrew. ```bash brew install ollama ``` -------------------------------- ### DSPy Evaluation Setup Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Initialize DSPy's Evaluate class with a training dataset and a chosen metric, such as answer_exact_match. ```python from dspy import Evaluate, metrics evaluate = Evaluate( trainset=train_examples, # Assuming train_examples is defined as above metric=metrics.answer_exact_match, # use exact match metric ) ``` -------------------------------- ### DSPy Installation with Additional Dependencies Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Install DSPy along with a comprehensive set of additional dependencies for extended functionality. ```bash pip install dspy-ai[all] ``` -------------------------------- ### Build a DSPy Agent with a Calculator Tool Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Create an agent that can reason and act by interacting with external tools. This example demonstrates an agent with a calculator tool using the ReAct module. ```python import dspy # Configure the model dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) # Define a calculator tool def calculator(expression: str) -> float: return eval(expression) # Create the agent module class CalculatorAgent(dspy.Module): def __init__(self): super().__init__() # Register the calculator tool with the ReAct module self.react = dspy.ReAct("question -> answer", tools=[calculator]) def forward(self, question: str): return self.react(question=question) agent = CalculatorAgent() print(agent("What is 2 + 2 * 5?")) # The model can call calculator() to compute the answer ``` -------------------------------- ### DSPy Evaluation Dataset Preparation Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Prepare a dataset for DSPy evaluation using dspy.Example objects. Each example should contain inputs and expected outputs, with inputs explicitly defined. ```python from dspy import Example # Define some QA examples train_examples = [ Example(question="What is the capital of France?", answer="Paris").with_inputs("question"), Example(question="Who wrote 1984?", answer="George Orwell").with_inputs("question"), ] ``` -------------------------------- ### DSPy Core Concepts Example Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Demonstrates the integration of signatures, modules, optimizers, and evaluation in DSPy. This example defines a sentiment analysis task, creates a module to perform it, prepares training data, optimizes the module using BootstrapFewShot, and evaluates both the baseline and optimized modules. ```python import dspy from dspy import Example, Evaluate, metrics from dspy.teleprompt import BootstrapFewShot # 1. Define signature (what we want to accomplish) class SentimentAnalysis(dspy.Signature): """Analyze sentiment in customer reviews.""" review: str = dspy.InputField(desc="Customer review text") sentiment: str = dspy.OutputField(desc="positive, negative, or neutral") confidence: float = dspy.OutputField(desc="Confidence score 0-1") # 2. Create module (how to accomplish it) class SentimentAnalyzer(dspy.Module): def __init__(self): super().__init__() self.analyze = dspy.ChainOfThought(SentimentAnalysis) def forward(self, review: str): return self.analyze(review=review) # 3. Prepare data for optimization train_data = [ Example(review="This product is amazing!", sentiment="positive", confidence=0.9), Example(review="Terrible quality, waste of money", sentiment="negative", confidence=0.95), # ... more examples ] # 4. Optimize the module analyzer = SentimentAnalyzer() optimizer = BootstrapFewShot(metric=metrics.answer_exact_match) optimized_analyzer = optimizer.compile(analyzer, trainset=train_data) # 5. Evaluate performance evaluator = Evaluate(testset=test_data, metric=metrics.answer_exact_match) baseline_score = evaluator(analyzer) optimized_score = evaluator(optimized_analyzer) print(f"Baseline: {baseline_score:.2%}") print(f"Optimized: {optimized_score:.2%}") ``` -------------------------------- ### Pull and Run Ollama Model Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Pull the Llama3 model from Ollama and start the Ollama server. ```bash ollama pull llama3 ollama serve ``` -------------------------------- ### BootstrapFewShot Optimizer Configuration Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Configure the BootstrapFewShot optimizer to automatically generate few-shot examples for improving a DSPy module. Specify metrics and the number of examples to generate and use. ```python from dspy.teleprompt import BootstrapFewShot from dspy import metrics, Example # Prepare training data train_examples = [ Example( question="What is the capital of France?", answer="Paris" ).with_inputs("question"), Example( question="Who wrote Romeo and Juliet?", answer="William Shakespeare" ).with_inputs("question"), # ... more examples ] # Create and configure optimizer teleprompter = BootstrapFewShot( metric=metrics.answer_exact_match, max_bootstrapped_demos=5, # Number of examples to generate max_labeled_demos=3 # Number of labeled examples to use ) # Original module qa_module = SimpleQA() # Optimized version optimized_qa = teleprompter.compile(qa_module, trainset=train_examples) ``` -------------------------------- ### Install Specific DSPy Dependencies Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Installs specific common dependencies for DSPy, such as those for OpenAI, Transformers, and Sentence Transformers, which can help resolve import errors. ```bash # Or install specific dependencies pip install openai transformers sentence-transformers ``` -------------------------------- ### Example of Prompt Brittleness Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Demonstrates how a minor change in prompt wording can significantly alter LLM output quality. Version A works well, while Version B performs poorly due to the removal of a single word. ```python # Version A (works well) prompt = "Analyze the following contract carefully and extract key terms:" # Version B (performs poorly) prompt = "Analyze the following contract and extract key terms:" # Removing "carefully" significantly impacts quality ``` -------------------------------- ### Implement a ReAct Module with Tools Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md The ReAct module combines reasoning with the ability to act using tools. This example shows a CalculatorAgent that can use a 'calculator' tool to evaluate expressions during its reasoning process. ```python def calculator(expression: str) -> float: """Safe calculator function.""" return eval(expression) class CalculatorAgent(dspy.Module): def __init__(self): super().__init__() self.react = dspy.ReAct("question -> answer", tools=[calculator]) def forward(self, question: str): # Can call tools during reasoning return self.react(question=question) ``` -------------------------------- ### Python Script to Test DSPy Installation Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md This script verifies basic DSPy functionality and checks environment configurations like API keys and Python version. It's designed to be saved as a Python file and executed. ```python import dspy import os def test_basic_functionality(): """Test basic DSPy functionality.""" try: # Configure with a simple model dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) # Create a simple module predictor = dspy.Predict("input -> output") result = predictor(input="Hello DSPy!") print("✅ DSPy is working correctly!") print(f"Test result: {result.output}") return True except Exception as e: print(f"❌ Error: {e}") return False def test_environment(): """Check environment configuration.""" issues = [] # Check API keys if not os.getenv('OPENAI_API_KEY'): issues.append("OPENAI_API_KEY not found in environment") # Check Python version import sys if sys.version_info < (3, 9): issues.append(f"Python {sys.version} is too old. Requires 3.9+") if issues: print("⚠️ Environment issues:") for issue in issues: print(f" - {issue}") else: print("✅ Environment looks good!") return len(issues) == 0 if __name__ == "__main__": print("Testing DSPy installation...") env_ok = test_environment() if env_ok: func_ok = test_basic_functionality() if func_ok: print("\n🎉 DSPy is ready to use!") else: print("\n🔧 Check your configuration and try again.") else: print("\n🔧 Fix environment issues and try again.") ``` -------------------------------- ### Signature with Constrained Output and Validation Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Advanced signatures can include constraints and validation for outputs. This example shows a 'category' output constrained to specific words and a 'confidence' score with a numerical range. ```python class ClassificationTask(dspy.Signature): """Classify text into predefined categories.""" text: str = dspy.InputField() # Constrained output with specific format category: str = dspy.OutputField( desc="One of: positive, negative, neutral", format="single_word" ) confidence: float = dspy.OutputField( desc="Confidence score between 0 and 1", constraints="0 <= confidence <= 1" ) ``` -------------------------------- ### Check Ollama Service Status Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Verifies if the Ollama service is running by making a request to its API. If it's not running, you can start it using `ollama serve`. ```bash # Check if Ollama is running curl http://localhost:11434/api/tags # Start Ollama service ollama serve # Pull required model ollama pull llama3 ``` -------------------------------- ### Define Signature Output Fields with Multiple Outputs Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Output fields specify expected outputs with descriptions. This example demonstrates defining multiple outputs for complex tasks, including structured data extraction like risk scores and factors. ```python class RiskAssessment(dspy.Signature): """Assess risk levels in business documents.""" document: str = dspy.InputField() # Multiple outputs risk_score: int = dspy.OutputField(desc="Risk score from 1-10") risk_factors: str = dspy.OutputField(desc="Key risk factors identified") mitigation: str = dspy.OutputField(desc="Recommended mitigation strategies") ``` -------------------------------- ### Minimal DSPy 'Hello World' Program Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md A basic DSPy program demonstrating a custom module with Chain-of-Thought reasoning for a math question. Configure the language model before use. ```python import dspy # Configure the language model (OpenAI's gpt‑4o‑mini for this example) dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) class MathQA(dspy.Module): def __init__(self): super().__init__() # Define the module using Chain‑of‑Thought reasoning self.solve = dspy.ChainOfThought("question -> answer: float") def forward(self, question: str): return self.solve(question=question) # Instantiate and invoke the module qa = MathQA() result = qa("What is 3 * 7 + 2?") print(result) ``` -------------------------------- ### COPRO Optimizer Configuration Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Set up the COPRO (Coordinate Ascent Prompt Optimization) optimizer for advanced prompt tuning. Configure parameters like breadth and depth for optimization iterations. ```python from dspy.teleprompt import COPRO # Advanced prompt optimization copro = COPRO( metric=metrics.answer_exact_match, breadth=10, # Number of prompt candidates to try depth=3 # Optimization iterations ) optimized_module = copro.compile(module, trainset=examples) ``` -------------------------------- ### Traditional Prompt Engineering for Support Ticket Analysis Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Illustrates a manual approach to analyzing support tickets using a hardcoded prompt. This method requires manual prompt tuning and lacks systematic evaluation, making it fragile and hard to maintain. ```python # ❌ Fragile, hard to maintain def analyze_support_ticket(ticket): prompt = f""" You are a customer support expert. Analyze this ticket and classify it. Ticket: {ticket} Classify as: technical, billing, or general Urgency: high, medium, low Response: professional response """ # Manual optimization required # Model-specific prompt tuning # No systematic evaluation return call_llm(prompt) ``` -------------------------------- ### Sequential Module Composition Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Demonstrates how to compose DSPy modules sequentially, where the output of one module serves as the input for the next. ```python class Pipeline(dspy.Module): def __init__(self): super().__init__() self.step1 = dspy.ChainOfThought("input -> intermediate") self.step2 = dspy.Predict("intermediate -> output") def forward(self, input_data): intermediate = self.step1(input=input_data) output = self.step2(intermediate=intermediate.intermediate) return output ``` -------------------------------- ### DSPy Automatic Prompt Optimization with BootstrapFewShot Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Shows how to use DSPy's BootstrapFewShot teleprompter to automatically optimize a module. This process involves generating, testing, and selecting the best prompt variations based on a training dataset and a specified metric. ```python from dspy.teleprompt import BootstrapFewShot from dspy import metrics # Automatic optimization teleprompter = BootstrapFewShot(metric=metrics.answer_exact_match) optimized_analyzer = teleprompter.compile( ContractAnalyzer(), trainset=contract_examples ) # DSPy automatically: # 1. Generates prompt variations # 2. Tests them on your data # 3. Selects the best performing version # 4. Creates few-shot examples ``` -------------------------------- ### Experiment with DSPy Signatures Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Illustrates two ways to define signatures in DSPy: a simple string-based format and a more detailed class-based format with input/output field descriptions. ```python # String-based signature simple_sig = dspy.Predict("question -> answer") # Detailed class-based signature class DetailedQA(dspy.Signature): """Provide detailed answers to questions with confidence scores.""" question: str = dspy.InputField(desc="User's question") context: str = dspy.InputField(desc="Additional context", default="") answer: str = dspy.OutputField(desc="Detailed answer") confidence: float = dspy.OutputField(desc="Confidence from 0-1") detailed_qa = dspy.Predict(DetailedQA) ``` -------------------------------- ### Experiment with Different DSPy Models Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Demonstrates how to configure DSPy to use various language models within the same code. This highlights DSPy's model-agnostic nature. ```python # Try different models with the same code models_to_test = [ 'openai/gpt-4o-mini', 'openai/gpt-4', 'anthropic/claude-3-sonnet', 'ollama_chat/llama3' # if you have Ollama running ] for model in models_to_test: print(f"\nTesting with {model}:") dspy.configure(lm=dspy.LM(model)) qa = SimpleQA() # Assuming SimpleQA is defined elsewhere result = qa("What is machine learning?") print(f"Answer: {result.answer[:100]}...") ``` -------------------------------- ### Reinstall DSPy with All Dependencies Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Use this command to uninstall DSPy and then reinstall it with all optional dependencies, which can resolve import errors. ```bash # Reinstall with dependencies pip uninstall dspy-ai pip install dspy-ai[all] ``` -------------------------------- ### Configure Environment Variables for API Keys Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Set environment variables for API keys for various language model providers like OpenAI, Anthropic, and Cohere, as well as for local Ollama models. ```bash # Required for OpenAI models OPENAI_API_KEY=your_openai_api_key_here # Optional: Other providers ANTHROPIC_API_KEY=your_anthropic_api_key_here COHERE_API_KEY=your_cohere_api_key_here # Optional: Local models OLLAMA_HOST=http://localhost:11434 OLLAMA_MODEL=llama3 ``` -------------------------------- ### Create a Retrieval-Augmented Generation Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Implements a basic Retrieval-Augmented Generation (RAG) pipeline. It first retrieves relevant context and then uses a ChainOfThought module to generate an answer. ```python import dspy dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) class RAG(dspy.Module): def __init__(self): super().__init__() self.retrieve = dspy.Retrieve(k=3) # retrieval step self.generate = dspy.ChainOfThought("question, context -> answer") def forward(self, question): context = self.retrieve(question) return self.generate(question=question, context=context) ``` -------------------------------- ### DSPy Declarative Approach for Support Ticket Analysis Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Demonstrates the DSPy approach using signatures and modules for analyzing support tickets. This method enables automatic optimization and evaluation, leading to more robust and self-improving AI applications. ```python # ✅ Robust, self-improving class SupportTicketAnalysis(dspy.Signature): """Analyze and classify customer support tickets""" ticket: str = dspy.InputField() category: str = dspy.OutputField(desc="technical, billing, or general") urgency: str = dspy.OutputField(desc="high, medium, or low") response: str = dspy.OutputField(desc="Professional customer response") class SupportAnalyzer(dspy.Module): def __init__(self): super().__init__() self.analyze = dspy.ChainOfThought(SupportTicketAnalysis) def forward(self, ticket: str): return self.analyze(ticket=ticket) # Automatic optimization and evaluation analyzer = SupportAnalyzer() optimized = optimize_with_data(analyzer, support_examples) ``` -------------------------------- ### BetterTogether Optimizer Configuration Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Initialize the BetterTogether optimizer for jointly optimizing multiple modules within a pipeline. Requires a custom metric for evaluation. ```python from dspy.teleprompt import BetterTogether # Jointly optimize multiple modules pipeline = ComplexPipeline() # Module with multiple sub-modules optimizer = BetterTogether(metric=custom_metric) optimized_pipeline = optimizer.compile(pipeline, trainset=examples) ``` -------------------------------- ### Configure DSPy with Local Ollama Model Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Configure DSPy to use a local Llama3 model served via Ollama. ```python import dspy # Configure DSPy to use local Ollama dspy.configure(lm=dspy.LM('ollama_chat/llama3')) ``` -------------------------------- ### Configure DSPy with Cohere Model Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Configure DSPy to use Cohere's Command-R-plus model. ```python import dspy # Cohere dspy.configure(lm=dspy.LM('cohere/command-r-plus')) ``` -------------------------------- ### Configure DSPy with OpenAI GPT Models Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Configure DSPy to use OpenAI's GPT-4o-mini and GPT-4 models. ```python import dspy # OpenAI GPT models dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) dspy.configure(lm=dspy.LM('openai/gpt-4')) ``` -------------------------------- ### Check OpenAI API Key Configuration Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Verifies if the OPENAI_API_KEY environment variable is set. It also shows how to set the API key directly in code, though this is not recommended for production environments. ```python # Check if API key is loaded import os print("OpenAI API Key:", os.getenv('OPENAI_API_KEY', 'Not found')) # Set API key in code (not recommended for production) os.environ['OPENAI_API_KEY'] = 'your-api-key-here' ``` -------------------------------- ### Define Basic and Class-based Signatures Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Signatures define the input/output behavior of a task. Use string-based signatures for simple tasks or class-based signatures for detailed input/output field definitions with descriptions. ```python import dspy # String-based signature (simple) qa_signature = "question -> answer" # Class-based signature (detailed) class QuestionAnswering(dspy.Signature): """Answer questions accurately based on given context.""" question: str = dspy.InputField(desc="User's question") context: str = dspy.InputField(desc="Relevant background information") answer: str = dspy.OutputField(desc="Accurate, concise answer") ``` -------------------------------- ### Configure DSPy with Anthropic Claude Model Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Configure DSPy to use Anthropic's Claude-3-Sonnet model. ```python import dspy # Anthropic Claude dspy.configure(lm=dspy.LM('anthropic/claude-3-sonnet')) ``` -------------------------------- ### Parallel Module Composition Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Illustrates parallel composition where multiple modules process input concurrently, and their results are combined. ```python class ParallelProcessor(dspy.Module): def __init__(self): super().__init__() self.analyzer1 = dspy.Predict("text -> sentiment") self.analyzer2 = dspy.Predict("text -> topics") self.combiner = dspy.ChainOfThought("sentiment, topics -> summary") def forward(self, text): # Parallel processing sentiment = self.analyzer1(text=text) topics = self.analyzer2(text=text) # Combine results summary = self.combiner( sentiment=sentiment.sentiment, topics=topics.topics ) return summary ``` -------------------------------- ### DSPy Retrieval-Augmented Generation (RAG) Pipeline Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md A DSPy pipeline demonstrating RAG by retrieving context from Wikipedia using ColBERTv2 and then generating an answer with Chain-of-Thought. Ensure your model is configured. ```python import dspy # Configure your model (e.g., local LLM or cloud API) dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) # Define a function to call an external retrieval service # Here we use DSPy's built‑in ColBERTv2 retriever; you could also use your own search API def search_wikipedia(query: str) -> list[str]: results = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')(query, k=3) return [x['text'] for x in results] class RAGPipeline(dspy.Module): def __init__(self): super().__init__() self.retrieve = dspy.Retrieve(k=3) # retrieval via ColBERT self.generate = dspy.ChainOfThought("question, context -> answer") def forward(self, question: str): # Step 1: fetch relevant context context = self.retrieve(question) # Step 2: ask the model to answer using the context return self.generate(question=question, context=context) # Usage rag = RAGPipeline() question = "Who invented the telephone?" answer = rag(question) print(answer) ``` -------------------------------- ### DSPy Configuration for Production Deployment Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Configures DSPy for production use by setting the language model, enabling automatic response caching, and activating built-in observability. This ensures efficient and monitored LLM interactions. ```python import dspy # Configure with caching and monitoring dspy.configure( lm=dspy.LM('openai/gpt-4'), cache=True, # Automatic response caching monitor=True # Built-in observability ) ``` -------------------------------- ### DSPy Summarization Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md A DSPy module for text summarization using Chain-of-Thought. Configure the language model before instantiation. ```python import dspy # Configure the model dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) class Summarizer(dspy.Module): def __init__(self): super().__init__() self.summarize = dspy.ChainOfThought("document -> summary") def forward(self, document: str): return self.summarize(document=document) # Example doc = "DSPy is a framework for programming language models..." summary = Summarizer()(doc) print(summary) ``` -------------------------------- ### Define Signature Input Fields Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Input fields specify the data a module expects. Include descriptions for better prompting and use type hints for validation. Supports various data types like strings and integers. ```python class DocumentAnalysis(dspy.Signature): """Analyze documents for key insights.""" document: str = dspy.InputField(desc="Raw document text") focus_area: str = dspy.InputField(desc="Specific area to analyze") max_length: int = dspy.InputField(desc="Maximum response length") ``` -------------------------------- ### DSPy Basic Question Answering Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Defines and uses a simple DSPy module for question answering using ChainOfThought. It's configured to use the 'openai/gpt-4o-mini' model. ```python import dspy # Configure the language model dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) class SimpleQA(dspy.Module): """Simple question-answering module using ChainOfThought.""" def __init__(self): super().__init__() self.generate_answer = dspy.ChainOfThought("question -> answer") def forward(self, question: str): return self.generate_answer(question=question) # Create and use the module qa = SimpleQA() response = qa("What is the capital of France?") print("Question:", "What is the capital of France?") print("Answer:", response.answer) if hasattr(response, 'rationale'): print("Reasoning:", response.rationale) ``` -------------------------------- ### Implement a ChainOfThought Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md The ChainOfThought module enables step-by-step reasoning. When called, it automatically includes intermediate reasoning steps in its process to arrive at the final answer. ```python class ReasoningQA(dspy.Module): def __init__(self): super().__init__() self.cot = dspy.ChainOfThought("question -> answer") def forward(self, question: str): # Automatically includes reasoning steps return self.cot(question=question) ``` -------------------------------- ### DSPy Math Question Answering Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/03_installation.md Defines and uses a DSPy module for mathematical reasoning with step-by-step solutions, configured for 'openai/gpt-4o-mini'. ```python import dspy dspy.configure(lm=dspy.LM('openai/gpt-4o-mini')) class MathQA(dspy.Module): """Mathematical reasoning with step-by-step solution.""" def __init__(self): super().__init__() self.solve = dspy.ChainOfThought("question -> answer: float") def forward(self, question: str): result = self.solve(question=question) return result # Test with a math problem math_qa = MathQA() problem = "What is 15% of 240, plus 30?" result = math_qa(problem) print(f"Problem: {problem}") print(f"Answer: {result.answer}") print(f"Reasoning: {result.rationale}") ``` -------------------------------- ### Implement a Basic Predict Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md The Predict module is the simplest DSPy module, performing basic prompting. It takes a signature and uses it to predict outputs based on given inputs. ```python class SimpleQA(dspy.Module): def __init__(self): super().__init__() self.predictor = dspy.Predict("question -> answer") def forward(self, question: str): return self.predictor(question=question) ``` -------------------------------- ### Tightly Coupled Prompt Logic in Python Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Illustrates how prompt logic embedded directly within Python functions leads to poor reusability, difficult testing, and maintenance challenges. This function is hard to adapt for different models or use cases. ```python # Tightly coupled, hard to reuse def analyze_contract(text): prompt = f""" You are an expert legal analyst. Please analyze this contract very carefully. Look for the following specific terms and conditions: 1. Payment terms and deadlines 2. Termination clauses and conditions 3. Liability limitations and responsibilities Contract text: {text} Format your response as JSON with the following structure: {{"payment_terms": "...", "termination": "...", "liability": "..."}} """ return openai.complete(prompt) ``` -------------------------------- ### Evaluate a DSPy Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Quantify the performance of a DSPy module using a dataset and a metric. Ensure you use a separate test set for final validation. ```python result = evaluate(rag) # rag is the RAGPipeline defined earlier print("Accuracy:", result) ``` -------------------------------- ### DSPy TypedPredictor with Pydantic Integration Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Demonstrates using DSPy's TypedPredictor to enforce structured output validation with Pydantic models. This ensures that the LLM's output conforms to a predefined schema, enhancing reliability. ```python # Pydantic integration for validation from pydantic import BaseModel class StructuredOutput(BaseModel): confidence: float analysis: str risk_level: str class ValidatedAnalyzer(dspy.Module): def __init__(self): super().__init__() self.analyze = dspy.TypedPredictor( ContractAnalysis, output_type=StructuredOutput ) ``` -------------------------------- ### Implement a ProgramOfThought Module for Code Generation Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md The ProgramOfThought module is designed for tasks requiring code generation and execution. It can generate and run code to solve problems, such as mathematical equations. ```python class MathSolver(dspy.Module): def __init__(self): super().__init__() self.pot = dspy.ProgramOfThought("math_problem -> solution") def forward(self, math_problem: str): # Generates and executes code to solve problems return self.pot(math_problem=math_problem) ``` -------------------------------- ### DSPy Declarative Signature for Contract Analysis Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Defines a contract analysis task using DSPy's declarative programming paradigm. This signature clearly specifies inputs and expected outputs, promoting type safety and readability. ```python import dspy # Declarative: What should happen class ContractAnalysis(dspy.Signature): """Extract key legal terms from contracts""" contract_text: str = dspy.InputField(desc="Raw contract text") payment_terms: str = dspy.OutputField(desc="Payment terms and deadlines") termination: str = dspy.OutputField(desc="Termination conditions") liability: str = dspy.OutputField(desc="Liability limitations") ``` -------------------------------- ### DSPy Composable Module for Contract Analysis Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/01_motivation.md Implements a reusable contract analysis module using DSPy's ChainOfThought primitive. This module leverages the defined signature and can be easily composed with other DSPy components. ```python # Composable module class ContractAnalyzer(dspy.Module): def __init__(self): super().__init__() self.analyze = dspy.ChainOfThought(ContractAnalysis) def forward(self, contract_text: str): return self.analyze(contract_text=contract_text) ``` -------------------------------- ### Define a Custom AdvancedRAG Module Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Create a custom DSPy module that composes multiple sub-modules for a multi-step retrieval-augmented generation pipeline. This includes query rewriting, retrieval, reranking, and generation. ```python class AdvancedRAG(dspy.Module): """Multi-step retrieval-augmented generation.""" def __init__(self, k=5): super().__init__() self.k = k # Compose multiple modules self.query_rewriter = dspy.ChainOfThought("question -> rewritten_query") self.retriever = dspy.Retrieve(k=self.k) self.reranker = dspy.Predict("query, contexts -> ranked_contexts") self.generator = dspy.ChainOfThought("query, context -> answer") def forward(self, question: str): # Multi-step pipeline rewritten = self.query_rewriter(question=question) contexts = self.retriever(rewritten.rewritten_query) ranked = self.reranker(query=question, contexts=contexts) answer = self.generator(query=question, context=ranked.ranked_contexts) return dspy.Prediction( answer=answer.answer, reasoning=answer.reasoning, contexts=contexts, rewritten_query=rewritten.rewritten_query ) ``` -------------------------------- ### Define a Custom Semantic Similarity Metric Source: https://github.com/evalops/dspy-0to1-guide/blob/main/docs/02_core_concepts.md Implement a custom metric for DSPy optimization using sentence embeddings and cosine similarity. This metric can be used to evaluate the semantic closeness of predictions to ground truth answers. ```python def semantic_similarity_metric(prediction, ground_truth): """Custom metric using sentence embeddings.""" from sentence_transformers import SentenceTransformer model = SentenceTransformer('all-MiniLM-L6-v2') pred_embedding = model.encode([prediction.answer]) true_embedding = model.encode([ground_truth.answer]) # Cosine similarity similarity = np.dot(pred_embedding[0], true_embedding[0]) / ( np.linalg.norm(pred_embedding[0]) * np.linalg.norm(true_embedding[0]) ) return max(0, similarity) # Ensure non-negative score # Use custom metric in optimization optimizer = BootstrapFewShot(metric=semantic_similarity_metric) ``` -------------------------------- ### Define a Question Answering Signature Source: https://github.com/evalops/dspy-0to1-guide/blob/main/README.md Defines the input and output fields for a question answering task. This signature acts as a contract for modules implementing this task. ```python import dspy class QA(dspy.Signature): """Question answering task.""" context: str = dspy.InputField(desc="Background information") question: str = dspy.InputField() answer: str = dspy.OutputField(desc="Accurate answer") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.