ReasoningAgent - Tree of Thoughts with Beam Search in AG2
TL;DR:
- We introduce ReasoningAgent, an AG2 agent that implements tree-of-thought reasoning with beam search to solve complex problems.
- ReasoningAgent explores multiple reasoning paths in parallel and uses a grader agent to evaluate and select the most promising paths.
- The exploration trajectory and thought tree can be saved locally for further analysis. These logs can even be saved as SFT dataset and preference dataset for DPO and PPO training.
Introduction
Large language models (LLMs) have shown impressive capabilities in various tasks, but they can still struggle with complex reasoning problems that require exploring multiple solution paths. To address this limitation, we introduce ReasoningAgent, an AG2 agent that implements tree-of-thought reasoning with beam search.
The key idea behind ReasoningAgent is to:
- Generate multiple possible reasoning steps at each point
- Evaluate these steps using a grader agent
- Keep track of the most promising paths using beam search
- Continue exploring those paths while pruning less promising ones
This approach allows the agent to systematically explore different reasoning strategies while managing computational resources efficiently.
How ReasoningAgent Works
ReasoningAgent consists of three main components:
- A Thinker Agent: Generates possible next steps in the reasoning process
- A Grader Agent: Evaluates the quality of different reasoning paths
- Beam Search: Maintains a fixed number of most promising paths
The process works as follows:
- The thinker agent generates multiple possible next steps from the current state
- The grader agent evaluates each path and assigns a score
- Beam search selects the top-k paths based on these scores
- The process repeats until a solution is found or maximum depth is reached
O1-Style Reasoning with Beam Size 1
When beam_size=1
, ReasoningAgent behaves similarly to Chain-of-Thought (CoT) or O1-style reasoning, where only a single reasoning path is explored. This is useful for:
- Simple Problems: When the problem is straightforward and multiple solution paths are unnecessary
- Resource Conservation: When you want to minimize API calls and computational costs
- Baseline Comparison: To compare performance with and without beam search
For example:
This configuration means:
- Only one path is explored at each step
- The grader still evaluates the path, but no comparison between paths is needed
- The process is more streamlined but may miss alternative solutions
Here’s a simple example of using ReasoningAgent:
Larger Beam Size for Complex Problems
For more complex problems, we can increase the beam size to explore multiple reasoning paths in parallel:
The agent will explore multiple approaches simultaneously:
- Formulating the objective function
- Defining decision variables
- Establishing constraints
Visualizing the Reasoning Process
ReasoningAgent includes built-in visualization support using graphviz:
This generates a tree diagram showing:
- Different reasoning paths explored
- Evaluation scores for each path
- Number of visits to each node
Save the Thought Tree as Training dataset
As you have played with the ReasoningAgent
, you may find the LLM’s API expense is really high.
On the other hand, the thought tree is a good training dataset for SFT, DPO, and PPO.
After asking a question to the ReasoningAgent
, you only need to simply call the to_dict
function to save the thought tree as a file.
You can also use pickle
directly to save the thought tree.
Cleaning for SFT
This step finds the best trajectory in the thought tree and converts it to a SFT dataset as a sequence of strings. The best trajectory is determined by following the highest-scoring path from root to leaf.
Cleaning for RLHF (DPO and PPO)
This step generates preference pairs by comparing sibling nodes in the tree. For each parent node with multiple children, we create training pairs where the higher-scored response is marked as preferred over the lower-scored one.
The resulting datasets can be used to:
- Fine-tune a base model using SFT with the best trajectories
- Train reward models or directly optimize policies using the preference pairs
- Analyze and improve the reasoning patterns of the agent
Key Benefits
-
Systematic Exploration: Instead of committing to a single reasoning path, ReasoningAgent explores multiple possibilities systematically.
-
Quality Control: The grader agent helps ensure that each step in the reasoning process is sound.
-
Transparency: The visualization tools help understand how the agent arrives at its conclusions.
Conclusion
ReasoningAgent demonstrates how combining tree-of-thought reasoning with beam search can enhance an LLM’s problem-solving capabilities. By systematically exploring and evaluating multiple solution paths, it can tackle complex problems more effectively than traditional approaches.
The implementation is flexible and can be customized for different types of problems by adjusting parameters like beam size and maximum depth. We encourage the community to experiment with ReasoningAgent and contribute to its development.
For Further Reading
- Documentation about ReasoningAgent
- Example notebook
- The Original research paper about Tree of Thoughts from Google DeepMind and Princeton University.
Do you have interesting use cases for ReasoningAgent? Would you like to see more features or improvements? Please join our Discord server for discussion.