diff --git a/docker-compose.gpu-amd.yml b/docker-compose.gpu-amd.yml index 6fe2ae28ae..ded857ffd4 100644 --- a/docker-compose.gpu-amd.yml +++ b/docker-compose.gpu-amd.yml @@ -107,6 +107,7 @@ services: - TAVILY_API_KEY=${TAVILY_API_KEY:-} - SERPER_API_KEY=${SERPER_API_KEY:-} - YDC_API_KEY=${YDC_API_KEY:-} + - SOFYA_API_KEY=${SOFYA_API_KEY:-} # PUID / PGID — the user/group the container drops to before # running uvicorn (entrypoint also chowns /app/data + /app/logs # to match, so bind-mounted files stay editable from the host). diff --git a/docker-compose.gpu-nvidia.yml b/docker-compose.gpu-nvidia.yml index 419e0fd526..0e4124e04d 100644 --- a/docker-compose.gpu-nvidia.yml +++ b/docker-compose.gpu-nvidia.yml @@ -106,6 +106,7 @@ services: - TAVILY_API_KEY=${TAVILY_API_KEY:-} - SERPER_API_KEY=${SERPER_API_KEY:-} - YDC_API_KEY=${YDC_API_KEY:-} + - SOFYA_API_KEY=${SOFYA_API_KEY:-} # PUID / PGID — the user/group the container drops to before # running uvicorn (entrypoint also chowns /app/data + /app/logs # to match, so bind-mounted files stay editable from the host). diff --git a/docker-compose.yml b/docker-compose.yml index 2c3be57648..c778b60527 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -95,6 +95,7 @@ services: - TAVILY_API_KEY=${TAVILY_API_KEY:-} - SERPER_API_KEY=${SERPER_API_KEY:-} - YDC_API_KEY=${YDC_API_KEY:-} + - SOFYA_API_KEY=${SOFYA_API_KEY:-} # PUID / PGID — the user/group the container drops to before # running uvicorn (entrypoint also chowns /app/data + /app/logs # to match, so bind-mounted files stay editable from the host). diff --git a/services/search/core.py b/services/search/core.py index bbee1c38d3..a0fca06bb4 100644 --- a/services/search/core.py +++ b/services/search/core.py @@ -43,6 +43,7 @@ tavily_search, serper_search, you_com_search, + sofya_search, _get_search_settings, _get_provider_key, _get_result_count, @@ -125,6 +126,8 @@ def _call_provider(provider_name: str, query: str, count: int, time_filter: str return serper_search(query, count, time_filter) elif provider_name == "you_com": return you_com_search(query, count, time_filter) + elif provider_name == "sofya": + return sofya_search(query, count, time_filter) return [] diff --git a/services/search/providers.py b/services/search/providers.py index 8ce92e6407..5d0df58734 100644 --- a/services/search/providers.py +++ b/services/search/providers.py @@ -1,4 +1,4 @@ -"""Search provider implementations: SearXNG, Brave, DuckDuckGo, Google PSE, Tavily, Serper, You.com.""" +"""Search provider implementations: SearXNG, Brave, DuckDuckGo, Google PSE, Tavily, Serper, You.com, Sofya.""" import json import logging @@ -27,6 +27,7 @@ # Keyless by default (free agent search); optional YDC_API_KEY upgrades # to the keyed Search API. needs_key stays False so fresh installs work. "you_com": ("You.com", False, False), + "sofya": ("Sofya", True, False), "disabled": ("Disabled", False, False), } @@ -67,6 +68,7 @@ def _get_provider_key(provider: str) -> str: "tavily": "tavily_api_key", "serper": "serper_api_key", "you_com": "ydc_api_key", + "sofya": "sofya_api_key", } field = key_map.get(provider, "") if field: @@ -87,6 +89,7 @@ def _get_provider_key(provider: str) -> str: "tavily": ("TAVILY_API_KEY",), "serper": ("SERPER_API_KEY",), "you_com": ("YDC_API_KEY",), + "sofya": ("SOFYA_API_KEY",), } for env_name in env_map.get(provider, ()): value = (os.environ.get(env_name) or "").strip() @@ -108,7 +111,7 @@ def provider_configured(provider: str) -> bool: """Configuration readiness only; a configured engine can still fail upstream.""" if provider in {"searxng", "searxng_yep", "duckduckgo", "you_com"}: return True - if provider not in {"brave", "google_pse", "tavily", "serper"}: + if provider not in {"brave", "google_pse", "tavily", "serper", "sofya"}: return False if not _get_provider_key(provider): return False @@ -853,3 +856,62 @@ def you_com_search(query: str, count: Optional[int] = None, time_filter: Optiona results = _you_com_parse_results(data, count) logger.info(f"You.com returned {len(results)} results") return results + + +# ── Sofya ── + +def sofya_search(query: str, count: Optional[int] = None, time_filter: Optional[str] = None) -> List[dict]: + """Search using Sofya API. Requires search_api_key or SOFYA_API_KEY env var.""" + count = count if count is not None else _get_result_count() + api_key = _get_provider_key("sofya") or os.environ.get("SOFYA_API_KEY", "") + if not api_key: + logger.warning("Sofya: no API key configured") + return [] + + # "snippets" returns the SERP only. Odysseus fetches the pages it wants + # itself, so the full-text "basic" depth would only add latency and cost. + payload = { + "query": query, + "max_results": count, + "search_depth": "snippets", + } + if time_filter in ("day", "week", "month", "year"): + payload["freshness"] = time_filter + + try: + response = httpx.post( + "https://sofya.co/v1/search", + json=payload, + headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}, + timeout=REQUEST_TIMEOUT, + ) + if response.status_code == 429: + raise RateLimitError("Sofya rate limit hit") + response.raise_for_status() + except httpx.RequestError as e: + error_logger.error(f"Sofya search failed: {e}") + return [] + except RateLimitError as e: + error_logger.error(str(e)) + return [] + + try: + data = response.json() + except json.JSONDecodeError as e: + error_logger.error(f"Sofya returned invalid JSON: {e}") + return [] + + results = [] + for item in data.get("results", [])[:count]: + url = item.get("url", "") + if not url: + continue + results.append({ + "title": item.get("title") or "", + "url": url, + "snippet": item.get("description") or item.get("content") or "", + "age": item.get("published_date") or "", + }) + + logger.info(f"Sofya returned {len(results)} results") + return results diff --git a/specs/search.md b/specs/search.md index afbd9ebfaa..86adcad945 100644 --- a/specs/search.md +++ b/specs/search.md @@ -37,7 +37,7 @@ Research provider naming is not fully normalized in the UI: some frontend select `services/search/service.py` owns `SearchService`, the async facade exported by `services.search` and `services`. It wraps the synchronous comprehensive search path off the event loop and maps route-style output into service result rows. -`services/search/providers.py` owns provider-specific calls for SearXNG, Brave, DuckDuckGo, Google PSE, Tavily, Serper, and You.com. `PROVIDER_INFO`, provider availability, missing-key behavior, and provider dispatch live there. +`services/search/providers.py` owns provider-specific calls for SearXNG, Brave, DuckDuckGo, Google PSE, Tavily, Serper, You.com, and Sofya. `PROVIDER_INFO`, provider availability, missing-key behavior, and provider dispatch live there. `services/search/query.py` owns query enhancement and sanitization, including stripping markdown/code-fence noise from model- or user-supplied queries before provider calls and extracting Unicode/non-ASCII capitalized entity names. `services/search/ranking.py` owns result ranking, including word-boundary title/snippet/subject matching so short query terms do not match unrelated substrings. diff --git a/src/agent_tools/admin_tools.py b/src/agent_tools/admin_tools.py index 8eb2c89bfb..af088bb1df 100644 --- a/src/agent_tools/admin_tools.py +++ b/src/agent_tools/admin_tools.py @@ -535,7 +535,8 @@ async def do_manage_settings(content: str, owner: Optional[str] = None) -> Dict: # so API keys never flow through chat. User sets these in the panel. _SECRET_KEYS = { "brave_api_key", "google_pse_key", "google_pse_cx", - "tavily_api_key", "serper_api_key", "ydc_api_key", "app_public_url", + "tavily_api_key", "serper_api_key", "ydc_api_key", "sofya_api_key", + "app_public_url", } def _is_secret(k): # `token` must be a suffix, not a substring: otherwise the int diff --git a/src/settings.py b/src/settings.py index a86c61a347..1cc944cb93 100644 --- a/src/settings.py +++ b/src/settings.py @@ -91,7 +91,8 @@ def _invalidate_caches(): # Google PSE safe=active (omitted for "off"; PSE has no middle tier) # Serper.dev safe=active (omitted for "off"; proxies Google's `safe`) # You.com safesearch=off/moderate/strict - # Providers NOT touched: Tavily (no SafeSearch knob; filters at index time) + # Providers NOT touched: Tavily (no SafeSearch knob; filters at index time), + # Sofya (no SafeSearch knob) # and any custom backend reached via search_url — they keep whatever the # backend itself decides, so operators stay in control of self-hosted / # niche search instances. @@ -103,6 +104,7 @@ def _invalidate_caches(): "tavily_api_key": "", "serper_api_key": "", "ydc_api_key": "", + "sofya_api_key": "", "research_endpoint_id": "", "research_model": "", "research_search_provider": "", diff --git a/static/index.html b/static/index.html index 7570d9c268..73184359bc 100644 --- a/static/index.html +++ b/static/index.html @@ -1844,6 +1844,7 @@