Commit ·
97fc18c
1
Parent(s): da8c40b
feat(run): add retry mechanism with error handling to agent entry point
Browse filesImplement retry logic in the run() function with max_retries parameter (default 3).
On failure, append error context to help the agent retry with a simpler approach.
Add recursion_limit config of 30 to prevent infinite loops. Also update
answer extractor output to wrap answer in quotes for better visibility.
- agent/agent.py +43 -10
- agent/agents/answer_extractor.py +1 -1
agent/agent.py
CHANGED
|
@@ -29,17 +29,50 @@ def supervisor_agent():
|
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
-
def run(query: str) -> str:
|
| 33 |
"""Entry point: let the supervisor agent finish the work."""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
+
def run(query: str, max_retries: int = 3) -> str:
|
| 33 |
"""Entry point: let the supervisor agent finish the work."""
|
| 34 |
+
last_error: str | None = None
|
| 35 |
+
|
| 36 |
+
for attempt in range(1, max_retries + 1):
|
| 37 |
+
print(
|
| 38 |
+
f"{Fore.CYAN}[Supervisor] Processing query (attempt {attempt}/{max_retries})...{Style.RESET_ALL}"
|
| 39 |
+
)
|
| 40 |
+
agent = supervisor_agent()
|
| 41 |
+
|
| 42 |
+
messages = [HumanMessage(content=query)]
|
| 43 |
+
if last_error:
|
| 44 |
+
messages.append(
|
| 45 |
+
HumanMessage(
|
| 46 |
+
content=(
|
| 47 |
+
f"Previous attempt failed with error: {last_error}\n"
|
| 48 |
+
f"Try thinking about the problem more simply. "
|
| 49 |
+
f"Use fewer steps and a more straightforward approach."
|
| 50 |
+
)
|
| 51 |
+
)
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
result = agent.invoke(
|
| 56 |
+
{"messages": messages},
|
| 57 |
+
# Max agent action is 30 turn.
|
| 58 |
+
config={"recursion_limit": 30},
|
| 59 |
+
)
|
| 60 |
+
content = result["messages"][-1].content
|
| 61 |
+
if isinstance(content, list):
|
| 62 |
+
content = content[0].get("text", "")
|
| 63 |
+
else:
|
| 64 |
+
content = str(content)
|
| 65 |
+
return extract_answer(content, query)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
last_error = str(e)
|
| 68 |
+
print(
|
| 69 |
+
f"{Fore.RED}[Supervisor] Attempt {attempt} failed: {last_error}{Style.RESET_ALL}"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
print(f"{Fore.RED}[Supervisor] All {max_retries} attempts failed.{Style.RESET_ALL}")
|
| 73 |
+
return extract_answer(
|
| 74 |
+
f"Agent failed after {max_retries} attempts. Last error: {last_error}", query
|
| 75 |
+
)
|
| 76 |
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
agent/agents/answer_extractor.py
CHANGED
|
@@ -80,5 +80,5 @@ Output:
|
|
| 80 |
print(
|
| 81 |
f"{Fore.CYAN}[AnswerExtractor] Process: {extracted.extract_process_description}{Style.RESET_ALL}"
|
| 82 |
)
|
| 83 |
-
print(f"{Fore.CYAN}[AnswerExtractor] Answer: {extracted.answer}{Style.RESET_ALL}")
|
| 84 |
return extracted.answer
|
|
|
|
| 80 |
print(
|
| 81 |
f"{Fore.CYAN}[AnswerExtractor] Process: {extracted.extract_process_description}{Style.RESET_ALL}"
|
| 82 |
)
|
| 83 |
+
print(f"{Fore.CYAN}[AnswerExtractor] Answer: '{extracted.answer}'{Style.RESET_ALL}")
|
| 84 |
return extracted.answer
|