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.
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."
})The react_module contains two tools:
evaluate_math: Handles mathematical operations.search_wikipediaInteracts with a Wikipedia Search API which uses a ColBERTv2 model.
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."
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.
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."
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.
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.
The agent returns the final answer: "3818".
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.
- Python 3.10+
- Docker and Docker Compose
- Valid OpenAI API key in your environment:
export OPENAI_API_KEY="your-api-key"
Clone the EggAI repository:
git clone git@github.com:eggai-tech/EggAI.gitMove into the examples/dspy_react folder:
cd examples/dspy_reactCreate and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate # For Windows: venv\Scripts\activateInstall the required dependencies:
pip install -r requirements.txtStart Redpanda using Docker Compose:
docker compose up -dpython main.pypytest- 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
pytestor 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.