-
Notifications
You must be signed in to change notification settings - Fork 627
feat(ai): add support for setting a thinking budget #6999
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
Open
thatfiredev
wants to merge
11
commits into
main
Choose a base branch
from
rpf/add-thinking-budget-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
55df2e5
feat(ai): add support for setting a thinking budget
thatfiredev 80deba1
chore(ai): minor version bump
thatfiredev b285f28
Merge branch 'main' of github.com:firebase/firebase-android-sdk into …
thatfiredev 67ccb8e
feat: add `UsageMetadata#thoughtsTokenCount`
thatfiredev 4fa568a
chore: bump responses version
thatfiredev 414d9ce
chore: update api.txt
thatfiredev 346908c
chore(ai): 16.2.1 --> 16.3.0
thatfiredev 468b487
internal constructor; update CHANGELOG
thatfiredev 74706c1
update api.txt
thatfiredev dfdbe77
Merge branch 'main' into rpf/add-thinking-budget-support
thatfiredev f780f6b
major version bump
thatfiredev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
firebase-ai/src/main/kotlin/com/google/firebase/ai/type/ThinkingConfig.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2025 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 parameters for thinking features. */ | ||
public class ThinkingConfig | ||
private constructor( | ||
internal val thinkingBudget: Int? = null, | ||
) { | ||
|
||
public class Builder() { | ||
@JvmField | ||
@set:JvmSynthetic // hide void setter from Java | ||
public var thinkingBudget: Int? = null | ||
|
||
/** | ||
* Indicates the thinking budget in tokens. 0 is DISABLED. -1 is AUTOMATIC. The default values | ||
* and allowed ranges are model dependent. | ||
*/ | ||
public fun setThinkingBudget(thinkingBudget: Int): Builder = apply { | ||
this.thinkingBudget = thinkingBudget | ||
} | ||
|
||
public fun build(): ThinkingConfig = ThinkingConfig(thinkingBudget = thinkingBudget) | ||
} | ||
|
||
internal fun toInternal() = Internal(thinkingBudget) | ||
|
||
@Serializable | ||
internal data class Internal(@SerialName("thinking_budget") val thinkingBudget: Int?) | ||
} | ||
|
||
/** | ||
* Helper method to construct a [ThinkingConfig] in a DSL-like manner. | ||
* | ||
* Example Usage: | ||
* ``` | ||
* thinkingConfig { | ||
* thinkingBudget = 0 // disable thinking | ||
* } | ||
* ``` | ||
*/ | ||
public fun thinkingConfig(init: ThinkingConfig.Builder.() -> Unit): ThinkingConfig { | ||
val builder = ThinkingConfig.Builder() | ||
builder.init() | ||
return builder.build() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
firebase-ai/src/test/java/com/google/firebase/ai/type/ThinkingConfigTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2025 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 io.kotest.assertions.json.shouldEqualJson | ||
import io.kotest.matchers.equals.shouldBeEqual | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.junit.Test | ||
|
||
internal class ThinkingConfigTest { | ||
|
||
@Test | ||
fun `Basic ThinkingConfig`() { | ||
val thinkingConfig = ThinkingConfig.Builder().setThinkingBudget(1024).build() | ||
|
||
val expectedJson = | ||
""" | ||
{ | ||
"thinking_budget": 1024 | ||
} | ||
""" | ||
.trimIndent() | ||
|
||
Json.encodeToString(thinkingConfig.toInternal()).shouldEqualJson(expectedJson) | ||
} | ||
|
||
@Test | ||
fun `thinkingConfig DSL correctly delegates to ThinkingConfig#Builder`() { | ||
val thinkingConfig = ThinkingConfig.Builder().setThinkingBudget(1024).build() | ||
|
||
val thinkingConfigDsl = thinkingConfig { thinkingBudget = 1024 } | ||
|
||
thinkingConfig.thinkingBudget?.shouldBeEqual(thinkingConfigDsl.thinkingBudget as Int) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.