Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 142904dff4 | |||
| b9e99da01b | |||
| 34f02ad4d4 | |||
| 0c9bfca346 | |||
| 85dd206154 | |||
| 1b9bb1090c | |||
| 2373040b04 | |||
| f0789d6bbc | |||
| e05233fb4f | |||
| 7fa143b5b0 | |||
| b573638bf8 | |||
| af997aa5c5 | |||
| b1feaee976 | |||
| 87f9f4a7d8 | |||
| a1f1032000 | |||
| 7d6a345a7d | |||
| c6f157aa97 | |||
| 6e87f59fab | |||
| 066d2fe44d | |||
| 23935a1a28 | |||
| 08c4f79313 | |||
| 186f69c87a | |||
| 8c90fd5641 | |||
| 248ac4e471 | |||
| 51cc8161f1 | |||
| a62fe929c1 | |||
| 6adeb9b371 | |||
| 051fd5c1c8 | |||
| 90d31dba69 | |||
| 3854d78c9c |
573
main_v2.py
573
main_v2.py
@@ -18,11 +18,11 @@ import os
|
||||
from models_v2 import (
|
||||
init_db, get_db, SessionLocal,
|
||||
User, Conversation, Message, SystemConfig,
|
||||
LLMProvider, Agent, Channel, ChannelAgentMapping, MatrixRoomMapping,
|
||||
LLMProvider, Agent, Channel, ChannelAgentMapping, MatrixRoomMapping, ToolConfig, ToolUsageLog,
|
||||
init_default_data
|
||||
)
|
||||
from services.llm_service import llm_service
|
||||
from services.agent_service import AgentService, LLMProviderService, ChannelService
|
||||
from services.agent_service import AgentService, LLMProviderService, ChannelService, ToolService
|
||||
from services.conversation_service import ConversationService
|
||||
|
||||
# 配置日志
|
||||
@@ -60,8 +60,13 @@ class ConnectionManager:
|
||||
for connection in self.active_connections[user_id]:
|
||||
try:
|
||||
await connection.send_json(message)
|
||||
except:
|
||||
pass
|
||||
logger.info(f"发送消息到用户 {user_id}: {message.get('type', 'unknown')}")
|
||||
except Exception as e:
|
||||
logger.error(f"发送消息失败: {e}")
|
||||
|
||||
async def ping(self, user_id: str):
|
||||
"""发送心跳ping"""
|
||||
await self.send_to_user(user_id, {"type": "ping"})
|
||||
|
||||
|
||||
manager = ConnectionManager()
|
||||
@@ -199,6 +204,7 @@ async def get_agents(db: Session = Depends(get_db)):
|
||||
"thinking_prompt": a.thinking_prompt,
|
||||
"thinking_prefix": a.thinking_prefix,
|
||||
"thinking_suffix": a.thinking_suffix,
|
||||
"tools": a.tools or [], # 工具列表
|
||||
"max_history": a.max_history,
|
||||
"temperature_override": a.temperature_override,
|
||||
"is_default": a.is_default,
|
||||
@@ -413,6 +419,162 @@ async def unbind_agent(mapping_id: int, db: Session = Depends(get_db)):
|
||||
return {"success": success}
|
||||
|
||||
|
||||
# ==================== 工具配置 API ====================
|
||||
|
||||
@app.get("/api/v2/tools")
|
||||
async def get_tools(tool_type: str = None, db: Session = Depends(get_db)):
|
||||
"""获取所有工具配置"""
|
||||
service = ToolService(db)
|
||||
if tool_type:
|
||||
tools = service.get_tools_by_type(tool_type)
|
||||
else:
|
||||
tools = service.get_all_tools()
|
||||
|
||||
return {
|
||||
"tools": [
|
||||
{
|
||||
"id": t.id,
|
||||
"name": t.name,
|
||||
"tool_type": t.tool_type,
|
||||
"provider": t.provider,
|
||||
"config": t.config,
|
||||
"is_active": t.is_active,
|
||||
"is_default": t.is_default,
|
||||
"total_calls": t.total_calls,
|
||||
"success_calls": t.success_calls,
|
||||
"failed_calls": t.failed_calls
|
||||
}
|
||||
for t in tools
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@app.post("/api/v2/tools")
|
||||
async def create_tool(data: dict, db: Session = Depends(get_db)):
|
||||
"""创建工具配置"""
|
||||
service = ToolService(db)
|
||||
tool = service.create_tool(data)
|
||||
return {"success": True, "tool": {"id": tool.id, "name": tool.name, "tool_type": tool.tool_type}}
|
||||
|
||||
|
||||
@app.put("/api/v2/tools/{tool_id}")
|
||||
async def update_tool(tool_id: int, data: dict, db: Session = Depends(get_db)):
|
||||
"""更新工具配置"""
|
||||
service = ToolService(db)
|
||||
|
||||
if data.get('is_default'):
|
||||
service.set_default_tool(tool_id)
|
||||
|
||||
tool = service.update_tool(tool_id, data)
|
||||
|
||||
if not tool:
|
||||
return {"success": False, "message": "工具不存在"}
|
||||
|
||||
return {"success": True, "tool": {"id": tool.id, "name": tool.name}}
|
||||
|
||||
|
||||
@app.delete("/api/v2/tools/{tool_id}")
|
||||
async def delete_tool(tool_id: int, db: Session = Depends(get_db)):
|
||||
"""删除工具配置"""
|
||||
service = ToolService(db)
|
||||
success = service.delete_tool(tool_id)
|
||||
|
||||
return {"success": success}
|
||||
|
||||
|
||||
@app.post("/api/v2/tools/{tool_id}/default")
|
||||
async def set_tool_default(tool_id: int, db: Session = Depends(get_db)):
|
||||
"""设置默认工具"""
|
||||
service = ToolService(db)
|
||||
success = service.set_default_tool(tool_id)
|
||||
|
||||
return {"success": success}
|
||||
|
||||
|
||||
@app.get("/api/v2/tools/stats")
|
||||
async def get_tool_stats(days: int = 7, db: Session = Depends(get_db)):
|
||||
"""获取工具使用统计"""
|
||||
service = ToolService(db)
|
||||
stats = service.get_usage_stats(days=days)
|
||||
return stats
|
||||
|
||||
|
||||
@app.post("/api/v2/tools/search")
|
||||
async def perform_search(data: dict, db: Session = Depends(get_db)):
|
||||
"""执行搜索(供前端或Agent调用)"""
|
||||
import httpx
|
||||
import time
|
||||
|
||||
query = data.get('query')
|
||||
if not query:
|
||||
return {"success": False, "message": "缺少搜索关键词"}
|
||||
|
||||
service = ToolService(db)
|
||||
tool_id = data.get('tool_id')
|
||||
|
||||
if tool_id:
|
||||
tool = service.get_tool(tool_id)
|
||||
else:
|
||||
tool = service.get_default_tool('search')
|
||||
|
||||
if not tool or not tool.config.get('api_key'):
|
||||
return {"success": False, "message": "未配置搜索工具"}
|
||||
|
||||
# Tavily Search API
|
||||
if tool.provider == 'tavily' or tool.tool_type == 'search':
|
||||
start_time = time.time()
|
||||
try:
|
||||
tavily_url = "https://api.tavily.com/search"
|
||||
config = tool.config
|
||||
payload = {
|
||||
"api_key": config.get('api_key'),
|
||||
"query": query,
|
||||
"max_results": config.get('max_results', 5),
|
||||
"include_raw_content": config.get('include_raw_content', False),
|
||||
"search_depth": config.get('search_depth', 'basic')
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient(timeout=30) as client:
|
||||
response = await client.post(tavily_url, json=payload)
|
||||
result = response.json()
|
||||
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
|
||||
# 更新统计和日志
|
||||
service.increment_stats(tool.id, True)
|
||||
service.log_usage({
|
||||
'tool_id': tool.id,
|
||||
'tool_type': 'search',
|
||||
'query': query,
|
||||
'success': True,
|
||||
'result_summary': f'{len(result.get("results", []))} results',
|
||||
'conversation_id': data.get('conversation_id'),
|
||||
'agent_id': data.get('agent_id'),
|
||||
'duration_ms': duration_ms
|
||||
})
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"results": result.get("results", []),
|
||||
"query": query
|
||||
}
|
||||
except Exception as e:
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
service.increment_stats(tool.id, False)
|
||||
service.log_usage({
|
||||
'tool_id': tool.id,
|
||||
'tool_type': 'search',
|
||||
'query': query,
|
||||
'success': False,
|
||||
'error_message': str(e),
|
||||
'conversation_id': data.get('conversation_id'),
|
||||
'duration_ms': duration_ms
|
||||
})
|
||||
return {"success": False, "message": str(e)}
|
||||
|
||||
return {"success": False, "message": "不支持的搜索提供商"}
|
||||
|
||||
|
||||
# ==================== 对话 API(保留原有) ====================
|
||||
|
||||
@app.get("/api/conversations")
|
||||
@@ -511,174 +673,289 @@ async def delete_conversation(conversation_id: str, db: Session = Depends(get_db
|
||||
# ==================== WebSocket路由 ====================
|
||||
|
||||
@app.websocket("/ws/{user_id}")
|
||||
async def websocket_endpoint(websocket: WebSocket, user_id: str, db: Session = Depends(get_db)):
|
||||
async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
"""WebSocket连接 - 实时对话"""
|
||||
actual_user_id = MAIN_USER_ID
|
||||
await manager.connect(websocket, actual_user_id)
|
||||
conv_service = ConversationService(db)
|
||||
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
|
||||
|
||||
# 获取默认Agent配置
|
||||
agent_service = AgentService(db)
|
||||
default_agent = agent_service.get_default_agent()
|
||||
# 初始化时获取默认Agent ID
|
||||
db = SessionLocal()
|
||||
try:
|
||||
agent_service = AgentService(db)
|
||||
default_agent = agent_service.get_default_agent()
|
||||
default_agent_id = default_agent.id if default_agent else None
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
current_conversation_id = None
|
||||
current_agent_id = default_agent.id if default_agent else None
|
||||
current_agent_id = default_agent_id
|
||||
|
||||
try:
|
||||
while True:
|
||||
data = await websocket.receive_json()
|
||||
action = data.get("action")
|
||||
|
||||
if action == "select_conversation":
|
||||
current_conversation_id = data.get("conversation_id")
|
||||
conversation = conv_service.get_conversation(current_conversation_id)
|
||||
|
||||
if conversation:
|
||||
messages = conv_service.get_messages(conversation.id)
|
||||
await websocket.send_json({
|
||||
"type": "history",
|
||||
"conversation_id": current_conversation_id,
|
||||
"messages": [
|
||||
{
|
||||
"role": m.role,
|
||||
"content": m.content,
|
||||
"thinking_content": m.thinking_content,
|
||||
"source": m.source,
|
||||
"created_at": m.created_at.isoformat()
|
||||
}
|
||||
for m in messages
|
||||
]
|
||||
})
|
||||
|
||||
elif action == "switch_agent":
|
||||
# 切换Agent
|
||||
new_agent_id = data.get("agent_id")
|
||||
agent = agent_service.get_agent(new_agent_id)
|
||||
if agent and agent.is_active:
|
||||
current_agent_id = new_agent_id
|
||||
await websocket.send_json({
|
||||
"type": "agent_switched",
|
||||
"agent_id": current_agent_id,
|
||||
"agent_name": agent.display_name or agent.name
|
||||
})
|
||||
|
||||
elif action == "chat":
|
||||
message = data.get("message", "")
|
||||
conversation_id = data.get("conversation_id")
|
||||
enable_thinking = data.get("enable_thinking", True) # 可临时关闭思考
|
||||
|
||||
if not message.strip():
|
||||
continue
|
||||
|
||||
# 获取或创建会话
|
||||
if conversation_id:
|
||||
conversation = conv_service.get_conversation(conversation_id)
|
||||
else:
|
||||
conversation = conv_service.create_conversation(user.id)
|
||||
conversation_id = conversation.conversation_id
|
||||
await websocket.send_json({
|
||||
"type": "conversation_created",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
|
||||
# 保存用户消息
|
||||
user_msg = conv_service.add_message(
|
||||
conversation_id=conversation.id,
|
||||
role='user',
|
||||
content=message,
|
||||
source='web'
|
||||
)
|
||||
|
||||
# 广播用户消息
|
||||
await manager.send_to_user(MAIN_USER_ID, {
|
||||
"type": "user_message",
|
||||
"conversation_id": conversation_id,
|
||||
"message": {
|
||||
"id": user_msg.id,
|
||||
"role": "user",
|
||||
"content": message,
|
||||
"source": "web",
|
||||
"created_at": user_msg.created_at.isoformat()
|
||||
}
|
||||
})
|
||||
|
||||
# 获取Agent配置
|
||||
agent_config = agent_service.get_agent_config(current_agent_id)
|
||||
|
||||
if not agent_config or not agent_config.get('provider'):
|
||||
await websocket.send_json({
|
||||
"type": "error",
|
||||
"message": "Agent配置不完整"
|
||||
})
|
||||
continue
|
||||
|
||||
# 获取对话历史
|
||||
history = conv_service.get_conversation_history(conversation_id, limit=agent_config['agent'].get('max_history', 20))
|
||||
|
||||
# 调用LLM
|
||||
try:
|
||||
data = await websocket.receive_json()
|
||||
except Exception as json_err:
|
||||
logger.error(f"JSON解析错误: {json_err}")
|
||||
# 如果连接已断开,退出循环
|
||||
if "disconnect" in str(json_err).lower() or "closed" in str(json_err).lower():
|
||||
logger.info("WebSocket已断开,退出循环")
|
||||
break
|
||||
try:
|
||||
# 发送"正在思考"状态
|
||||
if agent_config['agent'].get('enable_thinking') and enable_thinking:
|
||||
text_data = await websocket.receive_text()
|
||||
if text_data.strip():
|
||||
data = json.loads(text_data)
|
||||
else:
|
||||
continue
|
||||
except Exception as text_err:
|
||||
logger.error(f"文本消息解析错误: {text_err}")
|
||||
if "disconnect" in str(text_err).lower() or "closed" in str(text_err).lower():
|
||||
logger.info("WebSocket已断开,退出循环")
|
||||
break
|
||||
continue
|
||||
|
||||
action = data.get("action")
|
||||
logger.info(f"WebSocket收到消息: action={action}")
|
||||
|
||||
# 每次消息处理时创建新的数据库会话,处理完后关闭
|
||||
try:
|
||||
db = SessionLocal()
|
||||
conv_service = ConversationService(db)
|
||||
agent_service = AgentService(db)
|
||||
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
|
||||
|
||||
if action == "select_conversation":
|
||||
current_conversation_id = data.get("conversation_id")
|
||||
conversation = conv_service.get_conversation(current_conversation_id)
|
||||
|
||||
if conversation:
|
||||
messages = conv_service.get_messages(conversation.id)
|
||||
|
||||
# 获取对话使用的Agent ID
|
||||
conv_agent_id = conversation.current_agent_id
|
||||
|
||||
await websocket.send_json({
|
||||
"type": "thinking_start",
|
||||
"type": "history",
|
||||
"conversation_id": current_conversation_id,
|
||||
"agent_id": conv_agent_id, # 返回对话的Agent ID
|
||||
"messages": [
|
||||
{
|
||||
"role": m.role,
|
||||
"content": m.content,
|
||||
"thinking_content": m.thinking_content,
|
||||
"agent_id": m.agent_id, # 每条消息的Agent ID
|
||||
"source": m.source,
|
||||
"created_at": m.created_at.isoformat()
|
||||
}
|
||||
for m in messages
|
||||
]
|
||||
})
|
||||
|
||||
elif action == "switch_agent":
|
||||
# 切换Agent
|
||||
new_agent_id = data.get("agent_id")
|
||||
agent = agent_service.get_agent(new_agent_id)
|
||||
if agent and agent.is_active:
|
||||
current_agent_id = new_agent_id
|
||||
await websocket.send_json({
|
||||
"type": "agent_switched",
|
||||
"agent_id": current_agent_id,
|
||||
"agent_name": agent.display_name or agent.name
|
||||
})
|
||||
|
||||
elif action == "chat":
|
||||
message = data.get("message", "")
|
||||
conversation_id = data.get("conversation_id")
|
||||
enable_thinking = data.get("enable_thinking", True)
|
||||
agent_id_override = data.get("agent_id")
|
||||
disabled_tools = data.get("disabled_tools", []) # 禁用的工具列表
|
||||
|
||||
if agent_id_override:
|
||||
agent = agent_service.get_agent(agent_id_override)
|
||||
if agent and agent.is_active:
|
||||
current_agent_id = agent_id_override
|
||||
|
||||
if not message.strip():
|
||||
continue
|
||||
|
||||
# 获取Agent配置
|
||||
agent_config = agent_service.get_agent_config(current_agent_id)
|
||||
agent_tools = agent_config.get('agent', {}).get('tools', [])
|
||||
|
||||
# 检查是否需要执行搜索
|
||||
search_context = None
|
||||
if 'search' in agent_tools and 'search' not in disabled_tools:
|
||||
# 只要启用了搜索工具且未禁用,就执行搜索(不再依赖关键词检测)
|
||||
should_search = True
|
||||
|
||||
if should_search:
|
||||
# 执行搜索
|
||||
tool_service = ToolService(db)
|
||||
search_tool = tool_service.get_default_tool('search')
|
||||
|
||||
if search_tool and search_tool.config.get('api_key'):
|
||||
import httpx
|
||||
import time
|
||||
start_time = time.time()
|
||||
try:
|
||||
logger.info(f"执行搜索: query={message}")
|
||||
tavily_url = "https://api.tavily.com/search"
|
||||
config = search_tool.config
|
||||
payload = {
|
||||
"api_key": config.get('api_key'),
|
||||
"query": message,
|
||||
"max_results": config.get('max_results', 5),
|
||||
"search_depth": config.get('search_depth', 'basic')
|
||||
}
|
||||
|
||||
# 同步调用
|
||||
with httpx.Client(timeout=30) as client:
|
||||
resp = client.post(tavily_url, json=payload)
|
||||
search_result = resp.json()
|
||||
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
|
||||
if search_result.get("results"):
|
||||
# 构建搜索上下文
|
||||
search_context = "\n\n【搜索结果】\n"
|
||||
for i, r in enumerate(search_result["results"][:5], 1):
|
||||
search_context += f"{i}. {r.get('title', 'N/A')}\n {r.get('content', r.get('snippet', 'N/A'))[:200]}\n 来源: {r.get('url', 'N/A')}\n"
|
||||
logger.info(f"搜索完成: {len(search_result['results'])} 条结果")
|
||||
|
||||
# 更新统计和日志
|
||||
tool_service.increment_stats(search_tool.id, True)
|
||||
tool_service.log_usage({
|
||||
'tool_id': search_tool.id,
|
||||
'tool_type': 'search',
|
||||
'query': message,
|
||||
'success': True,
|
||||
'result_summary': f'{len(search_result["results"])} results',
|
||||
'conversation_id': conversation_id,
|
||||
'agent_id': current_agent_id,
|
||||
'duration_ms': duration_ms
|
||||
})
|
||||
except Exception as e:
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
logger.error(f"搜索失败: {e}")
|
||||
tool_service.increment_stats(search_tool.id, False)
|
||||
tool_service.log_usage({
|
||||
'tool_id': search_tool.id,
|
||||
'tool_type': 'search',
|
||||
'query': message,
|
||||
'success': False,
|
||||
'error_message': str(e),
|
||||
'conversation_id': conversation_id,
|
||||
'duration_ms': duration_ms
|
||||
})
|
||||
|
||||
# 获取或创建会话
|
||||
if conversation_id:
|
||||
conversation = conv_service.get_conversation(conversation_id)
|
||||
else:
|
||||
conversation = conv_service.create_conversation(user.id)
|
||||
conversation_id = conversation.conversation_id
|
||||
await websocket.send_json({
|
||||
"type": "conversation_created",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
|
||||
response, thinking_content = await llm_service.chat(
|
||||
messages=history,
|
||||
provider_config=agent_config['provider'],
|
||||
agent_config=agent_config['agent'],
|
||||
enable_thinking=enable_thinking
|
||||
)
|
||||
|
||||
# 发送思考内容
|
||||
if thinking_content:
|
||||
await websocket.send_json({
|
||||
"type": "thinking_content",
|
||||
"conversation_id": conversation_id,
|
||||
"content": thinking_content
|
||||
})
|
||||
|
||||
# 发送思考结束
|
||||
await websocket.send_json({
|
||||
"type": "thinking_end",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
|
||||
# 保存AI回复
|
||||
assistant_msg = conv_service.add_message(
|
||||
# 保存用户消息
|
||||
user_msg = conv_service.add_message(
|
||||
conversation_id=conversation.id,
|
||||
role='assistant',
|
||||
content=response,
|
||||
source='web',
|
||||
thinking_content=thinking_content,
|
||||
agent_id=current_agent_id,
|
||||
model_used=agent_config['provider'].get('default_model')
|
||||
role='user',
|
||||
content=message,
|
||||
source='web'
|
||||
)
|
||||
|
||||
# 广播AI回复
|
||||
# 广播用户消息
|
||||
await manager.send_to_user(MAIN_USER_ID, {
|
||||
"type": "assistant_message",
|
||||
"type": "user_message",
|
||||
"conversation_id": conversation_id,
|
||||
"message": {
|
||||
"id": assistant_msg.id,
|
||||
"role": "assistant",
|
||||
"content": response,
|
||||
"thinking_content": thinking_content,
|
||||
"id": user_msg.id,
|
||||
"role": "user",
|
||||
"content": message,
|
||||
"source": "web",
|
||||
"agent_id": current_agent_id,
|
||||
"agent_name": agent_config['agent'].get('display_name'),
|
||||
"created_at": assistant_msg.created_at.isoformat()
|
||||
"created_at": user_msg.created_at.isoformat()
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"LLM调用失败: {e}")
|
||||
await websocket.send_json({
|
||||
"type": "error",
|
||||
"message": f"AI服务暂时不可用: {str(e)}"
|
||||
})
|
||||
# 获取Agent配置
|
||||
agent_config = agent_service.get_agent_config(current_agent_id)
|
||||
|
||||
if not agent_config or not agent_config.get('provider'):
|
||||
await websocket.send_json({
|
||||
"type": "error",
|
||||
"message": "Agent配置不完整"
|
||||
})
|
||||
continue
|
||||
|
||||
# 获取对话历史
|
||||
history = conv_service.get_conversation_history(conversation_id, limit=agent_config['agent'].get('max_history', 20))
|
||||
|
||||
# 如果有搜索结果,添加到消息中
|
||||
if search_context:
|
||||
# 在系统提示中添加搜索结果说明
|
||||
modified_system_prompt = agent_config['agent'].get('system_prompt', '') + "\n\n如果提供了搜索结果,请基于搜索结果回答用户问题,并注明信息来源。"
|
||||
agent_config['agent']['system_prompt'] = modified_system_prompt
|
||||
# 将搜索结果作为系统消息添加到历史
|
||||
history.append({"role": "system", "content": f"以下是搜索到的相关信息,请参考这些内容回答用户问题:{search_context}"})
|
||||
|
||||
# 使用非流式调用LLM(简化版本,确保稳定)
|
||||
try:
|
||||
# 调用LLM(非流式)
|
||||
response, thinking_content = await llm_service.chat(
|
||||
messages=history,
|
||||
provider_config=agent_config['provider'],
|
||||
agent_config=agent_config['agent'],
|
||||
enable_thinking=enable_thinking
|
||||
)
|
||||
|
||||
logger.info(f"LLM响应: response长度={len(response)}, thinking长度={len(thinking_content) if thinking_content else 0}")
|
||||
|
||||
# 保存AI回复
|
||||
assistant_msg = conv_service.add_message(
|
||||
conversation_id=conversation.id,
|
||||
role='assistant',
|
||||
content=response,
|
||||
source='web',
|
||||
thinking_content=thinking_content if thinking_content else None,
|
||||
agent_id=current_agent_id,
|
||||
model_used=agent_config['provider'].get('default_model')
|
||||
)
|
||||
|
||||
# 发送完整回复(包含思考内容)
|
||||
await websocket.send_json({
|
||||
"type": "assistant_message",
|
||||
"conversation_id": conversation_id,
|
||||
"message": {
|
||||
"id": assistant_msg.id,
|
||||
"role": "assistant",
|
||||
"content": response,
|
||||
"thinking_content": thinking_content if thinking_content else None,
|
||||
"source": "web",
|
||||
"agent_id": current_agent_id,
|
||||
"agent_name": agent_config['agent'].get('display_name'),
|
||||
"created_at": assistant_msg.created_at.isoformat()
|
||||
}
|
||||
})
|
||||
|
||||
logger.info(f"AI回复已发送: conversation_id={conversation_id}")
|
||||
|
||||
# 启用发送按钮
|
||||
await websocket.send_json({
|
||||
"type": "stream_end",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"LLM调用失败: {e}")
|
||||
await websocket.send_json({
|
||||
"type": "error",
|
||||
"message": f"AI服务暂时不可用: {str(e)}"
|
||||
})
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
except WebSocketDisconnect:
|
||||
manager.disconnect(websocket, user_id)
|
||||
|
||||
69
models_v2.py
69
models_v2.py
@@ -58,6 +58,9 @@ class Agent(Base):
|
||||
name = Column(String(100), unique=True, index=True) # Agent名称
|
||||
display_name = Column(String(100)) # 显示名称
|
||||
|
||||
# 工具配置
|
||||
tools = Column(JSON, default=list) # 可用工具列表 ["search", "calculator", ...]
|
||||
|
||||
# 大模型配置
|
||||
llm_provider_id = Column(Integer, ForeignKey('llm_providers.id'))
|
||||
model_override = Column(String(100), nullable=True) # 覆盖Provider默认模型
|
||||
@@ -224,6 +227,60 @@ class MatrixRoomMapping(Base):
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
|
||||
|
||||
# ==================== 搜索工具配置 ====================
|
||||
|
||||
class ToolConfig(Base):
|
||||
"""工具配置(通用,支持搜索、计算器、代码执行等)"""
|
||||
__tablename__ = 'tool_configs'
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String(100)) # 工具名称,如 "Tavily Search"、"Calculator"
|
||||
tool_type = Column(String(50), index=True) # 工具类型:search, calculator, code_runner, image_gen, etc.
|
||||
provider = Column(String(50), nullable=True) # 提供商(可选):tavily, google, wolfram, etc.
|
||||
|
||||
# API配置(JSON,不同工具可能有不同配置)
|
||||
config = Column(JSON, default=dict)
|
||||
# search示例: {"api_key": "xxx", "max_results": 5, "search_depth": "basic"}
|
||||
# calculator示例: {"api_base": "xxx"}
|
||||
|
||||
# 状态
|
||||
is_active = Column(Boolean, default=True)
|
||||
is_default = Column(Boolean, default=False) # 是否为该类型的默认工具
|
||||
|
||||
# 统计
|
||||
total_calls = Column(Integer, default=0) # 总调用次数
|
||||
success_calls = Column(Integer, default=0) # 成功次数
|
||||
failed_calls = Column(Integer, default=0) # 失败次数
|
||||
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
|
||||
class ToolUsageLog(Base):
|
||||
"""工具使用日志"""
|
||||
__tablename__ = 'tool_usage_logs'
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
tool_id = Column(Integer, ForeignKey('tool_configs.id'))
|
||||
tool_type = Column(String(50), index=True)
|
||||
|
||||
# 调用信息
|
||||
query = Column(Text) # 调用参数/查询内容
|
||||
success = Column(Boolean, default=True)
|
||||
error_message = Column(Text, nullable=True)
|
||||
result_summary = Column(Text, nullable=True) # 结果摘要
|
||||
|
||||
# 关联信息
|
||||
conversation_id = Column(String(100), nullable=True)
|
||||
agent_id = Column(Integer, nullable=True)
|
||||
user_id = Column(String(100), nullable=True)
|
||||
|
||||
# 性能
|
||||
duration_ms = Column(Integer, nullable=True) # 调用耗时(毫秒)
|
||||
|
||||
called_at = Column(DateTime, default=datetime.utcnow)
|
||||
|
||||
|
||||
# ==================== 系统配置(保留) ====================
|
||||
|
||||
class SystemConfig(Base):
|
||||
@@ -335,6 +392,18 @@ def init_default_data():
|
||||
)
|
||||
db.add(matrix_mapping)
|
||||
|
||||
# 5. 创建默认搜索工具配置
|
||||
search_config = SearchToolConfig(
|
||||
name="Tavily Search",
|
||||
provider="tavily",
|
||||
api_key="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",
|
||||
max_results=5,
|
||||
search_depth="basic",
|
||||
is_active=True,
|
||||
is_default=True
|
||||
)
|
||||
db.add(search_config)
|
||||
|
||||
db.commit()
|
||||
print("默认数据初始化完成")
|
||||
|
||||
|
||||
@@ -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, ToolConfig, ToolUsageLog, 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,172 @@ class ChannelService:
|
||||
'is_active': channel.is_active,
|
||||
'is_primary': channel.is_primary,
|
||||
'agent_mappings': self.get_channel_agents(channel_id)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ToolService:
|
||||
"""工具管理服务"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_all_tools(self) -> List[ToolConfig]:
|
||||
"""获取所有工具配置"""
|
||||
return self.db.query(ToolConfig).order_by(ToolConfig.tool_type, ToolConfig.is_default.desc()).all()
|
||||
|
||||
def get_tools_by_type(self, tool_type: str) -> List[ToolConfig]:
|
||||
"""获取指定类型的工具"""
|
||||
return self.db.query(ToolConfig).filter(ToolConfig.tool_type == tool_type).all()
|
||||
|
||||
def get_active_tools(self) -> List[ToolConfig]:
|
||||
"""获取活跃的工具"""
|
||||
return self.db.query(ToolConfig).filter(ToolConfig.is_active == True).all()
|
||||
|
||||
def get_tool(self, tool_id: int) -> Optional[ToolConfig]:
|
||||
"""获取单个工具"""
|
||||
return self.db.query(ToolConfig).filter(ToolConfig.id == tool_id).first()
|
||||
|
||||
def get_default_tool(self, tool_type: str) -> Optional[ToolConfig]:
|
||||
"""获取指定类型的默认工具"""
|
||||
tool = self.db.query(ToolConfig).filter(
|
||||
ToolConfig.tool_type == tool_type,
|
||||
ToolConfig.is_default == True,
|
||||
ToolConfig.is_active == True
|
||||
).first()
|
||||
if not tool:
|
||||
tool = self.db.query(ToolConfig).filter(
|
||||
ToolConfig.tool_type == tool_type,
|
||||
ToolConfig.is_active == True
|
||||
).first()
|
||||
return tool
|
||||
|
||||
def create_tool(self, data: Dict) -> ToolConfig:
|
||||
"""创建工具配置"""
|
||||
tool = ToolConfig(
|
||||
name=data.get('name'),
|
||||
tool_type=data.get('tool_type', 'search'),
|
||||
provider=data.get('provider'),
|
||||
config=data.get('config', {}),
|
||||
is_active=data.get('is_active', True),
|
||||
is_default=data.get('is_default', False)
|
||||
)
|
||||
self.db.add(tool)
|
||||
self.db.commit()
|
||||
self.db.refresh(tool)
|
||||
return tool
|
||||
|
||||
def update_tool(self, tool_id: int, data: Dict) -> Optional[ToolConfig]:
|
||||
"""更新工具配置"""
|
||||
tool = self.get_tool(tool_id)
|
||||
if not tool:
|
||||
return None
|
||||
|
||||
for key, value in data.items():
|
||||
if hasattr(tool, key) and value is not None:
|
||||
setattr(tool, key, value)
|
||||
|
||||
self.db.commit()
|
||||
self.db.refresh(tool)
|
||||
return tool
|
||||
|
||||
def delete_tool(self, tool_id: int) -> bool:
|
||||
"""删除工具配置"""
|
||||
tool = self.get_tool(tool_id)
|
||||
if not tool:
|
||||
return False
|
||||
|
||||
self.db.delete(tool)
|
||||
self.db.commit()
|
||||
return True
|
||||
|
||||
def set_default_tool(self, tool_id: int) -> bool:
|
||||
"""设置默认工具"""
|
||||
tool = self.get_tool(tool_id)
|
||||
if not tool:
|
||||
return False
|
||||
|
||||
# 清除同类型的其他默认
|
||||
self.db.query(ToolConfig).filter(
|
||||
ToolConfig.tool_type == tool.tool_type
|
||||
).update({ToolConfig.is_default: False})
|
||||
|
||||
tool.is_default = True
|
||||
self.db.commit()
|
||||
return True
|
||||
|
||||
def increment_stats(self, tool_id: int, success: bool):
|
||||
"""更新工具调用统计"""
|
||||
tool = self.get_tool(tool_id)
|
||||
if tool:
|
||||
tool.total_calls += 1
|
||||
if success:
|
||||
tool.success_calls += 1
|
||||
else:
|
||||
tool.failed_calls += 1
|
||||
self.db.commit()
|
||||
|
||||
def log_usage(self, data: Dict) -> ToolUsageLog:
|
||||
"""记录工具使用日志"""
|
||||
log = ToolUsageLog(
|
||||
tool_id=data.get('tool_id'),
|
||||
tool_type=data.get('tool_type'),
|
||||
query=data.get('query'),
|
||||
success=data.get('success', True),
|
||||
error_message=data.get('error_message'),
|
||||
result_summary=data.get('result_summary'),
|
||||
conversation_id=data.get('conversation_id'),
|
||||
agent_id=data.get('agent_id'),
|
||||
user_id=data.get('user_id'),
|
||||
duration_ms=data.get('duration_ms')
|
||||
)
|
||||
self.db.add(log)
|
||||
self.db.commit()
|
||||
self.db.refresh(log)
|
||||
return log
|
||||
|
||||
def get_usage_stats(self, days: int = 7) -> Dict:
|
||||
"""获取工具使用统计"""
|
||||
from datetime import timedelta
|
||||
start_date = datetime.utcnow() - timedelta(days=days)
|
||||
|
||||
# 按工具类型统计
|
||||
logs = self.db.query(ToolUsageLog).filter(
|
||||
ToolUsageLog.called_at >= start_date
|
||||
).all()
|
||||
|
||||
stats = {
|
||||
'total_calls': len(logs),
|
||||
'success_rate': sum(1 for l in logs if l.success) / len(logs) * 100 if logs else 0,
|
||||
'by_type': {},
|
||||
'by_tool': {},
|
||||
'recent_errors': []
|
||||
}
|
||||
|
||||
for log in logs:
|
||||
# 按类型
|
||||
if log.tool_type not in stats['by_type']:
|
||||
stats['by_type'][log.tool_type] = {'total': 0, 'success': 0, 'failed': 0}
|
||||
stats['by_type'][log.tool_type]['total'] += 1
|
||||
if log.success:
|
||||
stats['by_type'][log.tool_type]['success'] += 1
|
||||
else:
|
||||
stats['by_type'][log.tool_type]['failed'] += 1
|
||||
|
||||
# 按工具
|
||||
tool = self.get_tool(log.tool_id) if log.tool_id else None
|
||||
tool_name = tool.name if tool else f'Tool#{log.tool_id}'
|
||||
if tool_name not in stats['by_tool']:
|
||||
stats['by_tool'][tool_name] = {'total': 0, 'success': 0}
|
||||
stats['by_tool'][tool_name]['total'] += 1
|
||||
if log.success:
|
||||
stats['by_tool'][tool_name]['success'] += 1
|
||||
|
||||
# 最近错误
|
||||
if not log.success and log.error_message:
|
||||
stats['recent_errors'].append({
|
||||
'tool': tool_name,
|
||||
'error': log.error_message[:100],
|
||||
'time': log.called_at.isoformat()
|
||||
})
|
||||
|
||||
return stats
|
||||
@@ -128,6 +128,8 @@ class LLMService:
|
||||
# 处理思考功能
|
||||
if enable_thinking and agent_config.get('enable_thinking', True):
|
||||
thinking_prompt = agent_config.get('thinking_prompt')
|
||||
thinking_prefix = agent_config.get('thinking_prefix', '')
|
||||
thinking_suffix = agent_config.get('thinking_suffix', '')
|
||||
|
||||
if supports_thinking and thinking_model:
|
||||
# 使用专门的思考模型
|
||||
@@ -139,22 +141,15 @@ class LLMService:
|
||||
thinking_result = await self._call_api(
|
||||
api_base, api_key, thinking_model, thinking_messages,
|
||||
max_tokens=min(max_tokens, 1000),
|
||||
temperature=0.3 # 思考时降低温度
|
||||
temperature=0.3
|
||||
)
|
||||
thinking_content = thinking_result
|
||||
except Exception as e:
|
||||
logger.warning(f"思考模型调用失败: {e}")
|
||||
|
||||
elif supports_thinking:
|
||||
# Provider支持思考但无单独模型,尝试在回复中获取思考部分
|
||||
pass # 在回复解析时处理
|
||||
|
||||
elif thinking_prompt:
|
||||
# Provider不支持思考,但Agent配置了思考提示词
|
||||
# 将思考提示词添加到系统提示
|
||||
enhanced_system = f"{system_prompt}\n\n在回答之前,请先思考问题。思考过程请用{agent_config.get('thinking_prefix', '')}和{agent_config.get('thinking_suffix', '')}包裹,然后再给出正式回答。"
|
||||
if thinking_prompt:
|
||||
enhanced_system += f"\n思考指导:{thinking_prompt}"
|
||||
# Agent配置了思考提示词,添加到系统提示中
|
||||
enhanced_system = f"{system_prompt}\n\n{thinking_prompt}"
|
||||
final_messages[0] = {"role": "system", "content": enhanced_system}
|
||||
|
||||
# 调用主模型
|
||||
@@ -165,19 +160,37 @@ class LLMService:
|
||||
temperature=temperature
|
||||
)
|
||||
|
||||
# 尝试从回复中提取思考内容
|
||||
if enable_thinking and not supports_thinking:
|
||||
# 尝试从回复中提取思考内容(支持DeepSeek R1、GLM等模型的思考模式)
|
||||
if enable_thinking and agent_config.get('enable_thinking', True):
|
||||
thinking_prefix = agent_config.get('thinking_prefix', '')
|
||||
thinking_suffix = agent_config.get('thinking_suffix', '')
|
||||
|
||||
if thinking_prefix and thinking_suffix:
|
||||
# 提取思考部分
|
||||
pattern = f"{re.escape(thinking_prefix)}(.*?)?{re.escape(thinking_suffix)}"
|
||||
match = re.search(pattern, response, re.DOTALL)
|
||||
if match:
|
||||
thinking_content = match.group(1).strip()
|
||||
# 移除思考部分,只保留回复
|
||||
response = re.sub(pattern, '', response, flags=re.DOTALL).strip()
|
||||
# 如果没有配置前缀后缀,使用常见的思考标记
|
||||
if not thinking_prefix:
|
||||
# 尝试常见的思考标记
|
||||
common_thinking_markers = [
|
||||
('<think>', '</think>'),
|
||||
('【思考】', '【回答】'),
|
||||
('Thought:', 'Answer:'),
|
||||
('思考:', '回答:'),
|
||||
]
|
||||
for prefix, suffix in common_thinking_markers:
|
||||
if prefix in response and suffix in response:
|
||||
thinking_prefix = prefix
|
||||
thinking_suffix = suffix
|
||||
break
|
||||
|
||||
# 提取思考部分
|
||||
if thinking_prefix and thinking_suffix and thinking_prefix in response:
|
||||
try:
|
||||
start_idx = response.find(thinking_prefix)
|
||||
end_idx = response.find(thinking_suffix, start_idx)
|
||||
if end_idx > start_idx:
|
||||
thinking_content = response[start_idx + len(thinking_prefix):end_idx].strip()
|
||||
# 移除思考部分,只保留回复
|
||||
response = response[end_idx + len(thinking_suffix):].strip()
|
||||
except Exception as e:
|
||||
logger.warning(f"提取思考内容失败: {e}")
|
||||
|
||||
return response, thinking_content
|
||||
|
||||
@@ -268,8 +281,7 @@ class LLMService:
|
||||
|
||||
thinking_prefix = agent_config.get('thinking_prefix', '')
|
||||
thinking_suffix = agent_config.get('thinking_suffix', '')
|
||||
in_thinking = False
|
||||
thinking_buffer = ""
|
||||
buffer = "" # 用于累积和检测思考部分
|
||||
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
async with client.stream("POST", url, headers=headers, json=payload) as response:
|
||||
@@ -284,35 +296,46 @@ class LLMService:
|
||||
delta = data['choices'][0].get('delta', {})
|
||||
if 'content' in delta:
|
||||
text = delta['content']
|
||||
buffer += text
|
||||
|
||||
# 检测思考部分
|
||||
if thinking_prefix and thinking_suffix:
|
||||
for char in text:
|
||||
if in_thinking:
|
||||
thinking_buffer += char
|
||||
# 检查是否结束思考
|
||||
if thinking_buffer.endswith(thinking_suffix):
|
||||
thinking_content = thinking_buffer[:-len(thinking_suffix)]
|
||||
yield {"type": "thinking", "text": thinking_content}
|
||||
in_thinking = False
|
||||
thinking_buffer = ""
|
||||
# 检测思考部分(简化逻辑)
|
||||
if thinking_prefix and thinking_suffix and thinking_prefix in buffer:
|
||||
# 尝试解析思考部分
|
||||
try:
|
||||
start_idx = buffer.find(thinking_prefix)
|
||||
if start_idx >= 0:
|
||||
# 找到思考开始,继续找结束
|
||||
end_idx = buffer.find(thinking_suffix, start_idx)
|
||||
if end_idx > start_idx:
|
||||
# 思考部分完整,发送思考然后发送内容
|
||||
thinking = buffer[start_idx + len(thinking_prefix):end_idx]
|
||||
yield {"type": "thinking", "text": thinking}
|
||||
# 发送思考后的内容
|
||||
remaining = buffer[end_idx + len(thinking_suffix):]
|
||||
if remaining:
|
||||
yield {"type": "content", "text": remaining}
|
||||
buffer = ""
|
||||
else:
|
||||
# 检查是否接近结束
|
||||
suffix_len = len(thinking_suffix)
|
||||
if len(thinking_buffer) >= suffix_len:
|
||||
yield {"type": "thinking", "text": thinking_buffer[-suffix_len:]}
|
||||
# 思考部分还没结束,先发送之前的内容
|
||||
if start_idx > 0:
|
||||
yield {"type": "content", "text": buffer[:start_idx]}
|
||||
# 等待更多数据来完成思考部分
|
||||
buffer = buffer[start_idx:]
|
||||
else:
|
||||
if char == thinking_prefix[0]:
|
||||
# 可能开始思考
|
||||
thinking_buffer = char
|
||||
if len(thinking_prefix) == 1:
|
||||
in_thinking = True
|
||||
else:
|
||||
yield {"type": "content", "text": char}
|
||||
# 没有思考标记,直接发送内容
|
||||
yield {"type": "content", "text": text}
|
||||
buffer = ""
|
||||
except:
|
||||
yield {"type": "content", "text": text}
|
||||
else:
|
||||
# 没有思考标记配置,直接发送内容
|
||||
yield {"type": "content", "text": text}
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
# 处理剩余buffer
|
||||
if buffer:
|
||||
yield {"type": "content", "text": buffer}
|
||||
|
||||
|
||||
# 全局实例
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
1165
templates/index.html
1165
templates/index.html
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user