agentchat.contrib.reasoning_agent
ThinkNode
class ThinkNode()
__init__
def __init__(content: str, parent: Optional["ThinkNode"] = None) -> None
A node in a tree structure representing a step in the reasoning process.
This class implements a tree node that stores content (text describing a reasoning step), maintains parent-child relationships, tracks node statistics, and provides utilities for traversing/visualizing the reasoning path.
Arguments:
content
str - The text content/description for this reasoning stepparent
Optional[ThinkNode] - The parent node in the tree, if any
Attributes:
-
content
str - The text content/description for this reasoning step -
value
Optional[float] - A numeric score/value assigned to this node -
parent
Optional[ThinkNode] - Reference to parent node -
depth
int - The depth of this node in the tree (root = 0) -
children
List[ThinkNode] - List of child nodes -
visits
int - Number of times this node has been visited during searchThe node automatically maintains the tree structure by:
- Setting its depth based on parent's depth + 1
- Adding itself to parent's children list if parent exists
- Providing trajectory utilities to get the full path from root to this node
trajectory
@property
def trajectory() -> str
Get a formatted string representation of the path from root to this node.
Returns:
str
- A formatted string showing the question and each step in the reasoning process
to_dict
def to_dict() -> Dict
Convert ThinkNode to dictionary representation.
Returns:
Dict
- Dictionary containing all node attributes and recursive children
from_dict
@classmethod
def from_dict(cls,
data: Dict,
parent: Optional["ThinkNode"] = None) -> "ThinkNode"
Create ThinkNode from dictionary representation.
Arguments:
data
Dict - Dictionary containing node dataparent
Optional[ThinkNode] - Parent node to attach to
Returns:
ThinkNode
- Reconstructed node with all children
visualize_tree
def visualize_tree(root: ThinkNode) -> None
Visualize the tree of thoughts using graphviz.
ReasoningAgent
class ReasoningAgent(AssistantAgent)
__init__
def __init__(name,
llm_config,
max_depth=4,
beam_size=3,
answer_approach="pool",
verbose=True,
**kwargs) -> None
Initialize a ReasoningAgent that uses tree-of-thought reasoning.,
Arguments:
name
- Name of the agentllm_config
- Configuration for the language modelmax_depth
int - Maximum depth of the reasoning treebeam_size
int - Number of parallel reasoning paths to maintainanswer_approach
str - Either "pool" or "best" - how to generate final answerverbose
bool - Whether to show intermediate steps
rate_node
def rate_node(node: ThinkNode) -> float
Rate the quality of a reasoning path using the grader agent.
Arguments:
node
ThinkNode - Node containing the reasoning trajectory to evaluate
Returns:
float
- Normalized score between 0 and 1 indicating trajectory quality
generate_response
def generate_response(messages, sender, config=None)
Generate a response using tree-of-thought reasoning.
Implements beam search through a tree of reasoning steps, using the thinker agent to generate possible next steps and the grader agent to evaluate paths.
Arguments:
messages
- Input messages to respond tosender
- Agent sending the messagesconfig
- Optional configuration
Returns:
Tuple[bool, str]: Success flag and generated response