当前位置: 首页 > news >正文

LangGraph:add_conditional_edges详解

在 LangGraph 中,add_conditional_edges 是构建动态工作流的关键,用于创建基于条件判断的分支路径;它允许工作流根据当前状态动态决定下一步的执行路径,种模式使 LangGraph 能够处理复杂的、状态驱动的对话流程,特别是在需要工具调用和多次交互的场景中。

示例

    # State Managementclass State(TypedDict):messages: Annotated[List[AnyMessage], add_messages]
​# Nodesdef chat_node(state: State) -> State:state["messages"] = chat_llm.invoke({"messages": state["messages"]})return state
​# Building the graphgraph_builder = StateGraph(State)graph_builder.add_node("chat_node", chat_node)graph_builder.add_node("tool_node", ToolNode(tools=tools))graph_builder.add_edge(START, "chat_node")graph_builder.add_conditional_edges("chat_node", tools_condition, {"tools": "tool_node", "__end__": END})graph_builder.add_edge("tool_node", "chat_node")graph = graph_builder.compile(checkpointer=MemorySaver())return graph

解读:

上述示例的执行流程如下:

image

 

细节描述

执行工具节点

class ToolNode:def __init__(self, tools):self.tools = toolsdef __call__(self, state: State) -> State:# 执行工具调用tool_results = []for tool_call in state["messages"][-1].tool_calls:tool = self.tools[tool_call["name"]]result = tool.invoke(tool_call["args"])tool_results.append(result)return {"messages": tool_results}

状态更新:将工具执行结果作为新消息添加

 

工具节点执行后,通过 graph_builder.add_edge("tool_node", "chat_node") 返回聊天节点继续生成对工具结果的响应

 

重点关注

add_conditional_edges,这个方法包含三个核心参数

image

A、源节点:条件分支的起点
B、条件函数:决定分支路径的函数
C、分支映射:将条件函数返回值映射到目标节点的字典

 

条件函数

条件函数是一个自定义函数,它接收当前状态作为输入,返回一个字符串值,表示下一步应该执行的路径。

在上面示例中,tools_condition 函数可能类似这样:

def tools_condition(state: State) -> str:"""判断是否需要调用工具"""# 获取最后一条消息last_message = state["messages"][-1]# 检查是否是工具调用请求if hasattr(last_message, "tool_calls") and last_message.tool_calls:return "tools"  # 需要调用工具else:return "__end__"  # 结束对话

 

tools_condition(LangGraph源码)

image

 

image

 

def tools_condition(state: list[AnyMessage] | dict[str, Any] | BaseModel,messages_key: str = "messages",
) -> Literal["tools", "__end__"]:"""Conditional routing function for tool-calling workflows.This utility function implements the standard conditional logic for ReAct-styleagents: if the last `AIMessage` contains tool calls, route to the tool executionnode; otherwise, end the workflow. This pattern is fundamental to most tool-callingagent architectures.The function handles multiple state formats commonly used in LangGraph applications,making it flexible for different graph designs while maintaining consistent behavior.Args:state: The current graph state to examine for tool calls. Supported formats:- Dictionary containing a messages key (for `StateGraph`)- `BaseModel` instance with a messages attributemessages_key: The key or attribute name containing the message list in the state.This allows customization for graphs using different state schemas.Returns:Either `'tools'` if tool calls are present in the last `AIMessage`, or `'__end__'`to terminate the workflow. These are the standard routing destinations fortool-calling conditional edges.Raises:ValueError: If no messages can be found in the provided state format.Example:Basic usage in a ReAct agent:```pythonfrom langgraph.graph import StateGraphfrom langchain.tools import ToolNodefrom langchain.tools.tool_node import tools_conditionfrom typing_extensions import TypedDictclass State(TypedDict):messages: listgraph = StateGraph(State)graph.add_node("llm", call_model)graph.add_node("tools", ToolNode([my_tool]))graph.add_conditional_edges("llm",tools_condition,  # Routes to "tools" or "__end__"{"tools": "tools", "__end__": "__end__"},)```Custom messages key:```pythondef custom_condition(state):return tools_condition(state, messages_key="chat_history")```!!! noteThis function is designed to work seamlessly with `ToolNode` and standardLangGraph patterns. It expects the last message to be an `AIMessage` whentool calls are present, which is the standard output format for tool-callinglanguage models."""if isinstance(state, list):ai_message = state[-1]elif (isinstance(state, dict) and (messages := state.get(messages_key, []))) or (messages := getattr(state, messages_key, [])):ai_message = messages[-1]else:msg = f"No messages found in input state to tool_edge: {state}"raise ValueError(msg)if hasattr(ai_message, "tool_calls") and len(ai_message.tool_calls) > 0:return "tools"return "__end__"

 

分支映射

分支映射是一个字典,将条件函数的返回值映射到具体的节点或特殊端点:

{"tools": "tool_node",  # 当条件返回 "tools" 时,跳转到 tool_node"__end__": END         # 当条件返回 "__end__" 时,结束工作流
}

 

特殊端点:

  • START:工作流起点
  • END:工作流终点

 

条件分支的高级应用

多分支条件

可以创建包含多个可能路径的条件分支

def advanced_condition(state: State) -> str:last_message = state["messages"][-1]if "help" in last_message.content:return "help_flow"elif "purchase" in last_message.content:return "checkout_flow"elif "cancel" in last_message.content:return "cancellation_flow"else:return "__end__"graph_builder.add_conditional_edges("chat_node",advanced_condition,{"help_flow": "help_node","checkout_flow": "checkout_node","cancellation_flow": "cancellation_node","__end__": END}
)

 

嵌套条件分支

# 第一层条件分支
graph_builder.add_conditional_edges("initial_node",determine_flow_type,{"support": "support_flow", "sales": "sales_flow"}
)# 支持流中的子分支
graph_builder.add_conditional_edges("support_flow",support_condition,{"technical": "tech_support_node", "billing": "billing_support_node"}
)# 销售流中的子分支
graph_builder.add_conditional_edges("sales_flow",sales_condition,{"new": "new_customer_node", "existing": "existing_customer_node"}
)

 

最佳实践

保持条件函数纯净

只读取状态,不修改状态
避免副作用

明确的返回值

使用描述性的字符串作为返回值
确保返回值在分支映射中有对应项

错误处理

def safe_condition(state: State) -> str:try:# 业务逻辑except Exception as e:# 记录错误state["errors"].append(str(e))return "error_handling"

状态设计

确保状态包含条件判断所需的所有信息
使用清晰的字段命名

http://www.zskr.cn/news/66209.html

相关文章:

  • 2025年中国水轮泵十大品牌推荐:水轮泵哪家性价比高
  • 【超音速专利 CN118134841A】一种光伏产品缺陷检测AI深度学习算法 - 教程
  • 2025东北优良大豆种子TOP5权威推荐:甄选优质品种助力农
  • 2025年十大GEO推广优化专业公司排行榜,新测评精选推荐
  • P10467 [CCC 2007] Snowflake Snow Snowflakes 题解
  • VSCode 常用快捷键/命令大全
  • 2025年十大广东机械设备源头厂家排行榜,新测评精选源头制造
  • 毕业生找工作TOP5权威推荐:精准破局求职困境,助力毕业生高
  • 2025苯板造型供应商TOP5权威推荐:甄选定制厂家,助力保
  • nestjs集成grpc服务 - 指南
  • 0,1序列
  • 提升开发效率的关键:Python 在工程应用中的五大实战技巧
  • 2025年十大专业的活动策划专业公司推荐,实力强的活动策划公
  • 2025年哈尔滨苯板立体雕刻加工厂/制造厂哪家更值得选?
  • 2025年黑龙江苯板雕刻制造商推荐:苯板雕刻优质供应商和生产
  • 实用指南:实验十三 Z-buffer算法实验
  • 关于python更换永久镜像源
  • 2025年电力在线监测系统推荐制造商:高性价比供应商与工厂深
  • 2025年国内防爆接地箱供应商推荐:接地箱定制靠谱企业有哪些
  • 实用指南:攻防世界-cat_cat_new(任意文件读取、Linux敏感文件、flask-session伪造)
  • 2025简历撰写网站TOP5权威推荐:资质齐全信誉好的简历平
  • `pytest + YAML + Allure` 的接口自动化测试框架是业界广泛运用的组合
  • 2025年口碑好的儿童保温杯/设计感保温杯实力厂家TOP推荐榜
  • AI元人文构想:未来的算法规制——从“代码律法”到“价值共生”
  • 2025年比较好的打包箱高评价厂家推荐榜
  • 2025年比较好的加装电梯TOP品牌厂家排行榜
  • 2025年比较好的双层保温饭盒厂家实力及用户口碑排行榜
  • 2025年热门的日本留学签证/日本留学官方认可榜
  • 2025年江浙沪老牌美术高中推荐:美术高中服务怎么联系?哪家
  • 2025年度十大混凝土密封固化剂专业供应商排行榜,新测评精选