translation/translated/documents/course/04-workflows/11-creating-an-ai-agent.md

68 lines
2.1 KiB
Markdown
Raw Normal View History

# 创建AI代理
学习如何创建一个可以在您的工作流内使用的Mastra代理以实现更智能的内容处理。
## 创建内容分析代理
`src/mastra/agents` 目录中为您的代理创建一个新文件。使用 `content-agent.ts` 作为文件名,内容如下:
```typescript
// src/mastra/agents/content-agent.ts
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
export const contentAgent = new Agent({
name: "Content Agent",
description: "AI agent for analyzing and improving content",
instructions: `
You are a professional content analyst. Your role is to:
1. Analyze content for clarity and engagement
2. Identify the main themes and topics
3. Provide a quality score from 1-10
4. Suggest specific improvements
Always provide constructive, actionable feedback.
`,
model: openai("gpt-4o-mini"),
});
```
## 理解代理
- **名称**:代理的唯一标识符
- **描述**:代理的作用
- **指令**指导AI行为的详细提示
- **模型**使用哪个AI模型GPT-4o-mini快速且经济高效
## 注册和测试您的代理
打开您的 `src/mastra/index.ts` 文件并添加您的代理您可能需要将其附加到Mastra类中的代理对象
```typescript
// Import your workflow
import { contentAgent } from "./agents/content-agent";
export const mastra = new Mastra({
// Register your agent here
agents: {
contentAgent,
},
// ...Existing code
});
```
您可以通过导航到代理选项卡并选择 `content-agent` 在操练场中测试此代理。使用聊天界面验证代理是否正常工作。
代理应提供内容分析,包括主题、质量评估和改进建议。
## 为什么在工作流中使用代理?
代理通过以下方式为工作流增加智能:
- **理解上下文**AI可以解释含义而不仅仅是处理数据
- **生成洞察**:提供简单逻辑无法提供的分析
- **适应响应**:根据内容类型给出不同的反馈
- **自然语言输出**:以人类可读的形式传达结果
您的AI代理已准备就绪接下来您将学习如何将其集成到工作流步骤中。