Introduce the error retransmission mechanism to enhance robustness.
This commit is contained in:
@@ -51,7 +51,7 @@ class DeepSearchAgent:
|
||||
# 确保输出目录存在
|
||||
os.makedirs(self.config.output_dir, exist_ok=True)
|
||||
|
||||
print(f"Deep Search Agent 已初始化")
|
||||
print(f"Query Agent已初始化")
|
||||
print(f"使用LLM: {self.llm_client.get_model_info()}")
|
||||
print(f"搜索工具集: TavilyNewsAgency (支持6种搜索工具)")
|
||||
|
||||
|
||||
@@ -4,10 +4,27 @@ DeepSeek LLM实现
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from typing import Optional, Dict, Any
|
||||
from openai import OpenAI
|
||||
from .base import BaseLLM
|
||||
|
||||
# 添加utils目录到Python路径并导入重试模块
|
||||
try:
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
root_dir = os.path.dirname(os.path.dirname(current_dir))
|
||||
utils_dir = os.path.join(root_dir, 'utils')
|
||||
if utils_dir not in sys.path:
|
||||
sys.path.append(utils_dir)
|
||||
from retry_helper import with_retry, with_graceful_retry, LLM_RETRY_CONFIG
|
||||
except ImportError:
|
||||
# 如果无法导入重试模块,使用空装饰器避免报错
|
||||
def with_retry(config):
|
||||
def decorator(func):
|
||||
return func
|
||||
return decorator
|
||||
LLM_RETRY_CONFIG = None
|
||||
|
||||
|
||||
class DeepSeekLLM(BaseLLM):
|
||||
"""DeepSeek LLM实现类"""
|
||||
@@ -39,6 +56,7 @@ class DeepSeekLLM(BaseLLM):
|
||||
"""获取默认模型名称"""
|
||||
return "deepseek-chat"
|
||||
|
||||
@with_retry(LLM_RETRY_CONFIG)
|
||||
def invoke(self, system_prompt: str, user_prompt: str, **kwargs) -> str:
|
||||
"""
|
||||
调用DeepSeek API生成回复
|
||||
|
||||
@@ -4,10 +4,27 @@ OpenAI LLM实现
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from typing import Optional, Dict, Any
|
||||
from openai import OpenAI
|
||||
from .base import BaseLLM
|
||||
|
||||
# 添加utils目录到Python路径并导入重试模块
|
||||
try:
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
root_dir = os.path.dirname(os.path.dirname(current_dir))
|
||||
utils_dir = os.path.join(root_dir, 'utils')
|
||||
if utils_dir not in sys.path:
|
||||
sys.path.append(utils_dir)
|
||||
from retry_helper import with_retry, with_graceful_retry, LLM_RETRY_CONFIG
|
||||
except ImportError:
|
||||
# 如果无法导入重试模块,使用空装饰器避免报错
|
||||
def with_retry(config):
|
||||
def decorator(func):
|
||||
return func
|
||||
return decorator
|
||||
LLM_RETRY_CONFIG = None
|
||||
|
||||
|
||||
class OpenAILLM(BaseLLM):
|
||||
"""OpenAI LLM实现类"""
|
||||
@@ -35,6 +52,7 @@ class OpenAILLM(BaseLLM):
|
||||
"""获取默认模型名称"""
|
||||
return "gpt-4o-mini"
|
||||
|
||||
@with_retry(LLM_RETRY_CONFIG)
|
||||
def invoke(self, system_prompt: str, user_prompt: str, **kwargs) -> str:
|
||||
"""
|
||||
调用OpenAI API生成回复
|
||||
|
||||
@@ -22,7 +22,17 @@
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
# 添加utils目录到Python路径
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
root_dir = os.path.dirname(os.path.dirname(current_dir))
|
||||
utils_dir = os.path.join(root_dir, 'utils')
|
||||
if utils_dir not in sys.path:
|
||||
sys.path.append(utils_dir)
|
||||
|
||||
from retry_helper import with_graceful_retry, SEARCH_API_RETRY_CONFIG
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
# 运行前请确保已安装Tavily库: pip install tavily-python
|
||||
@@ -82,6 +92,7 @@ class TavilyNewsAgency:
|
||||
raise ValueError("Tavily API Key未找到!请设置TAVILY_API_KEY环境变量或在初始化时提供")
|
||||
self._client = TavilyClient(api_key=api_key)
|
||||
|
||||
@with_graceful_retry(SEARCH_API_RETRY_CONFIG, default_return=TavilyResponse(query="搜索失败"))
|
||||
def _search_internal(self, **kwargs) -> TavilyResponse:
|
||||
"""内部通用的搜索执行器,所有工具最终都调用此方法"""
|
||||
try:
|
||||
@@ -109,7 +120,7 @@ class TavilyNewsAgency:
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"搜索时发生错误: {str(e)}")
|
||||
return TavilyResponse(query=kwargs.get("query", "Unknown Query"))
|
||||
raise e # 让重试机制捕获并处理
|
||||
|
||||
# --- Agent 可用的工具方法 ---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user