Merge pull request #17 from amszuidas/fix/tavily-api-key-config

fix: support loading tavily ak from config.yaml
This commit is contained in:
Henry Li
2026-01-24 10:14:04 +08:00
committed by GitHub

View File

@@ -5,7 +5,13 @@ from tavily import TavilyClient
from src.config import get_app_config
tavily_client = TavilyClient()
def _get_tavily_client() -> TavilyClient:
config = get_app_config().get_tool_config("web_search")
api_key = None
if config is not None and "api_key" in config.model_extra:
api_key = config.model_extra.get("api_key")
return TavilyClient(api_key=api_key)
@tool("web_search", parse_docstring=True)
@@ -19,7 +25,9 @@ def web_search_tool(query: str) -> str:
max_results = 5
if config is not None and "max_results" in config.model_extra:
max_results = config.model_extra.get("max_results")
res = tavily_client.search(query, max_results=max_results)
client = _get_tavily_client()
res = client.search(query, max_results=max_results)
normalized_results = [
{
"title": result["title"],
@@ -43,7 +51,8 @@ def web_fetch_tool(url: str) -> str:
Args:
url: The URL to fetch the contents of.
"""
res = tavily_client.extract([url])
client = _get_tavily_client()
res = client.extract([url])
if "failed_results" in res and len(res["failed_results"]) > 0:
return f"Error: {res['failed_results'][0]['error']}"
elif "results" in res and len(res["results"]) > 0: