-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Support Gemini thinking budget in inference API #133599
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
Merged
DonalEvans
merged 19 commits into
elastic:main
from
DonalEvans:add-gemini-thinking-budget
Sep 4, 2025
Merged
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6e9eca0
Support Gemini thinking budget in inference API
DonalEvans bd109a0
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans a39936f
Apply review feedback
DonalEvans 819aac6
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 9e42997
Fix transport versions
DonalEvans a61a649
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 95160b5
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 56fbae2
Update docs/changelog/133599.yaml
DonalEvans 5436712
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 4171c69
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 546f5ad
Move thinking config settings into task settings
DonalEvans b10aed9
Remove unnecessary override from test
DonalEvans 90e2a95
Remove transport version checks for task settings class
DonalEvans ef70439
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans b458892
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans ac00f5b
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 0f03736
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 779819a
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 83e5135
Merge branch 'main' into add-gemini-thinking-budget
DonalEvans 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
121 changes: 121 additions & 0 deletions
121
.../org/elasticsearch/xpack/inference/services/googlevertexai/completion/ThinkingConfig.java
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,121 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference.services.googlevertexai.completion; | ||
|
||
import org.elasticsearch.TransportVersions; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.ValidationException; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.inference.ModelConfigurations; | ||
import org.elasticsearch.xcontent.ToXContentFragment; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext; | ||
import org.elasticsearch.xpack.inference.services.ServiceUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.xpack.inference.services.ServiceUtils.removeFromMapOrDefaultEmpty; | ||
import static org.elasticsearch.xpack.inference.services.ServiceUtils.throwIfNotEmptyMap; | ||
|
||
/** | ||
* This class encapsulates the ThinkingConfig object contained within GenerationConfig. Only the thinkingBudget field is currently | ||
* supported, but the includeThoughts field may be added in the future | ||
DonalEvans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public class ThinkingConfig implements Writeable, ToXContentFragment { | ||
public static final String THINKING_CONFIG_FIELD = "thinking_config"; | ||
public static final String THINKING_BUDGET_FIELD = "thinking_budget"; | ||
|
||
private final Integer thinkingBudget; | ||
|
||
/** | ||
* Constructor for an empty {@code ThinkingConfig} | ||
*/ | ||
public ThinkingConfig() { | ||
this.thinkingBudget = null; | ||
} | ||
|
||
public ThinkingConfig(Integer thinkingBudget) { | ||
this.thinkingBudget = thinkingBudget; | ||
} | ||
|
||
public ThinkingConfig(StreamInput in) throws IOException { | ||
if (in.getTransportVersion().onOrAfter(TransportVersions.GEMINI_THINKING_BUDGET_ADDED)) { | ||
DonalEvans marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
thinkingBudget = in.readOptionalVInt(); | ||
} else { | ||
thinkingBudget = null; | ||
} | ||
} | ||
|
||
public static ThinkingConfig of( | ||
Map<String, Object> map, | ||
ThinkingConfig defaultValue, | ||
DonalEvans marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
ValidationException validationException, | ||
String serviceName, | ||
ConfigurationParseContext context | ||
) { | ||
Map<String, Object> thinkingConfigSettings = removeFromMapOrDefaultEmpty(map, THINKING_CONFIG_FIELD); | ||
Integer thinkingBudget = ServiceUtils.extractOptionalInteger( | ||
thinkingConfigSettings, | ||
THINKING_BUDGET_FIELD, | ||
ModelConfigurations.SERVICE_SETTINGS, | ||
validationException | ||
); | ||
|
||
if (ConfigurationParseContext.isRequestContext(context)) { | ||
throwIfNotEmptyMap(thinkingConfigSettings, serviceName); | ||
} | ||
|
||
return thinkingBudget == null ? defaultValue : new ThinkingConfig(thinkingBudget); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return thinkingBudget == null; | ||
} | ||
|
||
public Integer getThinkingBudget() { | ||
return thinkingBudget; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
if (out.getTransportVersion().onOrAfter(TransportVersions.GEMINI_THINKING_BUDGET_ADDED)) { | ||
out.writeOptionalVInt(thinkingBudget); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
if (thinkingBudget != null) { | ||
builder.startObject(THINKING_CONFIG_FIELD); | ||
builder.field(THINKING_BUDGET_FIELD, thinkingBudget); | ||
builder.endObject(); | ||
} | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ThinkingConfig that = (ThinkingConfig) o; | ||
return Objects.equals(thinkingBudget, that.thinkingBudget); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(thinkingBudget); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
} |
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
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.