开始使用

如需开始使用 Firebase Genkit,请安装 Genkit CLI 并运行 Node.js 项目中的 genkit init。本页的其余部分将介绍具体方法。

要求

Node.js 20 或更高版本。

建议:nvmnvm-windows 工具是 安装 Node.js 的便捷方式。

安装 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。请确保您所在的区域提供 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
      

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

    3. 选择其余问题的默认答案 使用一些示例代码初始化您的项目文件夹。

    genkit init 命令会创建一个示例源文件 index.ts, 定义了单个流 (menuSuggestionFlow),用于提示 LLM 提出建议 为具有指定主题的餐馆创建商品。

    该文件应如下所示(插件配置步骤 可能有所不同):

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

    genkit start
    

    欢迎使用 Genkit 开发者界面

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

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

  4. Genkit Developer UI 会在您运行 genkit start 命令。

    借助开发者界面,您可以查看已定义的 flow 和已配置的模型,运行这些 flow 和模型,并检查之前运行的跟踪记录。您可以尝试以下部分功能:

    • 运行标签页上,您会看到一个包含您定义的所有 flow 以及已由插件配置的所有模型的列表。

      点击 menuSuggestionFlow,然后尝试使用一些输入文本(例如, "cat").如果一切进展顺利,您将获得针对猫咪的菜单建议 主题餐厅。

    • 检查标签页中,您会看到 flow 执行历史记录。对于每个 您可以看到传递到流的参数和 每个步骤的运行轨迹

后续步骤

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