開始使用

如要開始使用 Firebase Genkit,請安裝 Genkit CLI,然後在 Node.js 專案中執行 genkit init。本頁其餘部分將說明如何操作。

需求條件

Node.js 20 以上版本。

程序

  1. 執行下列指令來安裝 Genkit CLI:

    npm i -g genkit
    
  2. 建立新的節點專案:

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

    查看 package.json,並確認 main 欄位已設為 lib/index.js

  3. 初始化 Genkit 專案:

    genkit init
    
    1. 選取「Other platform」(其他平台) 做為部署平台選項 (您也可以使用 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 不適用於您的區域,請考慮使用 Vertex AI API,其亦提供 Gemini 和其他模型。您必須擁有已啟用計費功能的 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,用來定義單一資料流 menuSuggestionFlow,藉此提示 LLM 推薦具有指定主題的餐廳項目。

    這個檔案如下所示 (選取 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 應用程式。