Implement a basic ReAct (Reasoning + Acting) pattern where an agent alternates between generating thoughts about the current situation and taking actions. The agent should:
- Generate a thought based on the observation and history
- Generate an action based on the thought
- Determine if it should continue (steps < max_steps)
Constraints:
- Must track history properly
- Should handle empty histories gracefully
- Must respect max_steps limit
- Return structured output with all three fields
Examples
Example 1:
Input:
react_agent('The user wants weather in Paris', [], [], max_steps=3)Output:
{'thought': 'I need to get weather information for Paris', 'action': 'weather_api(city="Paris")', 'should_continue': True}Explanation: Agent generates thought about needing weather, then appropriate action, and continues since steps < max_steps
Starter Code
def react_agent(observation, thought_history, action_history, max_steps=5):
"""
Implement a basic ReAct (Reasoning + Acting) agent loop.
The agent should alternate between Thought and Action.
Args:
observation: Current environment observation (string)
thought_history: List of previous thoughts
action_history: List of previous actions
max_steps: Maximum reasoning steps allowed
Returns:
dict with 'thought', 'action', 'should_continue'
"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready