← 返回 meta 的题目列表Implement a ReAct-style Agent Loop with Given APIs
类型:online_judge
Problem: Implement a ReAct-Style Agent Loop with Given APIs
You need to implement a simplified ReAct (Reason + Act) agent execution loop. The system provides several APIs (as functions). Your job is to wire them into a loop so the agent can iteratively complete a task via a “think / act / observe” cycle.
Given APIs (provided as pseudocode/function signatures in the interview)
llm(prompt: str) -> str
Returns the model output string for the current prompt.
parse(output: str) -> dict
Parses the model output into a structured dict. Possible keys:
"thought": str
"action": str
"action_input": str
"final": str (if present, the loop should stop)
tool(action: str, action_input: str) -> str
Executes the tool call and returns an observation.
Task
Implement react_loop(task: str, max_steps: int) -> str:
Initialize a prompt containing the task.
Loop up to max_steps times:
Call llm(prompt).
Parse with parse(...).
If final exists, return it.
Otherwise require action and action_input:
Call tool(action, action_input) to get an observation.
Append thought/action/action_input/observation to the prompt in a consistent format.
If max_steps is reached without a final, return a failure message (e.g., "Max steps reached").
Constraints / Edge Cases
Handle parse errors or missing fields. You may either fail fast or append an error observation to request a retry.
Focus: loop structure, state/prompt accumulation, termination conditions, robustness.
I/O (abstract for local testing)
Input: task and max_steps.
Output: final answer string.
Example
Input
task=Find the capital of France; max_steps=3
Output
Paris