ReAct Pattern Implementation
ReAct (Reasoning + Acting) interleaves chain-of-thought reasoning with tool actions.
Task
Implement a ReActAgent that:
- Maintains a trace of Thought → Action → Observation steps.
- Terminates when a final answer is produced or
max_stepsis reached. - Supports pluggable tools via a dictionary.
Constraints
- Each step must log: thought, action, observation.
- If no tool matches, return an error observation.
- Return
'[Max steps exceeded]'if limit reached.
Examples
Example 1:
Input:
tools = {'add': lambda a, b: a + b}
agent = ReActAgent(tools)
agent.run('What is 2 + 3?')Output:
'5'Explanation: Agent thinks, calls add(2,3), observes 5, returns answer.
Starter Code
from typing import Dict, Any, Optional
class ReActAgent:
def __init__(self, tools: Dict[str, callable]):
self.tools = tools
self.trace = []
def think(self, observation: str) -> str:
# TODO: Produce a thought given observation
pass
def act(self, thought: str) -> Dict[str, Any]:
# TODO: Return {'tool': name, 'args': {...}} or {'answer': '...'}
pass
def observe(self, tool: str, args: Dict) -> str:
# TODO: Execute tool and return observation
pass
def run(self, task: str, max_steps: int = 5) -> str:
# TODO: Implement Reason-Act-Observe loop
pass
Python3
ReadyLines: 1Characters: 0
Ready