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

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 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 All @@ -22,7 +64,6 @@ package com.google.firebase.vertexai {
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.FirebaseVertexAI getInstance();
method public static com.google.firebase.vertexai.FirebaseVertexAI getInstance(com.google.firebase.FirebaseApp app);
method public static com.google.firebase.vertexai.FirebaseVertexAI getInstance(com.google.firebase.FirebaseApp app = Firebase.app, String location);
method public static com.google.firebase.vertexai.FirebaseVertexAI getInstance(String location);
method @com.google.firebase.vertexai.type.PublicPreviewAPI public com.google.firebase.vertexai.ImagenModel imagenModel(String modelName);
Expand All @@ -40,7 +81,6 @@ package com.google.firebase.vertexai {

public static final class FirebaseVertexAI.Companion {
method public com.google.firebase.vertexai.FirebaseVertexAI getInstance();
method public com.google.firebase.vertexai.FirebaseVertexAI getInstance(com.google.firebase.FirebaseApp app);
method public com.google.firebase.vertexai.FirebaseVertexAI getInstance(com.google.firebase.FirebaseApp app = Firebase.app, String location);
method public com.google.firebase.vertexai.FirebaseVertexAI getInstance(String location);
property public final com.google.firebase.vertexai.FirebaseVertexAI instance;
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 in Vertex for Firebase_ 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-1.5-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.getGoogle()
}
}
}
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,
@Background private val backgroundDispatcher: CoroutineContext,
private val location: String,
private val appCheckProvider: Provider<InteropAppCheckTokenProvider>,
Expand Down Expand Up @@ -73,6 +75,13 @@ 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,
Expand All @@ -83,7 +92,7 @@ internal constructor(
)
}
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 @@ -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 @@ -174,7 +191,7 @@ internal constructor(
)
}
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,7 +219,7 @@ 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.getVertex(location)
}

private const val GEMINI_MODEL_NAME_PREFIX = "gemini-"
Expand All @@ -222,5 +237,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)
Loading
Loading