Features
- ReAct framework for reasoning and action
- Tool integration (Wikipedia search, Python interpreter)
- LLM orchestration with DSPy
- Multi-step task execution
- Structured answer generation
Prerequisites
- Python 3.10 or higher
- Nebius API key from Nebius Token Factory
Installation
Implementation
DSPy Configuration
Configure DSPy with Nebius AI:main.py
Tool Definitions
Define tools for the agent:main.py
ReAct Agent
Create the ReAct agent with tools:main.py
Usage
Run the DSPy agent:- Analyze the question
- Use Wikipedia to find Messi’s first Ballon d’Or year
- Use Python evaluation to perform the calculation
- Return the final answer
Example Tasks
Try these complex queries:- “What is the population of France multiplied by the number of Nobel Prizes won by Marie Curie?”
- “Calculate the square root of the year the Eiffel Tower was built.”
- “Find the capital of Japan and its population.”
- “Who won the Nobel Prize in Physics in 1921 and what was their age at the time?”
Technical Details
DSPy Architecture
LM Configuration
Language model setup with Nebius AI
ReAct Module
Reasoning and action framework
Tools
Python interpreter and Wikipedia search
Signatures
Input/output specifications
ReAct Pattern
DSPy’s ReAct module follows this pattern:- Thought: Reason about the question
- Action: Select and execute a tool
- Observation: Process tool results
- Repeat: Continue until answer is found
- Answer: Return final result
Extending the Agent
Add Custom Tools
Use Different Models
Add Structured Output
Chain Multiple Modules
Tool Implementation Details
Python Interpreter
Wikipedia Search
Best Practices
Tool Design
Tool Design
- Keep tools focused on single tasks
- Provide clear docstrings
- Handle errors gracefully
- Return structured, parseable data
Model Configuration
Model Configuration
- Choose appropriate model size
- Test with different temperature settings
- Consider cost vs. performance
- Use caching for repeated queries
Signature Design
Signature Design
- Use clear input/output specifications
- Include type hints
- Document expected formats
- Test with edge cases
DSPy Modules
DSPy provides several built-in modules:- ReAct: Reasoning and action with tools
- ChainOfThought: Step-by-step reasoning
- ProgramOfThought: Program generation
- MultiChainComparison: Compare multiple reasoning paths
- Retrieve: Document retrieval
Optimization
DSPy supports automatic optimization:Environment Variables
| Variable | Description | Required |
|---|---|---|
NEBIUS_API_KEY | Your Nebius API key | Yes |
Next Steps
Advanced DSPy
Build complex DSPy applications
RAG with DSPy
Add retrieval to your agents