通过 Vertex AI in Firebase SDK 开始使用 Gemini API


本指南将为您介绍如何开始调用 Vertex AI Gemini API Vertex AI in Firebase SDK(适用于您所选平台)。

前提条件

本指南假定您熟悉使用 Android Studio 开发 Android 应用

  • 确保您的开发环境和 Android 应用符合 以下要求:

    • Android Studio(最新版本)
    • 您的 Android 应用必须以 API 级别 21 或更高级别为目标平台。
  • (可选)查看示例应用。

    下载示例应用

    您可以快速试用该 SDK,并查看各种用法的完整实现。 或使用示例应用(如果您没有自己的 Android 应用)。 要使用示例应用,您需要: 将其关联到 Firebase 项目

第 1 步:设置一个 Firebase 项目,并将您的应用关联到 Firebase

如果您已有 Firebase 项目和已关联到 Firebase 的应用

  1. Firebase 控制台中,前往 Build with Gemini 页面中, 然后点击第二张卡片启动工作流程 后续任务。如果您在控制台中看到 Vertex AI 的标签页,则 这些任务都已完成。

  2. 继续执行本指南中的下一步,将 SDK 添加到您的应用中。

如果您还没有将 Firebase 项目和应用与 Firebase 相关联


第 2 步:添加 SDK

设置好 Firebase 项目并将应用关联到 Firebase 后 (参见上一步),您现在可以将 Vertex AI in Firebase SDK 添加到您的应用。

Android 版 Vertex AI in Firebase SDK (firebase-vertexai) 提供了 对 Vertex AI Gemini API 的访问权限。

在您的模块(应用级)Gradle 配置文件中 (如 <project>/<app-module>/build.gradle.kts),添加 Vertex AI in Firebase SDK for Android:

Kotlin+KTX

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Vertex AI in Firebase SDK for Android
  implementation("com.google.firebase:firebase-vertexai:16.0.0-beta04")
}

Java

对于 Java,您需要添加两个额外的库。

dependencies {
  // ... other androidx dependencies

  // add the dependency for the Vertex AI in Firebase SDK for Android
  implementation("com.google.firebase:firebase-vertexai:16.0.0-beta04")

  // 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 服务和生成模型

您需要先初始化 Vertex AI,然后才能进行任何 API 调用。 服务和生成模型。

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的其他功能

了解如何控制内容生成

您还可以使用以下方法对提示和模型配置进行实验: Vertex AI Studio


提供反馈 分享您对Vertex AI in Firebase的体验