Scott Cogan
commited on
Commit
·
d855f5d
1
Parent(s):
0776514
web search fall back
Browse files
app.py
CHANGED
|
@@ -111,7 +111,7 @@ def open_youtube_video(url: str, query: str) -> str:
|
|
| 111 |
return response.text
|
| 112 |
|
| 113 |
def google_search(query: str) -> str:
|
| 114 |
-
'''Performs a web search for the given query using DuckDuckGo.'''
|
| 115 |
try:
|
| 116 |
# Add delay between requests to avoid rate limiting
|
| 117 |
time.sleep(1)
|
|
@@ -175,6 +175,30 @@ def google_search(query: str) -> str:
|
|
| 175 |
if quoted_data.get("Answer"):
|
| 176 |
result.append(f"Answer: {quoted_data['Answer']}")
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
return "\n".join(result) if result else "No results found."
|
| 179 |
|
| 180 |
except requests.exceptions.Timeout:
|
|
@@ -508,6 +532,10 @@ class BasicAgent:
|
|
| 508 |
# Add tool result to messages
|
| 509 |
messages.append(AIMessage(content=f"Tool result: {result}"))
|
| 510 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
# If this was a google_search, analyze the results
|
| 512 |
if tool_name == "google_search":
|
| 513 |
# Add a prompt to analyze the search results
|
|
|
|
| 111 |
return response.text
|
| 112 |
|
| 113 |
def google_search(query: str) -> str:
|
| 114 |
+
'''Performs a web search for the given query using DuckDuckGo, and falls back to Wikipedia if no results are found.'''
|
| 115 |
try:
|
| 116 |
# Add delay between requests to avoid rate limiting
|
| 117 |
time.sleep(1)
|
|
|
|
| 175 |
if quoted_data.get("Answer"):
|
| 176 |
result.append(f"Answer: {quoted_data['Answer']}")
|
| 177 |
|
| 178 |
+
# If still no results, try Wikipedia as a fallback
|
| 179 |
+
if not result:
|
| 180 |
+
try:
|
| 181 |
+
wiki_response = requests.get(
|
| 182 |
+
"https://en.wikipedia.org/w/api.php",
|
| 183 |
+
params={
|
| 184 |
+
"action": "query",
|
| 185 |
+
"format": "json",
|
| 186 |
+
"prop": "extracts",
|
| 187 |
+
"exintro": True,
|
| 188 |
+
"explaintext": True,
|
| 189 |
+
"titles": query
|
| 190 |
+
},
|
| 191 |
+
timeout=10
|
| 192 |
+
)
|
| 193 |
+
wiki_data = wiki_response.json()
|
| 194 |
+
pages = wiki_data.get("query", {}).get("pages", {})
|
| 195 |
+
for page in pages.values():
|
| 196 |
+
extract = page.get("extract")
|
| 197 |
+
if extract:
|
| 198 |
+
result.append(extract)
|
| 199 |
+
except Exception as e:
|
| 200 |
+
logger.error(f"Wikipedia fallback error for query {query}: {str(e)}")
|
| 201 |
+
|
| 202 |
return "\n".join(result) if result else "No results found."
|
| 203 |
|
| 204 |
except requests.exceptions.Timeout:
|
|
|
|
| 532 |
# Add tool result to messages
|
| 533 |
messages.append(AIMessage(content=f"Tool result: {result}"))
|
| 534 |
|
| 535 |
+
# Loop breaker: If the result is 'No results found.', end with 'Not found'
|
| 536 |
+
if isinstance(result, str) and "no results found" in result.lower():
|
| 537 |
+
return {"messages": [AIMessage(content="Not found")], "next": END}
|
| 538 |
+
|
| 539 |
# If this was a google_search, analyze the results
|
| 540 |
if tool_name == "google_search":
|
| 541 |
# Add a prompt to analyze the search results
|