Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -36,6 +36,35 @@ def _download_file_for_task(task_id: str, ext: str) -> str:
|
|
| 36 |
return ""
|
| 37 |
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def ocr_image_tool(state: AgentState) -> AgentState:
|
| 40 |
"""
|
| 41 |
Expects state["ocr_path"] to be either:
|
|
|
|
| 36 |
return ""
|
| 37 |
|
| 38 |
|
| 39 |
+
def web_search_tool(state: AgentState) -> AgentState:
|
| 40 |
+
"""
|
| 41 |
+
Expects: state["web_search_query"] is a non‐empty string.
|
| 42 |
+
Returns: {"web_search_query": None, "web_search_result": <string>}
|
| 43 |
+
We also clear web_search_query so we don’t loop forever.
|
| 44 |
+
If the result is a DuckDuckGo 202 Ratelimit error, retry up to 5 times with a 5 second sleep between attempts.
|
| 45 |
+
"""
|
| 46 |
+
# print("reached web search tool")
|
| 47 |
+
query = state.get("web_search_query", "")
|
| 48 |
+
if not query:
|
| 49 |
+
return {} # nothing to do
|
| 50 |
+
|
| 51 |
+
ddg = DuckDuckGoSearchRun()
|
| 52 |
+
max_retries = 5
|
| 53 |
+
for attempt in range(max_retries):
|
| 54 |
+
result_text = ddg.run(query)
|
| 55 |
+
if "202 Ratelimit" not in result_text:
|
| 56 |
+
break
|
| 57 |
+
if attempt < max_retries - 1:
|
| 58 |
+
print(f"web_search_result: rate limit error, retrying in 10 seconds")
|
| 59 |
+
time.sleep(3)
|
| 60 |
+
print(f"web_search_result reached ")
|
| 61 |
+
return {
|
| 62 |
+
"web_search_query": None,
|
| 63 |
+
"web_search_result": result_text
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
def ocr_image_tool(state: AgentState) -> AgentState:
|
| 69 |
"""
|
| 70 |
Expects state["ocr_path"] to be either:
|