工作流-lian’shi

参考

  1. https://www.anthropic.com/engineering/building-effective-agents

流程说明

对于链式工作流, 整体的呈现效果就是用户输入userInput以后, 这个userInput在输入以后会链式的向下执行, 在每个节点携带上一个节点的回答和使用当前节点的prompt来和LLM对话

  1. 填充systemPrompts数组, 在这种情况下, Chain Workflow也被称作Prompts Chain工作流
  2. 遍历每个prompt
    1. 将上一轮的回答和现在的prompt组成一个新的input
    2. 对话获取response
  3. 输出最后的response

img

代码

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public String chain(String userInput) {
String response = usertInput;

for (String promt : systemPrompts) {
String input = String.format("{%s}\n {%s}", prompt, response);

String response = chatClient.prompt(input).call().conmtent();

if(!check(response)) {
return this.errorResponse;
}
}

return response;
}

系统prompt示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Step 1
"""
Extract only the numerical values and their associated metrics from the text.
Format each as'value: metric' on a new line.
Example format:
92: customer satisfaction
45%: revenue growth""",
// Step 2
"""
Convert all numerical values to percentages where possible.
If not a percentage or points, convert to decimal (e.g., 92 points -> 92%).
Keep one number per line.
Example format:
92%: customer satisfaction
45%: revenue growth""",
// Step 3
"""
Sort all lines in descending order by numerical value.
Keep the format 'value: metric' on each line.
Example:
92%: customer satisfaction
87%: employee satisfaction""",
// Step 4
"""
Format the sorted data as a markdown table with columns:
| Metric | Value |
|:--|--:|
| Customer Satisfaction | 92% | """

使用场景

此工作流非常适合于任务可以轻松明晰地分解成多个固定子步骤的场景. 用延迟换取更高的准确性

原文:

When to use this workflow: This workflow is ideal for situations where the task can be easily and cleanly decomposed into fixed subtasks. The main goal is to trade off latency for higher accuracy, by making each LLM call an easier task.

举例说明:

  • 撰写文章大纲, 检查大纲是否符合某些标准, 然后根据大纲完成文档
  • 从MCP中获取应用监控数据, 提取出来关键信息, 捕捉其中的异常, 推送给管理者