1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

genkit + vertexAIでTool使う

Last updated at Posted at 2025-04-27

通常のgenerate

import { genkit } from 'genkit';
import { vertexAI } from '@genkit-ai/vertexai';

const ai = genkit({
  plugins: [
    vertexAI({ location: 'asia-northeast1' }),
  ],
});

const response = await ai.generate({
  model,
  prompt
});

ユーザからの問い合わせに香水に関することがあったら、香水のデータを検索して使えるようにします

まずはツールの定義

import {z} from "zod";

const fetchPerfumeTool = ai.defineTool(
  {
    name: "fetchPerfume",
    description: "香水を検索します",
    inputSchema: z.object({
      maker: z.string().describe('香水のメーカー')
    }),
    outputSchema: z.object({
      lists: z.array(
        z.object({
          name: z.string(), description: z.string()
        })
      )
    }),
  },
  async (input) => {

    // DB検索したことにする。
    logger.log(input.maker)
    await new Promise(resolve => setTimeout(resolve, 300))


    return {lists: [
      {name: '香水1', description: 'さわやかな香り。柑橘系です'},
      {name: '香水2', description: 'スイカみてぇな匂いがします'},
      {name: '香水3', description: 'どこかハワイっぽいです'},
      {name: '香水4', description: 'スパイシーな感じです'},
      {name: '香水5', description: '木みてぇな匂いです'},
      {name: '香水6', description: '匂いませんが、肌によさそうです'},
    ]}
  }
);

input.makerにはメーカー情報が入ります

例えば「ディオールの香水で、さわやかなやつねぇ?」って聞いたら"ディオール"が入っています

定義したツールは以下のように使用します

const response = await ai.generate({
  model,
  prompt,
  tools: [fetchPerfumeTool]
});
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?