From 99427af6286fad02c04f4c5afa6dc04605b6fdfe Mon Sep 17 00:00:00 2001 From: "copybara-service[bot]" <56741989+copybara-service[bot]@users.noreply.github.com> Date: Wed, 20 Mar 2024 16:40:27 -0700 Subject: [PATCH] chore: [vertexai] make attributes final in GenerativeModel (#10584) PiperOrigin-RevId: 617648662 Co-authored-by: Jaycee Li --- .../generativeai/GenerativeModel.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java index 12ebe51bf2b1..e50ea1d90067 100644 --- a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java +++ b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/GenerativeModel.java @@ -32,7 +32,6 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -41,9 +40,9 @@ public final class GenerativeModel { private final String modelName; private final String resourceName; private final VertexAI vertexAi; - private GenerationConfig generationConfig = GenerationConfig.getDefaultInstance(); - private ImmutableList safetySettings = ImmutableList.of(); - private ImmutableList tools = ImmutableList.of(); + private final GenerationConfig generationConfig; + private final ImmutableList safetySettings; + private final ImmutableList tools; /** * Constructs a GenerativeModel instance. @@ -59,8 +58,8 @@ public GenerativeModel(String modelName, VertexAI vertexAi) { this( modelName, GenerationConfig.getDefaultInstance(), - new ArrayList(), - new ArrayList(), + ImmutableList.of(), + ImmutableList.of(), vertexAi); } @@ -81,22 +80,29 @@ public GenerativeModel(String modelName, VertexAI vertexAi) { private GenerativeModel( String modelName, GenerationConfig generationConfig, - List safetySettings, - List tools, + ImmutableList safetySettings, + ImmutableList tools, VertexAI vertexAi) { + checkArgument( + !Strings.isNullOrEmpty(modelName), + "modelName can't be null or empty. Please refer to" + + " https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models#gemini-models" + + " to find the right model name."); + checkNotNull(vertexAi, "VertexAI can't be null."); + checkNotNull(generationConfig, "GenerationConfig can't be null."); + checkNotNull(safetySettings, "ImmutableList can't be null."); + checkNotNull(tools, "ImmutableList can't be null."); + modelName = reconcileModelName(modelName); this.modelName = modelName; this.resourceName = String.format( "projects/%s/locations/%s/publishers/google/models/%s", vertexAi.getProjectId(), vertexAi.getLocation(), modelName); - checkNotNull(generationConfig, "GenerationConfig can't be null."); - checkNotNull(safetySettings, "List can't be null."); - checkNotNull(tools, "List can't be null."); this.vertexAi = vertexAi; this.generationConfig = generationConfig; - this.safetySettings = ImmutableList.copyOf(safetySettings); - this.tools = ImmutableList.copyOf(tools); + this.safetySettings = safetySettings; + this.tools = tools; } /** Builder class for {@link GenerativeModel}. */ @@ -163,7 +169,6 @@ public Builder setSafetySettings(List safetySettings) { checkNotNull( safetySettings, "safetySettings can't be null. Use an empty list if no safety settings is intended."); - safetySettings.removeIf(safetySetting -> safetySetting == null); this.safetySettings = ImmutableList.copyOf(safetySettings); return this; } @@ -175,7 +180,6 @@ public Builder setSafetySettings(List safetySettings) { @BetaApi public Builder setTools(List tools) { checkNotNull(tools, "tools can't be null. Use an empty list if no tool is to be used."); - tools.removeIf(tool -> tool == null); this.tools = ImmutableList.copyOf(tools); return this; }