Skip to content

feat(google genai): support sending labels with chat request #4148

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ Prompt buildRequestPrompt(Prompt prompt) {
runtimeOptions.getGoogleSearchRetrieval(), this.defaultOptions.getGoogleSearchRetrieval()));
requestOptions.setSafetySettings(ModelOptionsUtils.mergeOption(runtimeOptions.getSafetySettings(),
this.defaultOptions.getSafetySettings()));
requestOptions
.setLabels(ModelOptionsUtils.mergeOption(runtimeOptions.getLabels(), this.defaultOptions.getLabels()));
}
else {
requestOptions.setInternalToolExecutionEnabled(this.defaultOptions.getInternalToolExecutionEnabled());
Expand All @@ -487,6 +489,7 @@ Prompt buildRequestPrompt(Prompt prompt) {

requestOptions.setGoogleSearchRetrieval(this.defaultOptions.getGoogleSearchRetrieval());
requestOptions.setSafetySettings(this.defaultOptions.getSafetySettings());
requestOptions.setLabels(this.defaultOptions.getLabels());
}

ToolCallingChatOptions.validateToolCallbacks(requestOptions.getToolCallbacks());
Expand Down Expand Up @@ -677,6 +680,9 @@ GeminiRequest createGeminiRequest(Prompt prompt) {
configBuilder
.thinkingConfig(ThinkingConfig.builder().thinkingBudget(requestOptions.getThinkingBudget()).build());
}
if (requestOptions.getLabels() != null && !requestOptions.getLabels().isEmpty()) {
configBuilder.labels(requestOptions.getLabels());
}

// Add safety settings
if (!CollectionUtils.isEmpty(requestOptions.getSafetySettings())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public class GoogleGenAiChatOptions implements ToolCallingChatOptions {

@JsonIgnore
private List<GoogleGenAiSafetySetting> safetySettings = new ArrayList<>();

@JsonIgnore
private Map<String, String> labels = new HashMap<>();
// @formatter:on

public static Builder builder() {
Expand All @@ -170,6 +173,7 @@ public static GoogleGenAiChatOptions fromOptions(GoogleGenAiChatOptions fromOpti
options.setInternalToolExecutionEnabled(fromOptions.getInternalToolExecutionEnabled());
options.setToolContext(fromOptions.getToolContext());
options.setThinkingBudget(fromOptions.getThinkingBudget());
options.setLabels(fromOptions.getLabels());
return options;
}

Expand Down Expand Up @@ -332,6 +336,15 @@ public void setSafetySettings(List<GoogleGenAiSafetySetting> safetySettings) {
this.safetySettings = safetySettings;
}

public Map<String, String> getLabels() {
return this.labels;
}

public void setLabels(Map<String, String> labels) {
Assert.notNull(labels, "labels must not be null");
this.labels = labels;
}

@Override
public Map<String, Object> getToolContext() {
return this.toolContext;
Expand Down Expand Up @@ -363,15 +376,15 @@ public boolean equals(Object o) {
&& Objects.equals(this.toolNames, that.toolNames)
&& Objects.equals(this.safetySettings, that.safetySettings)
&& Objects.equals(this.internalToolExecutionEnabled, that.internalToolExecutionEnabled)
&& Objects.equals(this.toolContext, that.toolContext);
&& Objects.equals(this.toolContext, that.toolContext) && Objects.equals(this.labels, that.labels);
}

@Override
public int hashCode() {
return Objects.hash(this.stopSequences, this.temperature, this.topP, this.topK, this.candidateCount,
this.frequencyPenalty, this.presencePenalty, this.thinkingBudget, this.maxOutputTokens, this.model,
this.responseMimeType, this.toolCallbacks, this.toolNames, this.googleSearchRetrieval,
this.safetySettings, this.internalToolExecutionEnabled, this.toolContext);
this.safetySettings, this.internalToolExecutionEnabled, this.toolContext, this.labels);
}

@Override
Expand All @@ -382,7 +395,8 @@ public String toString() {
+ ", candidateCount=" + this.candidateCount + ", maxOutputTokens=" + this.maxOutputTokens + ", model='"
+ this.model + '\'' + ", responseMimeType='" + this.responseMimeType + '\'' + ", toolCallbacks="
+ this.toolCallbacks + ", toolNames=" + this.toolNames + ", googleSearchRetrieval="
+ this.googleSearchRetrieval + ", safetySettings=" + this.safetySettings + '}';
+ this.googleSearchRetrieval + ", safetySettings=" + this.safetySettings + ", labels=" + this.labels
+ '}';
}

@Override
Expand Down Expand Up @@ -510,6 +524,12 @@ public Builder thinkingBudget(Integer thinkingBudget) {
return this;
}

public Builder labels(Map<String, String> labels) {
Assert.notNull(labels, "labels must not be null");
this.options.labels = labels;
return this;
}

public GoogleGenAiChatOptions build() {
return this.options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -104,6 +106,29 @@ public void testEqualsAndHashCodeWithThinkingBudget() {
assertThat(options1.hashCode()).isNotEqualTo(options3.hashCode());
}

@Test
public void testEqualsAndHashCodeWithLabels() {
GoogleGenAiChatOptions options1 = GoogleGenAiChatOptions.builder()
.model("test-model")
.labels(Map.of("org", "my-org"))
.build();

GoogleGenAiChatOptions options2 = GoogleGenAiChatOptions.builder()
.model("test-model")
.labels(Map.of("org", "my-org"))
.build();

GoogleGenAiChatOptions options3 = GoogleGenAiChatOptions.builder()
.model("test-model")
.labels(Map.of("org", "other-org"))
.build();

assertThat(options1).isEqualTo(options2);
assertThat(options1.hashCode()).isEqualTo(options2.hashCode());
assertThat(options1).isNotEqualTo(options3);
assertThat(options1.hashCode()).isNotEqualTo(options3.hashCode());
}

@Test
public void testToStringWithThinkingBudget() {
GoogleGenAiChatOptions options = GoogleGenAiChatOptions.builder()
Expand All @@ -116,4 +141,16 @@ public void testToStringWithThinkingBudget() {
assertThat(toString).contains("test-model");
}

}
@Test
public void testToStringWithLabels() {
GoogleGenAiChatOptions options = GoogleGenAiChatOptions.builder()
.model("test-model")
.labels(Map.of("org", "my-org"))
.build();

String toString = options.toString();
assertThat(toString).contains("labels={org=my-org}");
assertThat(toString).contains("test-model");
}

}