from typing import List from duckduckgo_search import DDGS # pip install -U duckduckgo-search import re # -------- helper to shorten very long GAIA questions (optional but helpful) def tighten(q: str) -> str: quoted = re.findall(r'"([^"]+)"', q) caps = re.findall(r'\b([A-Z0-9][\w-]{2,})', q) short = " ".join(quoted + caps) return short or q # -------- the only search function your agent will call def simple_search(query: str, max_results: int = 5) -> List[str]: """ Perform a DuckDuckGo search and return 'title – url' snippets. """ query = tighten(query) # optional heuristic cleaner with DDGS() as ddgs: # context-manager is the recommended way 🐤 raw = list(ddgs.text(query, max_results=max_results)) # DDGS.text() returns list of dicts out = [] for r in raw: try: title = r.get("title", "") link = r.get("href") or r.get("link", "") out.append(f"{title} – {link}") except Exception: pass return out