-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
747 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
187 changes: 187 additions & 0 deletions
187
firebase-vertexai/src/test/java/com/google/firebase/vertexai/StreamingSnapshotTests.kt
This file contains 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,187 @@ | ||
/* | ||
* Copyright 2024 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.type.BlockReason | ||
import com.google.firebase.vertexai.type.FinishReason | ||
import com.google.firebase.vertexai.type.HarmCategory | ||
import com.google.firebase.vertexai.type.InvalidAPIKeyException | ||
import com.google.firebase.vertexai.type.PromptBlockedException | ||
import com.google.firebase.vertexai.type.ResponseStoppedException | ||
import com.google.firebase.vertexai.type.SerializationException | ||
import com.google.firebase.vertexai.type.ServerException | ||
import com.google.firebase.vertexai.type.TextPart | ||
import com.google.firebase.vertexai.util.goldenStreamingFile | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldContain | ||
import io.ktor.http.HttpStatusCode | ||
import kotlin.time.Duration.Companion.seconds | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.withTimeout | ||
import org.junit.Test | ||
|
||
internal class StreamingSnapshotTests { | ||
private val testTimeout = 5.seconds | ||
|
||
@Test | ||
fun `short reply`() = | ||
goldenStreamingFile("success-basic-reply-short.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val responseList = responses.toList() | ||
responseList.isEmpty() shouldBe false | ||
responseList.first().candidates.first().finishReason shouldBe FinishReason.STOP | ||
responseList.first().candidates.first().content.parts.isEmpty() shouldBe false | ||
responseList.first().candidates.first().safetyRatings.isEmpty() shouldBe false | ||
} | ||
} | ||
|
||
@Test | ||
fun `long reply`() = | ||
goldenStreamingFile("success-basic-reply-long.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val responseList = responses.toList() | ||
responseList.isEmpty() shouldBe false | ||
responseList.forEach { | ||
it.candidates.first().finishReason shouldBe FinishReason.STOP | ||
it.candidates.first().content.parts.isEmpty() shouldBe false | ||
it.candidates.first().safetyRatings.isEmpty() shouldBe false | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `unknown enum`() = | ||
goldenStreamingFile("success-unknown-enum.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val responseList = responses.toList() | ||
|
||
responseList.isEmpty() shouldBe false | ||
responseList.any { | ||
it.candidates.any { it.safetyRatings.any { it.category == HarmCategory.UNKNOWN } } | ||
} shouldBe true | ||
} | ||
} | ||
|
||
@Test | ||
fun `quotes escaped`() = | ||
goldenStreamingFile("success-quotes-escaped.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val responseList = responses.toList() | ||
|
||
responseList.isEmpty() shouldBe false | ||
val part = responseList.first().candidates.first().content.parts.first() as? TextPart | ||
part.shouldNotBeNull() | ||
part.text shouldContain "\"" | ||
} | ||
} | ||
|
||
@Test | ||
fun `prompt blocked for safety`() = | ||
goldenStreamingFile("failure-prompt-blocked-safety.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val exception = shouldThrow<PromptBlockedException> { responses.collect() } | ||
exception.response.promptFeedback?.blockReason shouldBe BlockReason.SAFETY | ||
} | ||
} | ||
|
||
@Test | ||
fun `empty content`() = | ||
goldenStreamingFile("failure-empty-content.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { shouldThrow<SerializationException> { responses.collect() } } | ||
} | ||
|
||
@Test | ||
fun `http errors`() = | ||
goldenStreamingFile("failure-http-error.txt", HttpStatusCode.PreconditionFailed) { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { shouldThrow<ServerException> { responses.collect() } } | ||
} | ||
|
||
@Test | ||
fun `stopped for safety`() = | ||
goldenStreamingFile("failure-finish-reason-safety.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val exception = shouldThrow<ResponseStoppedException> { responses.collect() } | ||
exception.response.candidates.first().finishReason shouldBe FinishReason.SAFETY | ||
} | ||
} | ||
|
||
@Test | ||
fun `citation parsed correctly`() = | ||
goldenStreamingFile("success-citations.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val responseList = responses.toList() | ||
responseList.any { it.candidates.any { it.citationMetadata.isNotEmpty() } } shouldBe true | ||
} | ||
} | ||
|
||
@Test | ||
fun `stopped for recitation`() = | ||
goldenStreamingFile("failure-recitation-no-content.txt") { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { | ||
val exception = shouldThrow<ResponseStoppedException> { responses.collect() } | ||
exception.response.candidates.first().finishReason shouldBe FinishReason.RECITATION | ||
} | ||
} | ||
|
||
@Test | ||
fun `image rejected`() = | ||
goldenStreamingFile("failure-image-rejected.txt", HttpStatusCode.BadRequest) { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { shouldThrow<ServerException> { responses.collect() } } | ||
} | ||
|
||
@Test | ||
fun `unknown model`() = | ||
goldenStreamingFile("failure-unknown-model.txt", HttpStatusCode.NotFound) { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { shouldThrow<ServerException> { responses.collect() } } | ||
} | ||
|
||
@Test | ||
fun `invalid api key`() = | ||
goldenStreamingFile("failure-api-key.txt", HttpStatusCode.BadRequest) { | ||
val responses = model.generateContentStream("prompt") | ||
|
||
withTimeout(testTimeout) { shouldThrow<InvalidAPIKeyException> { responses.collect() } } | ||
} | ||
} |
Oops, something went wrong.