SOLUTION-LAB-004: ReAct Agent¶
Reference solution for LAB-004: Building ReAct Agents.
Overview¶
Complete solution for building a ReAct (Reasoning + Acting) agent.
Core Solution¶
from typing import Callable
import json
class Tool:
def __init__(self, name: str, func: Callable, description: str):
self.name = name
self.func = func
self.description = description
class ReActAgent:
def __init__(self, llm, tools: list[Tool]):
self.llm = llm
self.tools = {tool.name: tool for tool in tools}
def run(self, query: str, max_steps: int = 10):
prompt = query
history = []
for step in range(max_steps):
# Thought
thought = self.llm.generate(f"{prompt}\nThought:")
history.append(f"Thought: {thought}")
# Action
action = self._parse_action(thought)
if action["type"] == "finish":
return action["answer"]
if action["type"] == "tool":
result = self.tools[action["tool"]].func(**action["input"])
history.append(f"Observation: {result}")
# Update prompt
prompt = f"{prompt}\nThought: {thought}\nObservation: {result}\nThought:"
def _parse_action(self, thought: str) -> dict:
# Parse "I should use TOOL_NAME..." or "Final answer is..."
if "final answer" in thought.lower():
return {"type": "finish", "answer": thought.split("is:")[-1]}
# Parse tool call
return {"type": "tool", "tool": "...", "input": {...}}
# Tools
def search_tool(query: str) -> str:
return f"Search results for: {query}"
def calculator(expression: str) -> str:
return str(eval(expression, {"__builtins__": {}}, {}))
# Usage
agent = ReActAgent(llm, [
Tool("search", search_tool, "Search the web"),
Tool("calculator", calculator, "Calculate expressions")
])
result = agent.run("What is 25 * 34 + 10?")
Difficulty: ⭐⭐⭐ Advanced