Skip to content

Add support for GoogleAI to firebase-vertexai #6772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
42 changes: 42 additions & 0 deletions firebase-vertexai/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,48 @@ package com.google.firebase.vertexai {
property public final java.util.List<com.google.firebase.vertexai.type.Content> history;
}

public final class FirebaseGenAI {
ctor public FirebaseGenAI();
method public com.google.firebase.vertexai.FirebaseGoogleAI getGoogleAI();
method public com.google.firebase.vertexai.FirebaseVertexAI getVertexAI();
method public com.google.firebase.vertexai.FirebaseGoogleAI googleAI();
method public com.google.firebase.vertexai.FirebaseGoogleAI googleAI(com.google.firebase.FirebaseApp app = Firebase.app);
method public com.google.firebase.vertexai.FirebaseVertexAI vertexAI();
method public com.google.firebase.vertexai.FirebaseVertexAI vertexAI(com.google.firebase.FirebaseApp app = Firebase.app);
method public com.google.firebase.vertexai.FirebaseVertexAI vertexAI(com.google.firebase.FirebaseApp app = Firebase.app, String location = "us-central1");
property public final com.google.firebase.vertexai.FirebaseGoogleAI googleAI;
property public final com.google.firebase.vertexai.FirebaseVertexAI vertexAI;
}

public final class FirebaseGenAIKt {
method public static com.google.firebase.vertexai.FirebaseGenAI genAI(com.google.firebase.Firebase);
method public static com.google.firebase.vertexai.FirebaseGenAI getGenAI(com.google.firebase.Firebase);
}

public final class FirebaseGoogleAI {
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName);
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName, com.google.firebase.vertexai.type.GenerationConfig? generationConfig = null);
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName, com.google.firebase.vertexai.type.GenerationConfig? generationConfig = null, java.util.List<com.google.firebase.vertexai.type.SafetySetting>? safetySettings = null);
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName, com.google.firebase.vertexai.type.GenerationConfig? generationConfig = null, java.util.List<com.google.firebase.vertexai.type.SafetySetting>? safetySettings = null, java.util.List<com.google.firebase.vertexai.type.Tool>? tools = null);
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName, com.google.firebase.vertexai.type.GenerationConfig? generationConfig = null, java.util.List<com.google.firebase.vertexai.type.SafetySetting>? safetySettings = null, java.util.List<com.google.firebase.vertexai.type.Tool>? tools = null, com.google.firebase.vertexai.type.ToolConfig? toolConfig = null);
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName, com.google.firebase.vertexai.type.GenerationConfig? generationConfig = null, java.util.List<com.google.firebase.vertexai.type.SafetySetting>? safetySettings = null, java.util.List<com.google.firebase.vertexai.type.Tool>? tools = null, com.google.firebase.vertexai.type.ToolConfig? toolConfig = null, com.google.firebase.vertexai.type.Content? systemInstruction = null);
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName, com.google.firebase.vertexai.type.GenerationConfig? generationConfig = null, java.util.List<com.google.firebase.vertexai.type.SafetySetting>? safetySettings = null, java.util.List<com.google.firebase.vertexai.type.Tool>? tools = null, com.google.firebase.vertexai.type.ToolConfig? toolConfig = null, com.google.firebase.vertexai.type.Content? systemInstruction = null, com.google.firebase.vertexai.type.RequestOptions requestOptions = com.google.firebase.vertexai.type.RequestOptions());
method public static com.google.firebase.vertexai.FirebaseGoogleAI getInstance();
method public static com.google.firebase.vertexai.FirebaseGoogleAI getInstance(com.google.firebase.FirebaseApp app = Firebase.app);
method @com.google.firebase.vertexai.type.PublicPreviewAPI public com.google.firebase.vertexai.ImagenModel imagenModel(String modelName);
method @com.google.firebase.vertexai.type.PublicPreviewAPI public com.google.firebase.vertexai.ImagenModel imagenModel(String modelName, com.google.firebase.vertexai.type.ImagenGenerationConfig? generationConfig = null);
method @com.google.firebase.vertexai.type.PublicPreviewAPI public com.google.firebase.vertexai.ImagenModel imagenModel(String modelName, com.google.firebase.vertexai.type.ImagenGenerationConfig? generationConfig = null, com.google.firebase.vertexai.type.ImagenSafetySettings? safetySettings = null);
method @com.google.firebase.vertexai.type.PublicPreviewAPI public com.google.firebase.vertexai.ImagenModel imagenModel(String modelName, com.google.firebase.vertexai.type.ImagenGenerationConfig? generationConfig = null, com.google.firebase.vertexai.type.ImagenSafetySettings? safetySettings = null, com.google.firebase.vertexai.type.RequestOptions requestOptions = com.google.firebase.vertexai.type.RequestOptions());
property public static final com.google.firebase.vertexai.FirebaseGoogleAI instance;
field public static final com.google.firebase.vertexai.FirebaseGoogleAI.Companion Companion;
}

public static final class FirebaseGoogleAI.Companion {
method public com.google.firebase.vertexai.FirebaseGoogleAI getInstance();
method public com.google.firebase.vertexai.FirebaseGoogleAI getInstance(com.google.firebase.FirebaseApp app = Firebase.app);
property public final com.google.firebase.vertexai.FirebaseGoogleAI instance;
}

public final class FirebaseVertexAI {
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName);
method public com.google.firebase.vertexai.GenerativeModel generativeModel(String modelName, com.google.firebase.vertexai.type.GenerationConfig? generationConfig = null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.vertexai

import com.google.firebase.Firebase
import com.google.firebase.FirebaseApp
import com.google.firebase.app

/** Entry point for all Firebase Generative AI functionality. */
public class FirebaseGenAI {

/**
* Returns the [FirebaseVertexAI] instance for the provided [FirebaseApp] and [location].
*
* @param location location identifier, defaults to `us-central1`; see available
* [Vertex AI regions](https://firebase.google.com/docs/vertex-ai/locations?platform=android#available-locations)
* .
*/
@JvmOverloads
public fun vertexAI(
app: FirebaseApp = Firebase.app,
location: String = "us-central1",
): FirebaseVertexAI = FirebaseVertexAI.getInstance(app, location)

/** Returns the [FirebaseGoogleAI] instance for the provided [FirebaseApp]. */
@JvmOverloads
public fun googleAI(app: FirebaseApp = Firebase.app): FirebaseGoogleAI =
FirebaseGoogleAI.getInstance(app)

/** Returns the [FirebaseGoogleAI] instance of the default [FirebaseApp]. */
public val googleAI: FirebaseGoogleAI
get() = FirebaseGoogleAI.instance

/** Returns the [FirebaseVertexAI] instance of the default [FirebaseApp]. */
public val vertexAI: FirebaseVertexAI
get() = FirebaseVertexAI.instance

internal companion object {
internal val INSTANCE = FirebaseGenAI()
}
}

/** Returns the [FirebaseGenAI] instance. */
public val Firebase.genAI: FirebaseGenAI
get() = FirebaseGenAI()

/** Returns the [FirebaseGenAI] instance. */
public fun Firebase.genAI(): FirebaseGenAI = FirebaseGenAI.INSTANCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.vertexai

import com.google.firebase.Firebase
import com.google.firebase.FirebaseApp
import com.google.firebase.app
import com.google.firebase.vertexai.type.Content
import com.google.firebase.vertexai.type.GenerationConfig
import com.google.firebase.vertexai.type.ImagenGenerationConfig
import com.google.firebase.vertexai.type.ImagenSafetySettings
import com.google.firebase.vertexai.type.PublicPreviewAPI
import com.google.firebase.vertexai.type.RequestOptions
import com.google.firebase.vertexai.type.SafetySetting
import com.google.firebase.vertexai.type.Tool
import com.google.firebase.vertexai.type.ToolConfig

/** Entry point for all _Google AI_ functionality. */
public class FirebaseGoogleAI internal constructor(private val proxy: FirebaseVertexAI) {

/**
* Instantiates a new [GenerativeModel] given the provided parameters.
*
* @param modelName The name of the model to use, for example `"gemini-2.0-pro"`.
* @param generationConfig The configuration parameters to use for content generation.
* @param safetySettings The safety bounds the model will abide to during content generation.
* @param tools A list of [Tool]s the model may use to generate content.
* @param toolConfig The [ToolConfig] that defines how the model handles the tools provided.
* @param systemInstruction [Content] instructions that direct the model to behave a certain way.
* Currently only text content is supported.
* @param requestOptions Configuration options for sending requests to the backend.
* @return The initialized [GenerativeModel] instance.
*/
@JvmOverloads
public fun generativeModel(
modelName: String,
generationConfig: GenerationConfig? = null,
safetySettings: List<SafetySetting>? = null,
tools: List<Tool>? = null,
toolConfig: ToolConfig? = null,
systemInstruction: Content? = null,
requestOptions: RequestOptions = RequestOptions(),
): GenerativeModel =
proxy.generativeModel(
modelName,
generationConfig,
safetySettings,
tools,
toolConfig,
systemInstruction,
requestOptions,
)

/**
* Instantiates a new [ImagenModel] given the provided parameters.
*
* @param modelName The name of the model to use, for example `"imagen-3.0-generate-001"`.
* @param generationConfig The configuration parameters to use for image generation.
* @param safetySettings The safety bounds the model will abide by during image generation.
* @param requestOptions Configuration options for sending requests to the backend.
* @return The initialized [ImagenModel] instance.
*/
@JvmOverloads
@PublicPreviewAPI
public fun imagenModel(
modelName: String,
generationConfig: ImagenGenerationConfig? = null,
safetySettings: ImagenSafetySettings? = null,
requestOptions: RequestOptions = RequestOptions(),
): ImagenModel = proxy.imagenModel(modelName, generationConfig, safetySettings, requestOptions)

public companion object {
/** The [FirebaseGoogleAI] instance for the default [FirebaseApp] */
@JvmStatic
public val instance: FirebaseGoogleAI
get() = getInstance()

/** Returns the [FirebaseGoogleAI] instance for the provided [FirebaseApp]. */
@JvmStatic
public fun getInstance(app: FirebaseApp = Firebase.app): FirebaseGoogleAI {
val multiResourceComponent = app[FirebaseVertexAIMultiResourceComponent::class.java]
return multiResourceComponent.getGoogleAI()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.google.firebase.auth.internal.InternalAuthProvider
import com.google.firebase.inject.Provider
import com.google.firebase.vertexai.type.Content
import com.google.firebase.vertexai.type.GenerationConfig
import com.google.firebase.vertexai.type.GenerativeBackend
import com.google.firebase.vertexai.type.ImagenGenerationConfig
import com.google.firebase.vertexai.type.ImagenSafetySettings
import com.google.firebase.vertexai.type.InvalidLocationException
Expand All @@ -41,6 +42,7 @@ import kotlin.coroutines.CoroutineContext
public class FirebaseVertexAI
internal constructor(
private val firebaseApp: FirebaseApp,
private val backend: GenerativeBackend,
@Blocking private val blockingDispatcher: CoroutineContext,
private val location: String,
private val appCheckProvider: Provider<InteropAppCheckTokenProvider>,
Expand Down Expand Up @@ -73,17 +75,24 @@ internal constructor(
if (location.trim().isEmpty() || location.contains("/")) {
throw InvalidLocationException(location)
}
val modelUri =
when (backend) {
GenerativeBackend.VERTEX_AI ->
"projects/${firebaseApp.options.projectId}/locations/${location}/publishers/google/models/${modelName}"
GenerativeBackend.GOOGLE_AI ->
"projects/${firebaseApp.options.projectId}/models/${modelName}"
}
if (!modelName.startsWith(GEMINI_MODEL_NAME_PREFIX)) {
Log.w(
TAG,
"""Unsupported Gemini model "${modelName}"; see
https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names.
"""
.trimIndent()
.trimIndent(),
)
}
return GenerativeModel(
"projects/${firebaseApp.options.projectId}/locations/${location}/publishers/google/models/${modelName}",
modelUri,
firebaseApp.options.apiKey,
firebaseApp,
generationConfig,
Expand All @@ -92,6 +101,7 @@ internal constructor(
toolConfig,
systemInstruction,
requestOptions,
backend,
appCheckProvider.get(),
internalAuthProvider.get(),
)
Expand Down Expand Up @@ -123,7 +133,7 @@ internal constructor(
"""Unsupported Gemini model "$modelName"; see
https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names.
"""
.trimIndent()
.trimIndent(),
)
}
if (location.trim().isEmpty() || location.contains("/")) {
Expand Down Expand Up @@ -161,6 +171,13 @@ internal constructor(
safetySettings: ImagenSafetySettings? = null,
requestOptions: RequestOptions = RequestOptions(),
): ImagenModel {
val modelUri =
when (backend) {
GenerativeBackend.VERTEX_AI ->
"projects/${firebaseApp.options.projectId}/locations/${location}/publishers/google/models/${modelName}"
GenerativeBackend.GOOGLE_AI ->
"projects/${firebaseApp.options.projectId}/models/${modelName}"
}
if (location.trim().isEmpty() || location.contains("/")) {
throw InvalidLocationException(location)
}
Expand All @@ -170,11 +187,11 @@ internal constructor(
"""Unsupported Imagen model "${modelName}"; see
https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names.
"""
.trimIndent()
.trimIndent(),
)
}
return ImagenModel(
"projects/${firebaseApp.options.projectId}/locations/${location}/publishers/google/models/${modelName}",
modelUri,
firebaseApp.options.apiKey,
firebaseApp,
generationConfig,
Expand All @@ -191,8 +208,6 @@ internal constructor(
public val instance: FirebaseVertexAI
get() = getInstance(location = "us-central1")

@JvmStatic public fun getInstance(app: FirebaseApp): FirebaseVertexAI = getInstance(app)

/**
* Returns the [FirebaseVertexAI] instance for the provided [FirebaseApp] and [location].
*
Expand All @@ -204,9 +219,12 @@ internal constructor(
@JvmOverloads
public fun getInstance(app: FirebaseApp = Firebase.app, location: String): FirebaseVertexAI {
val multiResourceComponent = app[FirebaseVertexAIMultiResourceComponent::class.java]
return multiResourceComponent.get(location)
return multiResourceComponent.getVertexAI(location)
}

/** Returns the [FirebaseVertexAI] instance for the provided [FirebaseApp] */
@JvmStatic public fun getInstance(app: FirebaseApp): FirebaseVertexAI = getInstance(app)

private const val GEMINI_MODEL_NAME_PREFIX = "gemini-"

private const val IMAGEN_MODEL_NAME_PREFIX = "imagen-"
Expand All @@ -222,5 +240,5 @@ public val Firebase.vertexAI: FirebaseVertexAI
/** Returns the [FirebaseVertexAI] instance of a given [FirebaseApp]. */
public fun Firebase.vertexAI(
app: FirebaseApp = Firebase.app,
location: String = "us-central1"
location: String = "us-central1",
): FirebaseVertexAI = FirebaseVertexAI.getInstance(app, location)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.firebase.annotations.concurrent.Blocking
import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
import com.google.firebase.auth.internal.InternalAuthProvider
import com.google.firebase.inject.Provider
import com.google.firebase.vertexai.type.GenerativeBackend
import kotlin.coroutines.CoroutineContext

/**
Expand All @@ -36,18 +37,38 @@ internal class FirebaseVertexAIMultiResourceComponent(
private val internalAuthProvider: Provider<InternalAuthProvider>,
) {

@GuardedBy("this") private val instances: MutableMap<String, FirebaseVertexAI> = mutableMapOf()
@GuardedBy("this")
private val vertexInstances: MutableMap<String, FirebaseVertexAI> = mutableMapOf()

fun get(location: String): FirebaseVertexAI =
@GuardedBy("this") private val googleInstance: MutableList<FirebaseGoogleAI> = mutableListOf()

fun getVertexAI(location: String): FirebaseVertexAI =
synchronized(this) {
instances[location]
vertexInstances[location]
?: FirebaseVertexAI(
app,
GenerativeBackend.VERTEX_AI,
blockingDispatcher,
location,
appCheckProvider,
internalAuthProvider
internalAuthProvider,
)
.also { vertexInstances[location] = it }
}

fun getGoogleAI(): FirebaseGoogleAI =
synchronized(this) {
googleInstance.getOrNull(0)
?: FirebaseGoogleAI(
FirebaseVertexAI(
app,
GenerativeBackend.GOOGLE_AI,
backgroundDispatcher,
"UNUSED",
appCheckProvider,
internalAuthProvider,
)
)
.also { instances[location] = it }
.also { googleInstance.add(it) }
}
}
Loading
Loading