构建你的首个AI代理:GitHub仓库分析器⚡
仓库地址

欢迎来到您的首个VoltAgent项目!
本教程将带您构建一个实用的GitHub仓库分析AI智能体系统。我们将获取项目星标数和贡献者列表,然后使用另一个智能体来分析这些信息。
此示例展示了VoltAgent多智能体架构的强大功能。
VoltAgent是什么?
VoltAgent 是一个开源的 TypeScript 框架,扮演着这一核心工具包的角色。它通过提供模块化构建块、标准化模式和抽象层,简化了 AI 智能体应用的开发流程。无论您是要开发聊天机器人、虚拟助手、自动化工作流,还是复杂的多智能体系统,VoltAgent 都能处理底层复杂性,让您专注于定义智能体的功能与逻辑。
先决条件
在我们开始之前,请确保您已具备以下条件:
-
已安装 Node.js(推荐使用 LTS 版本)。
-
一个OpenAI API密钥(或其他支持的LLM提供商的API密钥)。
创建您的VoltAgent项目
首先,让我们新建一个VoltAgent项目。我们将使用create-voltagent-app命令行工具快速启动。打开终端并运行:
npm create voltagent-app@latest github-repo-analyzer
按照提示操作:
- 选择您偏好的包管理器(npm、yarn 或 pnpm)。
安装完成后,请进入新创建的项目目录:
cd github-repo-analyzer
并在项目根目录下创建一个 .env 文件来存储您的 API 密钥:
//.env
OPENAI_API_KEY=sk-proj-xxxxx
替换 sk-proj-xxxxx 为你的真实 OpenAI API 密钥。
理解目标
我们的目标是创建一个代理系统,该系统接收一个GitHub仓库URL(如https://github.com/voltagent/voltagent 或简写为voltagent/voltagent),并根据其星标数和贡献者数量进行分析。
为此,我们将采用监督者-工作者模式:
-
监督代理(Supervisor Agent):接收用户输入(仓库URL)并协调工作。
-
星标获取代理:负责获取代码仓库的星标数量。
-
贡献者获取代理:获取该代码库的贡献者列表。
-
分析代理:接收星标数和贡献者列表,并生成洞察报告。
工具配置(概念篇)
智能体通常需要工具来与外部世界交互(例如API)。在实际应用中,您会定义工具来从GitHub API获取数据。在本教程中,假设我们有两个预构建工具:
-
fetchRepoStarsTool:一个工具,接收一个仓库名称(例如 voltagent/core)并返回其星标数量。
-
fetchRepoContributorsTool: 一个工具,接收仓库名称并返回贡献者列表。
假设这些工具定义在一个单独的文件中,例如 src/tools.ts。我们会将它们导入到主代理文件中。
定义代理
现在,让我们在 src/index.ts 中定义我们的代理。打开这个文件,并将其内容替换为以下代码:
// src/index.ts
import { VoltAgent, Agent } from "@voltagent/core";
import { VercelAIProvider } from "@voltagent/vercel-ai";
import { openai } from "@ai-sdk/openai";
// Assume these tools are defined elsewhere (e.g., src/tools.ts)
// import { fetchRepoContributorsTool, fetchRepoStarsTool } from "./tools";
// --- Mock Tools for Demonstration ---// In a real scenario, you'd use actual tool implementations.
// We use simple functions here to illustrate agent structure.
const mockFetchRepoStarsTool = {
name: "fetchRepoStars",
description: "Fetches the star count for a given GitHub repository (owner/repo).",
parameters: {
type: "object",
properties: {
repo: { type: "string", description: 'Repository name (e.g., "voltagent/core")' },
},
required: ["repo"],
},
execute: async ({ repo }: { repo: string }) => ({ stars: Math.floor(Math.random() * 5000) }), // Mock data
};
const mockFetchRepoContributorsTool = {
name: "fetchRepoContributors",
description: "Fetches the contributors for a given GitHub repository (owner/repo).",
parameters: {
type: "object",
properties: {
repo: { type: "string", description: 'Repository name (e.g., "voltagent/core")' },
},
required: ["repo"],
},
execute: async ({ repo }: { repo: string }) => ({ contributors: ["UserA", "UserB", "UserC"] }), // Mock data
};
// --- End Mock Tools ---
// 1. Create the stars fetcher agent
const starsFetcherAgent = new Agent({
name: "StarsFetcher",
description: "Fetches the number of stars for a GitHub repository using a tool.",
llm: new VercelAIProvider(),
model: openai("gpt-4o-mini"),
tools: [mockFetchRepoStarsTool], // Use the mock tool
});
// 2. Create the contributors fetcher agent
const contributorsFetcherAgent = new Agent({
name: "ContributorsFetcher",
description: "Fetches the list of contributors for a GitHub repository using a tool.",
llm: new VercelAIProvider(),
model: openai("gpt-4o-mini"),
tools: [mockFetchRepoContributorsTool], // Use the mock tool
});
// 3. Create the analyzer agent (no tools needed)
const analyzerAgent = new Agent({
name: "RepoAnalyzer",
description: "Analyzes repository statistics (stars, contributors) and provides insights.",
llm: new VercelAIProvider(),
model: openai("gpt-4o-mini"),
// This agent doesn't need tools; it processes data provided by the supervisor.
});
// 4. Create the supervisor agent that coordinates all the sub-agents
const supervisorAgent = new Agent({
name: "Supervisor",
description: `You are a GitHub repository analyzer. When given a GitHub repository URL or owner/repo format, you will:
1. Extract the owner/repo name.
2. Use the StarsFetcher agent to get the repository's star count.
3. Use the ContributorsFetcher agent to get the repository's contributors.
4. Pass the collected data (stars, contributors) to the RepoAnalyzer agent.
5. Return the analysis provided by the RepoAnalyzer.
Example input: https://github.com/vercel/ai-sdk or vercel/ai-sdk
`,
llm: new VercelAIProvider(),
model: openai("gpt-4o-mini"),
subAgents: [starsFetcherAgent, contributorsFetcherAgent, analyzerAgent], // Assign sub-agents
});
// 5. Initialize the VoltAgent with the agent hierarchy
new VoltAgent({
agents: {
// We only expose the supervisor externally.
// The supervisor will internally call the other agents.
supervisor: supervisorAgent,
},
});
console.log("GitHub Repo Analyzer Agent system started.");
说明:
-
导入:我们从VoltAgent和AI SDK库中导入必要的组件。
-
模拟工具:为简化流程,我们已在此文件中直接添加了工具的模拟版本。在实际应用中,您应导入真实的工具实现。
-
starsFetcherAgent:定义为包含名称、描述、LLM配置和mockFetchRepoStarsTool。其职责仅在被询问时使用该工具。
-
贡献者获取代理:类似于星标获取器,但配置了模拟获取仓库贡献者工具
-
分析员代理(analyzerAgent):该代理无需使用工具,其核心职责是接收数据(如星标数和贡献者信息),并运用其大型语言模型能力,根据预设描述生成分析报告。
-
supervisorAgent:这是主要的协调者。
-
它的描述清晰地概述了需要采取的步骤。
-
关键在于,它在 subAgents 数组中包含了另外三个智能体。这向监督者表明可以将任务委托给这些特定的智能体。
- new VoltAgent(…): 这里初始化了VoltAgent系统。我们将supervisorAgent注册在supervisor键下。这意味着当我们与应用程序交互时,实际上是在直接与监管者(supervisor)进行对话。
运行您的智能体系统
现在,让我们运行这个代理程序。回到你的终端(确保你当前位于 github-repo-analyzer 目录下),然后执行开发命令:
npm run dev
您应该会看到VoltAgent服务器的启动消息:
══════════════════════════════════════════════════
VOLTAGENT SERVER STARTED SUCCESSFULLY
══════════════════════════════════════════════════
✓ HTTP Server: http://localhost:3141
Developer Console: https://console.voltagent.dev
══════════════════════════════════════════════════
与您的AI代理互动
-
打开控制台:访问 https://console.voltagent.dev。
-
找到您的代理:寻找名为 supervisor 的代理(或您在 new VoltAgent 调用中指定的其他名称)。
-
打开代理详情:点击主管代理。
-
开始聊天:点击聊天图标。
-
发送消息:尝试发送如下消息:
分析仓库 voltagent/voltagent
或
告诉我关于 https://github.com/voltagent/voltagent 的信息

监督代理现在将执行其指令:
-
它可能会首先调用 StarsFetcher 来获取(模拟的)星标数量。
-
接着,它将调用 ContributorsFetcher 来获取(模拟的)贡献者列表。
-
最后,系统会将此信息传递给 RepoAnalyzer,并将分析结果通过聊天界面返回给您。
你可以在VoltAgent开发者控制台中观察到这个多步骤的处理过程!

总结
恭喜!您已成功使用VoltAgent构建了一个多智能体系统。您学会了如何:
-
设置一个VoltAgent项目。
-
定义多个具备特定角色和工具(即使是模拟工具)的智能体。
-
创建一个监督者代理来协调子代理之间的任务。
-
通过开发者控制台运行并与您的智能体系统进行交互。
这个示例展示了如何将复杂任务拆解为更小、更易管理的单元,每个单元由专门的智能体处理。
后续步骤
-
使用GitHub API替换模拟工具,实现真实功能。
-
尝试完整示例:查看完整的 GitHub 仓库分析器示例,其中包含实际工具实现。您也可以直接基于此示例创建项目:
npm create voltagent-app@latest -- --example github-repo-analyzer
-
探索不同的LLM提供商与模型。
-
了解更多关于 智能体记忆 的功能,为您的智能体提供上下文支持。
gent/voltagent/tree/main/examples/github-repo-analyzer),其中包含实际工具实现。您也可以直接基于此示例创建项目:
npm create voltagent-app@latest -- --example github-repo-analyzer

1118

被折叠的 条评论
为什么被折叠?



