refactor: 单图方案重构 + 动态模型选择 + chat_services优化
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 12m9s
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 12m9s
## 核心改动 ### 1. 单图方案重构 - 删除了多图(self.graphs),改为单图(self.graph) - 新增 MainGraphState.current_model 字段用于运行时注入模型 - llm_call 节点改为动态选择模型(create_dynamic_llm_call_node) ### 2. chat_services 优化 - 添加 _cached_services 缓存,避免重复初始化 - 新增 get_cached_chat_services() 函数,用于单图注入 - 新增 _check_http_service_available() 统一HTTP探测逻辑 - 减少重复代码,LocalVLLMChatProvider和LocalSmallModelProvider共用探测方法 ### 3. AIAgentService 重构 - initialize() 只构建一次图,传入 chat_services 字典 - 新增 _resolve_model() 模型回退逻辑 - 新增 _build_invocation() 统一构建调用参数 - process_message() 和 process_message_stream() 改为注入 current_model - 流式处理代码拆分,增加可读性 ### 4. 新增和删除文件 - 新增:backend/app/main_graph/main_graph_builder.py(图构建) - 新增:backend/app/main_graph/subgraph_wrapper.py(子图封装) - 新增:tools/test/test_tavily_search.py(测试) - 删除:backend/app/main_graph/graph.py(旧图) - 删除:backend/app/main_graph/utils/main_graph_builder.py(旧构建器) - 删除:backend/app/main_graph/utils/__init__.py ### 5. 其他更新 - README.md:新增模型服务使用情况详解章节 - backend/app/model_services/__init__.py:新增 get_cached_chat_services 导出 ## 方案优势 - 内存优化:N张图 → 1张图 - 灵活性:运行时动态选择模型,支持同会话不同模型 - 性能:模型服务缓存,初始化仅一次 - 可维护性:减少重复代码,统一HTTP探测逻辑
This commit is contained in:
@@ -6,28 +6,19 @@ LangGraph 图结构可视化脚本
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 确定项目根目录(Agent1 目录)
|
||||
# 当前文件位置:tools/visualize_graph.py
|
||||
# 向上 1 级到 Agent1
|
||||
# 路径设置
|
||||
PROJECT_ROOT = Path(__file__).parent.parent
|
||||
BACKEND_DIR = PROJECT_ROOT / "backend"
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
# 关键:把 backend 目录加入 sys.path,这样才能找到 rag_core
|
||||
# 注意:这只对直接运行脚本有效,对 -m 方式无效(因为 -m 方式在脚本运行前就导入了)
|
||||
if str(BACKEND_DIR) not in sys.path:
|
||||
sys.path.insert(0, str(BACKEND_DIR))
|
||||
if str(PROJECT_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(PROJECT_ROOT))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv(PROJECT_ROOT / ".env")
|
||||
|
||||
import asyncio
|
||||
from backend.app.agent.agent_service import AIAgentService
|
||||
from backend.app.config import DB_URI
|
||||
from backend.app.main_graph.checkpoint.postgres.aio import AsyncPostgresSaver
|
||||
import asyncio
|
||||
|
||||
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
|
||||
|
||||
async def visualize_graph():
|
||||
"""可视化 LangGraph 结构"""
|
||||
@@ -37,6 +28,8 @@ async def visualize_graph():
|
||||
print(f"项目根目录: {PROJECT_ROOT}")
|
||||
print(f"Backend 目录: {BACKEND_DIR}")
|
||||
|
||||
|
||||
|
||||
async with AsyncPostgresSaver.from_conn_string(DB_URI) as checkpointer:
|
||||
await checkpointer.setup()
|
||||
|
||||
@@ -45,37 +38,52 @@ async def visualize_graph():
|
||||
agent_service = AIAgentService(checkpointer)
|
||||
await agent_service.initialize()
|
||||
|
||||
for model_name, graph in agent_service.graphs.items():
|
||||
print(f"\n{'=' * 80}")
|
||||
print(f" 模型: {model_name}")
|
||||
print(f"{'=' * 80}")
|
||||
# 获取图(单图方案)
|
||||
graph = agent_service.graph
|
||||
print("\n✅ Agent 服务初始化完成")
|
||||
|
||||
# 获取图结构
|
||||
graph_structure = graph.get_graph()
|
||||
# 获取图结构
|
||||
graph_structure = graph.get_graph()
|
||||
|
||||
# 1. 直接打印节点和边
|
||||
print("\n[1] 节点列表:")
|
||||
print("-" * 80)
|
||||
for node_id, node in graph_structure.nodes.items():
|
||||
print(f" - {node_id}: {node.name}")
|
||||
# 1. 直接打印节点和边
|
||||
print("\n" + "=" * 80)
|
||||
print("[1] 节点列表")
|
||||
print("=" * 80)
|
||||
for node_id, node in graph_structure.nodes.items():
|
||||
print(f" 📦 {node_id}: {node.name}")
|
||||
|
||||
print("\n[2] 边列表:")
|
||||
print("-" * 80)
|
||||
for edge in graph_structure.edges:
|
||||
print(f" {edge.source} --> {edge.target}")
|
||||
print("\n" + "=" * 80)
|
||||
print("[2] 边列表")
|
||||
print("=" * 80)
|
||||
for edge in graph_structure.edges:
|
||||
print(f" {edge.source} --> {edge.target}")
|
||||
|
||||
# 3. ASCII 字符画(需要 grandalf)
|
||||
print("\n[3] ASCII 字符画:")
|
||||
print("-" * 80)
|
||||
try:
|
||||
print(graph_structure.draw_ascii())
|
||||
except Exception as e:
|
||||
print(f"⚠️ ASCII 绘制失败: {e}")
|
||||
# 2. ASCII 字符画
|
||||
print("\n" + "=" * 80)
|
||||
print("[3] ASCII 字符画")
|
||||
print("=" * 80)
|
||||
try:
|
||||
ascii_graph = graph_structure.draw_ascii()
|
||||
print(ascii_graph)
|
||||
except Exception as e:
|
||||
print(f"⚠️ ASCII 绘制失败: {e}")
|
||||
|
||||
# 4. Mermaid 源码
|
||||
print("\n[4] Mermaid 源码 (可复制到 https://mermaid.live/):")
|
||||
print("-" * 80)
|
||||
print(graph_structure.draw_mermaid())
|
||||
# 3. Mermaid 源码
|
||||
print("\n" + "=" * 80)
|
||||
print("[4] Mermaid 源码 (复制到 https://mermaid.live/)")
|
||||
print("=" * 80)
|
||||
try:
|
||||
mermaid_code = graph_structure.draw_mermaid()
|
||||
print(mermaid_code)
|
||||
except Exception as e:
|
||||
print(f"⚠️ Mermaid 生成失败: {e}")
|
||||
|
||||
# 4. 节点统计
|
||||
print("\n" + "=" * 80)
|
||||
print("[5] 图统计")
|
||||
print("=" * 80)
|
||||
print(f" 节点数量: {len(graph_structure.nodes)}")
|
||||
print(f" 边数量: {len(graph_structure.edges)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user