-
Notifications
You must be signed in to change notification settings - Fork 0
Add Google Search and Thinking support to firebase-ai #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * 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.ai.type | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| /** | ||
| * Configuration for the "Thinking" feature, which allows the model to process information more | ||
| * deeply before responding. | ||
| * | ||
| * @property includeThoughts Whether to include the model's thoughts in the response. | ||
| */ | ||
| public class ThinkingConfig( | ||
| public val includeThoughts: Boolean = false, | ||
| ) { | ||
| internal fun toInternal() = Internal(includeThoughts = includeThoughts) | ||
|
|
||
| @Serializable | ||
| internal data class Internal( | ||
| @SerialName("include_thoughts") val includeThoughts: Boolean, | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||
|
|
||||||
| package com.google.firebase.ai.type | ||||||
|
|
||||||
| import kotlinx.serialization.SerialName | ||||||
| import kotlinx.serialization.Serializable | ||||||
| import kotlinx.serialization.json.JsonObject | ||||||
|
|
||||||
|
|
@@ -26,13 +27,22 @@ import kotlinx.serialization.json.JsonObject | |||||
| * @param functionDeclarations The set of functions that this tool allows the model access to | ||||||
| */ | ||||||
| public class Tool | ||||||
| internal constructor(internal val functionDeclarations: List<FunctionDeclaration>?) { | ||||||
| internal fun toInternal() = Internal(functionDeclarations?.map { it.toInternal() } ?: emptyList()) | ||||||
| internal constructor( | ||||||
| internal val functionDeclarations: List<FunctionDeclaration>? = null, | ||||||
| internal val googleSearchRetrieval: Boolean = false | ||||||
| ) { | ||||||
| internal fun toInternal() = | ||||||
| Internal( | ||||||
| functionDeclarations = functionDeclarations?.map { it.toInternal() }, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change alters the serialization of
Suggested change
|
||||||
| googleSearchRetrieval = if (googleSearchRetrieval) JsonObject(emptyMap()) else null | ||||||
| ) | ||||||
|
|
||||||
| @Serializable | ||||||
| internal data class Internal( | ||||||
| val functionDeclarations: List<FunctionDeclaration.Internal>? = null, | ||||||
| @SerialName("function_declarations") val functionDeclarations: List<FunctionDeclaration.Internal>? = null, | ||||||
| // This is a json object because it is not possible to make a data class with no parameters. | ||||||
| val codeExecution: JsonObject? = null, | ||||||
| @SerialName("code_execution") val codeExecution: JsonObject? = null, | ||||||
| @SerialName("google_search_retrieval") val googleSearchRetrieval: JsonObject? = null, | ||||||
| ) | ||||||
| public companion object { | ||||||
|
|
||||||
|
|
@@ -43,7 +53,13 @@ internal constructor(internal val functionDeclarations: List<FunctionDeclaration | |||||
| */ | ||||||
| @JvmStatic | ||||||
| public fun functionDeclarations(functionDeclarations: List<FunctionDeclaration>): Tool { | ||||||
| return Tool(functionDeclarations) | ||||||
| return Tool(functionDeclarations = functionDeclarations) | ||||||
| } | ||||||
|
|
||||||
| /** Creates a [Tool] instance that provides the model with access to Google Search. */ | ||||||
| @JvmStatic | ||||||
| public fun googleSearchRetrieval(): Tool { | ||||||
| return Tool(googleSearchRetrieval = true) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better usability and adherence to Kotlin idioms for value-holding classes, consider converting
ThinkingConfiginto adata class. This will automatically provideequals(),hashCode(),toString(), andcopy()methods, which is useful for a configuration object.