From 9358e0fd52744d0885576265e43035c801c55ec4 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Wed, 29 May 2024 10:17:35 -0700 Subject: [PATCH 1/6] add optional param for Schema.arr to specify objects in array --- .../ai/client/generativeai/type/FunctionDeclarations.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt index 7c65fbac..56c6591b 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt @@ -205,8 +205,13 @@ class Schema( Schema(name = name, description = description, type = FunctionType.OBJECT) /** Registers a schema for an array */ - fun arr(name: String, description: String) = - Schema>(name = name, description = description, type = FunctionType.ARRAY) + fun arr(name: String, description: String, items: Schema = Schema.str("", "")) = + Schema>( + name = name, + description = description, + type = FunctionType.ARRAY, + items = items + ) /** Registers a schema for an enum */ fun enum(name: String, description: String, values: List) = From 4eea91b9c072c81b3d04fbedfbc0c591bb1aa21c Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Wed, 29 May 2024 10:24:13 -0700 Subject: [PATCH 2/6] ktfmt --- .../google/ai/sample/GenerativeAiViewModelFactory.kt | 11 +++++++++++ .../client/generativeai/type/FunctionDeclarations.kt | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt b/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt index 241fa595..7860fabc 100644 --- a/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt +++ b/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt @@ -20,6 +20,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewmodel.CreationExtras import com.google.ai.client.generativeai.GenerativeModel +import com.google.ai.client.generativeai.type.Schema import com.google.ai.client.generativeai.type.generationConfig import com.google.ai.sample.feature.chat.ChatViewModel import com.google.ai.sample.feature.multimodal.PhotoReasoningViewModel @@ -32,6 +33,16 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory { ): T { val config = generationConfig { temperature = 0.7f + responseSchema = Schema.arr( + "name", + "description", + Schema.obj( + "person", + "person", + Schema.str("name", "person's name"), + Schema.str("birthday", "person's birthday") + ) + ) } return with(viewModelClass) { diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt index 56c6591b..546322f1 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt @@ -210,7 +210,7 @@ class Schema( name = name, description = description, type = FunctionType.ARRAY, - items = items + items = items, ) /** Registers a schema for an enum */ From f38a020124bf727db417c547fbd1f8b575f29575 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Wed, 29 May 2024 10:24:49 -0700 Subject: [PATCH 3/6] remove unintended file --- .../google/ai/sample/GenerativeAiViewModelFactory.kt | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt b/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt index 7860fabc..241fa595 100644 --- a/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt +++ b/generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt @@ -20,7 +20,6 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewmodel.CreationExtras import com.google.ai.client.generativeai.GenerativeModel -import com.google.ai.client.generativeai.type.Schema import com.google.ai.client.generativeai.type.generationConfig import com.google.ai.sample.feature.chat.ChatViewModel import com.google.ai.sample.feature.multimodal.PhotoReasoningViewModel @@ -33,16 +32,6 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory { ): T { val config = generationConfig { temperature = 0.7f - responseSchema = Schema.arr( - "name", - "description", - Schema.obj( - "person", - "person", - Schema.str("name", "person's name"), - Schema.str("birthday", "person's birthday") - ) - ) } return with(viewModelClass) { From 6bb64ab6997d798604cd8cc8bbc163fd8b5738d2 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Mon, 3 Jun 2024 12:13:41 -0700 Subject: [PATCH 4/6] make items nullable to strictly preserve behavior --- .../google/ai/client/generativeai/type/FunctionDeclarations.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt index 584307cf..798b3023 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt @@ -233,7 +233,7 @@ class Schema( ) /** Registers a schema for an array */ - fun arr(name: String, description: String, items: Schema = Schema.str("", "")) = + fun arr(name: String, description: String, items: Schema? = null) = Schema>( name = name, description = description, From 75961930e8c7ff095b52061eb64e2485862cfa09 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Tue, 4 Jun 2024 11:02:51 -0700 Subject: [PATCH 5/6] better docs --- .../ai/client/generativeai/type/FunctionDeclarations.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt index 798b3023..8bd2940c 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt @@ -232,7 +232,10 @@ class Schema( nullable = false, ) - /** Registers a schema for an array */ + /** + * Registers a schema for an array. + * @param items can be used to specify the type of the array + */ fun arr(name: String, description: String, items: Schema? = null) = Schema>( name = name, From ea3a856a8f592e9d1ee0eed4c1fbda467966b9b0 Mon Sep 17 00:00:00 2001 From: David Motsonashvili Date: Tue, 4 Jun 2024 11:10:33 -0700 Subject: [PATCH 6/6] ktfmt --- .../google/ai/client/generativeai/type/FunctionDeclarations.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt index 8bd2940c..b9ede02c 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/FunctionDeclarations.kt @@ -234,6 +234,7 @@ class Schema( /** * Registers a schema for an array. + * * @param items can be used to specify the type of the array */ fun arr(name: String, description: String, items: Schema? = null) =