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 1b8d444659a0..a8b3b72858b3 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 @@ -134,7 +134,7 @@ public GenerativeModel build() { * Sets the name of the generative model. This is required for building a GenerativeModel * instance. Supported format: "gemini-pro", "models/gemini-pro", * "publishers/google/models/gemini-pro", where "gemini-pro" is the model name. Valid model - * names can be found in the Gemini models documentation + * names can be found in the Gemini models documentation: * https://cloud.google.com/vertex-ai/docs/generative-ai/learn/models#gemini-models */ @CanIgnoreReturnValue @@ -217,13 +217,9 @@ public Builder setSystemInstruction(Content systemInstruction) { * @return a new {@link GenerativeModel} instance with the specified GenerationConfig. */ public GenerativeModel withGenerationConfig(GenerationConfig generationConfig) { + checkNotNull(generationConfig, "GenerationConfig can't be null."); return new GenerativeModel( - modelName, - generationConfig, - ImmutableList.copyOf(safetySettings), - ImmutableList.copyOf(tools), - systemInstruction, - vertexAi); + modelName, generationConfig, safetySettings, tools, systemInstruction, vertexAi); } /** @@ -234,11 +230,14 @@ public GenerativeModel withGenerationConfig(GenerationConfig generationConfig) { * @return a new {@link GenerativeModel} instance with the specified safetySettings. */ public GenerativeModel withSafetySettings(List safetySettings) { + checkNotNull( + safetySettings, + "safetySettings can't be null. Use an empty list if no safety settings is intended."); return new GenerativeModel( modelName, generationConfig, ImmutableList.copyOf(safetySettings), - ImmutableList.copyOf(tools), + tools, systemInstruction, vertexAi); } @@ -251,10 +250,11 @@ public GenerativeModel withSafetySettings(List safetySettings) { * @return a new {@link GenerativeModel} instance with the specified tools. */ public GenerativeModel withTools(List tools) { + checkNotNull(tools, "tools can't be null. Use an empty list if no tool is to be used."); return new GenerativeModel( modelName, generationConfig, - ImmutableList.copyOf(safetySettings), + safetySettings, ImmutableList.copyOf(tools), systemInstruction, vertexAi); @@ -268,11 +268,15 @@ public GenerativeModel withTools(List tools) { * @return a new {@link GenerativeModel} instance with the specified tools. */ public GenerativeModel withSystemInstruction(Content systemInstruction) { + checkNotNull( + systemInstruction, + "system instruction can't be null. " + + "Use Optional.empty() if no system instruction should be provided."); return new GenerativeModel( modelName, generationConfig, - ImmutableList.copyOf(safetySettings), - ImmutableList.copyOf(tools), + safetySettings, + tools, Optional.of(systemInstruction), vertexAi); } @@ -506,7 +510,6 @@ private ApiFuture generateContentAsync(GenerateContentR */ private GenerateContentRequest buildGenerateContentRequest(List contents) { checkArgument(contents != null && !contents.isEmpty(), "contents can't be null or empty."); - GenerateContentRequest.Builder requestBuilder = GenerateContentRequest.newBuilder() .setModel(resourceName) diff --git a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java index 175ed2e7d3ab..e61f6fe32090 100644 --- a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java +++ b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/GenerativeModelTest.java @@ -325,17 +325,17 @@ public void testGenerateContentwithContents() throws Exception { } @Test - public void testGenerateContentwithSystemInstructions() throws Exception { - String systemInstructionText = - "You're a helpful assistant that starts all its answers with: \"COOL\""; - Content systemInstructions = ContentMaker.fromString(systemInstructionText); - - model = new GenerativeModel(MODEL_NAME, vertexAi).withSystemInstruction(systemInstructions); - + public void testGenerateContentwithSystemInstruction() throws Exception { when(mockPredictionServiceClient.generateContentCallable()).thenReturn(mockUnaryCallable); when(mockUnaryCallable.call(any(GenerateContentRequest.class))) .thenReturn(mockGenerateContentResponse); + String systemInstructionText = + "You're a helpful assistant that starts all its answers with: \"COOL\""; + Content systemInstruction = ContentMaker.fromString(systemInstructionText); + + model = new GenerativeModel(MODEL_NAME, vertexAi).withSystemInstruction(systemInstruction); + Content content = ContentMaker.fromString(TEXT); GenerateContentResponse unused = model.generateContent(Arrays.asList(content));