(1)使用langgrahp.rpebuilt中的tools_condition
from langgraph.prebuilt import tools_condition
builder.add_conditional_edges('llm节点',tools_condition)
(2)自定义 条件边
#(1)自定义路由函数 def rout_func(state:State): if hasattr(state,'messages'): messages = state.messages elif isinstance(state,dict) and 'messages' in state: messages = state.get('messages', []) else: raise ValueError(f"无法从状态中获取到messages:{state}") last_message = messages[-1] if hasattr(last_message,'tool_calls') and len(last_message.tool_calls)>0: return 'tools' #(2)条件边 builder.add_conditional_edges( 'llm节点', rout_func, path_map={ 'tools':'tools', END:END } )