92 lines
2.4 KiB
Markdown
92 lines
2.4 KiB
Markdown
# 在工作流中使用代理
|
||
|
||
现在您将创建一个使用您的AI代理提供智能内容分析的工作流步骤。
|
||
|
||
在每个步骤中,在执行函数中,您可以访问 `mastra` 类,该类为您提供访问代理、工具甚至其他工作流的能力。在这种情况下,我们使用 `mastra` 类来获取我们的代理并调用该代理的 `generate()` 函数。
|
||
|
||
## 创建AI分析步骤
|
||
|
||
将此步骤添加到您的工作流文件中:
|
||
|
||
```typescript
|
||
const aiAnalysisStep = createStep({
|
||
id: "ai-analysis",
|
||
description: "AI-powered content analysis",
|
||
inputSchema: z.object({
|
||
content: z.string(),
|
||
type: z.string(),
|
||
wordCount: z.number(),
|
||
metadata: z.object({
|
||
readingTime: z.number(),
|
||
difficulty: z.enum(["easy", "medium", "hard"]),
|
||
processedAt: z.string(),
|
||
}),
|
||
summary: z.string(),
|
||
}),
|
||
outputSchema: z.object({
|
||
content: z.string(),
|
||
type: z.string(),
|
||
wordCount: z.number(),
|
||
metadata: z.object({
|
||
readingTime: z.number(),
|
||
difficulty: z.enum(["easy", "medium", "hard"]),
|
||
processedAt: z.string(),
|
||
}),
|
||
summary: z.string(),
|
||
aiAnalysis: z.object({
|
||
score: z.number(),
|
||
feedback: z.string(),
|
||
}),
|
||
}),
|
||
execute: async ({ inputData, mastra }) => {
|
||
const { content, type, wordCount, metadata, summary } = inputData;
|
||
|
||
// Create prompt for the AI agent
|
||
const prompt = `
|
||
Analyze this ${type} content:
|
||
|
||
Content: "${content}"
|
||
Word count: ${wordCount}
|
||
Reading time: ${metadata.readingTime} minutes
|
||
Difficulty: ${metadata.difficulty}
|
||
|
||
Please provide:
|
||
1. A quality score from 1-10
|
||
2. Brief feedback on strengths and areas for improvement
|
||
|
||
Format as JSON: {"score": number, "feedback": "your feedback here"}
|
||
`;
|
||
|
||
// Get the contentAgent from the mastra instance.
|
||
const contentAgent = mastra.getAgent("contentAgent");
|
||
const { text } = await contentAgent.generate([
|
||
{ role: "user", content: prompt },
|
||
]);
|
||
|
||
// Parse AI response (with fallback)
|
||
let aiAnalysis;
|
||
try {
|
||
aiAnalysis = JSON.parse(text);
|
||
} catch {
|
||
aiAnalysis = {
|
||
score: 7,
|
||
feedback: "AI analysis completed. " + text,
|
||
};
|
||
}
|
||
|
||
console.log(`🤖 AI Score: ${aiAnalysis.score}/10`);
|
||
|
||
return {
|
||
content,
|
||
type,
|
||
wordCount,
|
||
metadata,
|
||
summary,
|
||
aiAnalysis,
|
||
};
|
||
},
|
||
});
|
||
```
|
||
|
||
您的代理驱动步骤已准备就绪!接下来,您将将其添加到您的工作流中以实现完整的AI增强内容处理。
|