Viewing Gentrace v2 documentation View Gentrace v1
Integrate Gentrace with Mastra’s agent framework for AI applications
npm install @mastra/core gentrace @ai-sdk/openai
import { init, interaction } from "gentrace"; import { Mastra } from "@mastra/core"; import { createOpenAI } from "@ai-sdk/openai"; async function main() { // Initialize Gentrace init({ apiKey: process.env.GENTRACE_API_KEY, }); // Create OpenAI provider const openai = createOpenAI({ apiKey: process.env.OPENAI_API_KEY, }); // Initialize Mastra with Gentrace telemetry const mastra = new Mastra({ openai, telemetry: { gentrace: { apiKey: process.env.GENTRACE_API_KEY, }, }, }); } // Run the main function main().catch(console.error);
import { Agent } from "@mastra/core"; // Create an agent const emailAssistant = new Agent({ name: "EmailAssistant", model: "gpt-4o-mini", instructions: "You are a helpful email assistant. Help users write professional emails.", provider: "OPENAI", }); // Use the agent within a Gentrace interaction async function generateEmail(subject: string, context: string) { return await interaction( "generate-email", async () => { const response = await emailAssistant.text({ messages: [ { role: "user", content: `Write a professional email about: ${subject}\nContext: ${context}`, }, ], }); return response.text; }, { pipelineId: process.env.GENTRACE_PIPELINE_ID!, metadata: { subject, agentName: "EmailAssistant", }, } ); }
.env
GENTRACE_API_KEY=your-gentrace-api-key GENTRACE_PIPELINE_ID=your-pipeline-id OPENAI_API_KEY=your-openai-api-key
const technicalAgent = new Agent({ name: "TechnicalWriter", model: "gpt-4o", instructions: "You are a technical documentation expert.", provider: "OPENAI", }); const creativeAgent = new Agent({ name: "CreativeWriter", model: "gpt-4o-mini", instructions: "You are a creative content writer.", provider: "OPENAI", });
const researchAgent = new Agent({ name: "ResearchAssistant", model: "gpt-4o", instructions: "You help with research tasks.", provider: "OPENAI", tools: { searchWeb: { description: "Search the web for information", input: z.object({ query: z.string(), }), execute: async ({ query }) => { // Your search implementation return `Search results for: ${query}`; }, }, }, });