Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -8,20 +8,28 @@ import pytesseract
|
|
| 8 |
from state import AgentState
|
| 9 |
from langchain.schema import HumanMessage
|
| 10 |
import regex as re
|
|
|
|
|
|
|
| 11 |
def web_search_tool(state: AgentState) -> AgentState:
|
| 12 |
"""
|
| 13 |
Expects: state["web_search_query"] is a non‐empty string.
|
| 14 |
Returns: {"web_search_query": None, "web_search_result": <string>}
|
| 15 |
We also clear web_search_query so we don’t loop forever.
|
|
|
|
| 16 |
"""
|
| 17 |
# print("reached web search tool")
|
| 18 |
query = state.get("web_search_query", "")
|
| 19 |
if not query:
|
| 20 |
return {} # nothing to do
|
| 21 |
|
| 22 |
-
# Run DuckDuckGo
|
| 23 |
ddg = DuckDuckGoSearchRun()
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
print(f"web_search_result: {result_text}")
|
| 26 |
return {
|
| 27 |
"web_search_query": None,
|
|
@@ -214,7 +222,7 @@ def wikipedia_search_tool(state: AgentState) -> AgentState:
|
|
| 214 |
search_data = search_resp.json()
|
| 215 |
|
| 216 |
search_results = search_data.get("query", {}).get("search", [])
|
| 217 |
-
print("wikipedia: search_results",search_results)
|
| 218 |
if not search_results:
|
| 219 |
return {"wiki_query": None, "wiki_result": f"No Wikipedia page found for '{query}'."}
|
| 220 |
|
|
|
|
| 8 |
from state import AgentState
|
| 9 |
from langchain.schema import HumanMessage
|
| 10 |
import regex as re
|
| 11 |
+
import time
|
| 12 |
+
|
| 13 |
def web_search_tool(state: AgentState) -> AgentState:
|
| 14 |
"""
|
| 15 |
Expects: state["web_search_query"] is a non‐empty string.
|
| 16 |
Returns: {"web_search_query": None, "web_search_result": <string>}
|
| 17 |
We also clear web_search_query so we don’t loop forever.
|
| 18 |
+
If the result is a DuckDuckGo 202 Ratelimit error, retry up to 5 times with a 5 second sleep between attempts.
|
| 19 |
"""
|
| 20 |
# print("reached web search tool")
|
| 21 |
query = state.get("web_search_query", "")
|
| 22 |
if not query:
|
| 23 |
return {} # nothing to do
|
| 24 |
|
|
|
|
| 25 |
ddg = DuckDuckGoSearchRun()
|
| 26 |
+
max_retries = 5
|
| 27 |
+
for attempt in range(max_retries):
|
| 28 |
+
result_text = ddg.run(query)
|
| 29 |
+
if result_text.strip() != "https://lite.duckduckgo.com/lite/ 202 Ratelimit":
|
| 30 |
+
break
|
| 31 |
+
if attempt < max_retries - 1:
|
| 32 |
+
time.sleep(5)
|
| 33 |
print(f"web_search_result: {result_text}")
|
| 34 |
return {
|
| 35 |
"web_search_query": None,
|
|
|
|
| 222 |
search_data = search_resp.json()
|
| 223 |
|
| 224 |
search_results = search_data.get("query", {}).get("search", [])
|
| 225 |
+
# print("wikipedia: search_results",search_results)
|
| 226 |
if not search_results:
|
| 227 |
return {"wiki_query": None, "wiki_result": f"No Wikipedia page found for '{query}'."}
|
| 228 |
|