101 lines
2.7 KiB
Markdown
101 lines
2.7 KiB
Markdown
# 构建并行工作流
|
||
|
||
现在您将创建一个以最大性能并行运行您分析步骤的工作流。
|
||
|
||
## 创建并行工作流
|
||
|
||
将此工作流添加到您的文件中:
|
||
|
||
```typescript
|
||
export const parallelAnalysisWorkflow = createWorkflow({
|
||
id: "parallel-analysis-workflow",
|
||
description: "Run multiple content analyses in parallel",
|
||
inputSchema: z.object({
|
||
content: z.string(),
|
||
type: z.enum(["article", "blog", "social"]).default("article"),
|
||
}),
|
||
outputSchema: z.object({
|
||
results: z.object({
|
||
seo: z.object({
|
||
seoScore: z.number(),
|
||
keywords: z.array(z.string()),
|
||
}),
|
||
readability: z.object({
|
||
readabilityScore: z.number(),
|
||
gradeLevel: z.string(),
|
||
}),
|
||
sentiment: z.object({
|
||
sentiment: z.enum(["positive", "neutral", "negative"]),
|
||
confidence: z.number(),
|
||
}),
|
||
}),
|
||
}),
|
||
})
|
||
.parallel([seoAnalysisStep, readabilityStep, sentimentStep])
|
||
.then(
|
||
createStep({
|
||
id: "combine-results",
|
||
description: "Combines parallel analysis results",
|
||
inputSchema: z.object({
|
||
"seo-analysis": z.object({
|
||
seoScore: z.number(),
|
||
keywords: z.array(z.string()),
|
||
}),
|
||
"readability-analysis": z.object({
|
||
readabilityScore: z.number(),
|
||
gradeLevel: z.string(),
|
||
}),
|
||
"sentiment-analysis": z.object({
|
||
sentiment: z.enum(["positive", "neutral", "negative"]),
|
||
confidence: z.number(),
|
||
}),
|
||
}),
|
||
outputSchema: z.object({
|
||
results: z.object({
|
||
seo: z.object({
|
||
seoScore: z.number(),
|
||
keywords: z.array(z.string()),
|
||
}),
|
||
readability: z.object({
|
||
readabilityScore: z.number(),
|
||
gradeLevel: z.string(),
|
||
}),
|
||
sentiment: z.object({
|
||
sentiment: z.enum(["positive", "neutral", "negative"]),
|
||
confidence: z.number(),
|
||
}),
|
||
}),
|
||
}),
|
||
execute: async ({ inputData }) => {
|
||
console.log("🔄 Combining parallel results...");
|
||
|
||
return {
|
||
results: {
|
||
seo: inputData["seo-analysis"],
|
||
readability: inputData["readability-analysis"],
|
||
sentiment: inputData["sentiment-analysis"],
|
||
},
|
||
};
|
||
},
|
||
}),
|
||
)
|
||
.commit();
|
||
```
|
||
|
||
## 理解并行数据流
|
||
|
||
当步骤并行运行时:
|
||
|
||
1. 每个步骤接收相同的输入数据
|
||
2. 步骤同时执行
|
||
3. 结果被收集到以步骤ID为键的对象中
|
||
4. 下一步接收所有并行结果
|
||
|
||
## 关键点
|
||
|
||
- **`.parallel([step1, step2, step3])`**:同时运行所有步骤
|
||
- **结果对象键**:使用步骤ID(例如"seo-analysis")
|
||
- **合并步骤**:一起处理所有并行结果
|
||
|
||
接下来,您将测试此并行工作流并看到性能提升!
|