Agents Guide: Building a Chef Assistant
In this guide, we’ll walk through creating a “Chef Assistant” agent that helps users cook meals with available ingredients.
Prerequisites
- Node.js installed
- Mastra installed:
npm install @mastra/core
Create the Agent
Define the Agent
Create a new file src/mastra/agents/chefAgent.ts
and define your agent:
src/mastra/agents/chefAgent.ts
import { Agent } from '@mastra/core';
export const chefAgent = new Agent({
name: 'chef-agent',
instructions:
'You are Michel, a practical and experienced home chef' +
'You helps people cook with whatever ingredients they have available.',
model: {
provider: 'OPEN_AI',
name: 'gpt-4o',
toolChoice: 'auto',
},
});
Set Up Environment Variables
Create a .env
file in your project root and add your OpenAI API key:
.env
OPENAI_API_KEY=your_openai_api_key
Register the Agent with Mastra
In your main file, register the agent:
src/mastra/index.ts
import { Mastra } from '@mastra/core';
import { chefAgent } from './agents/chefAgent';
export const mastra = new Mastra({
agents: { chefAgent },
});
Interacting with the Agent
Generating Text Responses
src/mastra/index.ts
async function main() {
const query =
'In my kitchen I have: pasta, canned tomatoes, garlic, olive oil, and some dried herbs (basil and oregano). What can I make?';
console.log(`Query: ${query}`);
const response = await chefAgent.generate([{ role: 'user', content: query }]);
console.log('\n👨🍳 Chef Michel:', response.text);
}
main();
Output:
Query: In my kitchen I have: pasta, canned tomatoes, garlic, olive oil, and some dried herbs (basil and oregano). What can I make?
👨🍳 Chef Michel: You can make a delicious pasta al pomodoro! Here's how...
Streaming Responses
src/mastra/index.ts
async function main() {
const query =
"Now I'm over at my friend's house, and they have: chicken thighs, coconut milk, sweet potatoes, and some curry powder.";
console.log(`Query: ${query}`);
const stream = await chefAgent.stream([{ role: 'user', content: query }]);
console.log('\n Chef Michel: ');
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}
console.log('\n\n✅ Recipe complete!');
}
main();
Output:
Query: Now I'm over at my friend's house, and they have: chicken thighs, coconut milk, sweet potatoes, and some curry powder.
👨🍳 Chef Michel:
Great! You can make a comforting chicken curry...
✅ Recipe complete!
Generating a Recipe with Structured Data
src/mastra/index.ts
import { z } from 'zod';
async function main() {
const query = 'I want to make lasagna, can you generate a lasagna recipe for me?';
console.log(`Query: ${query}`);
// Define the Zod schema
const schema = z.object({
ingredients: z.array(
z.object({
name: z.string(),
amount: z.string(),
}),
),
steps: z.array(z.string()),
});
const response = await chefAgent.generate([{ role: 'user', content: query }], { output: schema });
console.log('\n👨🍳 Chef Michel:', response.object);
}
main();
Output:
Query: I want to make lasagna, can you generate a lasagna recipe for me?
👨🍳 Chef Michel: {
ingredients: [
{ name: "Lasagna noodles", amount: "12 sheets" },
{ name: "Ground beef", amount: "1 pound" },
// ...
],
steps: [
"Preheat oven to 375°F (190°C).",
"Cook the lasagna noodles according to package instructions.",
// ...
]
}
Running the Agent Server
Using mastra dev
You can run your agent as a service using the mastra dev
command:
mastra dev
This will start a server exposing endpoints to interact with your registered agents.
Accessing the Chef Assistant API
By default, mastra dev
runs on http://localhost:4111
. Your Chef Assistant agent will be available at:
POST http://localhost:4111/api/agents/chefAgent/generate
Interacting with the Agent via curl
You can interact with the agent using curl
from the command line:
curl -X POST http://localhost:4111/api/agents/chefAgent/generate \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "I have eggs, flour, and milk. What can I make?"
}
]
}'
Sample Response:
{
"text": "You can make delicious pancakes! Here's a simple recipe..."
}