Skip to content

Commit

Permalink
Add a basic blog
Browse files Browse the repository at this point in the history
  • Loading branch information
BabyCNM committed Dec 2, 2024
1 parent 6e7fef2 commit a091d1e
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions website/blog/2024-12-02-ReasoningAgent2/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
---
title: ReasoningAgent - Tree of Thoughts with Beam Search in AutoGen
authors:
- Hk669
- skzhang1
- sonichi
tags: [LLM, GPT, research]
---

![Tree of Thoughts](img/tree_of_thoughts.png)

**TL;DR:**
* We introduce **ReasoningAgent**, an AutoGen 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.
* Experiments show that ReasoningAgent can achieve better results than basic prompting by exploring multiple solution paths systematically.

## 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 AutoGen agent that implements tree-of-thought reasoning with beam search.

The key idea behind ReasoningAgent is to:
1. Generate multiple possible reasoning steps at each point
2. Evaluate these steps using a grader agent
3. Keep track of the most promising paths using beam search
4. 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:

1. **A Thinker Agent**: Generates possible next steps in the reasoning process
2. **A Grader Agent**: Evaluates the quality of different reasoning paths
3. **Beam Search**: Maintains a fixed number of most promising paths

The process works as follows:

1. The thinker agent generates multiple possible next steps from the current state
2. The grader agent evaluates each path and assigns a score
3. Beam search selects the top-k paths based on these scores
4. 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:

1. **Simple Problems**: When the problem is straightforward and multiple solution paths are unnecessary
2. **Resource Conservation**: When you want to minimize API calls and computational costs
3. **Baseline Comparison**: To compare performance with and without beam search

For example:
```python
# Create a reasoning agent with beam size 1 (O1-style)
thought_agent = ReasoningAgent(
name="thought_agent",
llm_config={"config_list": config_list},
verbose=False,
beam_size=1, # Using beam size 1 for O1-style reasoning
max_depth=3,
)
```

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:

```python
import os
from autogen import AssistantAgent, UserProxyAgent
from autogen.agentchat.contrib.reasoning_agent import ReasoningAgent, visualize_tree

# Configure the model
config_list = [{"model": "gpt-4", "api_key": os.environ.get("OPENAI_API_KEY")}]

# Create a reasoning agent with beam search
thought_agent = ReasoningAgent(
name="thought_agent",
llm_config={"config_list": config_list},
verbose=False,
beam_size=1, # Using beam size 1 for O1-style reasoning
max_depth=3,
)

# Create a user proxy agent
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
code_execution_config={"use_docker": False},
max_consecutive_auto_reply=10,
)
```

## Larger Beam Size for Complex Problems

For more complex problems, we can increase the beam size to explore multiple reasoning paths in parallel:

```python
thought_agent = ReasoningAgent(
name="thought_agent",
llm_config={"config_list": config_list},
verbose=False,
beam_size=3, # Explore 3 paths in parallel
max_depth=3,
)

# Example complex problem
task = "Design a mixed integer linear program for a coffee roasting supply chain"
response = user_proxy.initiate_chat(
thought_agent,
message=task,
summary_method=last_meaningful_msg
)
```

The agent will explore multiple approaches simultaneously:
1. Formulating the objective function
2. Defining decision variables
3. Establishing constraints

## Visualizing the Reasoning Process

ReasoningAgent includes built-in visualization support using graphviz:

```python
# After running a query, visualize the reasoning tree
visualize_tree(thought_agent._root)
```

This generates a tree diagram showing:
- Different reasoning paths explored
- Evaluation scores for each path
- Number of visits to each node

## Key Benefits

1. **Systematic Exploration**: Instead of committing to a single reasoning path, ReasoningAgent explores multiple possibilities systematically.

2. **Quality Control**: The grader agent helps ensure that each step in the reasoning process is sound.

3. **Resource Efficiency**: Beam search focuses computational resources on the most promising paths.

4. **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](/docs/reference/agentchat/contrib/reasoning_agent)
* [Example notebook](https://github.com/microsoft/autogen/blob/main/notebook/agentchat_reasoning_agent.ipynb)
* [The Original research paper about Tree of Thoughts](https://arxiv.org/abs/2305.10601) 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](https://aka.ms/autogen-dc) server for discussion.*

0 comments on commit a091d1e

Please sign in to comment.