translation/translated/documents/course/04-workflows/15-creating-parallel-steps.md

116 lines
3.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 创建并行步骤
让我们创建三个可以同时运行的分析步骤来分析内容的不同方面。
## 创建分析步骤
将这三种步骤添加到您的工作流文件中:
```typescript
// SEO Analysis
const seoAnalysisStep = createStep({
id: "seo-analysis",
description: "SEO optimization analysis",
inputSchema: z.object({
content: z.string(),
type: z.enum(["article", "blog", "social"]).default("article"),
}),
outputSchema: z.object({
seoScore: z.number(),
keywords: z.array(z.string()),
}),
execute: async ({ inputData }) => {
console.log("🔍 Running SEO analysis...");
await new Promise((resolve) => setTimeout(resolve, 800));
const words = inputData.content.toLowerCase().split(/\s+/);
const keywords = words.filter((word) => word.length > 4).slice(0, 3);
return {
seoScore: Math.floor(Math.random() * 40) + 60,
keywords,
};
},
});
// Readability Analysis
const readabilityStep = createStep({
id: "readability-analysis",
description: "Content readability analysis",
inputSchema: z.object({
content: z.string(),
type: z.enum(["article", "blog", "social"]).default("article"),
}),
outputSchema: z.object({
readabilityScore: z.number(),
gradeLevel: z.string(),
}),
execute: async ({ inputData }) => {
console.log("📖 Running readability analysis...");
await new Promise((resolve) => setTimeout(resolve, 600));
const sentences = inputData.content.split(/[.!?]+/).length;
const words = inputData.content.split(/\s+/).length;
const avgWordsPerSentence = words / sentences;
const score = Math.max(0, 100 - avgWordsPerSentence * 3);
const gradeLevel = score > 80 ? "Easy" : score > 60 ? "Medium" : "Hard";
return {
readabilityScore: Math.floor(score),
gradeLevel,
};
},
});
// Sentiment Analysis
const sentimentStep = createStep({
id: "sentiment-analysis",
description: "Content sentiment analysis",
inputSchema: z.object({
content: z.string(),
type: z.enum(["article", "blog", "social"]).default("article"),
}),
outputSchema: z.object({
sentiment: z.enum(["positive", "neutral", "negative"]),
confidence: z.number(),
}),
execute: async ({ inputData }) => {
console.log("😊 Running sentiment analysis...");
await new Promise((resolve) => setTimeout(resolve, 700));
const content = inputData.content.toLowerCase();
const positiveWords = ["good", "great", "excellent", "amazing"];
const negativeWords = ["bad", "terrible", "awful", "horrible"];
const positive = positiveWords.filter((word) =>
content.includes(word),
).length;
const negative = negativeWords.filter((word) =>
content.includes(word),
).length;
let sentiment: "positive" | "neutral" | "negative" = "neutral";
if (positive > negative) sentiment = "positive";
if (negative > positive) sentiment = "negative";
return {
sentiment,
confidence: Math.random() * 0.3 + 0.7, // 0.7-1.0
};
},
});
```
## 注意时间
每个步骤都有不同的模拟处理时间:
- SEO800毫秒
- 可读性600毫秒
- 情感700毫秒
当顺序运行时总时间约为2.2秒。当并行运行时总时间约为800毫秒最长步骤
接下来,您将学习如何使用 `.parallel()` 方法并行运行这些步骤。