Skip to content

Commit 99427af

Browse files
chore: [vertexai] make attributes final in GenerativeModel (#10584)
PiperOrigin-RevId: 617648662 Co-authored-by: Jaycee Li <jayceeli@google.com>
1 parent a2407ab commit 99427af

File tree

1 file changed

+19
-15
lines changed
  • java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai

1 file changed

+19
-15
lines changed

java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.google.common.base.Strings;
3333
import com.google.common.collect.ImmutableList;
3434
import java.io.IOException;
35-
import java.util.ArrayList;
3635
import java.util.Arrays;
3736
import java.util.List;
3837

@@ -41,9 +40,9 @@ public final class GenerativeModel {
4140
private final String modelName;
4241
private final String resourceName;
4342
private final VertexAI vertexAi;
44-
private GenerationConfig generationConfig = GenerationConfig.getDefaultInstance();
45-
private ImmutableList<SafetySetting> safetySettings = ImmutableList.of();
46-
private ImmutableList<Tool> tools = ImmutableList.of();
43+
private final GenerationConfig generationConfig;
44+
private final ImmutableList<SafetySetting> safetySettings;
45+
private final ImmutableList<Tool> tools;
4746

4847
/**
4948
* Constructs a GenerativeModel instance.
@@ -59,8 +58,8 @@ public GenerativeModel(String modelName, VertexAI vertexAi) {
5958
this(
6059
modelName,
6160
GenerationConfig.getDefaultInstance(),
62-
new ArrayList<SafetySetting>(),
63-
new ArrayList<Tool>(),
61+
ImmutableList.of(),
62+
ImmutableList.of(),
6463
vertexAi);
6564
}
6665

@@ -81,22 +80,29 @@ public GenerativeModel(String modelName, VertexAI vertexAi) {
8180
private GenerativeModel(
8281
String modelName,
8382
GenerationConfig generationConfig,
84-
List<SafetySetting> safetySettings,
85-
List<Tool> tools,
83+
ImmutableList<SafetySetting> safetySettings,
84+
ImmutableList<Tool> tools,
8685
VertexAI vertexAi) {
86+
checkArgument(
87+
!Strings.isNullOrEmpty(modelName),
88+
"modelName can't be null or empty. Please refer to"
89+
+ " https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models#gemini-models"
90+
+ " to find the right model name.");
91+
checkNotNull(vertexAi, "VertexAI can't be null.");
92+
checkNotNull(generationConfig, "GenerationConfig can't be null.");
93+
checkNotNull(safetySettings, "ImmutableList<SafetySettings> can't be null.");
94+
checkNotNull(tools, "ImmutableList<Tool> can't be null.");
95+
8796
modelName = reconcileModelName(modelName);
8897
this.modelName = modelName;
8998
this.resourceName =
9099
String.format(
91100
"projects/%s/locations/%s/publishers/google/models/%s",
92101
vertexAi.getProjectId(), vertexAi.getLocation(), modelName);
93-
checkNotNull(generationConfig, "GenerationConfig can't be null.");
94-
checkNotNull(safetySettings, "List<SafetySettings> can't be null.");
95-
checkNotNull(tools, "List<Tool> can't be null.");
96102
this.vertexAi = vertexAi;
97103
this.generationConfig = generationConfig;
98-
this.safetySettings = ImmutableList.copyOf(safetySettings);
99-
this.tools = ImmutableList.copyOf(tools);
104+
this.safetySettings = safetySettings;
105+
this.tools = tools;
100106
}
101107

102108
/** Builder class for {@link GenerativeModel}. */
@@ -163,7 +169,6 @@ public Builder setSafetySettings(List<SafetySetting> safetySettings) {
163169
checkNotNull(
164170
safetySettings,
165171
"safetySettings can't be null. Use an empty list if no safety settings is intended.");
166-
safetySettings.removeIf(safetySetting -> safetySetting == null);
167172
this.safetySettings = ImmutableList.copyOf(safetySettings);
168173
return this;
169174
}
@@ -175,7 +180,6 @@ public Builder setSafetySettings(List<SafetySetting> safetySettings) {
175180
@BetaApi
176181
public Builder setTools(List<Tool> tools) {
177182
checkNotNull(tools, "tools can't be null. Use an empty list if no tool is to be used.");
178-
tools.removeIf(tool -> tool == null);
179183
this.tools = ImmutableList.copyOf(tools);
180184
return this;
181185
}

0 commit comments

Comments
 (0)