Skip to content

Commit a2bd5e7

Browse files
feat(ai): add support for imageConfig and NO_IMAGE FinishReason
1 parent eb8e24a commit a2bd5e7

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.ai.type
18+
19+
/** Represents the aspect ratio that the generated image should conform to. */
20+
public class AspectRatio private constructor(internal val internalVal: String) {
21+
public companion object {
22+
@JvmField public val SQUARE_1x1: AspectRatio = AspectRatio("1:1")
23+
@JvmField public val PORTRAIT_2x3: AspectRatio = AspectRatio("2:3")
24+
@JvmField public val LANDSCAPE_3x2: AspectRatio = AspectRatio("3:2")
25+
@JvmField public val PORTRAIT_3x4: AspectRatio = AspectRatio("3:4")
26+
@JvmField public val LANDSCAPE_4x3: AspectRatio = AspectRatio("4:3")
27+
@JvmField public val PORTRAIT_4x5: AspectRatio = AspectRatio("4:5")
28+
@JvmField public val LANDSCAPE_5x4: AspectRatio = AspectRatio("5:4")
29+
@JvmField public val PORTRAIT_9x16: AspectRatio = AspectRatio("9:16")
30+
@JvmField public val LANDSCAPE_16x9: AspectRatio = AspectRatio("16:9")
31+
@JvmField public val LANDSCAPE_21x9: AspectRatio = AspectRatio("21:9")
32+
}
33+
}

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Candidate.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ public class FinishReason private constructor(public val name: String, public va
237237
BLOCKLIST,
238238
PROHIBITED_CONTENT,
239239
SPII,
240-
MALFORMED_FUNCTION_CALL;
240+
MALFORMED_FUNCTION_CALL,
241+
@SerialName("NO_IMAGE") NO_IMAGE;
241242

242243
internal object Serializer : KSerializer<Internal> by FirstOrdinalSerializer(Internal::class)
243244

@@ -252,6 +253,7 @@ public class FinishReason private constructor(public val name: String, public va
252253
PROHIBITED_CONTENT -> FinishReason.PROHIBITED_CONTENT
253254
SPII -> FinishReason.SPII
254255
MALFORMED_FUNCTION_CALL -> FinishReason.MALFORMED_FUNCTION_CALL
256+
NO_IMAGE -> FinishReason.NO_IMAGE
255257
else -> FinishReason.UNKNOWN
256258
}
257259
}
@@ -291,6 +293,9 @@ public class FinishReason private constructor(public val name: String, public va
291293
/** The function call generated by the model is invalid. */
292294
@JvmField
293295
public val MALFORMED_FUNCTION_CALL: FinishReason = FinishReason("MALFORMED_FUNCTION_CALL", 9)
296+
297+
/** No image was generated. */
298+
@JvmField public val NO_IMAGE: FinishReason = FinishReason("NO_IMAGE", 10)
294299
}
295300
}
296301

firebase-ai/src/main/kotlin/com/google/firebase/ai/type/GenerationConfig.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private constructor(
9292
internal val responseSchema: Schema?,
9393
internal val responseModalities: List<ResponseModality>?,
9494
internal val thinkingConfig: ThinkingConfig?,
95+
internal val imageConfig: ImageConfig?,
9596
) {
9697

9798
/**
@@ -137,6 +138,7 @@ private constructor(
137138
@JvmField public var responseSchema: Schema? = null
138139
@JvmField public var responseModalities: List<ResponseModality>? = null
139140
@JvmField public var thinkingConfig: ThinkingConfig? = null
141+
@JvmField public var imageConfig: ImageConfig? = null
140142

141143
public fun setTemperature(temperature: Float?): Builder = apply {
142144
this.temperature = temperature
@@ -170,6 +172,9 @@ private constructor(
170172
public fun setThinkingConfig(thinkingConfig: ThinkingConfig?): Builder = apply {
171173
this.thinkingConfig = thinkingConfig
172174
}
175+
public fun setImageConfig(imageConfig: ImageConfig?): Builder = apply {
176+
this.imageConfig = imageConfig
177+
}
173178

174179
/** Create a new [GenerationConfig] with the attached arguments. */
175180
public fun build(): GenerationConfig =
@@ -185,7 +190,8 @@ private constructor(
185190
responseMimeType = responseMimeType,
186191
responseSchema = responseSchema,
187192
responseModalities = responseModalities,
188-
thinkingConfig = thinkingConfig
193+
thinkingConfig = thinkingConfig,
194+
imageConfig = imageConfig
189195
)
190196
}
191197

@@ -202,7 +208,8 @@ private constructor(
202208
responseMimeType = responseMimeType,
203209
responseSchema = responseSchema?.toInternal(),
204210
responseModalities = responseModalities?.map { it.toInternal() },
205-
thinkingConfig = thinkingConfig?.toInternal()
211+
thinkingConfig = thinkingConfig?.toInternal(),
212+
imageConfig = imageConfig?.toInternal()
206213
)
207214

208215
@Serializable
@@ -218,7 +225,8 @@ private constructor(
218225
@SerialName("frequency_penalty") val frequencyPenalty: Float? = null,
219226
@SerialName("response_schema") val responseSchema: Schema.Internal? = null,
220227
@SerialName("response_modalities") val responseModalities: List<String>? = null,
221-
@SerialName("thinking_config") val thinkingConfig: ThinkingConfig.Internal? = null
228+
@SerialName("thinking_config") val thinkingConfig: ThinkingConfig.Internal? = null,
229+
@SerialName("image_config") val imageConfig: ImageConfig.Internal? = null
222230
)
223231

224232
public companion object {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.ai.type
18+
19+
/**
20+
* Configuration parameters to use for image generation.
21+
*
22+
* @property aspectRatio The aspect ratio of the generated image.
23+
*/
24+
public class ImageConfig internal constructor(internal val aspectRatio: AspectRatio?) {
25+
26+
/**
27+
* Builder for creating an [ImageConfig].
28+
*
29+
* Mainly intended for Java interop. Kotlin consumers should use [imageConfig] for a more
30+
* idiomatic experience.
31+
*
32+
* @property aspectRatio See [ImageConfig.aspectRatio].
33+
* @see [imageConfig]
34+
*/
35+
public class Builder {
36+
@JvmField public var aspectRatio: AspectRatio? = null
37+
38+
public fun setAspectRatio(aspectRatio: AspectRatio?): Builder = apply { this.aspectRatio = aspectRatio }
39+
40+
/** Create a new [ImageConfig] with the attached arguments. */
41+
public fun build(): ImageConfig = ImageConfig(aspectRatio = aspectRatio)
42+
}
43+
44+
internal fun toInternal() = Internal(aspectRatio = aspectRatio?.internalVal)
45+
46+
internal data class Internal(val aspectRatio: String?)
47+
48+
public companion object {
49+
50+
/**
51+
* Alternative casing for [ImageConfig.Builder]:
52+
* ```
53+
* val config = ImageConfig.builder()
54+
* ```
55+
*/
56+
public fun builder(): Builder = Builder()
57+
}
58+
}
59+
60+
/**
61+
* Helper method to construct an [ImageConfig] in a DSL-like manner.
62+
*
63+
* Example Usage:
64+
* ```
65+
* imageConfig {
66+
* aspectRatio = AspectRatio.LANDSCAPE_16x9
67+
* }
68+
* ```
69+
*/
70+
public fun imageConfig(init: ImageConfig.Builder.() -> Unit): ImageConfig {
71+
val builder = ImageConfig.builder()
72+
builder.init()
73+
return builder.build()
74+
}

0 commit comments

Comments
 (0)