Back to prompt library
Mastra Agent Initialization and Tooling
Linked challenge: Real-time Market Trend Agent
Prompt source
planning100 lines13 sectionsNo variables1 code block
Initialize a Mastra AI agent named `MarketMonitorAgent`. Define tools that allow the agent to:
1. Fetch simulated daily active user (DAU) data for specified platforms.
2. Store and retrieve trend data from Pinecone.
3. Generate a formatted report or summary.
The agent should be configured to use `Mistral Large 2` via the Hugging Face Inference API for its primary reasoning.
```typescript
import { createAgent } from '@mastra-ai/core';
import { Configuration, OpenAIApi } from 'openai'; // Using OpenAI's API client for Pinecone embeddings
import { Pinecone } from '@pinecone-database/pinecone';
// --- Assume these are your API keys and configs ---
const HUGGING_FACE_API_KEY = process.env.HUGGING_FACE_API_KEY;
const PINECONE_API_KEY = process.env.PINECONE_API_KEY;
const PINECONE_ENVIRONMENT = process.env.PINECONE_ENVIRONMENT;
const PINECONE_INDEX_NAME = 'market-trends';
// Initialize Pinecone client
const pinecone = new Pinecone({
environment: PINECONE_ENVIRONMENT,
apiKey: PINECONE_API_KEY,
});
const index = pinecone.Index(PINECONE_INDEX_NAME);
// Simulated DAU data fetcher
const fetchDauData = async (platform: string) => {
console.log(`Fetching DAU data for ${platform}...`);
// In a real scenario, this would hit an external API
const simulatedData = {
'Threads': { 'current': 141.5, 'previous': 138.2, 'unit': 'M DAUs' },
'X': { 'current': 125.0, 'previous': 126.1, 'unit': 'M DAUs' },
'Instagram': { 'current': 600.0, 'previous': 598.5, 'unit': 'M DAUs' }
};
return simulatedData[platform] || null;
};
// Function to store a trend in Pinecone
const storeTrendInPinecone = async (platform: string, trendData: any) => {
console.log(`Storing trend for ${platform} in Pinecone...`);
// Generate embeddings for the trend description using an embedding model
// For simplicity, let's just store metadata for now
const embedding = [0.1, 0.2, 0.3]; // Replace with actual embedding generation
await index.upsert({
vectors: [{
id: `${platform}-${Date.now()}`,
values: embedding,
metadata: { platform, ...trendData, timestamp: new Date().toISOString() },
}],
});
return { status: 'success' };
};
// Function to search for trends in Pinecone
const searchTrendsInPinecone = async (query: string, topK: number = 3) => {
console.log(`Searching Pinecone for: ${query}`);
const queryEmbedding = [0.1, 0.2, 0.3]; // Replace with actual embedding generation for query
const queryResult = await index.query({
vector: queryEmbedding,
topK,
includeMetadata: true,
});
return queryResult.matches.map(match => match.metadata);
};
const MarketMonitorAgent = createAgent({
id: 'MarketMonitorAgent',
model: {
type: 'huggingface',
modelId: 'mistralai/Mistral-Large-2',
apiKey: HUGGING_FACE_API_KEY,
endpointUrl: 'https://api-inference.huggingface.co/models/mistralai/Mistral-Large-2' // Adjust if needed
},
tools: [
{
name: 'fetchDauData',
description: 'Fetches simulated daily active user (DAU) data for a given social media platform.',
inputSchema: { type: 'object', properties: { platform: { type: 'string' } }, required: ['platform'] },
handler: async (args) => fetchDauData(args.platform),
},
{
name: 'storeTrendInPinecone',
description: 'Stores identified market trend data in Pinecone for historical context.',
inputSchema: { type: 'object', properties: { platform: { type: 'string' }, trendData: { type: 'object' } }, required: ['platform', 'trendData'] },
handler: async (args) => storeTrendInPinecone(args.platform, args.trendData),
},
{
name: 'searchTrendsInPinecone',
description: 'Searches for historical market trends in Pinecone based on a query.',
inputSchema: { type: 'object', properties: { query: { type: 'string' }, topK: { type: 'number' } }, required: ['query'] },
handler: async (args) => searchTrendsInPinecone(args.query, args.topK),
}
],
// ... other Mastra config like memory
});
export default MarketMonitorAgent;
// Example of how to run the agent (in a separate file/script):
/*
import MarketMonitorAgent from './MarketMonitorAgent';
async function runAnalysis() {
const result = await MarketMonitorAgent.run({
prompt: 'Analyze the latest DAU data for Threads and identify any significant trends.',
});
console.log('Agent finished:', result);
}
runAnalysis();
*/
```