Loading...
Loading...
The ReAct (Reasoning + Acting) pattern is a prompting and agent design paradigm where an AI agent interleaves Thought (reasoning about the current state), Action (calling a tool or taking a step), and Observation (processing the result of that action) in a loop until a final answer is reached.
Your task is to implement a simplified ReAct Agent Simulator that processes a sequence of steps and determines the final answer based on a set of available "tools".
You are given:
"Thought: <reasoning>" — the agent reflects on what to do next."Action: <tool>(<argument>)" — the agent calls a tool with an argument."Observation: <result>" — the result of the last action (provided externally)."Answer: <final_answer>" — the agent concludes with a final answer.You have access to the following tools:
search(query) → returns the length of the query string (simulated search result count).calculate(expr) → evaluates a simple arithmetic expression (+, -, *, /) with two integer operands (e.g., "3+5" → 8).lookup(key) → returns the number of characters in key (simulated lookup).Your simulator should:
Action step is encountered, execute the tool and record its output.Observation step matches the tool's output. If it doesn't match, mark the trace as invalid.Answer step, or "NO_ANSWER" if no answer step exists, or "INVALID" if any observation mismatch is found.Function signature: react_agent(query: str, steps: List[str]) -> str
"NO_ANSWER", or "INVALID".Input:
query = "What is 3 plus 5?"
steps = [
"Thought: I need to calculate 3+5.",
"Action: calculate(3+5)",
"Observation: 8",
"Answer: The result is 8."
]
1 <= len(steps) <= 50 0 <= len(query) <= 200 Each step string has length <= 200 Arithmetic operands are non-negative integers <= 10^4 Division is always integer division; divisor is never 0 Tool names are always one of: search, calculate, lookup Arguments contain no spaces
Output: "The result is 8."
Input:
query = "Search for Python"
steps = [
"Thought: Let me search for Python.",
"Action: search(Python)",
"Observation: 6",
"Answer: Found 6 results."
]
Output: "Found 6 results."
Explanation: search("Python") returns len("Python") = 6. Observation matches. Final answer is returned.
Input:
query = "Lookup AI"
steps = [
"Action: lookup(AI)",
"Observation: 99",
"Answer: Done."
]
Output: "INVALID"
Explanation: lookup("AI") returns len("AI") = 2, but observation says 99. Mismatch → INVALID.
Thought:, Action:, Observation:, Answer:).calculate, handle integer arithmetic with +, -, *, / (integer division).