File size: 1,064 Bytes
2bf962d 622f2bb aeb4eba 2bf962d 622f2bb a9c5bda 622f2bb aeb4eba 622f2bb aeb4eba 622f2bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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
|