Build a Thought -> Action -> Observation loop that terminates on a Final Answer.
The full prompt is available without an account.
Implement run_agent(question, llm, tools) that runs a ReAct-style loop.
Requirements:
- On each step, call the LLM with the running scratchpad.
- Parse output for either Action: <tool>(<args>) or Final Answer: <text>.
- Execute tool calls, append Observation: <result> to the scratchpad.
- Stop when a Final Answer is produced or after MAX_STEPS=6.
- Return the final answer string.
Read-only Python 3.12 preview. Sign in to edit and execute it.
import re
MAX_STEPS = 6
def run_agent(question: str, llm, tools: dict) -> str:
scratchpad = f"Question: {question}\n"
for _ in range(MAX_STEPS):
output = llm(scratchpad)
scratchpad += output + "\n"
# TODO: parse for Final Answer or Action and act accordingly
pass
return "I could not find an answer."