Download src/tools/web/search.py from Snapkitty/sovereign-engine-v2: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/Snapkitty/sovereign-engine-v2/resolve/main/src/tools/web/search.py
- Command line
-
hf download hf://Snapkitty/sovereign-engine-v2/src/tools/web/search.py
-
curl -L -o search.py https://huggingface.co/Snapkitty/sovereign-engine-v2/resolve/main/src/tools/web/search.py
5.11 kB
| """ | |
| Web Search and Fetching Tools | |
| Part of SOVEREIGN PYTHON LLM ENGINE | |
| """ | |
| from typing import Any | |
| import httpx | |
| from bs4 import BeautifulSoup | |
| from ..registry import tool, RiskClass, ApprovalPolicy | |
| async def web_search(params: dict[str, Any]) -> dict[str, Any]: | |
| """ | |
| Search the web. | |
| Args: | |
| params: {query, max_results, search_depth, provider} | |
| Returns: | |
| {results, query} | |
| """ | |
| query = params['query'] | |
| max_results = params.get('max_results', 5) | |
| search_depth = params.get('search_depth', 'basic') | |
| provider = params.get('provider', 'tavily') | |
| if provider == 'tavily': | |
| from .providers.tavily import TavilySearch | |
| provider_impl = TavilySearch() | |
| elif provider == 'brave': | |
| from .providers.brave import BraveSearch | |
| provider_impl = BraveSearch() | |
| else: | |
| raise ValueError(f"Unknown provider: {provider}") | |
| results = await provider_impl.search( | |
| query=query, | |
| max_results=max_results, | |
| search_depth=search_depth | |
| ) | |
| return { | |
| 'results': results, | |
| 'query': query | |
| } | |
| async def web_fetch(params: dict[str, Any]) -> dict[str, Any]: | |
| """ | |
| Fetch web page content. | |
| Args: | |
| params: {url, timeout} | |
| Returns: | |
| {content, status_code, url} | |
| """ | |
| url = params['url'] | |
| timeout = params.get('timeout', 30) | |
| async with httpx.AsyncClient(timeout=timeout) as client: | |
| response = await client.get(url) | |
| return { | |
| 'content': response.text, | |
| 'status_code': response.status_code, | |
| 'url': str(response.url) | |
| } | |
| async def web_extract(params: dict[str, Any]) -> dict[str, Any]: | |
| """ | |
| Extract clean text from HTML. | |
| Args: | |
| params: {html} | |
| Returns: | |
| {text, title} | |
| """ | |
| html = params['html'] | |
| soup = BeautifulSoup(html, 'html.parser') | |
| # Remove script and style elements | |
| for script in soup(["script", "style"]): | |
| script.decompose() | |
| # Get text | |
| text = soup.get_text() | |
| # Clean up whitespace | |
| lines = (line.strip() for line in text.splitlines()) | |
| chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) | |
| text = '\n'.join(chunk for chunk in chunks if chunk) | |
| # Get title | |
| title = soup.title.string if soup.title else "" | |
| return { | |
| 'text': text, | |
| 'title': title | |
| } | |