text stringlengths 0 59.1k |
|---|
And create a `.env` file in the root of the project to store your API key: |
```bash title=".env" |
OPENAI_API_KEY=sk-proj- |
``` |
Replace `sk-proj-` with your actual OpenAI API key. |
## Step 2: Understanding the Goal |
Our goal is to create an agent system that takes a GitHub repository URL (like `https://github.com/voltagent/voltagent` or simply `voltagent/voltagent`) and provides an analysis based on its star count and contributors. |
To achieve this, we'll use a supervisor-worker pattern: |
1. **Supervisor Agent:** Takes the user's input (the repo URL) and coordinates the work. |
2. **Stars Fetcher Agent:** Fetches the star count for the repo. |
3. **Contributors Fetcher Agent:** Fetches the list of contributors for the repo. |
4. **Analyzer Agent:** Takes the star count and contributor list and generates insights. |
## Step 3: Setting Up the Tools (Conceptual) |
Agents often need tools to interact with the outside world (like APIs). In a real application, you would define tools to fetch data from the GitHub API. For this tutorial, imagine we have two pre-built tools: |
- `fetchRepoStarsTool`: A tool that takes a repository name (e.g., `voltagent/core`) and returns the number of stars. |
- `fetchRepoContributorsTool`: A tool that takes a repository name and returns a list of contributors. |
_(To learn how to create your own tools, check out the [Tool Creation documentation](/docs/agents/tools/).)_ |
Let's assume these tools are defined in a separate file, perhaps `src/tools.ts`. We'll import them into our main agent file. |
## Step 4: Defining the Agents |
Now, let's define our agents in `src/index.ts`. Open this file and replace its contents with the following code: |
```typescript title="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", |
instructions: "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", |
instructions: "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", |
instructions: "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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.