Skip to content

Commit

Permalink
Migrate VertexAI serialization to be localized
Browse files Browse the repository at this point in the history
  • Loading branch information
emilypgoogle committed Jan 17, 2025
1 parent 8e61ec2 commit bbfaa1e
Show file tree
Hide file tree
Showing 34 changed files with 850 additions and 847 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ 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.GRpcErrorResponse
import com.google.firebase.vertexai.type.InternalCountTokensResponse
import com.google.firebase.vertexai.type.InternalGenerateContentResponse
import com.google.firebase.vertexai.type.RequestOptions
import com.google.firebase.vertexai.type.Response
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.HttpClientEngine
Expand Down Expand Up @@ -106,31 +109,31 @@ internal constructor(
install(ContentNegotiation) { json(JSON) }
}

suspend fun generateContent(request: GenerateContentRequest): GenerateContentResponse =
suspend fun generateContent(request: GenerateContentRequest): InternalGenerateContentResponse =
try {
client
.post("${requestOptions.endpoint}/${requestOptions.apiVersion}/$model:generateContent") {
applyCommonConfiguration(request)
applyHeaderProvider()
}
.also { validateResponse(it) }
.body<GenerateContentResponse>()
.body<InternalGenerateContentResponse>()
.validate()
} catch (e: Throwable) {
throw FirebaseCommonAIException.from(e)
}

fun generateContentStream(request: GenerateContentRequest): Flow<GenerateContentResponse> =
fun generateContentStream(request: GenerateContentRequest): Flow<InternalGenerateContentResponse> =
client
.postStream<GenerateContentResponse>(
.postStream<InternalGenerateContentResponse>(
"${requestOptions.endpoint}/${requestOptions.apiVersion}/$model:streamGenerateContent?alt=sse"
) {
applyCommonConfiguration(request)
}
.map { it.validate() }
.catch { throw FirebaseCommonAIException.from(it) }

suspend fun countTokens(request: CountTokensRequest): CountTokensResponse =
suspend fun countTokens(request: CountTokensRequest): InternalCountTokensResponse =
try {
client
.post("${requestOptions.endpoint}/${requestOptions.apiVersion}/$model:countTokens") {
Expand Down Expand Up @@ -281,7 +284,7 @@ private fun getServiceDisabledErrorDetailsOrNull(error: GRpcError): GRpcErrorDet
}
}

private fun GenerateContentResponse.validate() = apply {
private fun InternalGenerateContentResponse.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 @@ -16,6 +16,7 @@

package com.google.firebase.vertexai.common

import com.google.firebase.vertexai.type.GenerateContentResponse
import io.ktor.serialization.JsonConvertException
import kotlinx.coroutines.TimeoutCancellationException

Expand Down Expand Up @@ -66,7 +67,7 @@ internal class InvalidAPIKeyException(message: String, cause: Throwable? = null)
* @property response the full server response for the request.
*/
internal class PromptBlockedException(
val response: GenerateContentResponse,
val response: GenerateContentResponse.InternalGenerateContentResponse,
cause: Throwable? = null
) :
FirebaseCommonAIException(
Expand Down Expand Up @@ -98,7 +99,7 @@ internal class InvalidStateException(message: String, cause: Throwable? = null)
* @property response the full server response for the request
*/
internal class ResponseStoppedException(
val response: GenerateContentResponse,
val response: GenerateContentResponse.InternalGenerateContentResponse,
cause: Throwable? = null
) :
FirebaseCommonAIException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.google.firebase.vertexai.common

import com.google.firebase.vertexai.common.client.GenerationConfig
import com.google.firebase.vertexai.common.client.Tool
import com.google.firebase.vertexai.common.client.ToolConfig
import com.google.firebase.vertexai.common.shared.Content
import com.google.firebase.vertexai.common.shared.SafetySetting
import com.google.firebase.vertexai.common.util.fullModelName
import com.google.firebase.vertexai.type.Content
import com.google.firebase.vertexai.type.GenerationConfig
import com.google.firebase.vertexai.type.SafetySetting
import com.google.firebase.vertexai.type.Tool
import com.google.firebase.vertexai.type.ToolConfig
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -30,21 +30,21 @@ internal sealed interface Request
@Serializable
internal data class GenerateContentRequest(
val model: String? = null,
val contents: List<Content>,
@SerialName("safety_settings") val safetySettings: List<SafetySetting>? = null,
@SerialName("generation_config") val generationConfig: GenerationConfig? = null,
val tools: List<Tool>? = null,
@SerialName("tool_config") var toolConfig: ToolConfig? = null,
@SerialName("system_instruction") val systemInstruction: Content? = null,
val contents: List<Content.InternalContent>,
@SerialName("safety_settings") val safetySettings: List<SafetySetting.InternalSafetySetting>? = null,
@SerialName("generation_config") val generationConfig: GenerationConfig.InternalGenerationConfig? = null,
val tools: List<Tool.InternalTool>? = null,
@SerialName("tool_config") var toolConfig: ToolConfig.InternalToolConfig? = null,
@SerialName("system_instruction") val systemInstruction: Content.InternalContent? = null,
) : Request

@Serializable
internal data class CountTokensRequest(
val generateContentRequest: GenerateContentRequest? = null,
val model: String? = null,
val contents: List<Content>? = null,
val tools: List<Tool>? = null,
@SerialName("system_instruction") val systemInstruction: Content? = null,
val contents: List<Content.InternalContent>? = null,
val tools: List<Tool.InternalTool>? = null,
@SerialName("system_instruction") val systemInstruction: Content.InternalContent? = null,
) : Request {
companion object {
fun forGenAI(generateContentRequest: GenerateContentRequest) =
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit bbfaa1e

Please sign in to comment.