Firebase 中的 Vertex AI

如果您的应用已集成 Firebase,则 Firebase 中的 Vertex AI 允许您从应用中访问 Gemini API。

Vertex AI in Firebase integration architecture
图 1. Firebase 中的 Vertex AI 集成架构。

从 Google Client AI SDK 迁移

Firebase 中的 Vertex AI 的 API 与 Google Client AI SDK 类似。如果您已将 Google Client AI SDK 集成到您的应用中,您可以 迁移到 Firebase 中的 Vertex AI

入门

Google AI Client SDK 类似,您可以在 Google AI Studio 中试用提示。或者,如果您的国家/地区尚无法使用 Gemini API(查看列表),则可以使用 Vertex AI Studio

对提示满意后,请转到 Firebase 控制台中的 使用 Gemini 构建,然后点击第二张卡片以启动一个工作流,帮助您完成本文档中所述的任务。如果您没有看到卡片布局,则表示这些任务已完成。

此外,请执行以下操作

添加 Gradle 依赖项

将以下 Gradle 依赖项添加到您的应用模块

Kotlin

dependencies {
  ...
  implementation("com.google.firebase:firebase-vertexai:16.0.0-beta01")
}

Java

dependencies {

   [...]

   implementation("com.google.firebase:firebase-vertexai:16.0.0-beta01")

   // Required to use `ListenableFuture` from Guava Android for one-shot generation
   implementation("com.google.guava:guava:31.0.1-android")

   // Required to use `Publisher` from Reactive Streams for streaming operations
   implementation("org.reactivestreams:reactive-streams:1.0.4")
}

初始化 Vertex AI 服务和生成模型

请参阅 可用模型列表。首先通过提供模型版本来实例化一个 GenerativeModel

Kotlin

val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash-001")

Java

GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel("gemini-1.5-flash-001");

您可以在 Firebase 文档 中详细了解 Firebase 中的 Vertex AI 中可用的模型。您还可以 配置模型参数

接下来,您就可以与 Gemini API 进行交互了。

生成文本

要生成文本响应,请使用您的提示调用 GenerativeModel.generateContent()

Kotlin

// Note: `generateContent()` is a `suspend` function, which integrates well
// with existing Kotlin code.

scope.launch {
  val response = model.generateContent("Write a story about the green robot")
}

Java

// in Java, create a GenerativeModelFutures from the GenerativeModel. Note that
// generateContent() returns a ListenableFuture. Learn more:
// https://developer.android.com/develop/background-work/background-tasks/asynchronous/listenablefuture

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Content prompt = new Content.Builder()
    .addText("Write a story about a green robot.")
    .build();

ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

图像提示

要使用图像增强文本提示,请在调用 generateContent() 时将图像作为位图传递。

Kotlin

scope.launch {
  val response = model.generateContent(
    content {
      image(bitmap)
      text("what is the object in the picture?")
    }
  )
}

Java

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sparky);

Content prompt = new Content.Builder()
        .addImage(bitmap)
        .addText("What developer tool is this mascot from?")
        .build();

ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String resultText = result.getText();
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

多轮对话

您还可以支持多轮对话。使用 startChat() 函数初始化对话。您可以选择提供消息历史记录。然后调用 sendMessage() 函数发送聊天消息。

Kotlin

val chat = generativeModel.startChat(
    history = listOf(
        content(role = "user") { text("Hello, I have 2 dogs in my house.") },
        content(role = "model") { text("Great to meet you. What would you like to know?") }
    )
)

scope.launch {
   val response = chat.sendMessage("How many paws are in my house?")
}

Java

// (Optional) create message history
Content.Builder userContentBuilder = new Content.Builder();
userContentBuilder.setRole("user");
userContentBuilder.addText("Hello, I have 2 dogs in my house.");
Content userContent = userContentBuilder.build();

Content.Builder modelContentBuilder = new Content.Builder();
modelContentBuilder.setRole("model");
modelContentBuilder.addText("Great to meet you. What would you like to know?");
Content modelContent = userContentBuilder.build();

List<Content> history = Arrays.asList(userContent, modelContent);

// Initialize the chat
ChatFutures chat = model.startChat(history);

// Create a new user message
Content.Builder messageBuilder = new Content.Builder();
messageBuilder.setRole("user");
messageBuilder.addText("How many paws are in my house?");

Content message = messageBuilder.build();

Publisher<GenerateContentResponse> streamingResponse =
        chat.sendMessageStream(message);

StringBuilder outputContent = new StringBuilder();

streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
    @Override
    public void onNext(GenerateContentResponse generateContentResponse) {
        String chunk = generateContentResponse.getText();
        outputContent.append(chunk);
    }

    @Override
    public void onComplete() {
        // ...
    }

    @Override
    public void onError(Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onSubscribe(Subscription s) {
        s.request(Long.MAX_VALUE);
    }

});

响应流

要从生成第一个标记开始逐步显示响应,请使用 generateContentStream() 并收集响应流。

Kotlin

scope.launch {
  var outputContent = ""

  generativeModel.generateContentStream(inputContent)
          .collect { response ->
            outputContent += response.text
          }
}

Java

// Note that in Java the method generateContentStream() returns a
// Publisher from the Reactive Streams library.
// https://www.reactive-streams.org/

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// Provide a prompt that contains text
Content prompt = new Content.Builder()
        .addText("Write a story about a green robot.")
        .build();

Publisher<GenerateContentResponse> streamingResponse =
    model.generateContentStream(prompt);

StringBuilder outputContent = new StringBuilder();
streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
  @Override
  public void onNext(GenerateContentResponse generateContentResponse) {
    String chunk = generateContentResponse.getText();
    outputContent.append(chunk);
  }

  @Override
  public void onComplete() {
    // ...
  }

  @Override
  public void onError(Throwable t) {
    t.printStackTrace();
  }

  @Override
  public void onSubscribe(Subscription s) {
    s.request(Long.MAX_VALUE);
  }
});

后续步骤