From 410df27ab95af7db7025f544e18add57e4040a4d Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Tue, 10 Sep 2024 09:35:37 -0400 Subject: [PATCH 01/15] Create schema builder file --- .../firebase/vertexai/type/SchemaBuilder.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt new file mode 100644 index 00000000000..24011a739a6 --- /dev/null +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt @@ -0,0 +1,21 @@ +/* + * 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.firebase.vertexai.type + +class SchemaBuilder { + private lateinit var schema: Schema +} \ No newline at end of file From dd97a942557e98c5464171bd13637879843a384a Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Tue, 10 Sep 2024 12:00:53 -0400 Subject: [PATCH 02/15] Update VertexAI schema declaration to better match genAI --- .../vertexai/internal/util/conversions.kt | 7 +- .../vertexai/type/FunctionDeclarations.kt | 315 ++++-------------- .../vertexai/type/FunctionParameter.kt | 2 +- .../vertexai/type/GenerationConfig.kt | 4 +- .../com/google/firebase/vertexai/type/Type.kt | 19 +- 5 files changed, 80 insertions(+), 267 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt index b16d5ffad36..2b52ee66021 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt @@ -158,13 +158,14 @@ internal fun FunctionDeclaration.toInternal() = name, description, Schema( - properties = getParameters().associate { it.name to it.toInternal() }, - required = getParameters().map { it.name }, + properties = parameters.associate { it.name to it.toInternal() }, + required = requiredParameters, type = "OBJECT", + nullable = false, ), ) -internal fun com.google.firebase.vertexai.type.Schema.toInternal(): Schema = +internal fun com.google.firebase.vertexai.type.Schema.toInternal(): Schema = Schema( type.name, description, diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt index 3d836bad27b..bc81f3dec91 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt @@ -19,172 +19,54 @@ package com.google.firebase.vertexai.type import org.json.JSONObject /** - * A declared zero param function, including implementation, that a model can be given access to in - * order to gain info or complete tasks. + * Representation of a function that a model can invoke. * - * @see [defineFunction] for how to create an instance of this class. + * @see defineFunction */ -class NoParameterFunction -internal constructor(name: String, description: String, val function: suspend () -> JSONObject) : - FunctionDeclaration(name, description) { - override fun getParameters() = listOf>() - - suspend fun execute() = function() - - override suspend fun execute(part: FunctionCallPart) = function() -} - -/** - * A declared one param function, including implementation, that a model can be given access to in - * order to gain info or complete tasks. - * - * @see [defineFunction] for how to create an instance of this class. - */ -class OneParameterFunction -internal constructor( - name: String, - description: String, - val param: Schema, - val function: suspend (T) -> JSONObject, -) : FunctionDeclaration(name, description) { - override fun getParameters() = listOf(param) - - override suspend fun execute(part: FunctionCallPart): JSONObject { - val arg1 = part.getArgOrThrow(param) - return function(arg1) - } -} - -/** - * A declared two param function, including implementation, that a model can be given access to in - * order to gain info or complete tasks. - * - * @see [defineFunction] for how to create an instance of this class. - */ -class TwoParameterFunction -internal constructor( - name: String, - description: String, - val param1: Schema, - val param2: Schema, - val function: suspend (T, U) -> JSONObject, -) : FunctionDeclaration(name, description) { - override fun getParameters() = listOf(param1, param2) - - override suspend fun execute(part: FunctionCallPart): JSONObject { - val arg1 = part.getArgOrThrow(param1) - val arg2 = part.getArgOrThrow(param2) - return function(arg1, arg2) - } -} - -/** - * A declared three param function, including implementation, that a model can be given access to in - * order to gain info or complete tasks. - * - * @see [defineFunction] for how to create an instance of this class. - */ -class ThreeParameterFunction -internal constructor( - name: String, - description: String, - val param1: Schema, - val param2: Schema, - val param3: Schema, - val function: suspend (T, U, V) -> JSONObject, -) : FunctionDeclaration(name, description) { - override fun getParameters() = listOf(param1, param2, param3) - - override suspend fun execute(part: FunctionCallPart): JSONObject { - val arg1 = part.getArgOrThrow(param1) - val arg2 = part.getArgOrThrow(param2) - val arg3 = part.getArgOrThrow(param3) - return function(arg1, arg2, arg3) - } -} - -/** - * A declared four param function, including implementation, that a model can be given access to in - * order to gain info or complete tasks. - * - * @see [defineFunction] for how to create an instance of this class. - */ -class FourParameterFunction -internal constructor( - name: String, - description: String, - val param1: Schema, - val param2: Schema, - val param3: Schema, - val param4: Schema, - val function: suspend (T, U, V, W) -> JSONObject, -) : FunctionDeclaration(name, description) { - override fun getParameters() = listOf(param1, param2, param3, param4) - - override suspend fun execute(part: FunctionCallPart): JSONObject { - val arg1 = part.getArgOrThrow(param1) - val arg2 = part.getArgOrThrow(param2) - val arg3 = part.getArgOrThrow(param3) - val arg4 = part.getArgOrThrow(param4) - return function(arg1, arg2, arg3, arg4) - } -} - -/** - * A declared function, including implementation, that a model can be given access to in order to - * gain info or complete tasks. - * - * @see [OneParameterFunction] - * @see [TwoParameterFunction] - * @see [ThreeParameterFunction] - * @see [FourParameterFunction] - */ -abstract class FunctionDeclaration(val name: String, val description: String) { - - /** The parameters of the attached function as a list of [Schema]. */ - abstract fun getParameters(): List> - - /** Run the attached function with the provided [arguments][part]. */ - abstract suspend fun execute(part: FunctionCallPart): JSONObject -} +class FunctionDeclaration( + val name: String, + val description: String, + val parameters: List, + val requiredParameters: List, +) /** * Represents a parameter for a declared function * + * ``` + * val currencyFrom = Schema.str("currencyFrom", "The currency to convert from.") + * ``` + * * @property name: The name of the parameter * @property description: The description of what the parameter should contain or represent * @property format: format information for the parameter, this can include bitlength in the case of - * int/float or keywords like "enum" for the string type + * int/float or keywords like "enum" for the string type * @property enum: contains the enum values for a string enum * @property type: contains the type info and parser * @property properties: if type is OBJECT, then this contains the description of the fields of the - * object by name + * object by name * @property required: if type is OBJECT, then this contains the list of required keys * @property items: if the type is ARRAY, then this contains a description of the objects in the - * array + * array */ -class Schema( +class Schema( val name: String, val description: String, val format: String? = null, val nullable: Boolean? = null, val enum: List? = null, - val properties: Map>? = null, + val properties: Map? = null, val required: List? = null, - val items: Schema? = null, - val type: FunctionType, + val items: Schema? = null, + val type: FunctionType, ) { - /** - * Parses an instance of this [Schema] from the provided [String]. - * - * This is done via the [parse][FunctionType.parse] method of [type]. - */ - fun fromString(value: String?) = type.parse(value) companion object { - /** Registers a schema for a 32-bit integer number */ + /** Registers a schema for a 32 bit integer number */ + @JvmStatic + @JvmName("numInt") fun int(name: String, description: String) = - Schema( + Schema( name = name, description = description, format = "int32", @@ -192,9 +74,11 @@ class Schema( nullable = false, ) - /** Registers a schema for a 64-bit integer number */ + /** Registers a schema for a 64 bit integer number */ + @JvmStatic + @JvmName("numLong") fun long(name: String, description: String) = - Schema( + Schema( name = name, description = description, type = FunctionType.LONG, @@ -202,8 +86,9 @@ class Schema( ) /** Registers a schema for a string */ + @JvmStatic fun str(name: String, description: String) = - Schema( + Schema( name = name, description = description, type = FunctionType.STRING, @@ -211,8 +96,9 @@ class Schema( ) /** Registers a schema for a boolean */ + @JvmStatic fun bool(name: String, description: String) = - Schema( + Schema( name = name, description = description, type = FunctionType.BOOLEAN, @@ -220,21 +106,10 @@ class Schema( ) /** Registers a schema for a floating point number */ - @Deprecated( - message = "Use `double` instead.", - replaceWith = ReplaceWith("double(name, description)"), - ) - fun num(name: String, description: String) = - Schema( - name = name, - description = description, - type = FunctionType.NUMBER, - nullable = false, - ) - - /** Registers a schema for a floating point number */ + @JvmStatic + @JvmName("numDouble") fun double(name: String, description: String) = - Schema( + Schema( name = name, description = description, type = FunctionType.NUMBER, @@ -244,13 +119,15 @@ class Schema( /** * Registers a schema for a complex object. In a function it will be returned as a [JSONObject] */ - fun obj(name: String, description: String, vararg contents: Schema) = - Schema( + @JvmStatic + fun obj(name: String, description: String, vararg contents: Schema) = + Schema( name = name, description = description, type = FunctionType.OBJECT, required = contents.map { it.name }, properties = contents.associateBy { it.name }.toMap(), + nullable = false, ) /** @@ -258,8 +135,9 @@ class Schema( * * @param items can be used to specify the type of the array */ - fun arr(name: String, description: String, items: Schema? = null) = - Schema>( + @JvmStatic + fun arr(name: String, description: String, items: Schema? = null) = + Schema( name = name, description = description, type = FunctionType.ARRAY, @@ -268,8 +146,10 @@ class Schema( ) /** Registers a schema for an enum */ + @JvmStatic + @JvmName("enumeration") fun enum(name: String, description: String, values: List) = - Schema( + Schema( name = name, description = description, format = "enum", @@ -281,95 +161,30 @@ class Schema( } /** - * Defines a function with zero parameters, including its implementation, that a model can be given - * access to in order to gain info or complete tasks. - * - * @param name The name of the function call, this should be clear and descriptive for the model - * @param description A description of what the function does and its output. - * @param function the function implementation - */ -fun defineFunction(name: String, description: String, function: suspend () -> JSONObject) = - NoParameterFunction(name, description, function) - -/** - * Defines a function with one parameter, including its implementation, that a model can be given - * access to in order to gain info or complete tasks. - * - * @param name The name of the function call, this should be clear and descriptive for the model - * @param description A description of what the function does and its output. - * @param arg1 A description of the first function parameter - * @param function the function implementation - */ -fun defineFunction( - name: String, - description: String, - arg1: Schema, - function: suspend (T) -> JSONObject, -) = OneParameterFunction(name, description, arg1, function) - -/** - * Defines a function with two parameters, including its implementation, that a model can be given - * access to in order to gain info or complete tasks. - * - * @param name The name of the function call, this should be clear and descriptive for the model - * @param description A description of what the function does and its output. - * @param arg1 A description of the first function parameter - * @param arg2 A description of the second function parameter - * @param function the function implementation - */ -fun defineFunction( - name: String, - description: String, - arg1: Schema, - arg2: Schema, - function: suspend (T, U) -> JSONObject, -) = TwoParameterFunction(name, description, arg1, arg2, function) - -/** - * Defines a function with three parameters, including its implementation, that a model can be given - * access to in order to gain info or complete tasks. + * A declared function, including implementation, that a model can be given access to in order to + * gain info or complete tasks. * - * @param name The name of the function call, this should be clear and descriptive for the model - * @param description A description of what the function does and its output. - * @param arg1 A description of the first function parameter - * @param arg2 A description of the second function parameter - * @param arg3 A description of the third function parameter - * @param function the function implementation - */ -fun defineFunction( - name: String, - description: String, - arg1: Schema, - arg2: Schema, - arg3: Schema, - function: suspend (T, U, W) -> JSONObject, -) = ThreeParameterFunction(name, description, arg1, arg2, arg3, function) - -/** - * Defines a function with four parameters, including its implementation, that a model can be given - * access to in order to gain info or complete tasks. + * ``` + * val getExchangeRate = defineFunction( + * name = "getExchangeRate", + * description = "Get the exchange rate for currencies between countries.", + * parameters = listOf( + * Schema.str("currencyFrom", "The currency to convert from."), + * Schema.str("currencyTo", "The currency to convert to.") + * ), + * requiredParameters = listOf("currencyFrom", "currencyTo") + * ) + * ``` * - * @param name The name of the function call, this should be clear and descriptive for the model + * @param name The name of the function call, this should be clear and descriptive for the model. * @param description A description of what the function does and its output. - * @param arg1 A description of the first function parameter - * @param arg2 A description of the second function parameter - * @param arg3 A description of the third function parameter - * @param arg4 A description of the fourth function parameter - * @param function the function implementation + * @param parameters A list of parameters that the function accepts. + * @param requiredParameters A list of parameters that the function requires to run. + * @see Schema */ -fun defineFunction( +fun defineFunction( name: String, description: String, - arg1: Schema, - arg2: Schema, - arg3: Schema, - arg4: Schema, - function: suspend (T, U, W, Z) -> JSONObject, -) = FourParameterFunction(name, description, arg1, arg2, arg3, arg4, function) - -private fun FunctionCallPart.getArgOrThrow(param: Schema): T { - return param.fromString(args[param.name]) - ?: throw RuntimeException( - "Missing argument for parameter \"${param.name}\" for function \"$name\"" - ) -} + parameters: List = emptyList(), + requiredParameters: List = emptyList(), +) = FunctionDeclaration(name, description, parameters, requiredParameters) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt index 08be1d9583c..031b8689aff 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt @@ -16,4 +16,4 @@ package com.google.firebase.vertexai.type -class FunctionParameter(val name: String, val description: String, val type: FunctionType) {} +class FunctionParameter(val name: String, val description: String, val type: FunctionType) {} diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/GenerationConfig.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/GenerationConfig.kt index e8ce71e7975..bfb0b388161 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/GenerationConfig.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/GenerationConfig.kt @@ -41,7 +41,7 @@ private constructor( val maxOutputTokens: Int?, val stopSequences: List?, val responseMimeType: String?, - val responseSchema: Schema<*>? = null, + val responseSchema: Schema? = null, ) { /** @@ -66,7 +66,7 @@ private constructor( @JvmField var maxOutputTokens: Int? = null @JvmField var stopSequences: List? = null @JvmField var responseMimeType: String? = null - @JvmField var responseSchema: Schema<*>? = null + @JvmField var responseSchema: Schema? = null /** Create a new [GenerationConfig] with the attached arguments. */ fun build() = diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt index 0a5afc09bdc..c5b037262bd 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt @@ -25,19 +25,16 @@ import org.json.JSONObject * * @property name: the enum name of the type * @property parse: the deserialization function - * @property T: the type of the object that this maps to in code. */ -class FunctionType(val name: String, val parse: (String?) -> T?) { +class FunctionType(val name: String) { companion object { - val STRING = FunctionType("STRING") { it } - val INTEGER = FunctionType("INTEGER") { it?.toIntOrNull() } - val LONG = FunctionType("INTEGER") { it?.toLongOrNull() } - val NUMBER = FunctionType("NUMBER") { it?.toDoubleOrNull() } - val BOOLEAN = FunctionType("BOOLEAN") { it?.toBoolean() } + val STRING = FunctionType("STRING") + val INTEGER = FunctionType("INTEGER") + val LONG = FunctionType("INTEGER") + val NUMBER = FunctionType("NUMBER") + val BOOLEAN = FunctionType("BOOLEAN") val ARRAY = - FunctionType>("ARRAY") { it -> - it?.let { Json.parseToJsonElement(it).jsonArray.map { element -> element.toString() } } - } - val OBJECT = FunctionType("OBJECT") { it?.let { JSONObject(it) } } + FunctionType("ARRAY") + val OBJECT = FunctionType("OBJECT") } } From cc0988e86703dfd4f1bd8fbbe34d8e299b6a7b52 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Thu, 12 Sep 2024 17:34:58 -0400 Subject: [PATCH 03/15] Rewrite schema declarations following the new format --- .../vertexai/internal/util/conversions.kt | 4 +- .../vertexai/type/FunctionDeclarations.kt | 136 +-------------- .../vertexai/type/FunctionParameter.kt | 2 +- .../google/firebase/vertexai/type/Schema.kt | 158 ++++++++++++++++++ .../firebase/vertexai/type/SchemaBuilder.kt | 21 --- .../com/google/firebase/vertexai/type/Type.kt | 23 --- .../google/firebase/vertexai/SchemaTests.kt | 152 +++++++++++++++++ 7 files changed, 315 insertions(+), 181 deletions(-) create mode 100644 firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt delete mode 100644 firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt create mode 100644 firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt index 2b52ee66021..67444468b76 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt @@ -158,7 +158,7 @@ internal fun FunctionDeclaration.toInternal() = name, description, Schema( - properties = parameters.associate { it.name to it.toInternal() }, + properties = parameters.mapValues { it.value.toInternal() }, required = requiredParameters, type = "OBJECT", nullable = false, @@ -167,7 +167,7 @@ internal fun FunctionDeclaration.toInternal() = internal fun com.google.firebase.vertexai.type.Schema.toInternal(): Schema = Schema( - type.name, + type, description, format, nullable, diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt index bc81f3dec91..ef6243c2652 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt @@ -16,8 +16,6 @@ package com.google.firebase.vertexai.type -import org.json.JSONObject - /** * Representation of a function that a model can invoke. * @@ -26,140 +24,10 @@ import org.json.JSONObject class FunctionDeclaration( val name: String, val description: String, - val parameters: List, + val parameters: Map, val requiredParameters: List, ) -/** - * Represents a parameter for a declared function - * - * ``` - * val currencyFrom = Schema.str("currencyFrom", "The currency to convert from.") - * ``` - * - * @property name: The name of the parameter - * @property description: The description of what the parameter should contain or represent - * @property format: format information for the parameter, this can include bitlength in the case of - * int/float or keywords like "enum" for the string type - * @property enum: contains the enum values for a string enum - * @property type: contains the type info and parser - * @property properties: if type is OBJECT, then this contains the description of the fields of the - * object by name - * @property required: if type is OBJECT, then this contains the list of required keys - * @property items: if the type is ARRAY, then this contains a description of the objects in the - * array - */ -class Schema( - val name: String, - val description: String, - val format: String? = null, - val nullable: Boolean? = null, - val enum: List? = null, - val properties: Map? = null, - val required: List? = null, - val items: Schema? = null, - val type: FunctionType, -) { - - companion object { - /** Registers a schema for a 32 bit integer number */ - @JvmStatic - @JvmName("numInt") - fun int(name: String, description: String) = - Schema( - name = name, - description = description, - format = "int32", - type = FunctionType.INTEGER, - nullable = false, - ) - - /** Registers a schema for a 64 bit integer number */ - @JvmStatic - @JvmName("numLong") - fun long(name: String, description: String) = - Schema( - name = name, - description = description, - type = FunctionType.LONG, - nullable = false, - ) - - /** Registers a schema for a string */ - @JvmStatic - fun str(name: String, description: String) = - Schema( - name = name, - description = description, - type = FunctionType.STRING, - nullable = false, - ) - - /** Registers a schema for a boolean */ - @JvmStatic - fun bool(name: String, description: String) = - Schema( - name = name, - description = description, - type = FunctionType.BOOLEAN, - nullable = false, - ) - - /** Registers a schema for a floating point number */ - @JvmStatic - @JvmName("numDouble") - fun double(name: String, description: String) = - Schema( - name = name, - description = description, - type = FunctionType.NUMBER, - nullable = false, - ) - - /** - * Registers a schema for a complex object. In a function it will be returned as a [JSONObject] - */ - @JvmStatic - fun obj(name: String, description: String, vararg contents: Schema) = - Schema( - name = name, - description = description, - type = FunctionType.OBJECT, - required = contents.map { it.name }, - properties = contents.associateBy { it.name }.toMap(), - nullable = false, - ) - - /** - * Registers a schema for an array. - * - * @param items can be used to specify the type of the array - */ - @JvmStatic - fun arr(name: String, description: String, items: Schema? = null) = - Schema( - name = name, - description = description, - type = FunctionType.ARRAY, - items = items, - nullable = false, - ) - - /** Registers a schema for an enum */ - @JvmStatic - @JvmName("enumeration") - fun enum(name: String, description: String, values: List) = - Schema( - name = name, - description = description, - format = "enum", - enum = values, - type = FunctionType.STRING, - nullable = false, - ) - } -} - /** * A declared function, including implementation, that a model can be given access to in order to * gain info or complete tasks. @@ -185,6 +53,6 @@ class Schema( fun defineFunction( name: String, description: String, - parameters: List = emptyList(), + parameters: Map = emptyMap(), requiredParameters: List = emptyList(), ) = FunctionDeclaration(name, description, parameters, requiredParameters) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt index 031b8689aff..6e932905d95 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt @@ -16,4 +16,4 @@ package com.google.firebase.vertexai.type -class FunctionParameter(val name: String, val description: String, val type: FunctionType) {} +class FunctionParameter(val name: String, val description: String, val schema: Schema) {} diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt new file mode 100644 index 00000000000..180de51a369 --- /dev/null +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -0,0 +1,158 @@ +/* + * 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.firebase.vertexai.type + +sealed class StringFormat(val value: String) { + class Custom(format: String) : StringFormat(format) +} + +/** + * Represents a parameter for a declared function + * + * ``` + * val currencyFrom = Schema.str("currencyFrom", "The currency to convert from.") + * ``` + * + * @property name: The name of the parameter + * @property description: The description of what the parameter should contain or represent + * @property format: format information for the parameter, this can include bitlength in the case of + * int/float or keywords like "enum" for the string type + * @property enum: contains the enum values for a string enum + * @property type: contains the type info and parser + * @property properties: if type is OBJECT, then this contains the description of the fields of the + * object by name + * @property required: if type is OBJECT, then this contains the list of required keys + * @property items: if the type is ARRAY, then this contains a description of the objects in the + * array + */ +class Schema +internal constructor( + val type: String, + val description: String? = null, + val format: String? = null, + val nullable: Boolean? = null, + val enum: List? = null, + val properties: Map? = null, + val required: List? = null, + val items: Schema? = null, +) { + + companion object { + + /** Registers a schema for a boolean */ + @JvmStatic + fun boolean(name: String? = null, description: String? = null, nullable: Boolean = false) = + Schema( + description = description, + nullable = nullable, + type = "BOOLEAN", + ) + + /** Registers a schema for a 32 bit integer number */ + @JvmStatic + @JvmName("numInt") + fun integer(description: String? = null, nullable: Boolean = false) = + Schema( + description = description, + format = "int32", + nullable = nullable, + type = "INTEGER", + ) + + /** Registers a schema for a 64 bit integer number */ + @JvmStatic + @JvmName("numLong") + fun long(description: String? = null, nullable: Boolean = false) = + Schema( + description = description, + nullable = nullable, + type = "INTEGER", + ) + + /** Registers a schema for a floating point number */ + @JvmStatic + @JvmName("numDouble") + fun double(description: String? = null, nullable: Boolean = false) = + Schema(description = description, nullable = nullable, type = "NUMBER", format = "double") + + /** Registers a schema for a floating point number */ + @JvmStatic + @JvmName("numFloat") + fun float(description: String? = null, nullable: Boolean = false) = + Schema(description = description, nullable = nullable, type = "NUMBER", format = "float") + + /** Registers a schema for a string */ + @JvmStatic + @JvmName("str") + fun string( + name: String? = null, + description: String? = null, + nullable: Boolean = false, + format: StringFormat? = null + ) = + Schema( + description = description, + format = format?.value, + nullable = nullable, + type = "STRING" + ) + + /** + * Registers a schema for a complex object. In a function it will be returned as a [JSONObject] + */ + @JvmStatic + fun obj( + properties: Map, + description: String? = null, + optionalProperties: List = emptyList(), + nullable: Boolean = false + ) = + Schema( + description = description, + nullable = nullable, + properties = properties, + required = properties.keys.minus(optionalProperties.toSet()).toList(), + type = "OBJECT", + ) + + /** + * Registers a schema for an array. + * + * @param items can be used to specify the type of the array + */ + @JvmStatic + fun array(items: Schema, description: String? = null, nullable: Boolean = false) = + Schema( + description = description, + nullable = nullable, + items = items, + type = "ARRAY", + ) + + /** Registers a schema for an enum */ + @JvmStatic + @JvmName("enumeration") + fun enumeration(values: List, description: String? = null, nullable: Boolean = false) = + Schema( + description = description, + format = "enum", + nullable = nullable, + enum = values, + type = "STRING", + ) + } +} diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt deleted file mode 100644 index 24011a739a6..00000000000 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/SchemaBuilder.kt +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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.firebase.vertexai.type - -class SchemaBuilder { - private lateinit var schema: Schema -} \ No newline at end of file diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt index c5b037262bd..ff33240aa24 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Type.kt @@ -15,26 +15,3 @@ */ package com.google.firebase.vertexai.type - -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.jsonArray -import org.json.JSONObject - -/** - * Represents and passes the type information for an automated function call. - * - * @property name: the enum name of the type - * @property parse: the deserialization function - */ -class FunctionType(val name: String) { - companion object { - val STRING = FunctionType("STRING") - val INTEGER = FunctionType("INTEGER") - val LONG = FunctionType("INTEGER") - val NUMBER = FunctionType("NUMBER") - val BOOLEAN = FunctionType("BOOLEAN") - val ARRAY = - FunctionType("ARRAY") - val OBJECT = FunctionType("OBJECT") - } -} diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt new file mode 100644 index 00000000000..6f48fc10a2d --- /dev/null +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt @@ -0,0 +1,152 @@ +package com.google.firebase.vertexai + +import com.google.firebase.vertexai.internal.util.toInternal +import com.google.firebase.vertexai.type.Schema +import com.google.firebase.vertexai.type.StringFormat +import io.kotest.assertions.json.shouldEqualJson +import io.kotest.matchers.shouldBe +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import org.junit.Test + +internal class SchemaTests { + @Test + fun `basic schema declaration`() { + val schemaDeclaration = + Schema.array( + Schema.obj( + mapOf( + "name" to Schema.string(), + "country" to Schema.string(), + "population" to Schema.integer(), + "coordinates" to + Schema.obj( + mapOf( + "latitude" to Schema.double(), + "longitude" to Schema.double(), + ) + ), + "hemisphere" to + Schema.obj( + mapOf( + "latitudinal" to Schema.enumeration(listOf("N", "S")), + "longitudinal" to Schema.enumeration(listOf("E", "W")), + ) + ), + "elevation" to Schema.double(), + "isCapital" to Schema.boolean(), + "foundingDate" to Schema.string(nullable = true, format = StringFormat.Custom("date")), + ), + optionalProperties = listOf("population") + ) + ) + + val expectedJson = + """ + { + "type": "ARRAY", + "items": { + "type": "OBJECT", + "properties": { + "name": {"type": "STRING"}, + "country": {"type": "STRING"}, + "population": {"type": "INTEGER", "format": "int32"}, + "coordinates": { + "type": "OBJECT", + "properties": { + "latitude": {"type": "NUMBER", "format": "double"}, + "longitude": {"type": "NUMBER","format": "double"} + }, + "required": ["latitude","longitude"] + }, + "hemisphere": { + "type": "OBJECT", + "properties": { + "latitudinal": {"type": "STRING","format": "enum","enum": ["N","S"]}, + "longitudinal": {"type": "STRING","format": "enum","enum": ["E","W"]} + }, + "required": ["latitudinal","longitudinal"] + }, + "elevation": {"type": "NUMBER","format": "double"}, + "isCapital": {"type": "BOOLEAN"}, + "foundingDate": {"type": "STRING","format": "date","nullable": true} + }, + "required": [ + "name","country","coordinates","hemisphere","elevation", + "isCapital","foundingDate"] + } + } + """ + .trimIndent() + + Json.encodeToString(schemaDeclaration.toInternal()).shouldEqualJson(expectedJson) + } + + @Test + fun `full schema declaration`() { + val simpleDeclaration = + Schema.array( + Schema.obj( + description = "generic description", + nullable = true, + properties = + mapOf( + "name" to Schema.string(description = null, nullable = false, format = null), + "country" to + Schema.string( + description = "country name", + nullable = true, + format = StringFormat.Custom("custom format") + ), + "population" to Schema.long(description = "population count", nullable = true), + "coordinates" to + Schema.obj( + description = "coordinates", + nullable = true, + properties = + mapOf( + "latitude" to Schema.double(description = "latitude", nullable = false), + "longitude" to Schema.double(description = "longitude", nullable = false), + ) + ), + "hemisphere" to + Schema.obj( + description = "hemisphere", + nullable = false, + properties = + mapOf( + "latitudinal" to + Schema.enumeration( + listOf("N", "S"), + description = "latitudinal", + nullable = true + ), + "longitudinal" to + Schema.enumeration( + listOf("E", "W"), + description = "longitudinal", + nullable = true + ), + ), + ), + "elevation" to Schema.float(description = "elevation", nullable = false), + "isCapital" to + Schema.boolean( + description = "True if the city is the capital of the country", + nullable = false + ), + "foundingDate" to + Schema.string( + description = "Founding date", + nullable = true, + format = StringFormat.Custom("date") + ), + ) + ) + ) + + val json = Json.encodeToString(simpleDeclaration.toInternal()) + print(json) + 4.shouldBe(2 + 2) + } +} From c408c6b281f38ea3657a558d2c4924da68330603 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Thu, 12 Sep 2024 17:39:27 -0400 Subject: [PATCH 04/15] Add missing schema test --- .../google/firebase/vertexai/SchemaTests.kt | 64 +++++++++++++++++-- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt index 6f48fc10a2d..da4cec6a542 100644 --- a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt @@ -4,7 +4,6 @@ import com.google.firebase.vertexai.internal.util.toInternal import com.google.firebase.vertexai.type.Schema import com.google.firebase.vertexai.type.StringFormat import io.kotest.assertions.json.shouldEqualJson -import io.kotest.matchers.shouldBe import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import org.junit.Test @@ -84,7 +83,7 @@ internal class SchemaTests { @Test fun `full schema declaration`() { - val simpleDeclaration = + val schemaDeclaration = Schema.array( Schema.obj( description = "generic description", @@ -145,8 +144,63 @@ internal class SchemaTests { ) ) - val json = Json.encodeToString(simpleDeclaration.toInternal()) - print(json) - 4.shouldBe(2 + 2) + val expectedJson = + """ + { + "type": "ARRAY", + "items": { + "type": "OBJECT", + "description": "generic description", + "nullable": true, + "properties": { + "name": {"type": "STRING"}, + "country": {"type": "STRING", "description": "country name", "format": "custom format", "nullable": true}, + "population": {"type": "INTEGER", "description": "population count", "nullable": true}, + "coordinates": { + "type": "OBJECT", + "description": "coordinates", + "nullable": true, + "properties": { + "latitude": {"type": "NUMBER", "description": "latitude", "format": "double"}, + "longitude": {"type": "NUMBER", "description": "longitude", "format": "double"} + }, + "required": ["latitude","longitude"] + }, + "hemisphere": { + "type": "OBJECT", + "description": "hemisphere", + "properties": { + "latitudinal": { + "type": "STRING", + "description": "latitudinal", + "format": "enum", + "nullable": true, + "enum": ["N","S"] + }, + "longitudinal": { + "type": "STRING", + "description": "longitudinal", + "format": "enum", + "nullable": true, + "enum": ["E","W"] + } + }, + "required": ["latitudinal","longitudinal"] + }, + "elevation": {"type": "NUMBER", "description": "elevation", "format": "float"}, + "isCapital": {"type": "BOOLEAN", "description": "True if the city is the capital of the country"}, + "foundingDate": {"type": "STRING", "description": "Founding date", "format": "date", "nullable": true} + }, + "required": [ + "name","country","population","coordinates","hemisphere", + "elevation","isCapital","foundingDate" + ] + } + } + + """ + .trimIndent() + + Json.encodeToString(schemaDeclaration.toInternal()).shouldEqualJson(expectedJson) } } From 870a74a34c784005f738e765dd245b97b34e5025 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Fri, 13 Sep 2024 17:16:34 -0400 Subject: [PATCH 05/15] Revamp function declaration using the new schema --- .../vertexai/internal/util/conversions.kt | 2 +- ...Declarations.kt => FunctionDeclaration.kt} | 40 +++------ .../vertexai/type/FunctionParameter.kt | 19 ---- .../google/firebase/vertexai/type/Schema.kt | 89 +++++++++++-------- 4 files changed, 65 insertions(+), 85 deletions(-) rename firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/{FunctionDeclarations.kt => FunctionDeclaration.kt} (57%) delete mode 100644 firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt index 67444468b76..46ff0ec17ea 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt @@ -159,7 +159,7 @@ internal fun FunctionDeclaration.toInternal() = description, Schema( properties = parameters.mapValues { it.value.toInternal() }, - required = requiredParameters, + required = parameters.keys.minus(optionalParameters.toSet()).toList(), type = "OBJECT", nullable = false, ), diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt similarity index 57% rename from firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt rename to firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt index ef6243c2652..6b4bf34215a 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclarations.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt @@ -17,42 +17,28 @@ package com.google.firebase.vertexai.type /** - * Representation of a function that a model can invoke. - * - * @see defineFunction - */ -class FunctionDeclaration( - val name: String, - val description: String, - val parameters: Map, - val requiredParameters: List, -) - -/** - * A declared function, including implementation, that a model can be given access to in order to - * gain info or complete tasks. + * A declared function that a model can be given access to in order to gain info or complete tasks. * * ``` - * val getExchangeRate = defineFunction( + * val getExchangeRate = FunctionDeclaration( * name = "getExchangeRate", * description = "Get the exchange rate for currencies between countries.", - * parameters = listOf( - * Schema.str("currencyFrom", "The currency to convert from."), - * Schema.str("currencyTo", "The currency to convert to.") - * ), - * requiredParameters = listOf("currencyFrom", "currencyTo") + * parameters = mapOf( + * "currencyFrom" to Schema.str("The currency to convert from."), + * "currencyTo" ot Schema.str("The currency to convert to.") + * ) * ) * ``` * * @param name The name of the function call, this should be clear and descriptive for the model. * @param description A description of what the function does and its output. * @param parameters A list of parameters that the function accepts. - * @param requiredParameters A list of parameters that the function requires to run. + * @param optionalParameters A list of parameters that can be omitted. * @see Schema */ -fun defineFunction( - name: String, - description: String, - parameters: Map = emptyMap(), - requiredParameters: List = emptyList(), -) = FunctionDeclaration(name, description, parameters, requiredParameters) +class FunctionDeclaration( + val name: String, + val description: String, + val parameters: Map, + val optionalParameters: List = emptyList(), +) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt deleted file mode 100644 index 6e932905d95..00000000000 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionParameter.kt +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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.firebase.vertexai.type - -class FunctionParameter(val name: String, val description: String, val schema: Schema) {} diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index 180de51a369..3aa678db8fc 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -16,29 +16,12 @@ package com.google.firebase.vertexai.type -sealed class StringFormat(val value: String) { +sealed class StringFormat(val value: String?) { + class None() : StringFormat(null) class Custom(format: String) : StringFormat(format) } -/** - * Represents a parameter for a declared function - * - * ``` - * val currencyFrom = Schema.str("currencyFrom", "The currency to convert from.") - * ``` - * - * @property name: The name of the parameter - * @property description: The description of what the parameter should contain or represent - * @property format: format information for the parameter, this can include bitlength in the case of - * int/float or keywords like "enum" for the string type - * @property enum: contains the enum values for a string enum - * @property type: contains the type info and parser - * @property properties: if type is OBJECT, then this contains the description of the fields of the - * object by name - * @property required: if type is OBJECT, then this contains the list of required keys - * @property items: if the type is ARRAY, then this contains a description of the objects in the - * array - */ +/** Represents a Schema */ class Schema internal constructor( val type: String, @@ -52,17 +35,21 @@ internal constructor( ) { companion object { - /** Registers a schema for a boolean */ @JvmStatic - fun boolean(name: String? = null, description: String? = null, nullable: Boolean = false) = + fun boolean(description: String? = null, nullable: Boolean = false) = Schema( description = description, nullable = nullable, type = "BOOLEAN", ) - /** Registers a schema for a 32 bit integer number */ + /** + * Registers a schema for a 32 bit integer number + * + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema + */ @JvmStatic @JvmName("numInt") fun integer(description: String? = null, nullable: Boolean = false) = @@ -73,7 +60,12 @@ internal constructor( type = "INTEGER", ) - /** Registers a schema for a 64 bit integer number */ + /** + * Registers a schema for a 64 bit integer number + * + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema + */ @JvmStatic @JvmName("numLong") fun long(description: String? = null, nullable: Boolean = false) = @@ -83,36 +75,50 @@ internal constructor( type = "INTEGER", ) - /** Registers a schema for a floating point number */ + /** + * Registers a schema for a floating point number + * + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema + */ @JvmStatic @JvmName("numDouble") fun double(description: String? = null, nullable: Boolean = false) = Schema(description = description, nullable = nullable, type = "NUMBER", format = "double") - /** Registers a schema for a floating point number */ + /** + * Registers a schema for a floating point number + * + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema + */ @JvmStatic @JvmName("numFloat") fun float(description: String? = null, nullable: Boolean = false) = Schema(description = description, nullable = nullable, type = "NUMBER", format = "float") - /** Registers a schema for a string */ + /** + * Registers a schema for a string + * + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema + * @property format: Pattern that values need to adhere to + */ @JvmStatic @JvmName("str") fun string( - name: String? = null, description: String? = null, nullable: Boolean = false, - format: StringFormat? = null + format: StringFormat = StringFormat.None() ) = - Schema( - description = description, - format = format?.value, - nullable = nullable, - type = "STRING" - ) + Schema(description = description, format = format.value, nullable = nullable, type = "STRING") /** * Registers a schema for a complex object. In a function it will be returned as a [JSONObject] + * + * @property properties: Map of the fields of the object to their schema + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema */ @JvmStatic fun obj( @@ -132,7 +138,9 @@ internal constructor( /** * Registers a schema for an array. * - * @param items can be used to specify the type of the array + * @property items: The schema of the elements of this array + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema */ @JvmStatic fun array(items: Schema, description: String? = null, nullable: Boolean = false) = @@ -143,9 +151,14 @@ internal constructor( type = "ARRAY", ) - /** Registers a schema for an enum */ + /** + * Registers a schema for an enum + * + * @property values: The list of valid values for this enumeration + * @property description: The description of what the parameter should contain or represent + * @property nullable: Whether null is a valid value for this schema + */ @JvmStatic - @JvmName("enumeration") fun enumeration(values: List, description: String? = null, nullable: Boolean = false) = Schema( description = description, From a5a3829a8122ae8dee7640aed9632f6252adb818 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Mon, 16 Sep 2024 10:37:45 -0400 Subject: [PATCH 06/15] Add missing copyright header to SchemaTests --- .../com/google/firebase/vertexai/SchemaTests.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt index da4cec6a542..4de73aebcde 100644 --- a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt @@ -1,3 +1,19 @@ +/* + * 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.firebase.vertexai import com.google.firebase.vertexai.internal.util.toInternal From 9179e52a5379125308b3e2e43e46a73b5b3c2ec9 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Mon, 16 Sep 2024 10:38:07 -0400 Subject: [PATCH 07/15] Reword refdocs for Schema --- .../google/firebase/vertexai/type/Schema.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index 3aa678db8fc..af87e4a3ddf 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -35,7 +35,7 @@ internal constructor( ) { companion object { - /** Registers a schema for a boolean */ + /** Returns a schema for a boolean */ @JvmStatic fun boolean(description: String? = null, nullable: Boolean = false) = Schema( @@ -45,7 +45,7 @@ internal constructor( ) /** - * Registers a schema for a 32 bit integer number + * Returns a schema for a 32 bit integer number * * @property description: The description of what the parameter should contain or represent * @property nullable: Whether null is a valid value for this schema @@ -61,7 +61,7 @@ internal constructor( ) /** - * Registers a schema for a 64 bit integer number + * Returns a schema for a 64 bit integer number * * @property description: The description of what the parameter should contain or represent * @property nullable: Whether null is a valid value for this schema @@ -76,7 +76,7 @@ internal constructor( ) /** - * Registers a schema for a floating point number + * Returns a schema for a floating point number * * @property description: The description of what the parameter should contain or represent * @property nullable: Whether null is a valid value for this schema @@ -87,7 +87,7 @@ internal constructor( Schema(description = description, nullable = nullable, type = "NUMBER", format = "double") /** - * Registers a schema for a floating point number + * Returns a schema for a floating point number * * @property description: The description of what the parameter should contain or represent * @property nullable: Whether null is a valid value for this schema @@ -98,7 +98,7 @@ internal constructor( Schema(description = description, nullable = nullable, type = "NUMBER", format = "float") /** - * Registers a schema for a string + * Returns a schema for a string * * @property description: The description of what the parameter should contain or represent * @property nullable: Whether null is a valid value for this schema @@ -114,7 +114,7 @@ internal constructor( Schema(description = description, format = format.value, nullable = nullable, type = "STRING") /** - * Registers a schema for a complex object. In a function it will be returned as a [JSONObject] + * Returns a schema for a complex object. In a function it will be returned as a [JSONObject] * * @property properties: Map of the fields of the object to their schema * @property description: The description of what the parameter should contain or represent @@ -136,7 +136,7 @@ internal constructor( ) /** - * Registers a schema for an array. + * Returns a schema for an array. * * @property items: The schema of the elements of this array * @property description: The description of what the parameter should contain or represent @@ -152,7 +152,7 @@ internal constructor( ) /** - * Registers a schema for an enum + * Returns a schema for an enumeration * * @property values: The list of valid values for this enumeration * @property description: The description of what the parameter should contain or represent From 085db3d35832660f2f843582cbe518f317a01c3c Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Mon, 16 Sep 2024 10:46:07 -0400 Subject: [PATCH 08/15] Fix tests after changes in StringFormat --- .../src/test/java/com/google/firebase/vertexai/SchemaTests.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt index 4de73aebcde..6056c0c435d 100644 --- a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt @@ -106,7 +106,7 @@ internal class SchemaTests { nullable = true, properties = mapOf( - "name" to Schema.string(description = null, nullable = false, format = null), + "name" to Schema.string(description = null, nullable = false, format = StringFormat.None()), "country" to Schema.string( description = "country name", From 0ca7ff2a2683c13a98fd4e3ca3ffcb289d49f3ad Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Mon, 16 Sep 2024 12:43:27 -0400 Subject: [PATCH 09/15] Reorder object arguments --- .../src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index af87e4a3ddf..b245889e58b 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -124,8 +124,8 @@ internal constructor( fun obj( properties: Map, description: String? = null, + nullable: Boolean = false, optionalProperties: List = emptyList(), - nullable: Boolean = false ) = Schema( description = description, From 1fd27e65d00eef828e6cc289a19072c3769ce975 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Mon, 16 Sep 2024 13:28:14 -0400 Subject: [PATCH 10/15] Use correct tag for Schema documentation --- .../google/firebase/vertexai/type/Schema.kt | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index b245889e58b..dfc206d2d4e 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -47,8 +47,8 @@ internal constructor( /** * Returns a schema for a 32 bit integer number * - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema */ @JvmStatic @JvmName("numInt") @@ -63,8 +63,8 @@ internal constructor( /** * Returns a schema for a 64 bit integer number * - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema */ @JvmStatic @JvmName("numLong") @@ -78,8 +78,8 @@ internal constructor( /** * Returns a schema for a floating point number * - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema */ @JvmStatic @JvmName("numDouble") @@ -89,8 +89,8 @@ internal constructor( /** * Returns a schema for a floating point number * - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema */ @JvmStatic @JvmName("numFloat") @@ -100,9 +100,9 @@ internal constructor( /** * Returns a schema for a string * - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema - * @property format: Pattern that values need to adhere to + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema + * @param format: Pattern that values need to adhere to */ @JvmStatic @JvmName("str") @@ -116,9 +116,9 @@ internal constructor( /** * Returns a schema for a complex object. In a function it will be returned as a [JSONObject] * - * @property properties: Map of the fields of the object to their schema - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema + * @param properties: Map of the fields of the object to their schema + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema */ @JvmStatic fun obj( @@ -138,9 +138,9 @@ internal constructor( /** * Returns a schema for an array. * - * @property items: The schema of the elements of this array - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema + * @param items: The schema of the elements of this array + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema */ @JvmStatic fun array(items: Schema, description: String? = null, nullable: Boolean = false) = @@ -154,9 +154,9 @@ internal constructor( /** * Returns a schema for an enumeration * - * @property values: The list of valid values for this enumeration - * @property description: The description of what the parameter should contain or represent - * @property nullable: Whether null is a valid value for this schema + * @param values: The list of valid values for this enumeration + * @param description: The description of what the parameter should contain or represent + * @param nullable: Whether null is a valid value for this schema */ @JvmStatic fun enumeration(values: List, description: String? = null, nullable: Boolean = false) = From 0bbb93ffcfc68a8e1b22131ba18c2f2e51df9a1e Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Mon, 16 Sep 2024 16:44:17 -0400 Subject: [PATCH 11/15] Use null instead of StringFormat.None() --- .../kotlin/com/google/firebase/vertexai/type/Schema.kt | 7 +++---- .../test/java/com/google/firebase/vertexai/SchemaTests.kt | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index dfc206d2d4e..de0fc815fe5 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -16,8 +16,7 @@ package com.google.firebase.vertexai.type -sealed class StringFormat(val value: String?) { - class None() : StringFormat(null) +sealed class StringFormat(val value: String) { class Custom(format: String) : StringFormat(format) } @@ -109,9 +108,9 @@ internal constructor( fun string( description: String? = null, nullable: Boolean = false, - format: StringFormat = StringFormat.None() + format: StringFormat? = null ) = - Schema(description = description, format = format.value, nullable = nullable, type = "STRING") + Schema(description = description, format = format?.value, nullable = nullable, type = "STRING") /** * Returns a schema for a complex object. In a function it will be returned as a [JSONObject] diff --git a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt index 6056c0c435d..4de73aebcde 100644 --- a/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt +++ b/firebase-vertexai/src/test/java/com/google/firebase/vertexai/SchemaTests.kt @@ -106,7 +106,7 @@ internal class SchemaTests { nullable = true, properties = mapOf( - "name" to Schema.string(description = null, nullable = false, format = StringFormat.None()), + "name" to Schema.string(description = null, nullable = false, format = null), "country" to Schema.string( description = "country name", From e03f1e70c8e0562be75fb1b97fe1c6ea81789b0a Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Date: Mon, 16 Sep 2024 17:05:25 -0400 Subject: [PATCH 12/15] Apply suggestions from code review Co-authored-by: rachelsaunders <52258509+rachelsaunders@users.noreply.github.com> --- .../com/google/firebase/vertexai/type/Schema.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index de0fc815fe5..773c10ac5b3 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -20,7 +20,7 @@ sealed class StringFormat(val value: String) { class Custom(format: String) : StringFormat(format) } -/** Represents a Schema */ +/** Represents a schema */ class Schema internal constructor( val type: String, @@ -44,7 +44,7 @@ internal constructor( ) /** - * Returns a schema for a 32 bit integer number + * Returns a schema for a 32-bit integer number * * @param description: The description of what the parameter should contain or represent * @param nullable: Whether null is a valid value for this schema @@ -60,7 +60,7 @@ internal constructor( ) /** - * Returns a schema for a 64 bit integer number + * Returns a schema for a 64-bit integer number * * @param description: The description of what the parameter should contain or represent * @param nullable: Whether null is a valid value for this schema @@ -101,7 +101,7 @@ internal constructor( * * @param description: The description of what the parameter should contain or represent * @param nullable: Whether null is a valid value for this schema - * @param format: Pattern that values need to adhere to + * @param format: The pattern that values need to adhere to */ @JvmStatic @JvmName("str") @@ -113,9 +113,9 @@ internal constructor( Schema(description = description, format = format?.value, nullable = nullable, type = "STRING") /** - * Returns a schema for a complex object. In a function it will be returned as a [JSONObject] + * Returns a schema for a complex object. In a function, it will be returned as a [JSONObject]. * - * @param properties: Map of the fields of the object to their schema + * @param properties: The map of the object's fields to their schema * @param description: The description of what the parameter should contain or represent * @param nullable: Whether null is a valid value for this schema */ From 4699edbf81b0b330a0e672c48a4a92acfa5c1769 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Date: Mon, 16 Sep 2024 17:37:47 -0400 Subject: [PATCH 13/15] Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt Co-authored-by: rachelsaunders <52258509+rachelsaunders@users.noreply.github.com> --- .../com/google/firebase/vertexai/type/FunctionDeclaration.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt index 6b4bf34215a..e8949a4a6ba 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/FunctionDeclaration.kt @@ -25,7 +25,7 @@ package com.google.firebase.vertexai.type * description = "Get the exchange rate for currencies between countries.", * parameters = mapOf( * "currencyFrom" to Schema.str("The currency to convert from."), - * "currencyTo" ot Schema.str("The currency to convert to.") + * "currencyTo" to Schema.str("The currency to convert to.") * ) * ) * ``` From fb8616bf0bad7fcd2848a737049064b089483540 Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Mon, 16 Sep 2024 22:27:03 -0400 Subject: [PATCH 14/15] Format fix --- .../kotlin/com/google/firebase/vertexai/type/Schema.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index 773c10ac5b3..73573b0c6b1 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -110,7 +110,12 @@ internal constructor( nullable: Boolean = false, format: StringFormat? = null ) = - Schema(description = description, format = format?.value, nullable = nullable, type = "STRING") + Schema( + description = description, + format = format?.value, + nullable = nullable, + type = "STRING" + ) /** * Returns a schema for a complex object. In a function, it will be returned as a [JSONObject]. From fbc9ebc943b16f09727d062ceb175e29aecce68e Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Tue, 17 Sep 2024 12:55:05 -0400 Subject: [PATCH 15/15] Adjust `obj` declaration --- .../src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt index 73573b0c6b1..4a3383e5c79 100644 --- a/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt +++ b/firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt @@ -127,9 +127,9 @@ internal constructor( @JvmStatic fun obj( properties: Map, + optionalProperties: List = emptyList(), description: String? = null, nullable: Boolean = false, - optionalProperties: List = emptyList(), ) = Schema( description = description,