使ってみる

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

要件

Node.js 20 以降。

推奨事項: nvmnvm-windowsのツールは 簡単にインストールできます。

Genkit をインストールする

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

npm i -g genkit

このコマンドにより、Genkit CLI が Node のインストール ディレクトリにインストールされます そのため、Node プロジェクトの外部で使用できるようにします。

サンプル プロジェクトを作成して確認する

  1. 新しい Node プロジェクトを作成します。

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

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

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

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

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

      Gemini(Google AI)

      最も簡単に始める方法は、Google AI Gemini API を使用することです。確認事項 です 利用可能な地域をご確認ください。

      アプリケーションの API キーを生成 Google AI Studio を使用する Gemini 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 プロジェクトを用意し、 次の環境変数を設定します。

      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 機能を構築する際、 入力の前処理など複数のステップからなるフローの作成、 高度なプロンプト構築、外部情報の統合、 などのソースがあります。

  3. Genkit の特徴とサンプル プロジェクトをローカルで実行して探索できるようになりました インストールします。Genkit デベロッパー UI をダウンロードして開始します。

    genkit start
    

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

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

    また、開発環境にいるため、Genkit はトレースと ローカル ファイルに保存されています。

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

    デベロッパー UI では、定義したフローや、作成したモデルを確認できます。 実行し、以前の実行のトレースを調べる。一部の機能を試してみましょう。

    • [Run] タブに、実行中のすべてのフローのリストが表示されます。 プラグインで構成されたモデルが含まれます。

      menuSuggestionFlow をクリックし、入力テキスト(例: "cat").問題がなければ、猫用のメニュー候補が表示されます。 レストランのメニューです。

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

次のステップ

Firebase を使用して Genkit アプリをビルドしてデプロイする方法をご覧ください。 Cloud Run、任意の Node.js プラットフォーム