Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from duckduckgo_search import DDGS | |
| def search(query: str, max_results: int = 5) -> list[dict]: | |
| """Search the web using DuckDuckGo. | |
| Args: | |
| query: Search query string. | |
| max_results: Maximum number of results to return. | |
| Returns: | |
| List of dicts with keys: title, url, snippet. | |
| """ | |
| try: | |
| with DDGS() as ddgs: | |
| results = list(ddgs.text(query, max_results=max_results)) | |
| return [ | |
| { | |
| "title": r.get("title", ""), | |
| "url": r.get("href", ""), | |
| "snippet": r.get("body", ""), | |
| } | |
| for r in results | |
| ] | |
| except Exception as e: | |
| return [{"title": "Search Error", "url": "", "snippet": str(e)}] | |