开始使用

如需开始使用 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. 选择 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 在您所在的地区不可用,请考虑使用同时提供 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
      

      请参阅 https://cloud.google.com/vertex-ai/generative-ai/pricing 了解 Vertex AI 价格。

    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 开发者界面:

    genkit start
    

    欢迎使用 Genkit 开发者界面

    Genkit Developer UI 现在正在您的机器上运行。当您在下一步中运行模型或数据流时,您的机器将执行使流程的各个步骤协同工作所需的编排任务;将继续针对实时服务器调用 Gemini API 等外部服务。

    此外,由于您处于开发环境,Genkit 会将跟踪记录和流程状态存储在本地文件中。

  5. 当您运行 genkit start 命令时,Genkit 开发者界面会自动下载并打开。

    在开发者界面中,您可以查看已定义的流和配置的模型,运行这些流,并检查先前运行的轨迹。您可以尝试以下部分功能:

    • Run 标签页上,您将看到您已定义的所有流以及插件配置的所有模型的列表。

      点击 menuSuggestionFlow 并尝试使用一些输入文本(例如 "cat")运行它。如果一切顺利,您将获得针对以猫为主题的餐馆的菜单建议。

    • Inspect 标签页中,您将看到流执行的历史记录。对于每个数据流,您都可以查看传递到该数据流的参数,以及每个步骤运行时的跟踪记录。

后续步骤

了解如何使用 FirebaseCloud Run 或任何 Node.js 平台构建和部署 Genkit 应用。