从 Vertex AI in Firebase SDK 的正式版迁移到 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,该 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 网址提供),但如果您想 利用其免费层级和合理的配额,它可能是一个不错的选择。

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

仅当您想改用 Gemini Developer API使用 Firebase AI Logic客户端 SDK 时,才需要执行此步骤。但是,如果您想继续使用 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:更新所有软件包:依次前往 File > Packages > Update to Latest Package Versions

    • 选项 2:单独更新 Firebase:前往名为 Package Dependencies 的部分中的 Firebase 软件包。右键点击 Firebase 软件包,然后选择 Update Package

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

  3. 在项目编辑器中选择应用的目标,然后前往 Frameworks, Libraries, and Embedded Content 部分。

  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:34.11.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:34.11.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 for 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. pubspec.yaml 文件中更新为使用 firebase_ai 软件包,方法是从 Flutter 项目目录运行以下命令:

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

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

    flutter pub remove firebase_vertexai

Unity

Vertex AI in Firebase”不支持 Unity。

了解如何 开始使用适用于 Unity 的 Firebase AI Logic SDK

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

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

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

Swift


import FirebaseAILogic

// Initialize the Vertex AI Gemini API backend service
let ai = FirebaseAI.firebaseAI(backend: .vertexAI())

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

Kotlin


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

Java


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

// 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, VertexAIBackend } 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 Vertex AI Gemini API backend service
const ai = getAI(firebaseApp, { backend: new VertexAIBackend() });

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

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 Vertex AI Gemini API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
      FirebaseAI.vertexAI().generativeModel(model: 'gemini-3-flash-preview');

Unity

Vertex AI in Firebase”不支持 Unity。

了解如何 开始使用适用于 Unity 的 Firebase AI Logic SDK

请注意,根据您使用的功能,您可能并不总是需要 创建 GenerativeModel 实例。如需使用 流式传输输入和输出,请创建 Gemini Live API, LiveModel 实例。

第 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。

了解如何 开始使用适用于 Unity 的 Firebase AI Logic SDK


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