始める

Firebase Genkit の使用を開始するには、Genkit CLI をインストールして、Node.js プロジェクトで genkit init を実行します。このページの残りの部分では、その方法について説明します。

要件

Node.js 20 以降。

手順

  1. 次のコマンドを実行して Genkit CLI をインストールします。

    npm i -g genkit
    
  2. 新しい Node プロジェクトを作成します。

    mkdir genkit-intro && cd genkit-intro
    npm init -y
    

    package.json で、main フィールドが lib/index.js に設定されていることを確認します。

  3. Genkit プロジェクトを初期化します。

    genkit init
    
    1. デプロイ プラットフォーム オプションとして [その他のプラットフォーム] を選択します(Firebase Cloud Functions と Google Cloud Run のテンプレートも使用できます)。

    2. モデルを選択します。

      Gemini(Google AI)

      最も簡単に始める方法は、Google AI Gemini API を使用することです。リージョンで利用可能であることを確認してください。

      Google AI Studio を使用して Gemini API の API キーを生成します。次に、GOOGLE_GENAI_API_KEY 環境変数を自分のキーに設定します。

      export GOOGLE_GENAI_API_KEY=<your API key>
      

      Gemini(Vertex AI)

      お住まいの地域で Google AI Gemini API が利用できない場合は、Gemini やその他のモデルも提供している Vertex AI API の使用を検討してください。課金が有効になっている Google Cloud プロジェクトを用意し、AI Platform API を有効にして、追加の環境変数を設定する必要があります。

      gcloud services enable aiplatform.googleapis.com
      export GCLOUD_PROJECT=<your project ID>
      export GCLOUD_LOCATION=us-central1
      

      Vertex AI の料金については、https://cloud.google.com/vertex-ai/generative-ai/pricing をご覧ください。

    3. 残りの質問に対するデフォルトの回答を選択します。これにより、プロジェクト フォルダがサンプルコードで初期化されます。

    genkit init コマンドは、サンプルのソースファイル index.ts を作成します。このファイルでは、特定のテーマのレストランのアイテムを LLM に提案するよう求める単一のフロー menuSuggestionFlow が定義されます。

    このファイルは、次のようになります(Vertex AI を選択した場合、プラグインの構成手順が異なる場合があります)。

    import * as z from 'zod';
    
    // Import the Genkit core libraries and plugins.
    import { generate } from '@genkit-ai/ai';
    import { configureGenkit } from '@genkit-ai/core';
    import { defineFlow, startFlowsServer } from '@genkit-ai/flow';
    import { googleAI } from '@genkit-ai/googleai';
    
    // Import models from the Google AI plugin. The Google AI API provides access to
    // several generative models. Here, we import Gemini 1.5 Flash.
    import { gemini15Flash } from '@genkit-ai/googleai';
    
    configureGenkit({
      plugins: [
        // Load the Google AI plugin. You can optionally specify your API key
        // by passing in a config object; if you don't, the Google AI plugin uses
        // the value from the GOOGLE_GENAI_API_KEY environment variable, which is
        // the recommended practice.
        googleAI(),
      ],
      // Log debug output to tbe console.
      logLevel: 'debug',
      // Perform OpenTelemetry instrumentation and enable trace collection.
      enableTracingAndMetrics: true,
    });
    
    // Define a simple flow that prompts an LLM to generate menu suggestions.
    export const menuSuggestionFlow = defineFlow(
      {
        name: 'menuSuggestionFlow',
        inputSchema: z.string(),
        outputSchema: z.string(),
      },
      async (subject) => {
        // Construct a request and send it to the model API.
        const llmResponse = await generate({
          prompt: `Suggest an item for the menu of a ${subject} themed restaurant`,
          model: gemini15Flash,
          config: {
            temperature: 1,
          },
        });
    
        // Handle the response from the model API. In this sample, we just convert
        // it to a string, but more complicated flows might coerce the response into
        // structured output or chain the response into another LLM call, etc.
        return llmResponse.text();
      }
    );
    
    // Start a flow server, which exposes your flows as HTTP endpoints. This call
    // must come last, after all of your plug-in configuration and flow definitions.
    // You can optionally specify a subset of flows to serve, and configure some
    // HTTP server options, but by default, the flow server serves all defined flows.
    startFlowsServer();
    

    Genkit を使用してアプリの AI 機能を構築する際、入力の前処理、高度なプロンプト構築、検索拡張生成(RAG)のための外部情報源の統合など、複数のステップからなるフローを作成する可能性があります。

  4. これで、Genkit 機能とサンプル プロジェクトをマシン上でローカルで実行して確認できるようになりました。Genkit デベロッパー UI をダウンロードして開始します。

    genkit start
    

    Genkit デベロッパー UI へようこそ

    これで Genkit デベロッパー UI がマシンで実行されました。次のステップでモデルまたはフローを実行すると、マシンはフローのステップを連携させるために必要なオーケストレーション タスクを実行します。Gemini API などの外部サービスの呼び出しは、引き続きライブサーバーに対して行われます。

    また、開発環境にいるため、Genkit はトレースとフローの状態をローカル ファイルに保存します。

  5. genkit start コマンドを実行すると、Genkit デベロッパー UI が自動的にダウンロードされて開きます。

    デベロッパー UI では、定義したフローと、構成したモデルを確認して実行し、以前の実行のトレースを調べることができます。次の機能をいくつか試してみましょう。

    • [Run] タブに、定義したすべてのフローとプラグインによって構成されたモデルのリストが表示されます。

      [menuSuggestionFlow] をクリックし、入力テキスト("cat" など)を指定して実行してみます。すべて問題がなければ、猫をテーマにしたレストランのメニューの候補が表示されます。

    • [Inspect] タブに、フロー実行の履歴が表示されます。フローごとに、フローに渡されたパラメータと、実行時の各ステップのトレースを確認できます。

次のステップ

FirebaseCloud Run、または任意の Node.js プラットフォームで Genkit アプリをビルドしてデプロイする方法を確認する。