Spaces:
Sleeping
Sleeping
File size: 1,333 Bytes
32f259e |
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 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from __future__ import annotations
from typing import List
# Prefer the new ddgs package, fall back to duckduckgo_search if needed
try:
from ddgs import DDGS # new package name
except ImportError:
from duckduckgo_search import DDGS # old package name
from ..state import ResearchSnippet
from ..config import settings
def web_search(query: str, max_results: int | None = None) -> List[ResearchSnippet]:
"""
Perform a web search and return normalized snippets.
Uses DuckDuckGo via ddgs (preferred) or duckduckgo_search.
"""
limit = max_results or settings.max_search_results
snippets: List[ResearchSnippet] = []
# region/safesearch can matter for results; 'wt-wt' = worldwide, 'off' = no filtering
with DDGS() as ddgs:
for result in ddgs.text(
query,
max_results=limit,
):
title = result.get("title") or ""
url = result.get("href") or result.get("url") or ""
snippet = result.get("body") or result.get("snippet") or ""
if not snippet.strip():
continue
snippets.append(
{
"title": title.strip(),
"url": url.strip(),
"snippet": snippet.strip(),
}
)
return snippets
|