本指南介绍了如何开始使用适用于所选平台的 Vertex AI in Firebase SDK 直接从应用调用 Vertex AI Gemini API。
请注意,您还可以使用本指南开始使用 Vertex AI in Firebase SDK 访问 Imagen 模型。
前提条件
Swift
本指南假定您熟悉如何使用 Xcode 为 Apple 平台(如 iOS)开发应用。
确保您的开发环境和 Apple 平台应用满足以下要求:
- Xcode 16.2 或更高版本
- 您的应用以 iOS 15 或更高版本或 macOS 12 或更高版本为目标平台
(可选)查看示例应用。
您可以快速试用 SDK,查看各种用例的完整实现,或者使用示例应用(如果您没有自己的 Apple 平台应用)。如需使用示例应用,您需要将其关联到 Firebase 项目。
Kotlin
本指南假定您熟悉如何使用 Android Studio 为 Android 开发应用。
确保您的开发环境和 Android 应用满足以下要求:
- Android Studio(最新版本)
- 您的应用以 API 级别 21 或更高级别为目标平台
(可选)查看示例应用。
您可以快速试用 SDK,查看各种用例的完整实现,或者使用示例应用(如果您没有自己的 Android 应用)。如需使用示例应用,您需要将其关联到 Firebase 项目。
Java
本指南假定您熟悉如何使用 Android Studio 为 Android 开发应用。
确保您的开发环境和 Android 应用满足以下要求:
- Android Studio(最新版本)
- 您的应用以 API 级别 21 或更高级别为目标平台
(可选)查看示例应用。
您可以快速试用 SDK,查看各种用例的完整实现,或者使用示例应用(如果您没有自己的 Android 应用)。如需使用示例应用,您需要将其关联到 Firebase 项目。
Web
本指南假定您熟悉如何使用 JavaScript 开发 Web 应用。本指南与框架无关。
确保您的开发环境和 Web 应用满足以下要求:
- (可选)Node.js
- 现代网络浏览器
(可选)查看示例应用。
您可以快速试用该 SDK,查看各种用例的完整实现,或者使用示例应用(如果您没有自己的 Web 应用)。如需使用示例应用,您需要将其关联到 Firebase 项目。
Dart
本指南假定您熟悉如何使用 Flutter 开发应用。
确保您的开发环境和 Flutter 应用满足以下要求:
- Dart 3.2.0 或更高版本
(可选)查看示例应用。
您可以快速试用该 SDK,查看各种用例的完整实现,或者使用示例应用(如果您没有自己的 Flutter 应用)。如需使用示例应用,您需要将其关联到 Firebase 项目。
第 1 步:设置 Firebase 项目并将您的应用连接到 Firebase
如果您已有 Firebase 项目和已与 Firebase 相关联的应用
在 Firebase 控制台中,前往 Vertex AI 页面。
点击 Vertex AI in Firebase 卡片可启动一个工作流,帮助您完成以下任务:
升级您的项目,以使用随用随付 Blaze 定价方案。
在项目中启用所需的 API(Vertex AI API 和 Vertex AI in Firebase API)。
继续执行本指南中的下一步,将 SDK 添加到您的应用。
如果您还没有 Firebase 项目和与 Firebase 关联的应用
第 2 步:添加 SDK
设置完 Firebase 项目并将应用关联到 Firebase(请参阅上一步)后,您现在可以将 Vertex AI in Firebase SDK 添加到应用了。
Swift
使用 Swift Package Manager 安装和管理 Firebase 依赖项。
Vertex AI in Firebase 库提供了用于与 Gemini 和 Imagen 模型交互的 API。该库包含在适用于 Apple 平台的 Firebase SDK (firebase-ios-sdk
) 中。
在 Xcode 中打开您的应用项目,依次点击 File(文件)> Add Packages(添加软件包)。
出现提示时,添加 Firebase Apple 平台 SDK 代码库:
https://github.com/firebase/firebase-ios-sdk
选择最新的 SDK 版本。
选择
FirebaseVertexAI
库。
完成之后,Xcode 将会自动开始在后台解析和下载您的依赖项。
Kotlin
Vertex AI in Firebase SDK for Android (firebase-vertexai
) 提供对 API 的访问权限,以便与 Gemini 和 Imagen 模型交互。
在您的模块(应用级)Gradle 文件(例如 <project>/<app-module>/build.gradle.kts
)中,添加适用于 Android 的 Vertex AI in Firebase 库的依赖项。我们建议使用 Firebase Android BoM 来实现库版本控制。
dependencies { // ... other androidx dependencies // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.13.0")) // Add the dependency for the Vertex AI in Firebase library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-vertexai") }
借助 Firebase Android BoM,可确保您的应用使用的始终是 Firebase Android 库的兼容版本。
Java
Vertex AI in Firebase SDK for Android (firebase-vertexai
) 提供对 API 的访问权限,以便与 Gemini 和 Imagen 模型交互。
在您的模块(应用级)Gradle 文件(例如 <project>/<app-module>/build.gradle.kts
)中,添加适用于 Android 的 Vertex AI in Firebase 库的依赖项。我们建议使用 Firebase Android BoM 来实现库版本控制。
对于 Java,您需要额外添加两个库。
dependencies { // ... other androidx dependencies // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.13.0")) // Add the dependency for the Vertex AI in Firebase library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-vertexai") // Required for one-shot operations (to use `ListenableFuture` from Guava Android) implementation("com.google.guava:guava:31.0.1-android") // Required for streaming operations (to use `Publisher` from Reactive Streams) implementation("org.reactivestreams:reactive-streams:1.0.4") }
借助 Firebase Android BoM,可确保您的应用使用的始终是 Firebase Android 库的兼容版本。
Web
Vertex AI in Firebase 库提供了用于与 Gemini 和 Imagen 模型交互的 API。该库包含在 Firebase JavaScript SDK for Web 中。
使用 npm 安装 Firebase JS SDK for Web:
npm install firebase
在您的应用中初始化 Firebase:
import { initializeApp } from "firebase/app"; // 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);
Dart
适用于 Flutter 的 Vertex AI in Firebase 插件 (firebase_vertexai
) 提供对 API 的访问权限,以便与 Gemini 和 Imagen 模型交互。
在您的 Flutter 项目目录中,运行以下命令以安装核心插件和 Vertex AI in Firebase 插件:
flutter pub add firebase_core && flutter pub add firebase_vertexai
在
lib/main.dart
文件中,导入您之前生成的 Firebase 核心插件、Vertex AI in Firebase 插件和配置文件:import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_vertexai/firebase_vertexai.dart'; import 'firebase_options.dart';
同样在
lib/main.dart
文件中,使用配置文件导出的DefaultFirebaseOptions
对象初始化 Firebase:await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, );
重新构建 Flutter 应用:
flutter run
第 3 步:初始化 Vertex AI 服务并创建 GenerativeModel
实例
您需要先初始化 Vertex AI 服务并创建 GenerativeModel
实例,然后才能进行任何 API 调用并向 Gemini 模型发送提示。
Swift
import FirebaseVertexAI
// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()
// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")
Kotlin
对于 Kotlin,此 SDK 中的方法是挂起函数,需要从协程作用域调用。// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")
Java
对于 Java,此 SDK 中的流式传输方法会从 Reactive Streams 库返回Publisher
类型。
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-2.0-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Web
import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";
// 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 service
const vertexAI = getVertexAI(firebaseApp);
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });
Dart
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');
完成本入门指南后,了解如何选择适合您的应用场景和应用的模型和(可选)位置。
第 4 步:向模型发送提示请求
现在,您已将应用与 Firebase 相关联、添加了 SDK,并初始化了 Vertex AI 服务和生成式模型,接下来可以向 Gemini 模型发送提示请求了。
您可以使用 generateContent()
从纯文本提示请求生成文本:
Swift
import FirebaseVertexAI
// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()
// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")
// Provide a prompt that contains text
let prompt = "Write a story about a magic backpack."
// To generate text output, call generateContent with the text input
let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")
Kotlin
对于 Kotlin,此 SDK 中的方法是挂起函数,需要从协程作用域调用。// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")
// Provide a prompt that contains text
val prompt = "Write a story about a magic backpack."
// To generate text output, call generateContent with the text input
val response = generativeModel.generateContent(prompt)
print(response.text)
Java
对于 Java,此 SDK 中的方法会返回ListenableFuture
。
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-2.0-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
// Provide a prompt that contains text
Content prompt = new Content.Builder()
.addText("Write a story about a magic backpack.")
.build();
// To generate text output, call generateContent with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";
// 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 service
const vertexAI = getVertexAI(firebaseApp);
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });
// Wrap in an async function so you can use await
async function run() {
// Provide a prompt that contains text
const prompt = "Write a story about a magic backpack."
// To generate text output, call generateContent with the text input
const result = await model.generateContent(prompt);
const response = result.response;
const text = response.text();
console.log(text);
}
run();
Dart
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');
// Provide a prompt that contains text
final prompt = [Content.text('Write a story about a magic backpack.')];
// To generate text output, call generateContent with the text input
final response = await model.generateContent(prompt);
print(response.text);
您还可以执行以下操作
详细了解支持的模型
了解适用于各种用例的模型及其配额和价格。
试用其他功能
- 详细了解如何根据仅包含文本的提示生成文本,包括如何流式传输回答。
- 通过提示各种文件类型(例如图片、PDF 文件、视频和音频)来生成文本。
- 构建多轮对话(聊天)。
- 从文本和多模态提示生成结构化输出(例如 JSON)。
- 根据文本提示生成图片。
- 使用 Gemini Live API 流式传输输入和输出(包括音频)。
- 使用函数调用将生成式模型连接到外部系统和信息。
了解如何控制内容生成
- 了解提示设计,包括最佳实践、策略和示例提示。
- 配置模型参数,例如温度和输出 token 数上限(适用于 Gemini)或宽高比和人物生成(适用于 Imagen)。
- 使用安全设置来调整收到可能被视为有害的回答的可能性。
就您使用 Vertex AI in Firebase 的体验提供反馈