🔀 条件路由与并行调用
# 用 IF 做条件路由
根据业务条件选择不同的 Agent 处理请求:
<chain name="routerChain">
THEN(
prepare,
IF(isMath, mathAgent, deepseekAgent),
recordReply
);
</chain>
isMath 是普通的 NodeBooleanComponent,mathAgent 和 deepseekAgent 都是 ReActAgentComponent 子类。
@LiteflowComponent("isMath")
public class IsMathCmp extends NodeBooleanComponent {
@Override
public boolean processBoolean() throws Exception {
String input = (String) this.getSlot().getChainReqData(getSlot().getChainId());
return input != null && input.contains("计算");
}
}
# 用 WHEN 并行调用多个模型
同时调用多个模型的 Agent 进行分析:
<chain name="parallelChain">
THEN(
prepare,
WHEN(deepseekAgent, dashscopeAgent).maxWaitSeconds(60),
recordReply
);
</chain>
默认情况下,同一条 chain 内的多个 Agent 共享同一个 conversationId,但因为 agentKey() 默认是各自的 nodeId,所以它们会使用不同的 Session、不同的锁和不同的 memory,可以并行执行。
并行注意事项
如果多个 Agent 覆写为相同的 agentKey(),它们会因为同一把 Session 锁而串行执行。要真正隔离并行,确保 (conversationId, agentKey) 组合不同:
@Override
protected String agentKey() {
return getNodeId() + "-" + getSlot().getRequestId();
}
# 复杂编排模式
<!-- 准备 → 并行分析 → 汇总决策 → 后处理 -->
THEN(
prepare,
WHEN(analyzerAgent, riskAgent),
summaryAgent,
notify
);
<!-- 根据条件选择不同处理链 -->
SWITCH(routeCmp).to(
THEN(agentA, processA),
THEN(agentB, processB),
THEN(agentC, processC)
);
<!-- 循环处理直到满足条件 -->
FOR(processAgent).doWhile(checkResult);
帮助我们改善此文档 (opens new window)
上次更新: 2026/05/24, 14:26:24


