feat: 完善词典子图,添加API调用和前端格式化工具
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m5s

- 完善词典子图:添加生词本功能
- 创建API调用工具:dictionary_api
- 添加前端格式化展示工具:result_formatter.py
- 创建通讯录和资讯子图的基本结构
- 更新主图状态结构,添加MainGraphState
- 添加subgraph_builder.py用于子图集成
This commit is contained in:
2026-04-25 18:29:23 +08:00
parent 03ba825645
commit a14744f18b
17 changed files with 2057 additions and 18 deletions

View File

@@ -3,6 +3,19 @@ Graph 子模块
"""
from .graph_builder import GraphBuilder
from .state import MessagesState, GraphContext
from .subgraph_builder import build_main_graph
from .state import (
MessagesState,
GraphContext,
MainGraphState,
CurrentAction
)
__all__ = ["GraphBuilder", "MessagesState", "GraphContext"]
__all__ = [
"GraphBuilder",
"build_main_graph",
"MessagesState",
"GraphContext",
"MainGraphState",
"CurrentAction"
]

View File

@@ -1,25 +1,75 @@
"""
LangGraph 状态定义模块
包含 MessagesState 和 GraphContext
主图状态定义 - 扩展版
Main Graph State Definition - Extended
"""
import operator
from typing import Annotated
from typing_extensions import TypedDict
from dataclasses import dataclass
from langchain_core.messages import AnyMessage
from enum import Enum, auto
from typing import Optional, Dict, Any, Annotated, Sequence, TypedDict
from dataclasses import dataclass, field
from langgraph.graph import add_messages
from langchain_core.messages import BaseMessage
# ========== 兼容旧代码的类型 ==========
class MessagesState(TypedDict):
"""对话状态类型定义"""
messages: Annotated[list[AnyMessage], operator.add]
"""旧的MessagesState类型保留兼容性"""
messages: Annotated[Sequence[BaseMessage], add_messages]
class GraphContext(TypedDict):
"""旧的GraphContext类型保留兼容性"""
llm_calls: int
memory_context: str
last_token_usage: dict # 本次调用的 token 使用详情
last_elapsed_time: float # 本次调用耗时(秒)
turns_since_last_summary: int # 距离上次生成摘要的轮数
system_prompt: str
# ========== 新的类型 ==========
class CurrentAction(Enum):
"""主图当前操作类型"""
NONE = auto()
GENERAL_CHAT = auto()
NEWS_ANALYSIS = auto()
DICTIONARY = auto()
CONTACT = auto()
@dataclass
class GraphContext:
"""图执行上下文"""
user_id: str
# 可扩展更多上下文信息
class MainGraphState:
"""
主图状态 - 兼容旧代码 + 新增子图功能
包含:
1. 旧代码的MessagesState兼容性字段
2. 主图控制字段
3. 子图结果占位
4. 用户信息
"""
# ========== 兼容性字段保留旧的MessagesState ==========
messages: Annotated[Sequence[BaseMessage], add_messages] = field(default_factory=list)
llm_calls: int = 0
memory_context: str = ""
system_prompt: str = ""
# ========== 主图控制字段 ==========
user_query: str = "" # 用户当前查询
current_action: CurrentAction = CurrentAction.NONE # 当前操作
intent_confidence: float = 0.0 # 意图识别置信度
# ========== 子图结果占位 ==========
news_result: Optional[Dict[str, Any]] = None # 资讯子图结果
dictionary_result: Optional[Dict[str, Any]] = None # 词典子图结果
contact_result: Optional[Dict[str, Any]] = None # 通讯录子图结果
# ========== 用户信息 ==========
user_id: str = ""
# ========== 执行状态 ==========
current_phase: str = "init"
error_message: str = ""
final_result: str = ""
success: bool = False
# ========== 元数据 ==========
start_time: Optional[str] = None
end_time: Optional[str] = None
debug_info: Dict[str, Any] = field(default_factory=dict)

View File

@@ -0,0 +1,157 @@
"""
子图整合主图构建器
Subgraph Integration Main Graph Builder
"""
from langgraph.graph import StateGraph, START, END
from typing import Dict, Any
from .state import MainGraphState, CurrentAction
from ..agent_subgraphs.contact import build_contact_subgraph
from ..agent_subgraphs.dictionary import build_dictionary_subgraph
from ..agent_subgraphs.news_analysis import build_news_analysis_subgraph
def parse_user_intent(state: MainGraphState) -> MainGraphState:
"""
解析用户意图节点
确定该路由到哪个子图
"""
state.current_phase = "intent_parsing"
# 从messages中提取用户查询如果user_query为空
if not state.user_query and state.messages:
# 获取最后一条消息的内容
last_msg = state.messages[-1]
state.user_query = last_msg.content
query_lower = state.user_query.lower()
# 简单的关键词匹配
if any(keyword in query_lower for keyword in ["通讯录", "联系人", "contact", "email"]):
state.current_action = CurrentAction.CONTACT
state.intent_confidence = 0.9
elif any(keyword in query_lower for keyword in ["词典", "单词", "翻译", "dictionary", "translate"]):
state.current_action = CurrentAction.DICTIONARY
state.intent_confidence = 0.9
elif any(keyword in query_lower for keyword in ["资讯", "新闻", "分析", "news", "report"]):
state.current_action = CurrentAction.NEWS_ANALYSIS
state.intent_confidence = 0.9
else:
# 默认是普通聊天
state.current_action = CurrentAction.GENERAL_CHAT
state.intent_confidence = 0.8
return state
def route_to_subgraph(state: MainGraphState) -> str:
"""
条件路由:决定路由到哪个子图
"""
if state.current_action == CurrentAction.NONE:
return "general_chat"
elif state.current_action == CurrentAction.GENERAL_CHAT:
return "general_chat"
elif state.current_action == CurrentAction.CONTACT:
return "contact_subgraph"
elif state.current_action == CurrentAction.DICTIONARY:
return "dictionary_subgraph"
elif state.current_action == CurrentAction.NEWS_ANALYSIS:
return "news_analysis_subgraph"
else:
return "general_chat"
def general_chat_node(state: MainGraphState) -> MainGraphState:
"""
普通聊天节点
目前是占位符后续整合旧的LLM调用逻辑
"""
state.current_phase = "general_chat"
state.final_result = f"普通聊天模式:{state.user_query}"
state.success = True
return state
def integrate_results(state: MainGraphState) -> MainGraphState:
"""
整合子图结果节点
"""
state.current_phase = "integrating"
# 整合通讯录子图结果
if state.contact_result:
state.final_result = state.contact_result.get("final_result", "")
# 整合词典子图结果
elif state.dictionary_result:
state.final_result = state.dictionary_result.get("final_result", "")
# 整合资讯子图结果
elif state.news_result:
state.final_result = state.news_result.get("final_result", "")
else:
# 没有子图结果
if not state.final_result:
state.final_result = "处理完成"
state.current_phase = "done"
return state
def build_main_graph() -> StateGraph:
"""
构建整合了子图的主图
Returns:
配置好的 StateGraph
"""
# 创建图
graph = StateGraph(MainGraphState)
# 添加节点
graph.add_node("parse_intent", parse_user_intent)
graph.add_node("general_chat", general_chat_node)
graph.add_node("integrate_results", integrate_results)
# 添加子图节点
contact_graph = build_contact_subgraph()
dictionary_graph = build_dictionary_subgraph()
news_analysis_graph = build_news_analysis_subgraph()
graph.add_node("contact_subgraph", contact_graph.compile())
graph.add_node("dictionary_subgraph", dictionary_graph.compile())
graph.add_node("news_analysis_subgraph", news_analysis_graph.compile())
# 添加边
# 从START开始
graph.add_edge(START, "parse_intent")
# 从parse_intent根据条件路由
graph.add_conditional_edges(
"parse_intent",
route_to_subgraph,
{
"general_chat": "general_chat",
"contact_subgraph": "contact_subgraph",
"dictionary_subgraph": "dictionary_subgraph",
"news_analysis_subgraph": "news_analysis_subgraph",
}
)
# 从普通聊天和子图到结果整合
graph.add_edge("general_chat", "integrate_results")
graph.add_edge("contact_subgraph", "integrate_results")
graph.add_edge("dictionary_subgraph", "integrate_results")
graph.add_edge("news_analysis_subgraph", "integrate_results")
# 最终到END
graph.add_edge("integrate_results", END)
return graph