DSPy Examples

repository·main·Indexed 41 days ago

https://github.com/mbakgun/dspy-examples

A collection of practical examples for the DSPy framework, demonstrating how to program language models using Signatures, ChainOfThought, ReAct, and MultiChainComparison. Includes guides for setting up a development environment with uv and Ollama, as well as implementing parallel processing with asyncio.

Tokens
778
Snippets
2
Records
4
Agent score
0%

What's inside DSPy

  1. How DSPy components work

    main

    DSPy provides several core abstractions for building language model programs:

    • Signature: Defines the input/output schema for a task. It enables structured data handling and supports type hints and documentation.
    • Predict: A simple prediction interface for direct question-answering with minimal configuration.
    • ChainOfThought: Used for complex reasoning tasks. It breaks problems down into steps and provides a transparent reasoning process.
    • ReAct: Combines reasoning and actions, allowing for the integration of custom tools (useful for interactive tasks).
    • Prediction: An object that stores and manages LLM response outputs. It provides access to step-by-step reasoning, intermediate thoughts, final answers, and multiple model completions.
    • MultiChainComparison: Compares multiple reasoning attempts and aggregates different model outputs to provide robust final answers.
    • Parallel: Enables concurrent processing of tasks with configurable thread counts, error handling, and progress tracking.
  2. Setup the DSPy development environment

    main

    To use these examples, you need to set up a Python environment with uv and configure a local Ollama instance.

    1. Initialize and install dependencies: Use uv to create a virtual environment with Python 3.10 and install dspy and requests.

    2. Configure Ollama:

      • Ensure Ollama is running locally.
      • The examples are configured to use the llama3.2:3b model.
      • The API base is set to http://localhost:11434.
    3. Enable Parallelism (Optional): To improve performance when running multiple tasks, set the OLLAMA_NUM_PARALLEL environment variable to allow Ollama to handle simultaneous requests.

    # Initialize project and install dependencies
    uv venv --python 3.10
    source .venv/bin/activate
    uv pip install -U dspy
    uv pip install requests
    
    # Enable parallel requests in Ollama
    export OLLAMA_NUM_PARALLEL=8
  3. Run DSPy examples

    main

    You can run the full suite of examples using uv run. To run specific examples, you must edit basic_dspy_example.py and uncomment the desired function call within the if __name__ == "__main__": block.

    uv run basic_dspy_example.py
  4. Run examples in parallel using asyncio

    main

    For better performance, you can execute multiple DSPy functions concurrently using asyncio.gather. This is particularly effective when combined with the OLLAMA_NUM_PARALLEL configuration in Ollama.

    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),
            # ... add other example functions here
        ]
        
        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())