Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make common parse lenient #179

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/common/distribution-doll-drop-bridge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"PATCH","changes":["Make JSON decoding lenient. This should fix issues with JSON literals."]}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import kotlinx.serialization.json.Json
internal val JSON = Json {
ignoreUnknownKeys = true
prettyPrint = false
isLenient = true
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.google.ai.client.generativeai.common.shared.FunctionCallPart
import com.google.ai.client.generativeai.common.shared.HarmCategory
import com.google.ai.client.generativeai.common.shared.TextPart
import com.google.ai.client.generativeai.common.util.goldenUnaryFile
import com.google.ai.client.generativeai.common.util.shouldNotBeNullOrEmpty
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.collections.shouldNotBeEmpty
import io.kotest.matchers.nulls.shouldNotBeNull
Expand Down Expand Up @@ -313,4 +314,21 @@ internal class UnarySnapshotTests {
callPart.functionCall.args["season"] shouldBe null
}
}

@Test
fun `function call contains json literal`() =
goldenUnaryFile("success-function-call-json-literal.json") {
withTimeout(testTimeout) {
val response = apiController.generateContent(textGenerateContentRequest("prompt"))
val content = response.candidates.shouldNotBeNullOrEmpty().first().content
val callPart =
content.let {
it.shouldNotBeNull()
it.parts.shouldNotBeEmpty()
it.parts.first().shouldBeInstanceOf<FunctionCallPart>()
}

callPart.functionCall.args["current"] shouldBe "true"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import com.google.ai.client.generativeai.common.RequestOptions
import com.google.ai.client.generativeai.common.server.Candidate
import com.google.ai.client.generativeai.common.shared.Content
import com.google.ai.client.generativeai.common.shared.TextPart
import io.kotest.matchers.collections.shouldNotBeEmpty
import io.kotest.matchers.nulls.shouldNotBeNull
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.respond
import io.ktor.http.HttpHeaders
Expand Down Expand Up @@ -184,3 +186,14 @@ internal fun loadGoldenFile(path: String): File = loadResourceFile("golden-files

/** Loads a file from the test resources directory. */
internal fun loadResourceFile(path: String) = File("src/test/resources/$path")

/**
* Ensures that a collection is neither null or empty.
*
* Syntax sugar for [shouldNotBeNull] and [shouldNotBeEmpty].
*/
inline fun <reified T : Any> Collection<T>?.shouldNotBeNullOrEmpty(): Collection<T> {
shouldNotBeNull()
shouldNotBeEmpty()
return this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"candidates": [
{
"content": {
"parts": [
{
"functionCall": {
"name": "functionName",
"args": {
"original_title": "String",
"current": true
}
}
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"safetyRatings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
}
]
}
],
"usageMetadata": {
"promptTokenCount": 774,
"candidatesTokenCount": 4176,
"totalTokenCount": 4950
}
}
Loading