Skip to content

Commit

Permalink
add all fields including full recursive description
Browse files Browse the repository at this point in the history
  • Loading branch information
David Motsonashvili committed Mar 18, 2024
1 parent 9d7a3eb commit 9d30869
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal data class GenerationConfig(
@SerialName("top_k") val topK: Int?,
@SerialName("candidate_count") val candidateCount: Int?,
@SerialName("max_output_tokens") val maxOutputTokens: Int?,
@SerialName("stop_sequences") val stopSequences: List<String>?
@SerialName("stop_sequences") val stopSequences: List<String>?,
)

@Serializable internal data class Tool(val functionDeclarations: List<FunctionDeclaration>)
Expand All @@ -35,20 +35,16 @@ internal data class GenerationConfig(
internal data class FunctionDeclaration(
val name: String,
val description: String,
val parameters: FunctionParameters
)

@Serializable
internal data class FunctionParameters(
val properties: Map<String, FunctionParameterProperties>,
val required: List<String>,
val type: String,
val parameters: FunctionParameterProperties,
)

@Serializable
internal data class FunctionParameterProperties(
val type: String,
val description: String,
val format: String?,
val enum: List<String>?
val description: String? = null,
val format: String? = null,
val enum: List<String>? = null,
val properties: Map<String, FunctionParameterProperties>? = null,
val required: List<String>? = null,
val items: FunctionParameterProperties? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,23 @@ internal fun FunctionDeclaration.toInternal() =
com.google.ai.client.generativeai.internal.api.client.FunctionDeclaration(
name,
description,
com.google.ai.client.generativeai.internal.api.client.FunctionParameters(
getParameters().associate { it.name to it.toInternal() },
getParameters().map { it.name },
"OBJECT",
FunctionParameterProperties(
properties = getParameters().associate { it.name to it.toInternal() },
required = getParameters().map { it.name },
type = "OBJECT",
),
)

internal fun <T> ParameterDeclaration<T>.toInternal() =
FunctionParameterProperties(type.name, description, format, enum)
internal fun <T> ParameterDeclaration<T>.toInternal(): FunctionParameterProperties =
FunctionParameterProperties(
type.name,
description,
format,
enum,
properties?.mapValues { it.value.toInternal() },
required,
items?.toInternal()
)

internal fun JSONObject.toInternal() = Json.decodeFromString<JsonObject>(toString())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,20 @@ abstract class FunctionDeclaration(val name: String, val description: String) {
* 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 ParameterDeclaration<T>(
val name: String,
val description: String,
val format: String? = null,
val enum: List<String>? = null,
val properties: Map<String, ParameterDeclaration<Any>>? = null,
val required: List<String>? = null,
val items: ParameterDeclaration<Any>? = null,
val type: FunctionType<T>,
) {
fun fromString(value: String?) = type.parse(value)
Expand Down

0 comments on commit 9d30869

Please sign in to comment.