工作流-编排器-工人工作流

流程说明

这个工作流可以看作是ParallelizationWorkflow的自动分配任务版本

中央LLM动态分解任务, 并将其委派给工作者LLM, 并综合结果

  1. 用户输入任务描述input
  2. 将用户的任务和编排器prompt输入给LLM用于划分任务
  3. 将划分后的任务集合和工人prompt输入给LLM用于执行划分后的任务
  4. 返回所有的执行结果

代码

执行代码, process

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public FinalResponse process(String taskDescription) {
// 获取编排器的编排结果
OrchestratorResponse orchestratorResponse = this.chatClient.prompt()
.user(u -> u.text(this.orchestratorPrompt))
.param("task", taskDescription)
.call()
.entity(OrchestratorResponse.class);

// 执行每个任务
List<String> workerResponse = orchestratorResponse.tasks().stram().map(task -> this.chatClient.prompt()
.user(u -> u.text(this.workerPrompt)
.param("original_task", taskDescription)
.param("task_type", task.type())
.param("task_description", task.description())
))
.call()
.content().toList();

return new FinalResponse(orchestratorResponse.analysis(), workerResponse);
}

orchestrator prompt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""
Analyze this task and break it down into 2-3 distinct approaches:

Task: {task}

Return your response in this JSON format:
\\{
"analysis": "Explain your understanding of the task and which variations would be valuable.
Focus on how each approach serves different aspects of the task.",
"tasks": [
\\{
"type": "formal",
"description": "Write a precise, technical version that emphasizes specifications"
\\},
\\{
"type": "conversational",
"description": "Write an engaging, friendly version that connects with readers"
\\}
]
\\}
""

worker

1
2
3
4
5
6
"""
Generate content based on:
Task: {original_task}
Style: {task_type}
Guidelines: {task_description}
"""

适用场景

非常适合无法预测所需子任务的复杂任务, 在拓扑结构上和并行化类似, 和并行化的关键区别是在于灵活性-子任务不需要预先定义, 而是由编排器根据具体的输入确定

原文:

When to use this workflow: This workflow is well-suited for complex tasks where you can’t predict the subtasks needed (in coding, for example, the number of files that need to be changed and the nature of the change in each file likely depend on the task). Whereas it’s topographically similar, the key difference from parallelization is its flexibility—subtasks aren’t pre-defined, but determined by the orchestrator based on the specific input.

  • 每次对多个文件进行复杂更改的编码产品
  • 搜索任务涉及从多个来源搜集和分析信息以获取可能相关的信息