从 Firebase SDK 中的 GA 版 Vertex AI 迁移到 Firebase AI Logic SDK


Firebase AI Logic 及其客户端 SDK 之前称为“Vertex AI in Firebase”。为了更好地反映我们扩展的服务和功能(例如,我们现在支持 Gemini Developer API!),我们将服务重命名并重新打包为 Firebase AI Logic

如需直接从移动应用或 Web 应用安全地访问 Google 的生成式 AI 模型,您现在可以选择“Gemini API”提供方,即长期可用的 Vertex AI Gemini API 或现在可用的 Gemini Developer API。这意味着,您现在可以选择使用 Gemini Developer API,它提供免费层级,并设置了合理的速率限制和配额。

改用 Firebase AI Logic SDK 的步骤概览

  • 第 1 步:为您的应用和使用情形选择最合适的“Gemini API”提供商。

  • 第 2 步:设置 Firebase 项目,以便您可以使用 Gemini Developer API
    仅在您改用 Gemini Developer API 而不是 Vertex AI Gemini API 时适用。

  • 第 3 步:更新应用中使用的库。

  • 第 4 步:更新应用中的初始化。

  • 第 5 步:根据您使用的功能更新代码。

第 1 步:为您的应用选择最合适的“Gemini API”提供方

通过此迁移,您可以选择“Gemini API”提供商:

  • 旧版“Vertex AI in Firebase”SDK 只能使用 Vertex AI Gemini API

  • 借助新的 Firebase AI Logic SDK,您可以选择要直接从移动应用或 Web 应用调用的“Gemini API”提供方,即 Gemini Developer APIVertex AI Gemini API

查看使用这两种 Gemini API 提供商之间的区别,尤其是在支持的功能、价格和速率限制方面。举个例子,Gemini Developer API 不支持使用 Cloud Storage 网址提供文件,但如果您想利用其免费层级和合理的配额,它可能是不错的选择。

  • 如果您想继续使用 Vertex AI Gemini API
    请跳过下一步,直接前往更新应用中的库,然后按照本指南的其余部分操作。

  • 如果您想改用 Gemini Developer API
    请继续执行下一步,设置 Firebase 项目以使用该 API,然后按照本指南的其余部分操作。

第 2 步:设置 Firebase 项目,以便您可以使用 Gemini Developer API

只有在您想改用 Firebase AI Logic 客户端 SDK 的 Gemini Developer API 时,才需要执行此步骤。不过,如果您想继续使用 Vertex AI Gemini API,请跳到下一步。

请注意,您可以在项目中同时启用这两个“Gemini API”提供方。

  1. Firebase 控制台中,前往 Firebase AI Logic 页面

  2. 前往设置标签页,然后选择 Gemini Developer API

  3. 启用 Gemini Developer API

    控制台将确保启用所需的 API,并在您的 Firebase 项目中生成 Gemini API 密钥。
    请勿将此 Gemini API 密钥添加到应用的代码库中。 了解详情。

  4. 请继续阅读本迁移指南,以更新应用中的库和初始化。

第 3 步:更新应用中使用的库

更新应用的代码库以使用 Firebase AI Logic 库。

Swift

  1. 在 Xcode 中打开您的应用项目,然后使用以下任一选项将 Firebase 软件包更新为 v11.13.0 或更高版本:

    • 方法 1:更新所有软件包:依次前往文件 > 软件包 > 更新到最新软件包版本

    • 方法 2:单独更新 Firebase:前往名为软件包依赖项的部分,找到 Firebase 软件包。右键点击 Firebase 软件包,然后选择 Update Package

  2. 确保 Firebase 软件包现在显示的是 v11.13.0 或更高版本。如果不是,请验证您指定的软件包要求是否允许更新到 v11.13.0 或更高版本。

  3. 在项目编辑器中选择应用的目标,然后前往框架、库和嵌入式内容部分。

  4. 添加新库:选择 + 按钮,然后从 Firebase 软件包中添加 FirebaseAI

  5. 完成应用迁移后(请参阅本指南中的其余部分),请务必移除旧库:
    选择 FirebaseVertexAI,然后按 按钮。

Kotlin

  1. 在您的模块(应用级)Gradle 文件(通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle)中,将旧依赖项(如适用)替换为以下依赖项。

    请注意,在删除旧依赖项之前,迁移应用的代码库(请参阅本指南的其余部分)可能会更轻松。

    // BEFORE
    dependencies {
      implementation(platform("com.google.firebase:firebase-bom:33.x.y"))
      implementation("com.google.firebase:firebase-vertexai")
      // OR if not using the BoM
      implementation("com.google.firebase:firebase-vertexai:16.x.y")
    }
    
    
    // AFTER
    dependencies {
      // Import the BoM for the Firebase platform
      implementation(platform("com.google.firebase:firebase-bom:33.15.0"))
    
      // Add the dependency for the Firebase AI Logic library
      // When using the BoM, you don't specify versions in Firebase library dependencies
      implementation("com.google.firebase:firebase-ai")
    }
  2. 将您的 Android 项目与 Gradle 文件同步。

请注意,如果您选择不使用 Firebase Android BoM,只需添加 firebase-ai 库的依赖项,并接受 Android Studio 建议的最新版本。

Java

  1. 在您的模块(应用级)Gradle 文件(通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle)中,将旧依赖项(如适用)替换为以下依赖项。

    请注意,在删除旧依赖项之前,迁移应用的代码库(请参阅本指南的其余部分)可能会更轻松。

    // BEFORE
    dependencies {
      implementation(platform("com.google.firebase:firebase-bom:33.x.y"))
      implementation("com.google.firebase:firebase-vertexai")
      // OR if not using the BoM
      implementation("com.google.firebase:firebase-vertexai:16.x.y")
    }
    
    
    // AFTER
    dependencies {
      // Import the BoM for the Firebase platform
      implementation(platform("com.google.firebase:firebase-bom:33.15.0"))
    
      // Add the dependency for the Firebase AI Logic library
      // When using the BoM, you don't specify versions in Firebase library dependencies
      implementation("com.google.firebase:firebase-ai")
    }
  2. 将您的 Android 项目与 Gradle 文件同步。

请注意,如果您选择不使用 Firebase Android BoM,只需添加 firebase-ai 库的依赖项,并接受 Android Studio 建议的最新版本。

Web

  1. 使用 npm 获取最新版本的 Firebase JS SDK(适用于 Web):

    npm i firebase@latest

    yarn add firebase@latest
  2. 无论您在何处导入了该库,请更新您的导入语句以改用 firebase/ai

    请注意,在删除旧导入之前,迁移应用的代码库(请参阅本指南的其余部分)可能会更轻松。

    // BEFORE
    import { initializeApp } from "firebase/app";
    import { getVertexAI, getGenerativeModel } from "firebase/vertexai";
    
    
    // AFTER
    import { initializeApp } from "firebase/app";
    import { getAI, getGenerativeModel } from "firebase/ai";

Dart

  1. 通过在 Flutter 项目目录中运行以下命令,更新为在 pubspec.yaml 文件中使用 firebase_ai 软件包:

    flutter pub add firebase_ai
  2. 重新构建 Flutter 项目:

    flutter run
  3. 完成应用迁移后(请参阅本指南中的其余部分),请务必删除旧软件包:

    flutter pub remove firebase_vertexai

Unity

Vertex AI in Firebase”不支持 Unity。

了解如何开始使用 Firebase AI Logic SDK for Unity

第 4 步:更新应用中的初始化

点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。

更新您为所选 API 提供方初始化服务的方式,并创建 GenerativeModel 实例。

Swift


import FirebaseAI

// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(modelName: "gemini-2.5-flash")

Kotlin


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
                        .generativeModel("gemini-2.5-flash")

Java


// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel("gemini-2.5-flash");

// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

Web


import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, { model: "gemini-2.5-flash" });

Dart


import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
      FirebaseAI.googleAI().generativeModel(model: 'gemini-2.5-flash');

Unity

Vertex AI in Firebase”不支持 Unity。

了解如何开始使用 Firebase AI Logic SDK for Unity

请注意,根据您使用的功能,您可能并不总是需要创建 GenerativeModel 实例

第 5 步:根据您使用的功能更新代码

此步骤介绍了可能需要进行的更改,具体取决于您使用的功能。

  • 如果您使用 Cloud Storage 网址在此迁移中改用 Gemini Developer API,则需要更新多模态请求,以将文件作为内嵌数据包含在内(或使用 YouTube 网址来表示视频)。

  • 查看以下列表,了解您可能需要在代码中进行的任何更改,以便适应采用 Firebase AI Logic SDK。

Swift

无其他更改。

Kotlin

  • Live API

    • 移除了枚举类 ResponseModalityUNSPECIFIED 值。请改为使用 null

Java

  • Live API

    • 移除了枚举类 ResponseModalityUNSPECIFIED 值。请改为使用 null
  • 更改了各种 Java 构建器方法,使其现在可以正确返回其类的实例,而不是 void。

Web

仅当您开始使用 Gemini Developer API(而非 Vertex AI Gemini API)时才需要进行更改:

  • 安全设置

    • 移除了不受支持的 SafetySetting.method 的用法。
  • 内嵌数据

    • 移除了不受支持的 InlineDataPart.videoMetadata 的用法。

Dart

无其他更改。

Unity

Vertex AI in Firebase”不支持 Unity。

了解如何开始使用 Firebase AI Logic SDK for Unity


就您使用 Firebase AI Logic 的体验提供反馈