Copy # 伪代码示例:用“图编排”构建带人工审核的客服智能体
# 1) 定义状态(包含消息、用户标识、是否需要人工审核等字段)
class CustomerServiceState(TypedDict):
messages: list
customer_id: str
requires_human: bool
# 2) 定义工具(查询订单、申请退款等)
tools = [get_order_status, request_refund]
# 3) 定义节点(智能体节点、工具执行节点、人工审核节点)
def agent_node(state):
response = llm.decide_next(messages=state["messages"], tools=tools)
requires_human = any(call.name == "request_refund" for call in response.tool_calls)
return {"messages": state["messages"] + [response], "requires_human": requires_human}
def tools_node(state):
tool_results = execute_tool_calls(state["messages"][-1].tool_calls)
return {"messages": state["messages"] + tool_results}
def human_review_node(state):
# 在此暂停,等待人类批准/拒绝/修改参数
decision = wait_for_human()
return apply_human_decision(state, decision)
# 4) 构建图:agent -> (tools | human_review | end)
app = Graph(State=CustomerServiceState)
app.add_node("agent", agent_node)
app.add_node("tools", tools_node)
app.add_node("human_review", human_review_node)
app.add_conditional_edges("agent", router=route)
app.add_edge("tools", "agent")
app.add_edge("human_review", "agent")
app.enable_checkpoint_and_interrupt(before=["human_review"]) # 支持中断/恢复