android-ai

google-ai-client-sdk
Edited: Sunday 2 June 2024

Google AI Client SDK
Note: The Google AI Client SDK is currently in preview and is recommended only for prototyping.
The Google AI Client SDK lets you to integrate the Gemini API to use Gemini Pro models in your Android app.

A free-of-charge tier lets you experiment at no cost. For other pricing details, see our pricing guide.

The Gemini API is available in some countries and regions. If the Gemini API isn’t available in your country or region, you can still access Gemini Pro models through the Vertex AI SDK for Android.

Gemini API integration architecture
Figure 1. Gemini API integration architecture.
Getting started
Design your prompt
Start by prototyping your prompt in Google AI Studio.

Google AI Studio is an IDE for prompt design and prototyping. It lets you upload files to test prompts with text and images and save a prompt to revisit it later.

Creating the right prompt for your use-case is more art than science, which makes experimentation critical. You can learn more about prompting in the official Gemini documentation.

Google AI Studio
Figure 2. Google AI Studio
To learn more about the advanced capabilities of Google AI Studio, see the Google AI Studio quickstart.

Generate your API Key
Once satisfied with your prompt, click Get API key to generate your Gemini API key. The key will be bundled with your application, which is ok for experimentation but not recommended for production use cases.

Also, to prevent your API key from being committed to your source code repository, use the Secrets gradle plugin.

Add the gradle dependency
Add the dependency for the Google AI client SDK to your app:

dependencies {
…
implementation(“com.google.ai.client.generativeai:generativeai:latest.version”)
}
Integrate Google AI Client SDK to your Kotlin code:
Create a GenerativeModel
Start by instantiating a GenerativeModel by providing the following:

The model: gemini-1.0-pro, gemini-1.0-pro-vision, or gemini-1.5-pro
Your API key generated with Google AI Studio.
You can optionally define the model parameters and provide values for the temperature, topK, topP, and the maximum output tokens.

You can also define the safety features for the following topics:

HARASSMENT
HATE_SPEECH
SEXUALLY_EXPLICIT
DANGEROUS_CONTENT

val model = GenerativeModel(
model = “gemini-1.5-pro”,
apiKey = BuildConfig.apikey,
generationConfig = generationConfig {
temperature = 0.15f
topK = 32
topP = 1f
maxOutputTokens = 4096
},
safetySettings = listOf(
SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.MEDIUM_AND_ABOVE),
SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE),
SafetySetting(HarmCategory.SEXUALLY_EXPLICIT, BlockThreshold.MEDIUM_AND_ABOVE),
SafetySetting(HarmCategory.DANGEROUS_CONTENT, BlockThreshold.MEDIUM_AND_ABOVE),
)
)

Text generation
To generate a text response, call GenerativeModel.generateContent() with your prompt:

scope.launch {
val response = model.generateContent(“Write a story about a green robot.”)
}
Note that generateContent() is a suspend function, which integrates well with existing Kotlin code.

Image prompting
To augment text prompt with images, pass an image as a bitmap when calling generateContent():

scope.launch {
val response = model.generateContent(
content {
image(bitmap)
text(“What is the object in this picture?”)
}
)
}
Multiturn chat
The Google AI client SDK also lets you implement 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:

val chat = model.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 {
chat.sendMessage(“How many paws are in my house?”)
}
Response streaming
To start displaying the response progressively as soon the first tokens are produced, use generateContentStream(), and collect the response stream:

someScope.launch {
var outputContent = ""

generativeModel.generateContentStream(inputContent)
.collect { response ->
outputContent += response.text
}
}
Android Studio
Android Studio provides additional tools to help you get started.

Gemini API starter template: This starter template helps you create an API key directly from Android Studio and generates a project that includes the necessary Android dependencies to use the Gemini APIs.
Generative AI sample: This sample lets you import the Google AI SDK for Android sample app in Android Studio.
Next steps
Review the Google AI SDK for Android sample app on GitHub.
Learn more about the Google AI client SDK in the official Gemini documentation.