|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.vertexai |
| 18 | + |
| 19 | +import com.google.firebase.vertexai.type.BlockReason |
| 20 | +import com.google.firebase.vertexai.type.FinishReason |
| 21 | +import com.google.firebase.vertexai.type.HarmCategory |
| 22 | +import com.google.firebase.vertexai.type.InvalidAPIKeyException |
| 23 | +import com.google.firebase.vertexai.type.PromptBlockedException |
| 24 | +import com.google.firebase.vertexai.type.ResponseStoppedException |
| 25 | +import com.google.firebase.vertexai.type.SerializationException |
| 26 | +import com.google.firebase.vertexai.type.ServerException |
| 27 | +import com.google.firebase.vertexai.type.TextPart |
| 28 | +import com.google.firebase.vertexai.util.goldenStreamingFile |
| 29 | +import io.kotest.assertions.throwables.shouldThrow |
| 30 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 31 | +import io.kotest.matchers.shouldBe |
| 32 | +import io.kotest.matchers.string.shouldContain |
| 33 | +import io.ktor.http.HttpStatusCode |
| 34 | +import kotlin.time.Duration.Companion.seconds |
| 35 | +import kotlinx.coroutines.flow.collect |
| 36 | +import kotlinx.coroutines.flow.first |
| 37 | +import kotlinx.coroutines.flow.toList |
| 38 | +import kotlinx.coroutines.withTimeout |
| 39 | +import org.junit.Test |
| 40 | + |
| 41 | +internal class StreamingSnapshotTests { |
| 42 | + private val testTimeout = 5.seconds |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `short reply`() = |
| 46 | + goldenStreamingFile("success-basic-reply-short.txt") { |
| 47 | + val responses = model.generateContentStream("prompt") |
| 48 | + |
| 49 | + withTimeout(testTimeout) { |
| 50 | + val responseList = responses.toList() |
| 51 | + responseList.isEmpty() shouldBe false |
| 52 | + responseList.first().candidates.first().finishReason shouldBe FinishReason.STOP |
| 53 | + responseList.first().candidates.first().content.parts.isEmpty() shouldBe false |
| 54 | + responseList.first().candidates.first().safetyRatings.isEmpty() shouldBe false |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `long reply`() = |
| 60 | + goldenStreamingFile("success-basic-reply-long.txt") { |
| 61 | + val responses = model.generateContentStream("prompt") |
| 62 | + |
| 63 | + withTimeout(testTimeout) { |
| 64 | + val responseList = responses.toList() |
| 65 | + responseList.isEmpty() shouldBe false |
| 66 | + responseList.forEach { |
| 67 | + it.candidates.first().finishReason shouldBe FinishReason.STOP |
| 68 | + it.candidates.first().content.parts.isEmpty() shouldBe false |
| 69 | + it.candidates.first().safetyRatings.isEmpty() shouldBe false |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `unknown enum`() = |
| 76 | + goldenStreamingFile("success-unknown-enum.txt") { |
| 77 | + val responses = model.generateContentStream("prompt") |
| 78 | + |
| 79 | + withTimeout(testTimeout) { |
| 80 | + val responseList = responses.toList() |
| 81 | + |
| 82 | + responseList.isEmpty() shouldBe false |
| 83 | + responseList.any { |
| 84 | + it.candidates.any { it.safetyRatings.any { it.category == HarmCategory.UNKNOWN } } |
| 85 | + } shouldBe true |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `quotes escaped`() = |
| 91 | + goldenStreamingFile("success-quotes-escaped.txt") { |
| 92 | + val responses = model.generateContentStream("prompt") |
| 93 | + |
| 94 | + withTimeout(testTimeout) { |
| 95 | + val responseList = responses.toList() |
| 96 | + |
| 97 | + responseList.isEmpty() shouldBe false |
| 98 | + val part = responseList.first().candidates.first().content.parts.first() as? TextPart |
| 99 | + part.shouldNotBeNull() |
| 100 | + part.text shouldContain "\"" |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `prompt blocked for safety`() = |
| 106 | + goldenStreamingFile("failure-prompt-blocked-safety.txt") { |
| 107 | + val responses = model.generateContentStream("prompt") |
| 108 | + |
| 109 | + withTimeout(testTimeout) { |
| 110 | + val exception = shouldThrow<PromptBlockedException> { responses.collect() } |
| 111 | + exception.response.promptFeedback?.blockReason shouldBe BlockReason.SAFETY |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `empty content`() = |
| 117 | + goldenStreamingFile("failure-empty-content.txt") { |
| 118 | + val responses = model.generateContentStream("prompt") |
| 119 | + |
| 120 | + withTimeout(testTimeout) { shouldThrow<SerializationException> { responses.collect() } } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + fun `http errors`() = |
| 125 | + goldenStreamingFile("failure-http-error.txt", HttpStatusCode.PreconditionFailed) { |
| 126 | + val responses = model.generateContentStream("prompt") |
| 127 | + |
| 128 | + withTimeout(testTimeout) { shouldThrow<ServerException> { responses.collect() } } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun `stopped for safety`() = |
| 133 | + goldenStreamingFile("failure-finish-reason-safety.txt") { |
| 134 | + val responses = model.generateContentStream("prompt") |
| 135 | + |
| 136 | + withTimeout(testTimeout) { |
| 137 | + val exception = shouldThrow<ResponseStoppedException> { responses.collect() } |
| 138 | + exception.response.candidates.first().finishReason shouldBe FinishReason.SAFETY |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun `citation parsed correctly`() = |
| 144 | + goldenStreamingFile("success-citations.txt") { |
| 145 | + val responses = model.generateContentStream("prompt") |
| 146 | + |
| 147 | + withTimeout(testTimeout) { |
| 148 | + val responseList = responses.toList() |
| 149 | + responseList.any { it.candidates.any { it.citationMetadata.isNotEmpty() } } shouldBe true |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun `stopped for recitation`() = |
| 155 | + goldenStreamingFile("failure-recitation-no-content.txt") { |
| 156 | + val responses = model.generateContentStream("prompt") |
| 157 | + |
| 158 | + withTimeout(testTimeout) { |
| 159 | + val exception = shouldThrow<ResponseStoppedException> { responses.collect() } |
| 160 | + exception.response.candidates.first().finishReason shouldBe FinishReason.RECITATION |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + fun `image rejected`() = |
| 166 | + goldenStreamingFile("failure-image-rejected.txt", HttpStatusCode.BadRequest) { |
| 167 | + val responses = model.generateContentStream("prompt") |
| 168 | + |
| 169 | + withTimeout(testTimeout) { shouldThrow<ServerException> { responses.collect() } } |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + fun `unknown model`() = |
| 174 | + goldenStreamingFile("failure-unknown-model.txt", HttpStatusCode.NotFound) { |
| 175 | + val responses = model.generateContentStream("prompt") |
| 176 | + |
| 177 | + withTimeout(testTimeout) { shouldThrow<ServerException> { responses.collect() } } |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + fun `invalid api key`() = |
| 182 | + goldenStreamingFile("failure-api-key.txt", HttpStatusCode.BadRequest) { |
| 183 | + val responses = model.generateContentStream("prompt") |
| 184 | + |
| 185 | + withTimeout(testTimeout) { shouldThrow<InvalidAPIKeyException> { responses.collect() } } |
| 186 | + } |
| 187 | +} |
0 commit comments