0
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(Node.js版)と OpenAI の API の組み合わせをサクッと試す

Last updated at Posted at 2025-05-09

はじめに

以下の Genkit(Node.js版)と OpenAI の API の組み合わせをサクッと試してみます。

●Genkit  |  Firebase
 https://firebase.google.com/docs/genkit?hl=ja#openai

image.png

サンプルコード

上記のページを見ると、以下のサンプルコードが示されています。

import { genkit } from 'genkit';
import { openAI, gpt4o } from 'genkitx-openai';

const ai = genkit({ plugins: [openAI()] });

const { text } = await ai.generate({
    model: gpt4o,
    prompt: 'Why is Firebase awesome?'
});

使われているパッケージ

冒頭の部分を見ると、以下の 2つのパッケージを使っているようです。

●genkit - npm
 https://www.npmjs.com/package/genkit?activeTab=readme

image.png

●genkitx-openai - npm
 https://www.npmjs.com/package/genkitx-openai

image.png

サポートされている OpenAI のモデル

この Firebase Genkit の OpenAI Plugin でサポートされているモデルは、以下となっているようです。

image.png

サクッと試す

簡単な内容でサクッと試していきます。

下調べ

対応しているモデルのリストを、念のため元のコードで確認してみます。

●genkit-plugins/plugins/openai/README.md at main · TheFireCo/genkit-plugins
 https://github.com/TheFireCo/genkit-plugins/blob/main/plugins/openai/README.md

該当箇所は、以下になりそうです。

●genkit-plugins/plugins/openai/src/index.ts at ec01c18567e320b344e6a0b5952f6c1d2a33eecc · TheFireCo/genkit-plugins
 https://github.com/TheFireCo/genkit-plugins/blob/ec01c18567e320b344e6a0b5952f6c1d2a33eecc/plugins/openai/src/index.ts#L29

image.png

npm のページに出ていなかった o4Mini も対応していそうです。今回、o4Mini を使ってみることにします。

環境の準備

パッケージのインストール、APIキーの設定を行います。

以下でパッケージ 2つをインストールします。

npm i genkitx-openai genkit

また、環境変数 OPENAI_API_KEY に、OpenAI の APIキーを設定しておきます。

コードと実行結果

環境を作れたので、以下のコードを実行してみます。

import { genkit } from "genkit";
import { openAI, o4Mini } from "genkitx-openai";

const ai = genkit({ plugins: [openAI()] });

const { text } = await ai.generate({
  model: o4Mini,
  prompt: "あなたは誰?",
});

console.log(text);

実行結果として、以下の応答を得ることができました。

image.png

余談: Genkit(Node.js版)を試したきかっけ

余談ですが、今回 Genkit(Node.js版)を試したきかっけに触れておきます。

流れとしては「A2A + TypeScript・JavaScript」という内容を調査していて、以下の部分を見かけました。

●google/A2A: An open protocol enabling communication and interoperability between opaque agentic applications.
 https://github.com/google/A2A/tree/main?tab=readme-ov-file#getting-started

image.png

それで、リンク先の以下を見たら Genkit が使われていると書いてあり、Genkit は試したことがなかったので、まずはシンプルに使ってみたという流れでした。

●A2A/samples/js at main · google/A2A
 https://github.com/google/A2A/tree/main/samples/js#javascript-samples

image.png

0
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
0
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?