Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
@@ -40,33 +40,52 @@ def _download_file_for_task(task_id: str, ext: str) -> str:
|
|
40 |
def web_search_tool(state: AgentState) -> AgentState:
|
41 |
"""
|
42 |
Expects: state["web_search_query"] is a non‐empty string.
|
43 |
-
Returns: {"web_search_query": None, "web_search_result": <string>}
|
44 |
-
|
45 |
-
If the result is a DuckDuckGo 202 Ratelimit error, retry up to 5 times with a 5 second sleep between attempts.
|
46 |
"""
|
47 |
-
# print("reached web search tool")
|
48 |
query = state.get("web_search_query", "")
|
49 |
if not query:
|
50 |
return {} # nothing to do
|
51 |
-
|
52 |
ddg = DuckDuckGoSearchRun()
|
53 |
max_retries = 5
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
return {
|
64 |
"web_search_query": None,
|
65 |
"web_search_result": result_text
|
66 |
}
|
67 |
|
68 |
|
69 |
-
|
70 |
def ocr_image_tool(state: AgentState) -> AgentState:
|
71 |
"""
|
72 |
Expects state["ocr_path"] to be either:
|
|
|
40 |
def web_search_tool(state: AgentState) -> AgentState:
|
41 |
"""
|
42 |
Expects: state["web_search_query"] is a non‐empty string.
|
43 |
+
Returns: {"web_search_query": None, "web_search_result": <string>}.
|
44 |
+
Retries up to 5 times on either a DuckDuckGo “202 Ratelimit” response or any exception (e.g. timeout).
|
|
|
45 |
"""
|
|
|
46 |
query = state.get("web_search_query", "")
|
47 |
if not query:
|
48 |
return {} # nothing to do
|
49 |
+
|
50 |
ddg = DuckDuckGoSearchRun()
|
51 |
max_retries = 5
|
52 |
+
result_text = ""
|
53 |
+
|
54 |
+
for attempt in range(1, max_retries + 1):
|
55 |
+
try:
|
56 |
+
result_text = ddg.run(query)
|
57 |
+
except Exception as e:
|
58 |
+
# Network error or timeout—retry up to max_retries
|
59 |
+
if attempt < max_retries:
|
60 |
+
print(f"web_search_tool: exception '{e}', retrying in 4 seconds ({attempt}/{max_retries})")
|
61 |
+
time.sleep(4)
|
62 |
+
continue
|
63 |
+
else:
|
64 |
+
# Final attempt failed
|
65 |
+
return {
|
66 |
+
"web_search_query": None,
|
67 |
+
"web_search_result": f"Error during DuckDuckGo search: {e}"
|
68 |
+
}
|
69 |
+
|
70 |
+
# Check for DuckDuckGo rate‐limit indicator
|
71 |
+
if "202 Ratelimit" in result_text:
|
72 |
+
if attempt < max_retries:
|
73 |
+
print(f"web_search_tool: received '202 Ratelimit', retrying in 4 seconds ({attempt}/{max_retries})")
|
74 |
+
time.sleep(4)
|
75 |
+
continue
|
76 |
+
else:
|
77 |
+
# Final attempt still rate‐limited
|
78 |
+
break
|
79 |
+
|
80 |
+
# Successful response (no exception and no rate‐limit text)
|
81 |
+
break
|
82 |
+
|
83 |
return {
|
84 |
"web_search_query": None,
|
85 |
"web_search_result": result_text
|
86 |
}
|
87 |
|
88 |
|
|
|
89 |
def ocr_image_tool(state: AgentState) -> AgentState:
|
90 |
"""
|
91 |
Expects state["ocr_path"] to be either:
|