Firebase AI Logic 及其客户端 SDK 以前称为“Vertex AI in Firebase”。为了更好地反映我们不断扩展的服务和功能(例如,我们现在支持 Gemini Developer API!),我们对服务进行了重命名和重新打包,并将其命名为 Firebase AI Logic。
如需直接从移动应用或 Web 应用安全地访问 Google 的生成式 AI 模型,您现在可以选择“Gemini API”提供程序,即长期可用的 Agent Platform Gemini API (formerly Vertex AI) 或现在的 Gemini Developer API。这意味着,您现在可以选择使用 Gemini Developer API,它提供免费层级,并具有合理的速率限制和配额。
迁移到 Firebase AI Logic SDK 的步骤概览
第 1 步:为您的应用和用例选择最合适的“Gemini API”提供商。
第 2 步:设置 Firebase 项目,以便您可以使用 Gemini Developer API。
仅在您切换为使用 Gemini Developer API 而不是 Agent Platform Gemini API (formerly Vertex AI) 时适用。第 3 步:更新应用中使用的库。
第 4 步:更新应用中的初始化。
第 5 步:根据您使用的功能更新代码。
第 1 步:为您的应用选择最合适的“Gemini API”提供方
在此次迁移中,您可以选择“Gemini API”提供商:
旧版“Vertex AI in Firebase”SDK 只能使用 Agent Platform Gemini API (formerly Vertex AI)。
借助新的 Firebase AI Logic SDK,您可以选择要直接从移动应用或 Web 应用调用的“Gemini API”提供方,即 Gemini Developer API 或 Agent Platform Gemini API (formerly Vertex AI)。
查看使用两个 Gemini API 提供商之间的差异,尤其是在支持的功能、价格和速率限制方面。举例来说,Gemini Developer API 不支持使用 Cloud Storage 网址提供文件,但如果您想利用其免费层级和合理的配额,它可能是一个不错的选择。
如果您想继续使用 Agent Platform Gemini API (formerly Vertex AI):
请跳过下一步,直接在应用中更新库,然后按照本指南的其余部分操作。如果您想改用 Gemini Developer API:
请继续执行下一步,设置 Firebase 项目以使用该 API,然后按照本指南的其余部分操作。
第 2 步:设置 Firebase 项目,以便使用 Gemini Developer API
只有在您想改用 Gemini Developer API 和 Firebase AI Logic 客户端 SDK 时,才需要执行此步骤。但是,如果您想继续使用 Agent Platform Gemini API (formerly Vertex AI),请跳至下一步。
请注意,您可以同时在项目中启用这两个“Gemini API”提供方。
在 Firebase 控制台中,依次前往 AI 服务 > AI Logic。
前往设置标签页,然后选择Gemini Developer API。
启用 Gemini Developer API。
控制台将确保启用所需的 API,并在您的 Firebase 项目中生成 Gemini API 密钥。
请勿将此 Gemini API 密钥添加到应用的代码库中。 了解详情。继续阅读本迁移指南,更新应用中的库和初始化。
第 3 步:更新应用中使用的库
更新应用的代码库,以使用 Firebase AI Logic 库。
Swift
在 Xcode 中打开您的应用项目,然后使用以下任一选项将 Firebase 软件包更新到 v12.5.0 或更高版本:
方法 1:更新所有软件包:依次前往 File > Packages > Update to Latest Package Versions(文件 > 软件包 > 更新到最新软件包版本)。
方法 2:单独更新 Firebase:前往名为 Package Dependencies 的部分中的 Firebase 软件包。右键点击 Firebase 软件包,然后选择更新软件包。
确保 Firebase 软件包现在显示 v12.5.0 或更高版本。如果没有,请验证您指定的软件包要求是否允许更新到 v12.5.0 或更高版本。
在 Project Editor 中选择应用的目标,然后前往 Frameworks, Libraries, and Embedded Content(框架、库和嵌入式内容)部分。
添加新库:选择 + 按钮,然后从 Firebase 软件包中添加 FirebaseAILogic。
完成应用迁移后(请参阅本指南中的其余部分),请务必移除旧库:
选择 FirebaseVertexAI,然后按 — 按钮。
Kotlin
在模块(应用级)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 BoMimplementation("com.google.firebase:firebase-vertexai:16.x.y")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:35.1.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") }将您的 Android 项目与 Gradle 文件同步。
请注意,如果您选择不使用 Firebase Android BoM,只需添加 firebase-ai 库的依赖项,并接受 Android Studio 建议的最新版本。
Java
在模块(应用级)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 BoMimplementation("com.google.firebase:firebase-vertexai:16.x.y")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:35.1.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") }将您的 Android 项目与 Gradle 文件同步。
请注意,如果您选择不使用 Firebase Android BoM,只需添加 firebase-ai 库的依赖项,并接受 Android Studio 建议的最新版本。
Web
使用 npm 获取最新版本 Firebase JS SDK for Web:
npm i firebase@latest
或
yarn add firebase@latest
在您导入该库的所有位置,将 import 语句更新为改用
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
在 Flutter 项目目录中运行以下命令,更新
pubspec.yaml文件以使用firebase_ai软件包:flutter pub add firebase_ai
重新构建您的 Flutter 项目:
flutter run
完成应用迁移后(请参阅本指南中的其余部分),请务必删除旧软件包:
flutter pub remove firebase_vertexai
Unity
“Vertex AI in Firebase”不提供对 Unity 的支持。
第 4 步:更新应用中的初始化代码
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
更新为所选 API 提供方初始化服务的方式,并创建 GenerativeModel 实例。
Swift
import FirebaseAILogic
// 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-3.8-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-3.8-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-3.8-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-3.8-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-3.8-flash');
Unity
“Vertex AI in Firebase”不支持 Unity。
请注意,根据您使用的功能,您可能并不总是需要创建 GenerativeModel 实例。如需使用 Gemini Live API 流式传输输入和输出,请创建 LiveModel 实例。
第 5 步:根据您使用的功能更新代码
此步骤介绍了可能需要进行的更改,具体取决于您使用的功能。
如果您使用 Cloud Storage 网址,并且在此迁移中改用 Gemini Developer API,则需要更新多模态请求,以将文件作为内嵌数据包含在内(或对视频使用 YouTube 网址)。
请查看以下列表,了解您可能需要在代码中进行哪些更改,以便采用 Firebase AI Logic SDK。
Swift
没有其他更改。
Kotlin
Live API
- 移除了枚举类
ResponseModality的UNSPECIFIED值。而是改用null。
- 移除了枚举类
Java
Live API
- 移除了枚举类
ResponseModality的UNSPECIFIED值。而是改用null。
- 移除了枚举类
更改了各种 Java 构建器方法,现在可以正确返回其类的实例,而不是 void。
Web
仅当您开始使用 Gemini Developer API(而非 Agent Platform Gemini API (formerly Vertex AI))时,才需要进行更改:
安全设置
- 移除了不受支持的
SafetySetting.method的用法。
- 移除了不受支持的
内嵌数据
- 移除了不受支持的
InlineDataPart.videoMetadata的用法。
- 移除了不受支持的
Dart
没有其他更改。
Unity
“Vertex AI in Firebase”不支持 Unity。
就您使用 Firebase AI Logic 的体验提供反馈