透過 Go 開始使用 Genkit (Alpha 版)

Go 適用的 Firebase Genkit 程式庫現已開放預先發布版!由於 Go 程式庫目前為 Alpha 版,您可能會看到 API 和功能變更,例如: 我們會加快腳步。建議只用於原型設計 探索。

如果發現程式庫或這份說明文件有任何問題,請回報 列在 GitHub 存放區中。

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

需求條件

  • Go 1.22 以上版本

  • Node.js 20 以上版本 (適用於 Genkit CLI 和 UI)

    建議:nvmnvm-windows 工具是 可以輕鬆安裝特定版本的節點 安裝在系統上這些工具會為每位使用者安裝節點 不必進行整個系統的變更

    如要在類似 Unix 系統 (例如 macOS 或 Linux) 上安裝 nvm,請執行下列指令: 指令:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    

    接著,如要使用 nvm 安裝節點,請開啟新殼層並執行下列指令 指令:

    nvm install 20
    

程序

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

    npm i -g genkit
    

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

  2. 建立新的專案目錄:

    mkdir genkit-intro && cd genkit-intro
    
  3. 初始化 Genkit 專案:

    genkit init
    
    1. 選取 Go 做為執行階段環境。

    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 定價

    3. 指定任何模組名稱。例如:example/genkit-intro

    4. 選擇其餘問題的預設答案, 使用一些程式碼範例初始化專案資料夾。

    genkit init 指令會建立範例 Go 模組,並安裝 所需的依附元件main.go 檔案含有單一資料流。 menuSuggestionFlow,就會提示 LLM 以及特定主題的餐廳

    這個檔案的內容如下所示 (外掛程式設定步驟) 如果選擇 Vertex AI,看起來可能會有所不同):

    package main
    
    import (
        "context"
        "errors"
        "fmt"
        "log"
    
        // Import Genkit and the Google AI plugin
        "github.com/firebase/genkit/go/ai"
        "github.com/firebase/genkit/go/genkit"
        "github.com/firebase/genkit/go/plugins/googleai"
    )
    
    func main() {
        ctx := context.Background()
    
        // Initialize the Google AI plugin. When you pass nil for the
        // Config parameter, the Google AI plugin will get the API key from the
        // GOOGLE_GENAI_API_KEY environment variable, which is the recommended
        // practice.
        if err := googleai.Init(ctx, nil); err != nil {
            log.Fatal(err)
        }
    
        // Define a simple flow that prompts an LLM to generate menu suggestions.
        genkit.DefineFlow("menuSuggestionFlow", func(ctx context.Context, input string) (string, error) {
            // The Google AI API provides access to several generative models. Here,
            // we specify gemini-1.5-flash.
            m := googleai.Model("gemini-1.5-flash")
            if m == nil {
                return "", errors.New("menuSuggestionFlow: failed to find model")
            }
    
            // Construct a request and send it to the model API (Google AI).
            resp, err := m.Generate(ctx,
                ai.NewGenerateRequest(
                    &ai.GenerationCommonConfig{Temperature: 1},
                    ai.NewUserTextMessage(fmt.Sprintf(`Suggest an item for the menu of a %s themed restaurant`, input))),
                nil)
            if err != nil {
                return "", err
            }
    
            // 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.
            text, err := resp.Text()
            if err != nil {
                return "", fmt.Errorf("menuSuggestionFlow: %v", err)
            }
            return text, nil
        })
    
        // Initialize Genkit and start a flow server. This call must come last,
        // after all of your plug-in configuration and flow definitions. When you
        // pass a nil configuration to Init, Genkit starts a local flow server,
        // which you can interact with using the developer UI.
        if err := genkit.Init(ctx, nil); err != nil {
            log.Fatal(err)
        }
    }
    

    透過 Genkit 建構應用程式的 AI 功能 包含多個步驟的流程,例如輸入預先處理、較複雜的 提示建構、整合外部資訊來源 包括檢索增強生成 (RAG) 等等

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

    genkit start
    

    歡迎來到
Genkit 開發人員 UI

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

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

  5. 當您執行 genkit start 指令。

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

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

      請按一下「menuSuggestionFlow」,然後試著輸入一些輸入文字 (例如: 例如 "cat")。如果一切順利,你將收到選單獎勵 與貓主題餐廳的建議。

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