本指南介绍了如何开始使用适用于所选平台的 Vertex AI in Firebase SDK 直接从应用调用 Vertex AI Gemini API。
使用 Gemini API 的其他方式
可选择试用 Gemini API 的替代“Google AI”版本
使用 Google AI Studio 和 Google AI 客户端 SDK 免费访问(在可用的情况下,且受限)。 这些 SDK 应仅用于移动应用和 Web 应用中的原型设计。熟悉 Gemini API 的运作方式后,请迁移到我们的 Vertex AI in Firebase SDK(本文档)。这些 SDK 提供了许多对移动应用和 Web 应用至关重要的其他功能,例如使用 Firebase App Check 保护 API 免遭滥用,以及支持请求中的大型媒体文件。
可选地在服务器端调用 Vertex AI Gemini API(例如使用 Python、Node.js 或 Go)
对于 Gemini API,请使用服务器端 Vertex AI SDK、Firebase Genkit 或 Firebase Extensions。
前提条件
本指南假定您熟悉如何使用 Android Studio 为 Android 开发应用。
确保您的开发环境和 Android 应用满足以下要求:
- Android Studio(最新版本)
- 您的 Android 应用必须以 API 级别 21 或更高级别为目标平台。
(可选)查看示例应用。
您可以快速试用 SDK,查看各种用例的完整实现,或者使用示例应用(如果您没有自己的 Android 应用)。如需使用示例应用,您需要将其关联到 Firebase 项目。
第 1 步:设置 Firebase 项目并将应用连接到 Firebase
如果您已有 Firebase 项目和已与 Firebase 相关联的应用
在 Firebase 控制台中,前往使用 Gemini 进行构建页面。
点击 Vertex AI in Firebase 卡片可启动一个工作流,帮助您完成以下任务:
升级您的项目,以使用随用随付 Blaze 定价方案。
在项目中启用所需的 API(Vertex AI API 和 Vertex AI in Firebase API)。
继续执行本指南中的下一步,将 SDK 添加到您的应用。
如果您还没有 Firebase 项目和与 Firebase 关联的应用
设置 Firebase 项目
登录 Firebase 控制台。
点击创建项目,然后使用以下任一方法:
方法 1:在“创建项目”工作流的第一步中输入新项目名称,创建一个全新的 Firebase 项目(该操作会自动创建相应的底层 Google Cloud 项目)。
方法 2:在“创建项目”工作流的第一步中从下拉菜单中选择一个现有 Google Cloud 项目的名称,将 Firebase 添加到该现成的 Google Cloud 项目。
请注意,如果系统提示您是否要设置 Google Analytics,您无需进行此项设置即可使用 Vertex AI in Firebase SDK。
在 Firebase 控制台中,前往使用 Gemini 进行构建页面。
点击 Vertex AI in Firebase 卡片可启动一个工作流,帮助您完成以下任务:
升级您的项目,以使用随用随付 Blaze 定价方案。
在项目中启用所需的 API(Vertex AI API 和 Vertex AI in Firebase API)。
将您的应用关联至 Firebase
继续在控制台的生成式 AI 工作流中操作,将您的应用关联到 Firebase,其中包括以下任务:
在 Firebase 项目中注册您的应用。
将 Firebase 配置文件 (
) 和google-services.json
Gradle 插件添加到您的应用。google-services
在本指南的后续步骤中,您将向应用添加 Vertex AI in Firebase SDK,并完成使用该 SDK 和 Gemini API 所需的初始化。
第 2 步:添加 SDK
设置完 Firebase 项目并将应用关联到 Firebase(请参阅上一步)后,您现在可以将 Vertex AI in Firebase SDK 添加到应用了。
Vertex AI in Firebase SDK for Android (firebase-vertexai
) 提供对 Vertex AI Gemini API 的访问权限。
在您的模块(应用级)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.9.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") }
对于 Java,您需要额外添加两个库。
dependencies { // ... other androidx dependencies // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.9.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 库的兼容版本。
(替代方法) 在不使用 BoM 的情况下添加 Firebase 库依赖项
如果您选择不使用 Firebase BoM,则必须在每个 Firebase 库的依赖项行中指定相应的库版本。
请注意,如果您在应用中使用多个 Firebase 库,我们强烈建议您使用 BoM 来管理库版本,从而确保所有版本都兼容。
dependencies { // Add the dependency for the Vertex AI in Firebase library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-vertexai:16.1.0") }
第 3 步:初始化 Vertex AI 服务和生成式模型
您需要先初始化 Vertex AI 服务和生成式模型,然后才能发出任何 API 调用。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")
Publisher
类型。
// Initialize the Vertex AI service and the generative model
// 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);
完成本入门指南后,了解如何选择适合您的应用场景和应用的 Gemini 模型和(可选)位置。
第 4 步:调用 Vertex AI Gemini API
现在,您已将应用与 Firebase 相关联、添加了 SDK,并初始化了 Vertex AI 服务和生成式模型,接下来就可以调用 Vertex AI Gemini API 了。
您可以使用 generateContent()
根据纯文本提示请求生成文本:
// Initialize the Vertex AI service and the generative model
// 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)
ListenableFuture
。
// Initialize the Vertex AI service and the generative model
// 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);
您还可以执行以下操作
详细了解 Gemini 模型
了解适用于各种用例的模型及其配额和价格。
试用 Gemini API 的其他功能
- 详细了解如何根据仅包含文本的提示生成文本,包括如何流式传输回答。
- 从多模态提示(包括文本、图片、PDF、视频和音频)生成文本。
- 构建多轮对话(聊天)。
- 从文本和多模态提示生成结构化输出(例如 JSON)。
- 使用函数调用将生成式模型连接到外部系统和信息。
了解如何控制内容生成
您还可以使用 Vertex AI Studio 对提示和模型配置进行实验。提供有关 Vertex AI in Firebase 使用体验的反馈