本指南将为您介绍如何开始调用 使用 Vertex AI for Firebase SDK。
前提条件
本指南假定您熟悉使用 Android Studio 开发 Android 应用
确保您的开发环境和 Android 应用符合 以下要求:
- Android Studio(最新版本)
- 您的 Android 应用必须以 API 级别 21 或更高级别为目标平台。
(可选)查看示例应用。
您可以快速试用该 SDK,并查看各种用法的完整实现。 或使用示例应用(如果您没有自己的 Android 应用)。 要使用示例应用,您需要: 将其关联到 Firebase 项目。
第 1 步:设置一个 Firebase 项目,并将您的应用关联到 Firebase
如果您已有 Firebase 项目和已关联到 Firebase 的应用
在 Firebase 控制台中,前往 Build with Gemini 页面 然后点击第二张卡片启动工作流程 后续任务。如果您在控制台中看到 Vertex AI 标签页, 这些任务都已完成。
升级您的项目以使用 Blaze 随用随付定价方案。
为您的项目启用以下两个 API:
aiplatform.googleapis.com
和firebaseml.googleapis.com
。
继续执行本指南中的下一步,将 SDK 添加到您的应用中。
如果您还没有将 Firebase 项目和应用与 Firebase 相关联
第 2 步:添加 SDK
设置好 Firebase 项目并将应用关联到 Firebase 后 (请参阅上一步),您现在可以将 Vertex AI for Firebase SDK 添加到您的应用。
Android 版 Vertex AI for Firebase SDK (firebase-vertexai
) 提供了
访问 Vertex AI Gemini API。
在您的模块(应用级)Gradle 配置文件中
(如 <project>/<app-module>/build.gradle.kts
),添加
Android 版 Vertex AI for Firebase SDK:
Kotlin+KTX
dependencies {
// ... other androidx dependencies
// add the dependency for the Vertex AI for Firebase SDK for Android
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta03")
}
Java
对于 Java,您需要添加两个额外的库。
dependencies {
// ... other androidx dependencies
// add the dependency for the Vertex AI for Firebase SDK for Android
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta03")
// 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")
}
第 3 步:初始化 Vertex AI 服务和生成模型
在进行任何 API 调用之前,您需要先初始化 Vertex AI 服务和生成模型。
Kotlin+KTX
对于 Kotlin,此 SDK 中的方法是挂起函数,需要调用 从协程作用域进行控制。// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash")
Java
对于 Java,此 SDK 中的流式传输方法会返回 Reactive Streams 库中的Publisher
类型。
// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-1.5-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()
根据纯文本提示生成文本
请求:
Kotlin+KTX
对于 Kotlin,此 SDK 中的方法是挂起函数,需要调用 从协程作用域进行控制。// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-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 the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-1.5-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 文件、视频和音频)。
- 建立多轮对话(聊天)。
- 使用函数调用进行连接 生成模型与外部系统和信息共享。
了解如何控制内容生成
。 您还可以使用以下方法对提示和模型配置进行实验: Vertex AI Studio。提供反馈 Vertex AI for Firebase 使用体验