从 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

只有当您想改用 Gemini Developer APIFirebase 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 密钥。

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

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

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

Swift

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

    • 方法 1:更新所有软件包:依次选择 File > Packages > Update to Latest Package Versions

    • 方法 2:单独更新 Firebase:前往名为软件包依赖项的部分,找到 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:33.13.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.13.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 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.0-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.0-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.0-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.0-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.0-flash');

Unity

“Vertex AI in Firebase”不支持 Unity。

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

请注意,您可能并不总是需要创建 GenerativeModel 实例,具体取决于您使用的 capability

第 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 的体验提供反馈