feat: 添加搜索工具功能(Tavily Search)
- 新增 SearchToolConfig 模型:支持搜索工具配置 - Agent 增加 tools 字段:可配置可用工具列表 - 后台管理增加搜索工具配置页面 - Agent 管理增加工具启用开关 - 网页端增加搜索工具禁用复选框 - WebSocket chat 处理增加搜索调用逻辑 - 默认配置 Tavily Search API
This commit is contained in:
@@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
|
||||
from typing import List, Optional, Dict
|
||||
import logging
|
||||
|
||||
from models_v2 import Agent, LLMProvider, ChannelAgentMapping, Channel, init_default_data
|
||||
from models_v2 import Agent, LLMProvider, ChannelAgentMapping, Channel, SearchToolConfig, init_default_data
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -52,6 +52,7 @@ class AgentService:
|
||||
thinking_prompt=data.get('thinking_prompt'),
|
||||
thinking_prefix=data.get('thinking_prefix', ''),
|
||||
thinking_suffix=data.get('thinking_suffix', ''),
|
||||
tools=data.get('tools', []), # 工具列表
|
||||
max_history=data.get('max_history', 20),
|
||||
temperature_override=data.get('temperature_override'),
|
||||
is_active=data.get('is_active', True),
|
||||
@@ -405,4 +406,87 @@ class ChannelService:
|
||||
'is_active': channel.is_active,
|
||||
'is_primary': channel.is_primary,
|
||||
'agent_mappings': self.get_channel_agents(channel_id)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class SearchToolService:
|
||||
"""搜索工具管理服务"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_all_configs(self) -> List[SearchToolConfig]:
|
||||
"""获取所有搜索工具配置"""
|
||||
return self.db.query(SearchToolConfig).order_by(SearchToolConfig.is_default.desc(), SearchToolConfig.name).all()
|
||||
|
||||
def get_active_configs(self) -> List[SearchToolConfig]:
|
||||
"""获取活跃的搜索工具配置"""
|
||||
return self.db.query(SearchToolConfig).filter(SearchToolConfig.is_active == True).all()
|
||||
|
||||
def get_config(self, config_id: int) -> Optional[SearchToolConfig]:
|
||||
"""获取单个配置"""
|
||||
return self.db.query(SearchToolConfig).filter(SearchToolConfig.id == config_id).first()
|
||||
|
||||
def get_default_config(self) -> Optional[SearchToolConfig]:
|
||||
"""获取默认配置"""
|
||||
config = self.db.query(SearchToolConfig).filter(
|
||||
SearchToolConfig.is_default == True,
|
||||
SearchToolConfig.is_active == True
|
||||
).first()
|
||||
if not config:
|
||||
config = self.db.query(SearchToolConfig).filter(SearchToolConfig.is_active == True).first()
|
||||
return config
|
||||
|
||||
def create_config(self, data: Dict) -> SearchToolConfig:
|
||||
"""创建搜索工具配置"""
|
||||
config = SearchToolConfig(
|
||||
name=data.get('name'),
|
||||
provider=data.get('provider', 'tavily'),
|
||||
api_key=data.get('api_key'),
|
||||
api_base=data.get('api_base'),
|
||||
max_results=data.get('max_results', 5),
|
||||
include_raw_content=data.get('include_raw_content', False),
|
||||
search_depth=data.get('search_depth', 'basic'),
|
||||
is_active=data.get('is_active', True),
|
||||
is_default=data.get('is_default', False)
|
||||
)
|
||||
self.db.add(config)
|
||||
self.db.commit()
|
||||
self.db.refresh(config)
|
||||
return config
|
||||
|
||||
def update_config(self, config_id: int, data: Dict) -> Optional[SearchToolConfig]:
|
||||
"""更新配置"""
|
||||
config = self.get_config(config_id)
|
||||
if not config:
|
||||
return None
|
||||
|
||||
for key, value in data.items():
|
||||
if hasattr(config, key) and value is not None:
|
||||
setattr(config, key, value)
|
||||
|
||||
self.db.commit()
|
||||
self.db.refresh(config)
|
||||
return config
|
||||
|
||||
def delete_config(self, config_id: int) -> bool:
|
||||
"""删除配置"""
|
||||
config = self.get_config(config_id)
|
||||
if not config:
|
||||
return False
|
||||
|
||||
self.db.delete(config)
|
||||
self.db.commit()
|
||||
return True
|
||||
|
||||
def set_default_config(self, config_id: int) -> bool:
|
||||
"""设置默认配置"""
|
||||
self.db.query(SearchToolConfig).update({SearchToolConfig.is_default: False})
|
||||
|
||||
config = self.get_config(config_id)
|
||||
if not config:
|
||||
return False
|
||||
|
||||
config.is_default = True
|
||||
self.db.commit()
|
||||
return True
|
||||
Reference in New Issue
Block a user