工作流-路由工作流

流程说明

用户输入userInput和可行的路由Map<String, Object> String是对这个路由的简介, Object就是实际的路由内容

这里以角色分配, 将不同的任务分配给不同的promt角色的使用方式举例

  1. 用户输入初始的userInput和Map<String, String>
  2. 根据userInput和Map的keySet让LLM决策当前任务使用的prompt角色, 给出原因和选择, 使用结构化的输出到RoutingResponse中
  3. 使用对应的prompt角色, 输出最后的结果

代码

  • route方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public String route(String userInput, Map<String, Object> routes, String mode) {
RoutingResponse routingResponse = determineRoute(input, routes.keySet);

Object select = routes.get(routingResponse.getSelect());

switch (mode) {
case "prompts":
// 选择不同的prompt解决这个问题
return chatClient.prompt((String) select + "\nInput: " + userInput).call().content();
case "modelChoose":
// 选择不同的client解决这个问题
return (chatClient) select.prompt(userInput).call().content();
}
}
  • determineRoute方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private String determineRoute(String input, Iterable<String> availableRoutes) {
String selectorPrompt = String.format("""
Analyze the input and select the most appropriate support team from these options: %s
First explain your reasoning, then provide your selection in this JSON format:

\\{
"reasoning": "Brief explanation of why this ticket should be routed to a specific team.
Consider key terms, user intent, and urgency level.",
"selection": "The chosen team name"
\\}

Input: %s""", availableRoutes, input);

RoutingResponse routingResponse = chatClient.prompt(selectorPrompt).call().entity(RoutingResponse.class);
return routingResponse.selection();
}

适用场景

适合处理存在不同的分类, 分别处理的复杂任务

原文:

When to use this workflow: Routing works well for complex tasks where there are distinct categories that are better handled separately, and where classification can be handled accurately, either by an LLM or a more traditional classification model/algorithm.

  • 将简单/常见问题路由到较小的模型(如 Claude 3.5 Haiku),将困难/不寻常的问题路由到功能更强大的模型(如 Claude 3.5 Sonnet),以优化成本和速度。
  • 将不同类型的客户服务查询(一般问题、退款请求、技术支持)引导到不同的下游流程、提示和工具中。