Skip to content

Commit 4377cfb

Browse files
committed
feat(gemini-client): add GenerationConfig support
Introduce the GenerationConfig data class to configure text generation requests, including new properties such as temperature and responseMimeType. Additionally, add unit tests for GenerateContentRequest and GenerationConfig to ensure the correct serialization of these configurations.
1 parent 7d063e6 commit 4377cfb

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/api/textGeneration/api/GenerateContentRequest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ data class GenerateContentRequest(
2828
@SerialName("system_instruction")
2929
val systemInstruction: Content? = null,
3030
val contents: List<Content>,
31+
val generationConfig: GenerationConfig? = null,
3132
@Transient val model: GeminiModel = GeminiModel.GEMINI_1_5_FLASH,
3233
@Transient val stream: Boolean = false
3334
) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.tddworks.gemini.api.textGeneration.api
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* {
8+
* "contents": [{
9+
* "parts": [{
10+
* "text": "Write a story about a magic backpack."
11+
* }]
12+
* }],
13+
* "safetySettings": [{
14+
* "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
15+
* "threshold": "BLOCK_ONLY_HIGH"
16+
* }],
17+
* "generationConfig": {
18+
* "stopSequences": [
19+
* "Title"
20+
* ],
21+
* "temperature": 1.0,
22+
* "response_mime_type": "application/json",
23+
* "maxOutputTokens": 800,
24+
* "topP": 0.8,
25+
* "topK": 10
26+
* }
27+
* }
28+
*/
29+
@Serializable
30+
data class GenerationConfig(
31+
val temperature: Float? = null,
32+
@SerialName("response_mime_type") val responseMimeType: String? = null
33+
)

gemini-client/gemini-client-core/src/jvmTest/kotlin/com/tddworks/gemini/api/textGeneration/api/GenerateContentRequestTest.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,48 @@ import org.skyscreamer.jsonassert.JSONAssert
99

1010
class GenerateContentRequestTest {
1111

12+
@Test
13+
fun `should return correct text request with response mime type`() {
14+
// Given
15+
val generateContentRequest = GenerateContentRequest(
16+
contents = listOf(
17+
Content(
18+
parts = listOf(
19+
Part.TextPart(text = "hello")
20+
)
21+
)
22+
),
23+
stream = false,
24+
generationConfig = GenerationConfig(
25+
responseMimeType = "application/json"
26+
)
27+
)
28+
29+
// When
30+
val result = Json.encodeToString(
31+
GenerateContentRequest.serializer(),
32+
generateContentRequest
33+
)
34+
35+
// Then
36+
JSONAssert.assertEquals(
37+
"""
38+
{
39+
"contents": [{
40+
"parts": [{
41+
"text": "hello"
42+
}]
43+
}],
44+
"generationConfig": {
45+
"response_mime_type": "application/json"
46+
}
47+
}
48+
""".trimIndent(),
49+
result,
50+
false
51+
)
52+
}
53+
1254
@Test
1355
fun `should return correct text and image request`() {
1456

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.tddworks.gemini.api.textGeneration.api
2+
3+
import kotlinx.serialization.json.Json
4+
import org.junit.jupiter.api.Test
5+
import org.skyscreamer.jsonassert.JSONAssert
6+
7+
class GenerationConfigTest {
8+
9+
@Test
10+
fun `should return correct generation config`() {
11+
// Given
12+
val generationConfig = GenerationConfig(
13+
temperature = 1.0f,
14+
responseMimeType = "application/json"
15+
)
16+
17+
// When
18+
val result = Json.encodeToString(
19+
GenerationConfig.serializer(),
20+
generationConfig
21+
)
22+
23+
// Then
24+
JSONAssert.assertEquals(
25+
"""
26+
{
27+
"temperature": 1.0,
28+
"response_mime_type": "application/json"
29+
}
30+
""".trimIndent(),
31+
result,
32+
false
33+
)
34+
}
35+
}

0 commit comments

Comments
 (0)