fix: lazy init TavilySearch to avoid startup crash without API key
- Add get_tavily_search() for lazy initialization (renamed from _get_tavily_search) - Update deep_agent/graph.py to use lazy initialization - Add assertion check when DeepAgent loads to ensure search tool is available - Convert research_sub_agent to function to receive tools dynamically When TAVILY_API_KEY is not configured, the API server can now start normally. DeepAgent will show a clear error message when used without the API key.
This commit is contained in:
parent
982d83da11
commit
6fa14f77a6
@ -4,15 +4,25 @@ from typing import Annotated, Any
|
||||
|
||||
from langchain.tools import tool
|
||||
from langchain_core.tools import StructuredTool
|
||||
from langchain_tavily import TavilySearch
|
||||
from langgraph.types import interrupt
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from src import config, graph_base, knowledge_base
|
||||
from src.utils import logger
|
||||
|
||||
search = TavilySearch()
|
||||
search.metadata = {"name": "Tavily 网页搜索"}
|
||||
# Lazy initialization for TavilySearch (only when TAVILY_API_KEY is available)
|
||||
_tavily_search_instance = None
|
||||
|
||||
|
||||
def get_tavily_search():
|
||||
"""Get TavilySearch instance lazily, only when API key is available."""
|
||||
global _tavily_search_instance
|
||||
if _tavily_search_instance is None and config.enable_web_search:
|
||||
from langchain_tavily import TavilySearch
|
||||
|
||||
_tavily_search_instance = TavilySearch()
|
||||
_tavily_search_instance.metadata = {"name": "Tavily 网页搜索"}
|
||||
return _tavily_search_instance
|
||||
|
||||
|
||||
@tool(name_or_callable="计算器", description="可以对给定的2个数字选择进行 add, subtract, multiply, divide 运算")
|
||||
@ -101,7 +111,9 @@ def get_static_tools() -> list:
|
||||
|
||||
# 检查是否启用网页搜索
|
||||
if config.enable_web_search:
|
||||
static_tools.append(search)
|
||||
tavily_search = get_tavily_search()
|
||||
if tavily_search:
|
||||
static_tools.append(tavily_search)
|
||||
|
||||
return static_tools
|
||||
|
||||
|
||||
@ -8,25 +8,26 @@ from langchain.agents.middleware import ModelRequest, SummarizationMiddleware, T
|
||||
|
||||
from src.agents.common import BaseAgent, load_chat_model
|
||||
from src.agents.common.middlewares import inject_attachment_context
|
||||
from src.agents.common.tools import search
|
||||
from src.agents.common.tools import get_tavily_search
|
||||
|
||||
from .context import DeepContext
|
||||
from .prompts import DEEP_PROMPT
|
||||
|
||||
search_tools = [search]
|
||||
|
||||
def _get_research_sub_agent(search_tools: list) -> dict:
|
||||
"""Get research sub-agent config with search tools."""
|
||||
return {
|
||||
"name": "research-agent",
|
||||
"description": ("利用搜索工具,用于研究更深入的问题。将调研结果写入到主题研究文件中。"),
|
||||
"system_prompt": (
|
||||
"你是一位专注的研究员。你的工作是根据用户的问题进行研究。"
|
||||
"进行彻底的研究,然后用详细的答案回复用户的问题,只有你的最终答案会被传递给用户。"
|
||||
"除了你的最终信息,他们不会知道任何其他事情,所以你的最终报告应该就是你的最终信息!"
|
||||
"将调研结果保存到主题研究文件中 /sub_research/xxx.md 中。"
|
||||
),
|
||||
"tools": search_tools,
|
||||
}
|
||||
|
||||
research_sub_agent = {
|
||||
"name": "research-agent",
|
||||
"description": ("利用搜索工具,用于研究更深入的问题。将调研结果写入到主题研究文件中。"),
|
||||
"system_prompt": (
|
||||
"你是一位专注的研究员。你的工作是根据用户的问题进行研究。"
|
||||
"进行彻底的研究,然后用详细的答案回复用户的问题,只有你的最终答案会被传递给用户。"
|
||||
"除了你的最终信息,他们不会知道任何其他事情,所以你的最终报告应该就是你的最终信息!"
|
||||
"将调研结果保存到主题研究文件中 /sub_research/xxx.md 中。"
|
||||
),
|
||||
"tools": search_tools,
|
||||
}
|
||||
|
||||
critique_sub_agent = {
|
||||
"name": "critique-agent",
|
||||
@ -73,7 +74,16 @@ class DeepAgent(BaseAgent):
|
||||
|
||||
async def get_tools(self):
|
||||
"""返回 Deep Agent 的专用工具"""
|
||||
tools = search_tools
|
||||
tools = []
|
||||
tavily_search = get_tavily_search()
|
||||
if tavily_search:
|
||||
tools.append(tavily_search)
|
||||
|
||||
# Assert that search tool is available for DeepAgent
|
||||
assert tools, (
|
||||
"DeepAgent requires at least one search tool. "
|
||||
"Please configure TAVILY_API_KEY environment variable to enable web search."
|
||||
)
|
||||
return tools
|
||||
|
||||
async def get_graph(self, **kwargs):
|
||||
@ -88,6 +98,9 @@ class DeepAgent(BaseAgent):
|
||||
sub_model = load_chat_model(context.subagents_model)
|
||||
tools = await self.get_tools()
|
||||
|
||||
# Build subagents with search tools
|
||||
research_sub_agent = _get_research_sub_agent(tools)
|
||||
|
||||
# 使用 create_deep_agent 创建深度智能体
|
||||
graph = create_agent(
|
||||
model=model,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user