If your app is already integrating Firebase, Vertex AI in Firebase lets you access the Gemini API from your app.
Migration from Google client AI SDK
The API of Vertex AI in Firebase is similar to the Google AI client SDK. If you've already integrated the Google AI client SDK into your app, you can Migrate to Vertex AI in Firebase.
Getting started
Similar to the Google AI Client SDK, you can experiment with prompts in Google AI Studio. Alternatively, if the Gemini API isn't available in your country or region (see list), you can use Vertex AI studio.
Once you are satisfied with your prompts, go to Build with Gemini in the Firebase console, and then click the second card to launch a workflow that helps you do the tasks described in this document. If you don't see a card layout, then these tasks have already been completed.
In addition, do the following:
- Upgrade your project to use the Blaze pay-as-you-go pricing plan.
- Enable the following two APIs for your project:
aiplatform.googleapis.com
firebaseml.googleapis.com
.
Add the Gradle dependency
Add the following Gradle dependency to your app module:
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") }
Initialize the Vertex AI service and the generative model
See the list of available models.
Start by instantiating a GenerativeModel
by providing the model version:
Kotlin
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash-001")
Java
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel("gemini-1.5-flash-001");
You can learn more about the models available in Vertex AI in Firebase in the Firebase documentation. You can also configure model parameters.
Next, you're ready to interact with the Gemini API.
Generate text
To generate a text response, call GenerativeModel.generateContent()
with
your prompt.
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);
Image prompting
To augment text prompt with images, pass an image as a bitmap when calling
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);
Multi-turn chat
You can also support multi-turn conversations. Initialize a chat with the
startChat()
function. You can optionally provide a message history. Then
call the sendMessage()
function to send chat messages:
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); } });
Response streaming
To start displaying the response progressively as soon the first tokens are
produced, use generateContentStream()
, and collect the response stream:
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); } });
Next steps
- Review the Vertex AI in Firebase sample app on GitHub.
- Learn more about the Vertex AI in Firebase in the Firebase documentation.