ReActive Agents with DSPy
This example demonstrates how to integrate DSPy’s powerful ReAct module into EggAI agents for dynamic, real-time question-answering tasks. ReAct enables agents to tackle complex problems by continuously refining their responses through an iterative cycle of reasoning (Thought), decision-making (Action), and feedback (Observation). As the agent processes information, it adjusts its actions based on ongoing observations, allowing it to adapt and improve over time. This flexible, real-time iterative approach makes ReAct ideal for tasks that require dynamic reasoning, rapid decision-making, and continuous adaptation to new information.
The code for this example is available here.
How it works
In this example, we test the ReAct agent by sending a question to the agent channel. The agent dynamically processes the question and iteratively refines its responses.
For example:
await agents_channel.publish({
"type": "question",
"payload": "Give me the year of construction of the Eiffel Tower summed with the year of construction of the Empire State Building."
})
Available tools
The react_module
contains two tools:
- evaluate_math
: Handles mathematical operations.
- search_wikipedia
Interacts with a Wikipedia Search API which uses a ColBERTv2 model.
Agent Input
The agent receives the question: "Give me the year of construction of the Eiffel Tower summed with the year of construction of the Empire State Building."
First Thought
The agent begins by reasoning: "I need to find the years of construction for both the Eiffel Tower and the Empire State Building in order to sum them." It uses the search_wikipedia
tool to gather relevant data.
Iterative Search
If initial results don’t provide sufficient information, the agent refines its search by querying more specific terms: - "Eiffel Tower construction year, Empire State Building construction year." - "Eiffel Tower construction year." - "Empire State Building construction year." - "Eiffel Tower history construction year."
Observation and Refinement
The agent analyzes the results of each search, refining its queries as needed. When relevant data is missing, it generates new queries until the necessary information is found.
Reasoning and Calculation
Once the agent has sufficient information, it reasons: "The construction year of
the Eiffel Tower is 1887, and the Empire State Building was constructed from 1930 to 1931. To find the total, I will sum 1887 and 1931 (the year it was completed)." The agent uses the evaluate_math
tool to perform the calculation: 1887 + 1931 = 3818.
Output
The agent returns the final answer: "3818".
Key Takeaways
This example shows the power of integrating DSPy’s ReAct module into EggAI agents. By leveraging iterative reasoning and dynamic tool usage, the agent can:
- Adapt its queries based on observations.
- Combine tools (e.g., mathematical evaluation and information retrieval) for complex reasoning.
- Deliver accurate and context-aware results.
The result is a robust and flexible system capable of handling diverse and challenging tasks with minimal hardcoding.
Prerequisites
- Python 3.10+
- Docker and Docker Compose
- Valid OpenAI API key in your environment:
Setup Instructions
Clone the EggAI repository:
Move into the examples/dspy_react
folder:
Create and activate a virtual environment:
Install the required dependencies:
Start Redpanda using Docker Compose:
Run the Example
Run the Tests
Next Steps
- Additional Guards: Check out Guardrails.ai’s documentation to chain multiple guards for diverse content moderation needs.
- Scale Out: Integrate with more advanced DSPy pipelines or domain-specific systems (e.g., CRM, knowledge bases).
- CI/CD Testing: Use
pytest
or similar to maintain performance and safety standards through version upgrades. - Contribute: Open an issue or submit a pull request on EggAI to enhance our guardrails example!
Enjoy creating safe, scalable, and versatile LLM-powered agents! For any issues, reach out via GitHub or the EggAI community.