-
Notifications
You must be signed in to change notification settings - Fork 642
[VertexAI] Add initial support to export covered API #6749
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
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2377948
Initial version of the serialization to json functionality
rlazo e6904a2
Move JSON format to more closely ressemble a discovery document
rlazo 04a76b8
Implement map operation as used by the Schema struct
rlazo ab7f6a9
Undo version change
rlazo f3945d0
Rename method and remove dead code
rlazo a0ec909
Move code to test directory
rlazo 971ccb6
Use a non-colliding name for the util
rlazo d56e534
Simplify code to make it easier to read
rlazo 4e442c6
Use the same name for the file as for the method
rlazo 3788154
Merge branch 'main' into rl.ser.to.json
rlazo 4c03d23
Update firebase-vertexai/src/test/java/com/google/firebase/vertexai/S…
rlazo 02d67c5
Add comment over TODO to describe the scenario
rlazo c139938
Add description for the `simpleNameFromSerialName` function
rlazo 39e5671
Merge branch 'main' into rl.ser.to.json
rlazo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
firebase-vertexai/src/test/java/com/google/firebase/vertexai/SerializationTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
/* | ||
* 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.vertexai.common.util.descriptorToJson | ||
import com.google.firebase.vertexai.type.Candidate | ||
import com.google.firebase.vertexai.type.CountTokensResponse | ||
import com.google.firebase.vertexai.type.GenerateContentResponse | ||
import com.google.firebase.vertexai.type.ModalityTokenCount | ||
import com.google.firebase.vertexai.type.Schema | ||
import io.kotest.assertions.json.shouldEqualJson | ||
import org.junit.Test | ||
|
||
internal class SerializationTests { | ||
@Test | ||
fun `test countTokensResponse serialization as Json`() { | ||
val expectedJsonAsString = | ||
""" | ||
{ | ||
"id": "CountTokensResponse", | ||
"type": "object", | ||
"properties": { | ||
"totalTokens": { | ||
"type": "integer" | ||
}, | ||
"totalBillableCharacters": { | ||
"type": "integer" | ||
}, | ||
"promptTokensDetails": { | ||
"type": "array", | ||
"items": { | ||
"${'$'}ref": "ModalityTokenCount" | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val actualJson = descriptorToJson(CountTokensResponse.Internal.serializer().descriptor) | ||
expectedJsonAsString shouldEqualJson actualJson.toString() | ||
} | ||
|
||
@Test | ||
fun `test modalityTokenCount serialization as Json`() { | ||
val expectedJsonAsString = | ||
""" | ||
{ | ||
"id": "ModalityTokenCount", | ||
"type": "object", | ||
"properties": { | ||
"modality": { | ||
"type": "string", | ||
"enum": [ | ||
"UNSPECIFIED", | ||
"TEXT", | ||
"IMAGE", | ||
"VIDEO", | ||
"AUDIO", | ||
"DOCUMENT" | ||
] | ||
}, | ||
"tokenCount": { | ||
"type": "integer" | ||
} | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val actualJson = descriptorToJson(ModalityTokenCount.Internal.serializer().descriptor) | ||
expectedJsonAsString shouldEqualJson actualJson.toString() | ||
} | ||
|
||
@Test | ||
fun `test GenerateContentResponse serialization as Json`() { | ||
val expectedJsonAsString = | ||
""" | ||
{ | ||
"id": "GenerateContentResponse", | ||
"type": "object", | ||
"properties": { | ||
"candidates": { | ||
"type": "array", | ||
"items": { | ||
"${'$'}ref": "Candidate" | ||
} | ||
}, | ||
"promptFeedback": { | ||
"${'$'}ref": "PromptFeedback" | ||
}, | ||
"usageMetadata": { | ||
"${'$'}ref": "UsageMetadata" | ||
} | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val actualJson = descriptorToJson(GenerateContentResponse.Internal.serializer().descriptor) | ||
expectedJsonAsString shouldEqualJson actualJson.toString() | ||
} | ||
|
||
@Test | ||
fun `test Candidate serialization as Json`() { | ||
val expectedJsonAsString = | ||
""" | ||
{ | ||
"id": "Candidate", | ||
"type": "object", | ||
"properties": { | ||
"content": { | ||
"${'$'}ref": "Content" | ||
}, | ||
"finishReason": { | ||
"type": "string", | ||
"enum": [ | ||
"UNKNOWN", | ||
"UNSPECIFIED", | ||
"STOP", | ||
"MAX_TOKENS", | ||
"SAFETY", | ||
"RECITATION", | ||
"OTHER", | ||
"BLOCKLIST", | ||
"PROHIBITED_CONTENT", | ||
"SPII", | ||
"MALFORMED_FUNCTION_CALL" | ||
] | ||
}, | ||
"safetyRatings": { | ||
"type": "array", | ||
"items": { | ||
"${'$'}ref": "SafetyRating" | ||
} | ||
}, | ||
"citationMetadata": { | ||
"${'$'}ref": "CitationMetadata" | ||
}, | ||
"groundingMetadata": { | ||
"${'$'}ref": "GroundingMetadata" | ||
} | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val actualJson = descriptorToJson(Candidate.Internal.serializer().descriptor) | ||
expectedJsonAsString shouldEqualJson actualJson.toString() | ||
} | ||
|
||
@Test | ||
fun `test Schema serialization as Json`() { | ||
// Unlike the actual schema in the background, we don't represent "type" as an enum, but | ||
// rather as a string. This is because we restrict we values can be used using helper methods | ||
// rather than type | ||
val expectedJsonAsString = | ||
""" | ||
{ | ||
"id": "Schema", | ||
"type": "object", | ||
"properties": { | ||
"type": { | ||
"type": "string" | ||
}, | ||
"format": { | ||
"type": "string" | ||
}, | ||
"description": { | ||
"type": "string" | ||
}, | ||
"nullable": { | ||
"type": "boolean" | ||
}, | ||
"items": { | ||
"${'$'}ref": "Schema" | ||
}, | ||
"enum": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"properties": { | ||
"type": "object", | ||
"additionalProperties": { | ||
"${'$'}ref": "Schema" | ||
} | ||
}, | ||
"required": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
.trimIndent() | ||
val actualJson = descriptorToJson(Schema.Internal.serializer().descriptor) | ||
expectedJsonAsString shouldEqualJson actualJson.toString() | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
firebase-vertexai/src/test/java/com/google/firebase/vertexai/common/util/descriptorToJson.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* 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.common.util | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialKind | ||
import kotlinx.serialization.descriptors.StructureKind | ||
import kotlinx.serialization.descriptors.elementDescriptors | ||
import kotlinx.serialization.descriptors.elementNames | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonObjectBuilder | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
import kotlinx.serialization.json.putJsonObject | ||
|
||
/** | ||
* Returns a [JsonObject] representing the classes in the hierarchy of a serialization [descriptor]. | ||
* | ||
* The format of the JSON object is similar to that of a Discovery Document, but restricted to these | ||
* fields: | ||
* - id | ||
* - type | ||
* - properties | ||
* - items | ||
* - $ref | ||
* | ||
* @param descriptor The [SerialDescriptor] to process. | ||
*/ | ||
@OptIn(ExperimentalSerializationApi::class) | ||
internal fun descriptorToJson(descriptor: SerialDescriptor): JsonObject { | ||
return buildJsonObject { | ||
put("id", simpleNameFromSerialName(descriptor.serialName)) | ||
put("type", typeNameFromKind(descriptor.kind)) | ||
if (descriptor.kind != StructureKind.CLASS) { | ||
throw UnsupportedOperationException("Only classes can be serialized to JSON for now.") | ||
} | ||
// For top-level enums, add them directly. | ||
if (descriptor.serialName == "FirstOrdinalSerializer") { | ||
addEnumDescription(descriptor) | ||
} else { | ||
addObjectProperties(descriptor) | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal fun JsonObjectBuilder.addListDescription(descriptor: SerialDescriptor) = | ||
putJsonObject("items") { | ||
val itemDescriptor = descriptor.elementDescriptors.first() | ||
val nestedIsPrimitive = (descriptor.elementsCount == 1 && itemDescriptor.kind is PrimitiveKind) | ||
if (nestedIsPrimitive) { | ||
put("type", typeNameFromKind(itemDescriptor.kind)) | ||
} else { | ||
put("\$ref", simpleNameFromSerialName(itemDescriptor.serialName)) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal fun JsonObjectBuilder.addEnumDescription(descriptor: SerialDescriptor): JsonElement? { | ||
put("type", typeNameFromKind(SerialKind.ENUM)) | ||
return put("enum", JsonArray(descriptor.elementNames.map { JsonPrimitive(it) })) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal fun JsonObjectBuilder.addObjectProperties(descriptor: SerialDescriptor): JsonElement? { | ||
return putJsonObject("properties") { | ||
for (i in 0 until descriptor.elementsCount) { | ||
val elementDescriptor = descriptor.getElementDescriptor(i) | ||
val elementName = descriptor.getElementName(i) | ||
putJsonObject(elementName) { | ||
when (elementDescriptor.kind) { | ||
StructureKind.LIST -> { | ||
put("type", typeNameFromKind(elementDescriptor.kind)) | ||
addListDescription(elementDescriptor) | ||
} | ||
StructureKind.CLASS -> { | ||
if (elementDescriptor.serialName.startsWith("FirstOrdinalSerializer")) { | ||
addEnumDescription(elementDescriptor) | ||
} else { | ||
put("\$ref", simpleNameFromSerialName(elementDescriptor.serialName)) | ||
} | ||
} | ||
StructureKind.MAP -> { | ||
put("type", typeNameFromKind(elementDescriptor.kind)) | ||
putJsonObject("additionalProperties") { | ||
put( | ||
"\$ref", | ||
simpleNameFromSerialName(elementDescriptor.getElementDescriptor(1).serialName) | ||
) | ||
} | ||
} | ||
else -> { | ||
put("type", typeNameFromKind(elementDescriptor.kind)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal fun typeNameFromKind(kind: SerialKind): String { | ||
return when (kind) { | ||
PrimitiveKind.BOOLEAN -> "boolean" | ||
PrimitiveKind.BYTE -> "integer" | ||
PrimitiveKind.CHAR -> "string" | ||
PrimitiveKind.DOUBLE -> "number" | ||
PrimitiveKind.FLOAT -> "number" | ||
PrimitiveKind.INT -> "integer" | ||
PrimitiveKind.LONG -> "integer" | ||
PrimitiveKind.SHORT -> "integer" | ||
PrimitiveKind.STRING -> "string" | ||
StructureKind.CLASS -> "object" | ||
StructureKind.LIST -> "array" | ||
SerialKind.ENUM -> "string" | ||
StructureKind.MAP -> "object" | ||
else -> TODO() | ||
rlazo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
internal fun simpleNameFromSerialName(serialName: String): String = | ||
rlazo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
serialName | ||
.split(".") | ||
.let { | ||
if (it.last().startsWith("Internal")) { | ||
it[it.size - 2] | ||
} else { | ||
it.last() | ||
} | ||
} | ||
.replace("?", "") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.