diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-06-23 18:37:46 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-06-23 18:37:46 +0900 |
| commit | 21b59cc1d0928dc04dcf5eac24470aaa05013b2d (patch) | |
| tree | 9888aa23aafea3907ce96eb958a06d4369dad86e /src/ollama_web/tools/search.py | |
| parent | de0513e7cb0593b1aa141e367ff4da7156d6110f (diff) | |
基本機能実装
Diffstat (limited to 'src/ollama_web/tools/search.py')
| -rw-r--r-- | src/ollama_web/tools/search.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/ollama_web/tools/search.py b/src/ollama_web/tools/search.py new file mode 100644 index 0000000..0af2a3e --- /dev/null +++ b/src/ollama_web/tools/search.py @@ -0,0 +1,45 @@ +"""Web search tool backed by DuckDuckGo (ddgs).""" + +from __future__ import annotations + +from ddgs import DDGS + +from ..config import settings + + +def web_search(query: str, max_results: int | None = None) -> list[dict[str, str]]: + """Search the web using DuckDuckGo and return compact result summaries. + + Args: + query: The search query string. + max_results: Maximum number of results to return (default from settings). + + Returns: + A list of dictionaries with keys 'title', 'href' and 'body' for each + result. Returns an empty list if the search fails or no results are found. + """ + if not query or not query.strip(): + return [] + + limit = ( + max_results + if max_results is not None and max_results > 0 + else settings.search_max_results + ) + + results: list[dict[str, str]] = [] + try: + with DDGS() as ddgs: + for item in ddgs.text(query, max_results=limit): + results.append( + { + "title": str(item.get("title", "")).strip(), + "href": str(item.get("href") or item.get("url") or "").strip(), + "body": str(item.get("body") or item.get("snippet") or "").strip(), + } + ) + except Exception: # noqa: BLE001 + # Swallow network/runtime errors so the chat loop can continue. + return [] + + return results[:limit] |
