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探测逻辑
110 lines
3.3 KiB
Python
110 lines
3.3 KiB
Python
"""
|
||
通讯录子图构建器
|
||
Contact Subgraph Builder
|
||
支持 API 注入的工厂模式
|
||
"""
|
||
|
||
from langgraph.graph import StateGraph, START, END
|
||
|
||
from .state import ContactState
|
||
from .nodes import create_contact_nodes
|
||
|
||
|
||
def build_contact_subgraph(contact_api=None):
|
||
"""
|
||
构建通讯录子图(工厂模式)
|
||
|
||
Args:
|
||
contact_api: 可选的 ContactAPIClient 实例(支持真实数据库或模拟模式)
|
||
不传入则使用默认模拟 API(向后兼容)
|
||
|
||
Returns:
|
||
配置好的 StateGraph
|
||
"""
|
||
# 创建节点(传入 API)
|
||
nodes = create_contact_nodes(contact_api) if contact_api else None
|
||
|
||
# 如果没有传入 API,使用向后兼容的导入
|
||
if nodes is None:
|
||
from .nodes import (
|
||
parse_intent,
|
||
list_contacts,
|
||
add_contact,
|
||
list_emails,
|
||
generate_email_draft,
|
||
human_review,
|
||
send_email,
|
||
sniff_contacts,
|
||
format_result,
|
||
should_continue
|
||
)
|
||
else:
|
||
parse_intent = nodes["parse_intent"]
|
||
list_contacts = nodes["list_contacts"]
|
||
add_contact = nodes["add_contact"]
|
||
list_emails = nodes["list_emails"]
|
||
generate_email_draft = nodes["generate_email_draft"]
|
||
human_review = nodes["human_review"]
|
||
send_email = nodes["send_email"]
|
||
sniff_contacts = nodes["sniff_contacts"]
|
||
format_result = nodes["format_result"]
|
||
should_continue = nodes["should_continue"]
|
||
|
||
# 创建图
|
||
graph = StateGraph(ContactState)
|
||
|
||
# 添加节点
|
||
graph.add_node("parse_intent", parse_intent)
|
||
graph.add_node("list_contacts", list_contacts)
|
||
graph.add_node("add_contact", add_contact)
|
||
graph.add_node("list_emails", list_emails)
|
||
graph.add_node("generate_email_draft", generate_email_draft)
|
||
graph.add_node("human_review", human_review)
|
||
graph.add_node("send_email", send_email)
|
||
graph.add_node("sniff_contacts", sniff_contacts)
|
||
graph.add_node("format_result", format_result)
|
||
|
||
# 添加边
|
||
# 从START开始
|
||
graph.add_edge(START, "parse_intent")
|
||
|
||
# 从parse_intent根据条件路由
|
||
graph.add_conditional_edges(
|
||
"parse_intent",
|
||
should_continue,
|
||
{
|
||
"list_contacts": "list_contacts",
|
||
"add_contact": "add_contact",
|
||
"list_emails": "list_emails",
|
||
"generate_email_draft": "generate_email_draft",
|
||
"sniff_contacts": "sniff_contacts",
|
||
}
|
||
)
|
||
|
||
# 从各个操作节点到format_result
|
||
graph.add_edge("list_contacts", "format_result")
|
||
graph.add_edge("add_contact", "format_result")
|
||
graph.add_edge("list_emails", "format_result")
|
||
graph.add_edge("sniff_contacts", "format_result")
|
||
|
||
# 邮件发送的特殊流程
|
||
graph.add_edge("generate_email_draft", "human_review")
|
||
|
||
# 从human_review根据条件路由
|
||
graph.add_conditional_edges(
|
||
"human_review",
|
||
should_continue,
|
||
{
|
||
"send_email": "send_email",
|
||
"format_result": "format_result",
|
||
}
|
||
)
|
||
|
||
# 发送邮件后到格式化
|
||
graph.add_edge("send_email", "format_result")
|
||
|
||
# 最终到END
|
||
graph.add_edge("format_result", END)
|
||
|
||
return graph
|