본문으로 건너뛰기

AI 셰프 어시스턴트 구축

이 가이드에서는 사용자가 사용 가능한 재료로 식사를 요리하는 데 도움이 되는 "Chef Assistant" Agent를 만듭니다.

Agent를 생성하고 이를 Mastra에 등록하는 방법을 알아봅니다. 다음으로, 터미널을 통해 Agent와 상호 작용하고 다양한 응답 형식을 알아봅니다. 그런 다음 Mastra의 로컬 API 엔드포인트를 통해 Agent에 액세스합니다.

전제조건
전제조건에 대한 직접 링크

  • Node.jsv22.13.0 or later installed
  • 지원되는 API 키Model Provider
  • 기존 Mastra 프로젝트(다음을 따르세요.installation guide to set up a new project)

Agent 만들기
Agent 만들기에 대한 직접 링크

Mastra에서 Agent를 생성하려면Agent 클래스로 정의한 다음 Mastra에 등록합니다.

  1. 새 파일 만들기src/mastra/agents/chefAgent.ts and define your agent:

    src/mastra/agents/chefAgent.ts
    import { Agent } from '@mastra/core/agent'

    export const chefAgent = new Agent({
    id: 'chef-agent',
    name: 'chef-agent',
    instructions:
    'You are Michel, a practical and experienced home chef' +
    'You help people cook with whatever ingredients they have available.',
    model: 'openai/gpt-5.6-sol',
    })
  2. 당신의src/mastra/index.ts 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 },
    })

Agent과 상호작용
Agent과 상호작용에 대한 직접 링크

요구 사항에 따라 다양한 형식으로 Agent과 상호 작용하고 응답을 받을 수 있습니다. 다음 단계에서는 구조화된 출력을 생성, 스트리밍 및 가져오는 방법을 알아봅니다.

  1. 새 파일 만들기src/index.ts and add a main() 함수를 추가합니다. 함수 내부에서 Agent에 전달할 쿼리를 작성하고 응답을 로그에 기록합니다.

    src/index.ts
    import { chefAgent } from './mastra/agents/chefAgent'

    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()

    그런 다음 스크립트를 실행합니다.

    npx bun src/index.ts

    다음과 유사한 출력이 표시됩니다.

    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...
  2. 이전 예에서는 진행 상황 없이 응답을 조금 기다렸을 수 있습니다. Agent가 생성되는 출력을 표시하려면 대신 해당 응답을 터미널로 스트리밍해야 합니다.

    src/index.ts
    import { chefAgent } from './mastra/agents/chefAgent'

    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()

    그런 다음 스크립트를 다시 실행하십시오.

    npx bun src/index.ts

    아래와 비슷한 출력이 표시됩니다. 이번에는 하나의 큰 블록 대신 한 줄씩 읽을 수 있습니다.

    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!
  3. 사람에게 Agent의 응답을 표시하는 대신 코드의 다른 부분에 전달하고 싶을 수도 있습니다. 이러한 경우 Agent는 다음을 반환해야 합니다.structured output.

    당신의 변경src/index.ts to the following:

    src/index.ts
    import { chefAgent } from './mastra/agents/chefAgent'
    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 }], {
    structuredOutput: {
    schema,
    },
    })
    console.log('\n👨‍🍳 Chef Michel:', response.object)
    }

    main()

    스크립트를 다시 실행하면 다음과 유사한 출력이 표시됩니다.

    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.",
    ]
    }

Agent 서버 실행
Agent 서버 실행에 대한 직접 링크

Mastra의 API를 통해 Agent와 상호 작용하는 방법을 알아보세요.

  1. 다음을 사용하여 Agent를 서비스로 실행할 수 있습니다.mastra dev command:

    mastra dev

    등록된 Agent와 상호 작용하기 위해 엔드포인트를 노출하는 서버가 시작됩니다. 이내에Studio you can test your agent through a UI.

  2. 기본적으로mastra dev runs on http://localhost:4111. Chef Assistant Agent는 다음 주소에서 사용할 수 있습니다:

    POST http://localhost:4111/api/agents/chefAgent/generate
  3. 다음을 사용하여 Agent와 상호작용할 수 있습니다.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?"
    }
    ]
    }'

    샘플 응답:

    {
    "text": "You can make delicious pancakes! Here's a simple recipe..."
    }