Skip to content

Commit

Permalink
add tests for generative content response and change #text to return … (
Browse files Browse the repository at this point in the history
#144)

…all text parts

---------

Co-authored-by: David Motsonashvili <davidmotson@google.com>
  • Loading branch information
davidmotson and David Motsonashvili authored May 9, 2024
1 parent 3c6c73c commit c746583
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class GenerateContentResponse(
val promptFeedback: PromptFeedback?,
val usageMetadata: UsageMetadata?
) {
/** Convenience field representing the first text part in the response, if it exists. */
val text: String? by lazy { firstPartAs<TextPart>()?.text }
/** Convenience field representing all the text parts in the response, if they exists. */
val text: String? by lazy {
candidates.first().content.parts.filterIsInstance<TextPart>().joinToString(" ") { it.text }
}

/** Convenience field representing the first function call part in the request, if it exists */
@Deprecated("Use functionCalls instead", ReplaceWith("functionCalls"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.ai.client.generativeai

import com.google.ai.client.generativeai.type.Candidate
import com.google.ai.client.generativeai.type.FunctionCallPart
import com.google.ai.client.generativeai.type.GenerateContentResponse
import com.google.ai.client.generativeai.type.content
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import org.junit.Test

internal class GenerateContentResponseTest {

@Test
fun `generate response should pull all functions requests`() {
val response =
GenerateContentResponse(
candidates =
listOf(
Candidate(
content {
part(FunctionCallPart("blah", mapOf()))
part(FunctionCallPart("blah2", mapOf()))
text("This is a textPart")
},
listOf(),
listOf(),
null
)
),
null,
null
)

response.functionCalls shouldHaveSize 2
}

@Test
fun `generate response should get strings even if they are not the first part`() {
val response =
GenerateContentResponse(
candidates =
listOf(
Candidate(
content {
part(FunctionCallPart("blah", mapOf()))
part(FunctionCallPart("blah2", mapOf()))
text("This is a textPart")
},
listOf(),
listOf(),
null
)
),
null,
null
)

response.text shouldBe "This is a textPart"
}

@Test
fun `generate response should get strings and concatenate them together`() {
val response =
GenerateContentResponse(
candidates =
listOf(
Candidate(
content {
part(FunctionCallPart("blah", mapOf()))
part(FunctionCallPart("blah2", mapOf()))
text("This is a textPart")
text("This is another textPart")
},
listOf(),
listOf(),
null
)
),
null,
null
)

response.text shouldBe "This is a textPart This is another textPart"
}
}

0 comments on commit c746583

Please sign in to comment.