Skip to content

Update endpoint #6300

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

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package com.google.firebase.vertexai.common

import android.util.Log
import com.google.firebase.Firebase
import com.google.firebase.options
import com.google.firebase.vertexai.common.server.FinishReason
import com.google.firebase.vertexai.common.server.GRpcError
import com.google.firebase.vertexai.common.server.GRpcErrorDetails
import com.google.firebase.vertexai.common.util.decodeToFlow
import com.google.firebase.vertexai.common.util.fullModelName
import com.google.firebase.vertexai.type.RequestOptions
Expand Down Expand Up @@ -239,12 +243,32 @@ private suspend fun validateResponse(response: HttpResponse) {
if (message.contains("quota")) {
throw QuotaExceededException(message)
}
if (error.details?.any { "SERVICE_DISABLED" == it.reason } == true) {
throw ServiceDisabledException(message)
getServiceDisabledErrorDetailsOrNull(error)?.let {
val errorMessage =
if (it.metadata?.get("service") == "firebasevertexai.googleapis.com") {
"""
The Vertex AI for Firebase SDK requires the Firebase Vertex AI API
`firebasevertexai.googleapis.com` to be enabled for your project. Enable it by visiting
the Firebase Console at https://console.firebase.google.com/project/${Firebase.options.projectId}/genai/vertex then
retry. If you enabled this API recently, wait a few minutes for the action to propagate
to our systems and retry.
"""
.trimIndent()
} else {
error.message
}

throw ServiceDisabledException(errorMessage)
}
throw ServerException(message)
}

private fun getServiceDisabledErrorDetailsOrNull(error: GRpcError): GRpcErrorDetails? {
return error.details?.firstOrNull {
it.reason == "SERVICE_DISABLED" && it.domain == "googleapis.com"
}
}

private fun GenerateContentResponse.validate() = apply {
if ((candidates?.isEmpty() != false) && promptFeedback == null) {
throw SerializationException("Error deserializing response, found no valid fields")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,9 @@ internal data class GRpcError(
val details: List<GRpcErrorDetails>? = null
)

@Serializable internal data class GRpcErrorDetails(val reason: String? = null)
@Serializable
internal data class GRpcErrorDetails(
val reason: String? = null,
val domain: String? = null,
val metadata: Map<String, String>? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import kotlin.time.toDuration
class RequestOptions
internal constructor(
internal val timeout: Duration,
internal val endpoint: String = "https://firebaseml.googleapis.com",
internal val apiVersion: String = "v2beta",
internal val endpoint: String = "https://firebasevertexai.googleapis.com",
internal val apiVersion: String = "v1beta",
) {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal class RequestFormatTests {
}
}

mockEngine.requestHistory.first().url.host shouldBe "firebaseml.googleapis.com"
mockEngine.requestHistory.first().url.host shouldBe "firebasevertexai.googleapis.com"
}

@Test
Expand Down
Loading