From 7853cf452225ac489c492a25a29efba3d09215d2 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Mon, 30 Sep 2024 17:27:39 -0700 Subject: [PATCH 1/7] remove extra converstions to string --- .../com/google/firebase/vertexai/common/shared/Types.kt | 2 +- .../google/firebase/vertexai/internal/util/conversions.kt | 8 ++++---- .../unary/success-function-call-json-literal.json | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/shared/Types.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/shared/Types.kt index f6c1bc22b88..c34af862def 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/shared/Types.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/shared/Types.kt @@ -62,7 +62,7 @@ internal data class FunctionResponsePart(val functionResponse: FunctionResponse) @Serializable internal data class FunctionResponse(val name: String, val response: JsonObject) @Serializable -internal data class FunctionCall(val name: String, val args: Map? = null) +internal data class FunctionCall(val name: String, val args: Map? = null) @Serializable internal data class FileDataPart(@SerialName("file_data") val fileData: FileData) : Part diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt index 5a86ee8de4a..c460c414e00 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt @@ -58,8 +58,9 @@ import com.google.firebase.vertexai.type.content import java.io.ByteArrayOutputStream import java.util.Calendar import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject -import kotlinx.serialization.json.JsonPrimitive import org.json.JSONObject private const val BASE_64_FLAGS = Base64.NO_WRAP @@ -99,7 +100,7 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part internal fun FunctionCall.toInternal() = com.google.firebase.vertexai.common.shared.FunctionCall( name, - args.orEmpty().mapValues { it.value.toString() } + args ) internal fun FunctionResponse.toInternal() = @@ -240,8 +241,7 @@ internal fun com.google.firebase.vertexai.common.shared.FunctionCall.toPublic() FunctionCall( name, args.orEmpty().mapValues { - val argValue = it.value - if (argValue == null) JsonPrimitive(null) else Json.parseToJsonElement(argValue) + it.value ?: JsonNull } ) diff --git a/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json b/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json index fe4571880a8..a713757634e 100644 --- a/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json +++ b/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json @@ -7,7 +7,7 @@ "functionCall": { "name": "functionName", "args": { - "original_title": "String", + "original_title": "String test", "current": true } } From 40975a84a232121561027111d143ed8a01354dbd Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Mon, 30 Sep 2024 17:28:35 -0700 Subject: [PATCH 2/7] remove extra diff --- .../golden-files/unary/success-function-call-json-literal.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json b/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json index a713757634e..fe4571880a8 100644 --- a/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json +++ b/firebase-vertexai/src/test/resources/golden-files/unary/success-function-call-json-literal.json @@ -7,7 +7,7 @@ "functionCall": { "name": "functionName", "args": { - "original_title": "String test", + "original_title": "String", "current": true } } From 7a2e0d9355b1f4954f410c7a55bb6dba1a98f393 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Mon, 30 Sep 2024 17:33:34 -0700 Subject: [PATCH 3/7] format --- .../firebase/vertexai/internal/util/conversions.kt | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt index c460c414e00..ed846375d23 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt @@ -58,7 +58,6 @@ import com.google.firebase.vertexai.type.content import java.io.ByteArrayOutputStream import java.util.Calendar import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject import org.json.JSONObject @@ -98,10 +97,7 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part } internal fun FunctionCall.toInternal() = - com.google.firebase.vertexai.common.shared.FunctionCall( - name, - args - ) + com.google.firebase.vertexai.common.shared.FunctionCall(name, args) internal fun FunctionResponse.toInternal() = com.google.firebase.vertexai.common.shared.FunctionResponse(name, response) @@ -238,12 +234,7 @@ internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part { } internal fun com.google.firebase.vertexai.common.shared.FunctionCall.toPublic() = - FunctionCall( - name, - args.orEmpty().mapValues { - it.value ?: JsonNull - } - ) + FunctionCall(name, args.orEmpty().mapValues { it.value ?: JsonNull }) internal fun com.google.firebase.vertexai.common.shared.FunctionResponse.toPublic() = FunctionResponse( From 6e06134f3648f0c9127b1da6b95dc3d5ee185b50 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Mon, 30 Sep 2024 17:50:12 -0700 Subject: [PATCH 4/7] remove final reference to genai-common --- .github/workflows/ci_tests.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/ci_tests.yml b/.github/workflows/ci_tests.yml index 508d5d6f8e5..56ae9711c0e 100644 --- a/.github/workflows/ci_tests.yml +++ b/.github/workflows/ci_tests.yml @@ -56,14 +56,6 @@ jobs: distribution: temurin cache: gradle - - name: Pull genai-common - if: matrix.module == ':firebase-vertexai' - run: | - git clone https://github.com/google-gemini/generative-ai-android.git - cd generative-ai-android - ./gradlew :common:updateVersion common:publishToMavenLocal - cd .. - - name: Clone mock responses if: matrix.module == ':firebase-vertexai' run: | From 3a2e29ea57cb50a962ba5487c8e77b680d4a2953 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Tue, 1 Oct 2024 10:24:35 -0700 Subject: [PATCH 5/7] add extra unary snapshot test & rollback changes to CI --- .github/workflows/ci_tests.yml | 8 ++++++++ .../firebase/vertexai/UnarySnapshotTests.kt | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/.github/workflows/ci_tests.yml b/.github/workflows/ci_tests.yml index 56ae9711c0e..508d5d6f8e5 100644 --- a/.github/workflows/ci_tests.yml +++ b/.github/workflows/ci_tests.yml @@ -56,6 +56,14 @@ jobs: distribution: temurin cache: gradle + - name: Pull genai-common + if: matrix.module == ':firebase-vertexai' + run: | + git clone https://github.com/google-gemini/generative-ai-android.git + cd generative-ai-android + ./gradlew :common:updateVersion common:publishToMavenLocal + cd .. + - name: Clone mock responses if: matrix.module == ':firebase-vertexai' run: | diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt index 50113660208..32e6fe37183 100644 --- a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt @@ -372,6 +372,23 @@ internal class UnarySnapshotTests { } } + @Test + fun `function call with complex json literal parses correctly`() = + goldenUnaryFile("unary-success-function-call-complex-json-literal.json") { + withTimeout(testTimeout) { + val response = model.generateContent("prompt") + val content = response.candidates.shouldNotBeNullOrEmpty().first().content + val callPart = + content.let { + it.shouldNotBeNull() + it.parts.shouldNotBeEmpty() + it.parts.first().shouldBeInstanceOf() + } + + callPart.functionCall.args["current"] shouldBe JsonPrimitive(true) + } + } + @Test fun `function call contains no arguments`() = goldenUnaryFile("unary-success-function-call-no-arguments.json") { From cad0c758064d45148462730d19593586ddab0ed0 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Tue, 1 Oct 2024 13:01:54 -0700 Subject: [PATCH 6/7] fix common tests --- .../com/google/firebase/vertexai/common/UnarySnapshotTests.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/common/UnarySnapshotTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/common/UnarySnapshotTests.kt index b08eb104248..6749a31e952 100644 --- a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/common/UnarySnapshotTests.kt +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/common/UnarySnapshotTests.kt @@ -36,6 +36,7 @@ import io.ktor.http.HttpStatusCode import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.withTimeout import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonPrimitive import org.junit.Test @Serializable internal data class MountainColors(val name: String, val colors: List) @@ -330,7 +331,7 @@ internal class UnarySnapshotTests { } callPart.functionCall.args shouldNotBe null - callPart.functionCall.args?.get("current") shouldBe "true" + callPart.functionCall.args?.get("current") shouldBe JsonPrimitive(true) } } From b7af14d715059471e52cb5489b65e7c4e7b3467c Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Tue, 1 Oct 2024 13:10:17 -0700 Subject: [PATCH 7/7] add extra specificity to new test --- .../java/com/google/firebase/vertexai/UnarySnapshotTests.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt index 32e6fe37183..a71d8e16c08 100644 --- a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/UnarySnapshotTests.kt @@ -45,6 +45,8 @@ import io.ktor.http.HttpStatusCode import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.withTimeout import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive import org.json.JSONArray import org.junit.Test @@ -386,6 +388,10 @@ internal class UnarySnapshotTests { } callPart.functionCall.args["current"] shouldBe JsonPrimitive(true) + callPart.functionCall.args["testObject"]!! + .jsonObject["testProperty"]!! + .jsonPrimitive + .content shouldBe "string property" } }