diff --git a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ContentMaker.java b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ContentMaker.java index e9338b91eb50..1816e377896e 100644 --- a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ContentMaker.java +++ b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ContentMaker.java @@ -16,8 +16,11 @@ package com.google.cloud.vertexai.generativeai; +import static com.google.common.base.Preconditions.checkArgument; + import com.google.cloud.vertexai.api.Content; import com.google.cloud.vertexai.api.Part; +import com.google.common.base.Strings; /** Helper class to create content. */ public class ContentMaker { @@ -34,6 +37,7 @@ public static ContentMakerForRole forRole(String role) { } private static Content fromStringWithRole(String role, String text) { + checkArgument(!Strings.isNullOrEmpty(text), "text message can't be null or empty."); return Content.newBuilder().addParts(Part.newBuilder().setText(text)).setRole(role).build(); } 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 815122a01a3c..4312e422525e 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 @@ -228,7 +228,6 @@ public GenerativeModel withTools(List tools) { */ @BetaApi public CountTokensResponse countTokens(String text) throws IOException { - // TODO(b/330402637): Check null and empty values for the input string. return countTokens(ContentMaker.fromString(text)); } @@ -255,6 +254,9 @@ public CountTokensResponse countTokens(Content content) throws IOException { */ @BetaApi public CountTokensResponse countTokens(List contents) throws IOException { + if (contents == null || contents.isEmpty()) { + throw new IllegalArgumentException("contents can't be null or empty."); + } CountTokensRequest request = CountTokensRequest.newBuilder() .setEndpoint(resourceName) @@ -287,7 +289,6 @@ private CountTokensResponse countTokensFromRequest(CountTokensRequest request) * @throws IOException if an I/O error occurs while making the API call */ public GenerateContentResponse generateContent(String text) throws IOException { - // TODO(b/330402637): Check null and empty values for the input string. return generateContent(ContentMaker.fromString(text)); } @@ -447,6 +448,9 @@ private ApiFuture generateContentAsync(GenerateContentR * contents and model configurations. */ private GenerateContentRequest buildGenerateContentRequest(List contents) { + if (contents == null || contents.isEmpty()) { + throw new IllegalArgumentException("contents can't be null or empty."); + } return GenerateContentRequest.newBuilder() .setModel(resourceName) .addAllContents(contents) diff --git a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ContentMakerTest.java b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ContentMakerTest.java index d0813890ecea..3a318aaeb453 100644 --- a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ContentMakerTest.java +++ b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ContentMakerTest.java @@ -17,6 +17,7 @@ package com.google.cloud.vertexai.generativeai; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.cloud.vertexai.api.Content; import com.google.protobuf.ByteString; @@ -38,6 +39,24 @@ public void fromString_returnsContentWithText() { assertThat(content.getParts(0).getText()).isEqualTo(stringInput); } + @Test + public void fromString_throwsIllegalArgumentException_withEmptyString() { + String stringInput = ""; + + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> ContentMaker.fromString(stringInput)); + assertThat(thrown).hasMessageThat().isEqualTo("text message can't be null or empty."); + } + + @Test + public void fromString_throwsIllegalArgumentException_withNullString() { + String stringInput = null; + + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> ContentMaker.fromString(stringInput)); + assertThat(thrown).hasMessageThat().isEqualTo("text message can't be null or empty."); + } + @Test public void forRole_returnsContentWithArbitraryRoleSet() { // Although in our docstring, we said only three roles are acceptable, we make sure the code 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 84f8d0b61fa5..2495fe0694e9 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 @@ -48,6 +48,7 @@ import com.google.cloud.vertexai.api.Type; import com.google.cloud.vertexai.api.VertexAISearch; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -451,6 +452,16 @@ public void testGenerateContentwithFluentApi() throws Exception { assertThat(request.getValue().getTools(0)).isEqualTo(TOOL); } + @Test + public void generateContent_withNullContents_throws() throws Exception { + model = new GenerativeModel(MODEL_NAME, vertexAi); + List contents = null; + + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> model.generateContent(contents)); + assertThat(thrown).hasMessageThat().isEqualTo("contents can't be null or empty."); + } + @Test public void testGenerateContentStreamwithText() throws Exception { model = new GenerativeModel(MODEL_NAME, vertexAi); @@ -635,6 +646,16 @@ public void testGenerateContentStreamwithFluentApi() throws Exception { assertThat(request.getValue().getTools(0)).isEqualTo(TOOL); } + @Test + public void generateContentStream_withEmptyContents_throws() throws Exception { + model = new GenerativeModel(MODEL_NAME, vertexAi); + List contents = new ArrayList<>(); + + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> model.generateContentStream(contents)); + assertThat(thrown).hasMessageThat().isEqualTo("contents can't be null or empty."); + } + @Test public void generateContentAsync_withText_sendsCorrectRequest() throws Exception { model = new GenerativeModel(MODEL_NAME, vertexAi);