Skip to content

Commit

Permalink
Add support for usage metadata (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlazo authored Mar 22, 2024
1 parent 4638406 commit b2afc55
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ sealed interface Response
data class GenerateContentResponse(
val candidates: List<Candidate>? = null,
val promptFeedback: PromptFeedback? = null,
val usageMetadata: UsageMetadata? = null
) : Response

@Serializable data class CountTokensResponse(val totalTokens: Int) : Response

@Serializable data class GRpcErrorResponse(val error: GRpcError) : Response

@Serializable
data class UsageMetadata(
val promptTokenCount: Int,
val candidatesTokenCount: Int,
val totalTokenCount: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.google.ai.client.generativeai.common.util.goldenUnaryFile
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.ktor.http.HttpStatusCode
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.withTimeout
Expand Down Expand Up @@ -132,6 +133,19 @@ internal class UnarySnapshotTests {
}
}

@Test
fun `response includes usage metadata`() =
goldenUnaryFile("success-usage-metadata.json") {
withTimeout(testTimeout) {
val response = apiController.generateContent(textGenerateContentRequest("prompt"))

response.candidates?.isEmpty() shouldBe false
response.candidates?.first()?.finishReason shouldBe FinishReason.STOP
response.usageMetadata shouldNotBe null
response.usageMetadata?.totalTokenCount shouldBe 363
}
}

@Test
fun `citation returns correctly when using alternative name`() =
goldenUnaryFile("success-citations-altname.json") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Mountain View, California, United States"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"safetyRatings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
],
"usageMetadata": {
"promptTokenCount": 6,
"candidatesTokenCount": 357,
"totalTokenCount": 363
},
"promptFeedback": {
"safetyRatings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
}

0 comments on commit b2afc55

Please sign in to comment.