開始使用

如要開始使用 Firebase Genkit,請安裝 Genkit CLI genkit init。本頁其餘部分將說明如何操作。

需求條件

Node.js 20 以上版本。

建議:nvmnvm-windows 工具是 安裝節點

安裝 Genkit

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

npm i -g genkit

這個指令會將 Genkit CLI 安裝至您的 Node 安裝目錄 以便在節點專案以外的地方使用

建立及探索範例專案

  1. 建立新的節點專案:

    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 不適用於您的區域,請考慮 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) 等來源。

  3. 您現在可以在本機執行及探索 Genkit 功能和範例專案 虛擬機器下載並啟動 Genkit 開發人員 UI:

    genkit start
    

    歡迎使用 Genkit 開發人員 UI

    Genkit 開發人員 UI 現已在您的電腦上執行。執行模型 機器就會執行自動化調度管理任務 才能整合流程各步驟撥打外部電話 Gemini API 等服務 伺服器

    此外,由於您處於開發環境,因此 Genkit 會儲存追蹤記錄 本機檔案中的流程狀態

  4. 當您執行 genkit start 指令。

    開發人員 UI 會顯示您已定義的流程和模型 設定、執行映像檔,並檢查先前執行作業的追蹤記錄。試用其他 這些功能:

    • 在「Run」分頁中,您會看到所有流程清單 以及由外掛程式設定的所有模型

      請按一下「menuSuggestionFlow」,然後試著輸入一些輸入文字 (例如 "cat").如果一切順利,系統會顯示貓咪的選單建議 主題餐廳

    • 「Inspect」分頁會顯示流程執行歷史記錄。對於每項 流程中,您可以查看傳遞至流程的參數 追蹤每個步驟的執行狀況

後續步驟

瞭解如何利用 Firebase 建構及部署 Genkit 應用程式。 Cloud Run 或任何 Node.js 平台