Skip to content

Commit 7a313c3

Browse files
davidmotsonDavid Motsonashvili
and
David Motsonashvili
authored
Make default schema objects explicitly non-nullable, and add helpers … (#6013)
…to arr and obj --------- Co-authored-by: David Motsonashvili <[email protected]>
1 parent fef9525 commit 7a313c3

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class Schema<T>(
170170
val name: String,
171171
val description: String,
172172
val format: String? = null,
173+
val nullable: Boolean? = null,
173174
val enum: List<String>? = null,
174175
val properties: Map<String, Schema<out Any>>? = null,
175176
val required: List<String>? = null,
@@ -186,29 +187,64 @@ class Schema<T>(
186187
companion object {
187188
/** Registers a schema for an integer number */
188189
fun int(name: String, description: String) =
189-
Schema<Long>(name = name, description = description, type = FunctionType.INTEGER)
190+
Schema<Long>(
191+
name = name,
192+
description = description,
193+
type = FunctionType.INTEGER,
194+
nullable = false,
195+
)
190196

191197
/** Registers a schema for a string */
192198
fun str(name: String, description: String) =
193-
Schema<String>(name = name, description = description, type = FunctionType.STRING)
199+
Schema<String>(
200+
name = name,
201+
description = description,
202+
type = FunctionType.STRING,
203+
nullable = false,
204+
)
194205

195206
/** Registers a schema for a boolean */
196207
fun bool(name: String, description: String) =
197-
Schema<Boolean>(name = name, description = description, type = FunctionType.BOOLEAN)
208+
Schema<Boolean>(
209+
name = name,
210+
description = description,
211+
type = FunctionType.BOOLEAN,
212+
nullable = false,
213+
)
198214

199215
/** Registers a schema for a floating point number */
200216
fun num(name: String, description: String) =
201-
Schema<Double>(name = name, description = description, type = FunctionType.NUMBER)
217+
Schema<Double>(
218+
name = name,
219+
description = description,
220+
type = FunctionType.NUMBER,
221+
nullable = false,
222+
)
202223

203224
/**
204225
* Registers a schema for a complex object. In a function it will be returned as a [JSONObject]
205226
*/
206-
fun obj(name: String, description: String) =
207-
Schema<JSONObject>(name = name, description = description, type = FunctionType.OBJECT)
227+
fun obj(name: String, description: String, vararg contents: Schema<out Any>) =
228+
Schema<JSONObject>(
229+
name = name,
230+
description = description,
231+
type = FunctionType.OBJECT,
232+
required = contents.map { it.name },
233+
properties = contents.associateBy { it.name }.toMap(),
234+
)
208235

209-
/** Registers a schema for an array */
210-
fun arr(name: String, description: String) =
211-
Schema<List<String>>(name = name, description = description, type = FunctionType.ARRAY)
236+
/**
237+
* Registers a schema for an array.
238+
* @param items can be used to specify the type of the array
239+
*/
240+
fun arr(name: String, description: String, items: Schema<out Any>? = null) =
241+
Schema<List<String>>(
242+
name = name,
243+
description = description,
244+
type = FunctionType.ARRAY,
245+
items = items,
246+
nullable = false,
247+
)
212248

213249
/** Registers a schema for an enum */
214250
fun enum(name: String, description: String, values: List<String>) =
@@ -218,6 +254,7 @@ class Schema<T>(
218254
format = "enum",
219255
enum = values,
220256
type = FunctionType.STRING,
257+
nullable = false,
221258
)
222259
}
223260
}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Part.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class BlobPart(val mimeType: String, val blob: ByteArray) : Part
4949
* @param name the name of the function to call
5050
* @param args the function parameters and values as a [Map]
5151
*/
52-
class FunctionCallPart(val name: String, val args: Map<String, String>) : Part
52+
class FunctionCallPart(val name: String, val args: Map<String, String?>) : Part
5353

5454
/**
5555
* Represents function call output to be returned to the model when it requests a function call.

0 commit comments

Comments
 (0)