diff --git a/common/src/main/kotlin/com/google/ai/client/generativeai/common/client/Types.kt b/common/src/main/kotlin/com/google/ai/client/generativeai/common/client/Types.kt index e32b9196..dd3cca42 100644 --- a/common/src/main/kotlin/com/google/ai/client/generativeai/common/client/Types.kt +++ b/common/src/main/kotlin/com/google/ai/client/generativeai/common/client/Types.kt @@ -39,6 +39,7 @@ data class Tool( val functionDeclarations: List? = null, // This is a json object because it is not possible to make a data class with no parameters. val codeExecution: JsonObject? = null, + val googleSearchRetrieval: GoogleSearchRetrieval? = null, ) @Serializable @@ -60,6 +61,17 @@ data class FunctionCallingConfig(val mode: Mode) { @Serializable data class FunctionDeclaration(val name: String, val description: String, val parameters: Schema) +@Serializable data class GoogleSearchRetrieval(val dynamicRetrievalConfig: DynamicRetrievalConfig) + +@Serializable +data class DynamicRetrievalConfig(val mode: Mode, val dynamicThreshold: Float?) { + @Serializable + enum class Mode { + @SerialName("MODE_DYNAMIC") DYNAMIC, + @SerialName("MODE_UNSPECIFIED") UNSPECIFIED + } +} + @Serializable data class Schema( val type: String, diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/internal/util/conversions.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/internal/util/conversions.kt index b9d0e6bf..59d57034 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/internal/util/conversions.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/internal/util/conversions.kt @@ -22,7 +22,9 @@ import android.util.Base64 import com.google.ai.client.generativeai.common.CountTokensResponse import com.google.ai.client.generativeai.common.GenerateContentResponse import com.google.ai.client.generativeai.common.RequestOptions +import com.google.ai.client.generativeai.common.client.DynamicRetrievalConfig import com.google.ai.client.generativeai.common.client.GenerationConfig +import com.google.ai.client.generativeai.common.client.GoogleSearchRetrieval import com.google.ai.client.generativeai.common.client.Schema import com.google.ai.client.generativeai.common.server.BlockReason import com.google.ai.client.generativeai.common.server.Candidate @@ -52,6 +54,7 @@ import com.google.ai.client.generativeai.common.shared.SafetySetting import com.google.ai.client.generativeai.common.shared.TextPart import com.google.ai.client.generativeai.type.BlockThreshold import com.google.ai.client.generativeai.type.CitationMetadata +import com.google.ai.client.generativeai.type.DynamicRetrievalMode import com.google.ai.client.generativeai.type.ExecutionOutcome import com.google.ai.client.generativeai.type.FunctionCallingConfig import com.google.ai.client.generativeai.type.FunctionDeclaration @@ -144,6 +147,7 @@ internal fun Tool.toInternal() = com.google.ai.client.generativeai.common.client.Tool( functionDeclarations?.map { it.toInternal() }, codeExecution = codeExecution?.toInternal(), + googleSearchRetrieval = googleSearchRetrieval?.toInternal(), ) internal fun ToolConfig.toInternal() = @@ -189,6 +193,24 @@ internal fun com.google.ai.client.generativeai.type.Schema.toInternal(): internal fun JSONObject.toInternal() = Json.decodeFromString(toString()) +internal fun com.google.ai.client.generativeai.type.GoogleSearchRetrieval.toInternal(): + GoogleSearchRetrieval { + return GoogleSearchRetrieval(dynamicRetrievalConfig.toInternal()) +} + +internal fun com.google.ai.client.generativeai.type.DynamicRetrievalConfig.toInternal(): + DynamicRetrievalConfig { + return DynamicRetrievalConfig( + when (mode) { + DynamicRetrievalMode.DYNAMIC -> + com.google.ai.client.generativeai.common.client.DynamicRetrievalConfig.Mode.DYNAMIC + DynamicRetrievalMode.UNSPECIFIED -> + com.google.ai.client.generativeai.common.client.DynamicRetrievalConfig.Mode.UNSPECIFIED + }, + dynamicThreshold, + ) +} + internal fun Candidate.toPublic(): com.google.ai.client.generativeai.type.Candidate { val safetyRatings = safetyRatings?.map { it.toPublic() }.orEmpty() val citations = citationMetadata?.citationSources?.map { it.toPublic() }.orEmpty() diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/DynamicRetrievalConfig.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/DynamicRetrievalConfig.kt new file mode 100644 index 00000000..3931d18a --- /dev/null +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/DynamicRetrievalConfig.kt @@ -0,0 +1,33 @@ +/* + * 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.ai.client.generativeai.type + +import androidx.annotation.FloatRange + +/* + * Specifies the dynamic retrieval configuration for the given source. + */ +data class DynamicRetrievalConfig( + /* + * The mode of the predictor to be used in dynamic retrieval. + */ + val mode: DynamicRetrievalMode, + /* + * (Optional) The threshold to be used in dynamic retrieval. If not set, a system default value is used. + */ + @FloatRange(0.0, 1.0) val dynamicThreshold: Float? = null, +) diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/DynamicRetrievalMode.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/DynamicRetrievalMode.kt new file mode 100644 index 00000000..6c10865d --- /dev/null +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/DynamicRetrievalMode.kt @@ -0,0 +1,25 @@ +/* + * 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.ai.client.generativeai.type + +/* + * The mode of the predictor to be used in [DynamicRetrievalConfig]. + */ +enum class DynamicRetrievalMode { + DYNAMIC, + UNSPECIFIED +} diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/GoogleSearchRetrieval.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/GoogleSearchRetrieval.kt new file mode 100644 index 00000000..7dcc0335 --- /dev/null +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/GoogleSearchRetrieval.kt @@ -0,0 +1,20 @@ +/* + * 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.ai.client.generativeai.type + +/** Retrieval tool that is powered by Google search. */ +data class GoogleSearchRetrieval(val dynamicRetrievalConfig: DynamicRetrievalConfig) diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/type/Tool.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/type/Tool.kt index d6980ebc..da85a0e6 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/type/Tool.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/type/Tool.kt @@ -24,12 +24,14 @@ import org.json.JSONObject * * @param functionDeclarations The set of functions that this tool allows the model access to * @param codeExecution This is a flag value to enable Code Execution. Use [CODE_EXECUTION]. + * @param googleSearchRetrieval This is a Retrieval tool that is powered by Google search. */ class Tool @JvmOverloads constructor( val functionDeclarations: List? = null, val codeExecution: JSONObject? = null, + val googleSearchRetrieval: GoogleSearchRetrieval? = null, ) { companion object { @JvmField val CODE_EXECUTION = Tool(codeExecution = JSONObject())