Commit ·
cee18af
1
Parent(s): 2f08174
fix(websearch): add recursion limit and error handling for search agent
Browse filesAdd recursion_limit=3 configuration and GraphRecursionError handling to prevent infinite loops in the web search agent. The agent now returns a partial result message when the recursion limit is reached instead of crashing.
- .gitignore +3 -1
- agent/agents/websearch.py +17 -6
.gitignore
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
-
.env
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
*.pyc
|
| 3 |
+
__pycache__/
|
agent/agents/websearch.py
CHANGED
|
@@ -2,6 +2,7 @@ from datetime import datetime, timezone
|
|
| 2 |
from colorama import Fore, Style # type: ignore[import]
|
| 3 |
from langchain_core.tools import tool
|
| 4 |
from langchain.agents import create_agent
|
|
|
|
| 5 |
from agent.tools.search import web_search
|
| 6 |
|
| 7 |
|
|
@@ -25,14 +26,24 @@ def websearch_agent(query: str) -> str:
|
|
| 25 |
f"Your memory are out of date. "
|
| 26 |
f"All of truth that you believe without search are wrong. "
|
| 27 |
f"You must search the web and find the lastest answer."
|
|
|
|
| 28 |
),
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
content =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
print(
|
| 37 |
f"{Fore.YELLOW}[WebSearchAgent -> SupervisorAgent] {content}{Style.RESET_ALL}"
|
| 38 |
)
|
|
|
|
| 2 |
from colorama import Fore, Style # type: ignore[import]
|
| 3 |
from langchain_core.tools import tool
|
| 4 |
from langchain.agents import create_agent
|
| 5 |
+
from langgraph.errors import GraphRecursionError
|
| 6 |
from agent.tools.search import web_search
|
| 7 |
|
| 8 |
|
|
|
|
| 26 |
f"Your memory are out of date. "
|
| 27 |
f"All of truth that you believe without search are wrong. "
|
| 28 |
f"You must search the web and find the lastest answer."
|
| 29 |
+
f"Just run 1 turn search. "
|
| 30 |
),
|
| 31 |
)
|
| 32 |
+
try:
|
| 33 |
+
result = base_agent.invoke(
|
| 34 |
+
{"messages": [{"role": "user", "content": query}]},
|
| 35 |
+
config={"recursion_limit": 3},
|
| 36 |
+
)
|
| 37 |
+
content = result["messages"][-1].content
|
| 38 |
+
if isinstance(content, list):
|
| 39 |
+
content = content[0].get("text", "")
|
| 40 |
+
else:
|
| 41 |
+
content = str(content)
|
| 42 |
+
except GraphRecursionError:
|
| 43 |
+
print(
|
| 44 |
+
f"{Fore.RED}[WebSearchAgent] Recursion limit reached, returning partial results.{Style.RESET_ALL}"
|
| 45 |
+
)
|
| 46 |
+
content = "Search completed but no definitive answer was found within the allowed steps."
|
| 47 |
print(
|
| 48 |
f"{Fore.YELLOW}[WebSearchAgent -> SupervisorAgent] {content}{Style.RESET_ALL}"
|
| 49 |
)
|