Loading repository data…
Loading repository data…
mbakgun / repository
This codebase demonstrates various DSPy functionalities through practical examples.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
DSPy is a framework for programming—rather than prompting—language models. It enables you to write compositional Python code to:
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.
# Initialize project and install dependencies
uv venv --python 3.10
source .venv/bin/activate
uv pip install -U dspy
uv pip install requests
llama3.2:3b modelhttp://localhost:11434Question: "Three dice are tossed. What is the probability that the sum equals 3?"
Answer: 0.00462963
Implementation (getFloatAnswerExample)
ChainOfThought for mathematical reasoningQuestion: "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)
dspy.Signature to define input/output structureQuestion: "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)
Page Size: 20
Interval Minutes: 60
Total Images: 400
Implementation (RagWithDataExtractionExample)
dspy.Signature for schema definitionInterval in minutes: 60
Interval in seconds: 3600
Implementation (reActWithRag)
Word: "strawberry"
Letter 'r' count: 3
Implementation (countLetterInWord)
Input: Long text about DSPy framework
Output: Concise summary of DSPy's key features
Implementation (summarizeTextExample)
ChainOfThought for text summarizationInput: "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)
ChainOfThought for accurate translationQuestion: "What is the capital of Germany?"
Answer: "Berlin"
Implementation (basicPredictExample)
dspy.PredictQuestion: "Which planet is known as the Red Planet?"
Options: A) Venus, B) Mars, C) Jupiter, D) Saturn
Implementation (multipleChoiceExample)
MultipleChoice signaturedspy.MultiChainComparison and dspy.Predict for robust answersInput: Multiple text snippets
Output: Category for each text
Implementation (parallelProcessingExample)
dspy.Parallel for efficient executionInput: Question about Naruto's friends
Output: Structured JSON data with character names and clans
Implementation (typedChainOfThoughtExample)
ChainOfThought for structured reasoningQuestion: "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)
Run any example using uv:
uv run basic_dspy_example.py
Example console outputs can be found in console_logs.
Or run specific functions by uncommenting them in the main block:
if __name__ == "__main__":
# Uncomment the example you want to run
# getFloatAnswerExample()
# GetBasicAnswer()
# ...
For a comprehensive example of DSPy in action, check out our AI writing assistant at writebreeze.com. This project demonstrates real-world implementation of the concepts covered above.
All examples include execution time measurement. In our test run:
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)
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.