Overview
The ReAct (Reasoning + Acting) pattern combines LLM reasoning with tool execution. This comprehensive example demonstrates:- Creating ReAct agents with custom tools
- Using built-in tools (calculator, datetime, string, JSON)
- Chain agents for sequential workflows
- Parallel agents for concurrent execution
- MapReduce patterns for distributed processing
- AutoAgent for automatic strategy selection
What You’ll Learn
- Building custom tools with
ReActTooltrait - Configuring ReAct agents with
ReActAgent::builder() - Implementing tool execution logic
- Chaining multiple agents together
- Parallel multi-agent execution
- Using the Actor model for concurrent agents
Prerequisites
- Rust 1.75 or higher
- OpenAI API key
- Understanding of async/await
Source Code Overview
This is a large example with 960+ lines. Here are the key components:Running the Example
export OPENAI_API_KEY="your-api-key"
# Optional: Custom endpoint
export OPENAI_BASE_URL="http://localhost:11434/v1"
export OPENAI_MODEL="gpt-4"
Example Scenarios
The example includes 10 different scenarios:1. Basic ReAct Agent
1. Basic ReAct Agent
Simple agent with web search and calculator tools.Demonstrates: Tool selection, reasoning steps, final answer
2. Actor Model
2. Actor Model
ReAct agent running in an actor system.Demonstrates: Concurrent execution, status monitoring, message passing
3. AutoAgent
3. AutoAgent
Automatically selects between direct response and ReAct reasoning.Demonstrates: Strategy selection, performance optimization
4. Built-in Tools
4. Built-in Tools
Using calculator, string, JSON, datetime tools.Demonstrates: Tool ecosystem, composition
5. Streaming Output
5. Streaming Output
Stream reasoning steps in real-time.Demonstrates: Async streaming, progress updates
6. Custom Tools
6. Custom Tools
Combining custom and built-in tools.Demonstrates: Tool integration, extensibility
7. Chain Agent
7. Chain Agent
Sequential pipeline: Researcher → Writer → Editor.Demonstrates: Multi-stage processing, data flow
8. Parallel Agent
8. Parallel Agent
Concurrent expert analysis from multiple perspectives.Demonstrates: Parallel execution, result aggregation
9. LLM Summarizer
9. LLM Summarizer
Parallel agents with LLM-based result synthesis.Demonstrates: Intelligent aggregation, synthesis
10. MapReduce
10. MapReduce
Distribute tasks, process in parallel, reduce results.Demonstrates: Distributed processing, scalability
Key Concepts
ReAct Loop
The ReAct agent follows a reasoning loop:Built-in Tools
MoFA provides several ready-to-use tools:Calculator
Evaluate mathematical expressions
DateTime
Get current time and date
String
String manipulation operations
JSON
Parse and manipulate JSON
Tool Implementation
To create a custom tool:Configuration Options
Agent Builder
Chain Configuration
Parallel Configuration
Expected Output
Common Use Cases
Research Assistant
Search, analyze, and summarize information
Code Analysis
Analyze codebases and suggest improvements
Data Processing
Transform and analyze data with tools
Task Automation
Automate complex multi-step workflows
Troubleshooting
Tool Execution Failure
Tool Execution Failure
Problem: Tool returns an errorSolution: Implement error handling in tools:
Max Iterations Reached
Max Iterations Reached
Problem: Agent hits iteration limitSolution: Increase max_iterations or improve prompts:
Slow Performance
Slow Performance
Problem: Agent takes too longSolution: Use AutoAgent or reduce iterations:
Next Steps
Multi-Agent
Coordinate multiple ReAct agents
Workflow
Orchestrate complex workflows
Custom Tools
Build advanced custom tools
ReAct Guide
Deep dive into ReAct pattern