Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat: add Tavily as configurable web search backend (#788)
Evan Rimer9f07cf0bc4de
diff --git a/bot/vikingbot/agent/tools/factory.py b/bot/vikingbot/agent/tools/factory.pyindex fc57942a4..b85a43d68 100644--- a/bot/vikingbot/agent/tools/factory.py+++ b/bot/vikingbot/agent/tools/factory.py@@ -58,6 +58,7 @@ def register_default_tools(exec_config = config.tools.execbrave_api_key = config.tools.web.search.api_key if config.tools.web.search else Noneexa_api_key = None # TODO: Add to config if needed+ tavily_api_key = config.tools.web.search.tavily_api_key if config.tools.web.search else None# Get provider API key and base from config@@ -80,7 +81,7 @@ def register_default_tools(# Web toolsregistry.register(- WebSearchTool(backend="auto", brave_api_key=brave_api_key, exa_api_key=exa_api_key)+ WebSearchTool(backend="auto", brave_api_key=brave_api_key, exa_api_key=exa_api_key, tavily_api_key=tavily_api_key))registry.register(WebFetchTool())diff --git a/bot/vikingbot/agent/tools/websearch/__init__.py b/bot/vikingbot/agent/tools/websearch/__init__.pyindex ac2d37840..e36a561b2 100644--- a/bot/vikingbot/agent/tools/websearch/__init__.py+++ b/bot/vikingbot/agent/tools/websearch/__init__.py@@ -1,5 +1,5 @@"""-Web search tool with multiple backends (brave, ddgs, exa).+Web search tool with multiple backends (brave, ddgs, exa, tavily).To add a new backend:1. Create new file: websearch/mybackend.py@@ -18,7 +18,7 @@ from .base import WebSearchBackendfrom .registry import registry# Import backends to register them-from . import brave, ddgs, exa+from . import brave, ddgs, exa, tavilyclass WebSearchTool(Tool):@@ -66,28 +66,31 @@ class WebSearchTool(Tool):backend: Union[str, WebSearchBackend] = "auto",brave_api_key: Optional[str] = None,exa_api_key: Optional[str] = None,+ tavily_api_key: Optional[str] = None,max_results: int = 5,):"""Initialize WebSearchTool.Args:- backend: Backend name ("auto", "brave", "ddgs", "exa") or WebSearchBackend instance+ backend: Backend name ("auto", "brave", "ddgs", "exa", "tavily") or WebSearchBackend instancebrave_api_key: Brave Search API keyexa_api_key: Exa AI API key+ tavily_api_key: Tavily Search API keymax_results: Default max results"""self.max_results = max_resultsself._brave_api_key = brave_api_keyself._exa_api_key = exa_api_key+ self._tavily_api_key = tavily_api_key# Select backendif isinstance(backend, WebSearchBackend):self._backend = backendelif backend == "auto":- self._backend = registry.select_auto(brave_api_key, exa_api_key)+ self._backend = registry.select_auto(brave_api_key, exa_api_key, tavily_api_key)else:- self._backend = registry.create(backend, brave_api_key, exa_api_key)+ self._backend = registry.create(backend, brave_api_key, exa_api_key, tavily_api_key)if not self._backend:raise ValueError(f"Unknown backend: {backend}")diff --git a/bot/vikingbot/agent/tools/websearch/registry.py b/bot/vikingbot/agent/tools/websearch/registry.pyindex 06b19932b..d82738371 100644--- a/bot/vikingbot/agent/tools/websearch/registry.py+++ b/bot/vikingbot/agent/tools/websearch/registry.py@@ -37,7 +37,11 @@ class WebSearchBackendRegistry:return list(self._backends.keys())def create(- self, name: str, brave_api_key: Optional[str] = None, exa_api_key: Optional[str] = None+ self,+ name: str,+ brave_api_key: Optional[str] = None,+ exa_api_key: Optional[str] = None,+ tavily_api_key: Optional[str] = None,) -> Optional[WebSearchBackend]:"""Create a backend instance.@@ -46,6 +50,7 @@ class WebSearchBackendRegistry:name: Backend namebrave_api_key: Brave API key (for brave backend)exa_api_key: Exa API key (for exa backend)+ tavily_api_key: Tavily API key (for tavily backend)Returns:Backend instance or None@@ -59,21 +64,26 @@ class WebSearchBackendRegistry:return backend_class(api_key=brave_api_key)elif name == "exa":return backend_class(api_key=exa_api_key)+ elif name == "tavily":+ return backend_class(api_key=tavily_api_key)else:return backend_class()def select_auto(- self, brave_api_key: Optional[str] = None, exa_api_key: Optional[str] = None+ self,+ brave_api_key: Optional[str] = None,+ exa_api_key: Optional[str] = None,+ tavily_api_key: Optional[str] = None,) -> WebSearchBackend:"""Auto-select the best available backend.- Priority: exa → brave → ddgs+ Priority: tavily → exa → brave → ddgs"""- priority = ["exa", "brave", "ddgs"]+ priority = ["tavily", "exa", "brave", "ddgs"]for name in priority:- backend = self.create(name, brave_api_key, exa_api_key)+ backend = self.create(name, brave_api_key, exa_api_key, tavily_api_key)if backend and backend.is_available:return backenddiff --git a/bot/vikingbot/agent/tools/websearch/tavily.py b/bot/vikingbot/agent/tools/websearch/tavily.pynew file mode 100644index 000000000..dd0b6c116--- /dev/null+++ b/bot/vikingbot/agent/tools/websearch/tavily.py@@ -0,0 +1,54 @@+"""Tavily Search backend."""++import os+from typing import Any++from .base import WebSearchBackend+from .registry import register_backend+++@register_backend+class TavilyBackend(WebSearchBackend):+ """Tavily Search API backend."""++ name = "tavily"++ def __init__(self, api_key: str | None = None):+ self.api_key = api_key or os.environ.get("TAVILY_API_KEY", "")+ if self.api_key:+ from tavily import AsyncTavilyClient++ self._client = AsyncTavilyClient(api_key=self.api_key)+ else:+ self._client = None++ @property+ def is_available(self) -> bool:+ return bool(self.api_key)++ async def search(self, query: str, count: int, **kwargs: Any) -> str:+ if not self._client:+ return "Error: TAVILY_API_KEY not configured"++ try:+ n = min(max(count, 1), 20)+ response = await self._client.search(+ query=query,+ max_results=n,+ search_depth="basic",+ )++ results = response.get("results", [])+ if not results:+ return f"No results for: {query}"++ lines = [f"Results for: {query}\n"]+ for i, item in enumerate(results[:n], 1):+ lines.append(f"{i}. {item.get('title', '')}\n {item.get('url', '')}")+ if content := item.get("content"):+ snippet = content[:500]+ suffix = "..." if len(content) > 500 else ""+ lines.append(f" {snippet}{suffix}")+ return "\n".join(lines)+ except Exception as e:+ return f"Error: {e}"diff --git a/bot/vikingbot/config/schema.py b/bot/vikingbot/config/schema.pyindex 9443be610..78948bbbc 100644--- a/bot/vikingbot/config/schema.py+++ b/bot/vikingbot/config/schema.py@@ -444,6 +444,7 @@ class WebSearchConfig(BaseModel):"""Web search tool configuration."""api_key: str = "" # Brave Search API key+ tavily_api_key: str = "" # Tavily Search API keymax_results: int = 5diff --git a/pyproject.toml b/pyproject.tomlindex c26aa7cfc..6aaf2c03c 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -125,6 +125,7 @@ bot = ["html2text>=2020.1.16","beautifulsoup4>=4.12.0","ddgs>=9.0.0",+ "tavily-python>=0.5.0","gradio>=6.6.0","py-machineid>=1.0.0",]