Skip to main content

Mastra 快速入门

🌐 Mastra Quickstart

create mastra CLI 命令是入门最快的方法。它会引导你完成设置,并为你创建示例代理、工作流程和工具,方便你在本地运行或进行修改。

🌐 The create mastra CLI command is the quickest way to get started. It walks you through setup and creates example agents, workflows, and tools for you to run locally or adapt.

如果你需要对设置有更多控制,请参阅手动安装指南。你也可以在现有项目中使用mastra init

🌐 If you need more control over the setup, see the manual installation guide. You can also use mastra init for existing projects.

在你开始之前
Direct link to 在你开始之前

🌐 Before you begin

  • 你需要从支持的模型提供商获取一个 API 密钥。如果你没有偏好,可以使用OpenAI

初始化 Mastra
Direct link to 初始化 Mastra

🌐 Initialize Mastra

你可以在你的计算机上的任何地方运行 create mastra

🌐 You can run create mastra anywhere on your machine.

在提示时,选择一个提供商(例如 OpenAI)并输入你的密钥:

🌐 When prompted, choose a provider (e.g. OpenAI) and enter your key:

npm create mastra@latest

这会为你的项目创建一个新目录,其中包含一个 src/mastra 文件夹,里面有一个示例天气代理和以下文件:

🌐 This creates a new directory for your project with a src/mastra folder containing an example weather agent and the following files:

  • index.ts - Mastra 配置,包括内存
  • tools/weather-tool.ts - 一个用于获取给定位置天气的工具
  • agents/weather-agent.ts - 一个使用该工具的天气代理及其提示
tip

你可以在 create mastra 中使用 flags,例如使用 --no-example 来跳过示例天气代理,或使用 --template 从特定的 模板 开始。

🌐 You can use flags with create mastra like --no-example to skip the example weather agent or --template to start from a specific template.

测试你的代理
Direct link to 测试你的代理

🌐 Test your agent

设置完成后,请按照终端中的指示启动 Mastra 开发服务器,然后在 localhost:4111 打开 Studio。

🌐 Once setup is complete, follow the instructions in your terminal to start the Mastra dev server, then open Studio at localhost:4111.

试着询问天气。如果你的 API 密钥设置正确,你将收到一个回应:

🌐 Try asking about the weather. If your API key is set up correctly, you'll get a response:

Studio 让你可以快速构建和原型化代理,无需创建用户界面。一旦准备就绪,你可以使用下面的指南将你的 Mastra 代理集成到应用中。

下一步
Direct link to 下一步

🌐 Next steps