Skip to content

This codebase demonstrates various DSPy functionalities through practical examples.

Notifications You must be signed in to change notification settings

mbakgun/dspy-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

DSPy Examples

DSPy is a framework for programming—rather than prompting—language models. It enables you to write compositional Python code to:

  • Build reliable language model applications
  • Create composable prompting programs
  • Optimize prompts and chains automatically
  • Integrate retrieval and tool use seamlessly
  • Develop reproducible LLM-based systems

The framework enables developers to write declarative programs that can be optimized and improved through teleprompter learning, making it easier to create robust AI applications.

This repository contains various examples demonstrating the usage of DSPy, a framework for programming with language models. Each example showcases different capabilities and patterns.

Setup

  1. Create and activate the environment:
# Initialize project and install dependencies
uv venv --python 3.10
source .venv/bin/activate
uv pip install -U dspy
uv pip install requests
  1. Configure Ollama:
  • Make sure you have Ollama running locally
  • The examples use llama3.2:3b model
  • API base is configured to http://localhost:11434

Examples

1. Probability Calculation: getFloatAnswerExample
Question: "Three dice are tossed. What is the probability that the sum equals 3?"
Answer: 0.00462963

Implementation (getFloatAnswerExample)

  • Uses ChainOfThought for mathematical reasoning
  • Returns floating-point probability value
  • Simple one-step calculation with direct output
2. Basic Question Answering: GetBasicAnswer
Question: "Turkey is a country in which continent?"
Answer: "Europe"
Reasoning: "The continent that Turkey is located on can be determined by considering its geographical position."

Implementation (GetBasicAnswer)

  • Uses dspy.Signature to define input/output structure
  • Provides factoid answers with reasoning
  • Demonstrates basic question-answering pattern
3. External API Integration: ragExampleWithMjApi
Question: "What is the github repo of the Mj API?"
Answer: "The GitHub repository of the Mj API is https://s.akgns.com/3Aw"

Implementation (ragExampleWithMjApi)

  • Fetches data from mj.akgns.com
  • Uses RAG (Retrieval Augmented Generation)
  • Processes external API response as context
4. Data Extraction: RagWithDataExtractionExample
Page Size: 20
Interval Minutes: 60
Total Images: 400

Implementation (RagWithDataExtractionExample)

  • Extracts structured data from API response
  • Defines specific output fields with types
  • Uses dspy.Signature for schema definition
5. Time Conversion: reActWithRag
Interval in minutes: 60
Interval in seconds: 3600

Implementation (reActWithRag)

  • Uses ReAct pattern with custom math tool
  • Combines API data with calculation
  • Demonstrates tool integration
6. String Analysis: countLetterInWord
Word: "strawberry"
Letter 'r' count: 3

Implementation (countLetterInWord)

  • Custom tool for letter counting
  • Uses ReAct for simple text analysis
  • Shows basic tool usage pattern
7. Text Summarization: summarizeTextExample
Input: Long text about DSPy framework
Output: Concise summary of DSPy's key features

Implementation (summarizeTextExample)

  • Uses ChainOfThought for text summarization
  • Processes multi-sentence input text
  • Generates concise, coherent summaries
8. Text Translation: translateTextExample
Input: "Hello, world! DSPy is a great tool for building AI applications."
Output: Merhaba dünya! DSPy, yapay zeka uygulamaları geliştirmek için harika bir araçtır.

Implementation (translateTextExample)

  • Translates text to specified target language
  • Uses ChainOfThought for accurate translation
  • Maintains context and meaning
9. Basic Prediction: basicPredictExample
Question: "What is the capital of Germany?"
Answer: "Berlin"

Implementation (basicPredictExample)

  • Simple question-answering using dspy.Predict
  • Direct prediction without complex reasoning
  • Demonstrates basic model usage
10. Multiple Choice Questions: multipleChoiceExample
Question: "Which planet is known as the Red Planet?"
Options: A) Venus, B) Mars, C) Jupiter, D) Saturn

Implementation (multipleChoiceExample)

  • Custom MultipleChoice signature
  • Uses dspy.MultiChainComparison and dspy.Predict for robust answers
  • Provides reasoning for selected answer
11. Parallel Processing: parallelProcessingExample
Input: Multiple text snippets
Output: Category for each text

Implementation (parallelProcessingExample)

  • Processes multiple inputs in parallel
  • Uses dspy.Parallel for efficient execution
  • Demonstrates batch processing capabilities
12. Typed Chain of Thought with JSON: typedChainOfThoughtExample
Input: Question about Naruto's friends
Output: Structured JSON data with character names and clans

Implementation (typedChainOfThoughtExample)

  • Uses ChainOfThought for structured reasoning
  • Processes JSON input and generates structured output
  • Demonstrates complex reasoning with JSON
13. Stacked LLM Calls: stackedLLMCallsExample
Question: "What is the total years between the Roman Empire's founding and the fall of Rome?"
Thought Process: Step-by-step historical analysis
Final Answer: 503

Implementation (stackedLLMCallsExample)

  • Uses multiple LLM calls to answer a complex question
  • Demonstrates the ability to integrate multiple models
  • Shows how to handle multi-step reasoning

DSPy Components Used

ChainOfThought

  • Used for complex reasoning tasks
  • Breaks down problems into steps
  • Provides transparent reasoning process

ReAct

  • Combines reasoning and actions
  • Allows integration of custom tools
  • Useful for interactive tasks

Signature

  • Defines input/output schemas
  • Enables structured data handling
  • Supports type hints and documentation

Predict

  • Simple prediction interface
  • Direct question-answering
  • Minimal configuration needed

Prediction

  • Stores and manages LLM response outputs
  • Provides access to step-by-step reasoning process
  • Captures intermediate thoughts and final answers
  • Supports accessing multiple model completions

MultiChainComparison

  • Compares multiple reasoning attempts
  • Aggregates different model outputs
  • Provides robust final answers

Parallel

  • Enables concurrent processing
  • Configurable thread count
  • Error handling and progress tracking

Running Examples

Run any example using uv:

uv run basic_dspy_example.py

Example console outputs can be found in console_logs.txt.

Or run specific functions by uncommenting them in the main block:

if __name__ == "__main__":
    # Uncomment the example you want to run
    # getFloatAnswerExample()
    # GetBasicAnswer()
    # ...

Performance Notes

All examples include execution time measurement. In our test run:

  • Mac mini M4 Pro: 12-core CPU, 20-core GPU, 64GB unified memory
  • Total execution time: ~31.6 seconds (Total time taken: 31654.63ms)
  • Each example runs sequentially
  • Performance may vary based on model and system configuration

Parallel Execution

You can run the examples in parallel using asyncio for better performance. Note that you'll need to configure Ollama for parallel execution:

import asyncio
import time

async def run_example(func):
    return await asyncio.to_thread(func)

async def main():
    start_time = time.time()
    
    tasks = [
        run_example(getFloatAnswerExample),
        run_example(GetBasicAnswer),
        run_example(ragExampleWithMjApi),
        run_example(RagWithDataExtractionExample),
        run_example(reActWithRag),
        run_example(countLetterInWord),
        run_example(summarizeTextExample),
        run_example(translateTextExample),
        run_example(basicPredictExample),
        run_example(multipleChoiceExample),
        run_example(parallelProcessingExample),
        run_example(typedChainOfThoughtExample),
        run_example(stackedLLMCallsExample)
    ]
    
    await asyncio.gather(*tasks)
    
    elapsed_ms = (time.time() - start_time) * 1000
    print(f"\nTotal time taken: {elapsed_ms:.2f}ms")

if __name__ == "__main__":
    asyncio.run(main())

# Total execution time: ~29.7 seconds (29714.53ms)

Ollama Configuration

To enable parallel requests in Ollama, set the following environment variable:

export OLLAMA_NUM_PARALLEL=8  # Adjust based on your system's capabilities

Or add to your system configuration:

<key>OLLAMA_NUM_PARALLEL</key>
<string>8</string>

This allows Ollama to handle multiple requests simultaneously, significantly improving performance when running examples in parallel.

DSPy provides built-in observability features for debugging and monitoring your AI systems.

About

This codebase demonstrates various DSPy functionalities through practical examples.

Topics

Resources

Stars

Watchers

Forks

Languages