Paritosh Upadhyay commited on
Commit ·
eb2ebb3
1
Parent(s): fa0757c
Neural Diversification: Google Scavenger Fallback & Requirement Update
Browse files
backend/app/services/tools/search.py
CHANGED
|
@@ -6,6 +6,10 @@ from urllib.parse import urljoin, urlparse
|
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
import httpx
|
| 8 |
from app.services import state
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
logger = logging.getLogger("friday.search")
|
| 11 |
# Silence noisy network telemetry
|
|
@@ -74,11 +78,43 @@ def search_web(query: str, max_results: int = 5, bypass_cache: bool = False) ->
|
|
| 74 |
logger.warning(f"Search Tactical Maneuver: {e}. Swapping Identity & Retrying in {wait_time:.1f}s...")
|
| 75 |
time.sleep(wait_time)
|
| 76 |
else:
|
| 77 |
-
logger.
|
| 78 |
-
return
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
-
logger.error(f"Web search failed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return []
|
| 83 |
|
| 84 |
def extract_links(html: str, base_url: str, priority_keywords: List[str] = None) -> Set[str]:
|
|
|
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
import httpx
|
| 8 |
from app.services import state
|
| 9 |
+
try:
|
| 10 |
+
from googlesearch import search as gsearch
|
| 11 |
+
except ImportError:
|
| 12 |
+
gsearch = None
|
| 13 |
|
| 14 |
logger = logging.getLogger("friday.search")
|
| 15 |
# Silence noisy network telemetry
|
|
|
|
| 78 |
logger.warning(f"Search Tactical Maneuver: {e}. Swapping Identity & Retrying in {wait_time:.1f}s...")
|
| 79 |
time.sleep(wait_time)
|
| 80 |
else:
|
| 81 |
+
logger.warning(f"Search Front Exhausted: {e}. Pivoting to Secondary Engine (Google)...")
|
| 82 |
+
return google_search_scavenge(query, max_results)
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
logger.error(f"Web search failed: {e}. Attempting emergency Google scavenging...")
|
| 86 |
+
return google_search_scavenge(query, max_results)
|
| 87 |
+
|
| 88 |
+
def google_search_scavenge(query: str, max_results: int = 3) -> List[Dict]:
|
| 89 |
+
"""Fallback scavenger to retrieve results from Google if DDG is blocked."""
|
| 90 |
+
if not gsearch:
|
| 91 |
+
logger.error("Google Scavenger library not found.")
|
| 92 |
+
return []
|
| 93 |
+
|
| 94 |
+
logger.info(f"Scavenging Google link-forage for: {query}")
|
| 95 |
+
results = []
|
| 96 |
+
try:
|
| 97 |
+
# Fetch top URLs from Google
|
| 98 |
+
urls = list(gsearch(query, num_results=max_results, lang="en"))
|
| 99 |
+
|
| 100 |
+
# Scrape bodies for the top 2-3 results to provide context
|
| 101 |
+
for url in urls[:max_results]:
|
| 102 |
+
try:
|
| 103 |
+
# Synchronous fallback for scraping in this context
|
| 104 |
+
body = asyncio.run(scrape_url(url))
|
| 105 |
+
if body and len(body) > 100:
|
| 106 |
+
results.append({
|
| 107 |
+
"title": f"Google Intelligence: {urlparse(url).netloc}",
|
| 108 |
+
"href": url,
|
| 109 |
+
"body": body[:500] # Limit header size
|
| 110 |
+
})
|
| 111 |
+
except Exception as e:
|
| 112 |
+
logger.warning(f"Google Scrape failed for {url}: {e}")
|
| 113 |
+
results.append({"title": "Google Link", "href": url, "body": "Direct link retrieved, scrape failed."})
|
| 114 |
+
|
| 115 |
+
return results
|
| 116 |
+
except Exception as e:
|
| 117 |
+
logger.error(f"Google Scavenging failed: {e}")
|
| 118 |
return []
|
| 119 |
|
| 120 |
def extract_links(html: str, base_url: str, priority_keywords: List[str] = None) -> Set[str]:
|
backend/requirements.txt
CHANGED
|
@@ -47,6 +47,7 @@ beautifulsoup4==4.13.3
|
|
| 47 |
# --- Utilities ---
|
| 48 |
rich==14.0.0
|
| 49 |
duckduckgo_search==7.3.2
|
|
|
|
| 50 |
|
| 51 |
# --- Sovereign Neural Core (Apple Silicon) ---
|
| 52 |
mlx-lm==0.21.4
|
|
|
|
| 47 |
# --- Utilities ---
|
| 48 |
rich==14.0.0
|
| 49 |
duckduckgo_search==7.3.2
|
| 50 |
+
googlesearch-python==1.3.0
|
| 51 |
|
| 52 |
# --- Sovereign Neural Core (Apple Silicon) ---
|
| 53 |
mlx-lm==0.21.4
|