diff --git a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ChatSession.java b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ChatSession.java index 536e7b241fab..d2d692bf4a80 100644 --- a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ChatSession.java +++ b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ChatSession.java @@ -24,8 +24,6 @@ import com.google.cloud.vertexai.api.Candidate.FinishReason; import com.google.cloud.vertexai.api.Content; import com.google.cloud.vertexai.api.GenerateContentResponse; -import com.google.cloud.vertexai.api.GenerationConfig; -import com.google.cloud.vertexai.api.SafetySetting; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -55,7 +53,7 @@ public ChatSession(GenerativeModel model) { */ @BetaApi public ResponseStream sendMessageStream(String text) throws IOException { - return sendMessageStream(text, null, null); + return sendMessageStream(text, GenerateContentConfig.newBuilder().build()); } /** @@ -73,64 +71,6 @@ public ResponseStream sendMessageStream( return sendMessageStream(ContentMaker.fromString(text), config); } - /** - * Sends a message to the model and returns a stream of responses. - * - * @param text the message to be sent. - * @param generationConfig the generation config. - * @return an iterable in which each element is a GenerateContentResponse. Can be converted to - * stream by stream() method. - * @deprecated use {@link #sendMessageStream(String, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public ResponseStream sendMessageStream( - String text, GenerationConfig generationConfig) throws IOException { - return sendMessageStream(text, generationConfig, null); - } - - /** - * Sends a message to the model and returns a stream of responses. - * - * @param text the message to be sent. - * @param safetySettings the safety settings. - * @return an iterable in which each element is a GenerateContentResponse. Can be converted to - * stream by stream() method. - * @deprecated use {@link #sendMessageStream(String, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream sendMessageStream( - String text, List safetySettings) throws IOException { - return sendMessageStream(text, null, safetySettings); - } - - /** - * Sends a message to the model and returns a stream of responses. - * - * @param text the message to be sent. - * @param generationConfig the generation config. - * @param safetySettings the safety settings. - * @return an iterable in which each element is a GenerateContentResponse. Can be converted to - * stream by stream() method. - * @deprecated use {@link #sendMessageStream(String, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream sendMessageStream( - String text, GenerationConfig generationConfig, List safetySettings) - throws IOException { - checkLastResponseAndEditHistory(); - - history.add(ContentMaker.fromString(text)); - - ResponseStream respStream = - model.generateContentStream(history, generationConfig, safetySettings); - currentResponseStream = respStream; - currentResponse = null; - return respStream; - } - /** * Sends a message to the model and returns a stream of responses. * @@ -141,7 +81,7 @@ public ResponseStream sendMessageStream( @BetaApi public ResponseStream sendMessageStream(Content content) throws IOException, IllegalArgumentException { - return sendMessageStream(content, null, null); + return sendMessageStream(content, GenerateContentConfig.newBuilder().build()); } /** @@ -165,65 +105,6 @@ public ResponseStream sendMessageStream( return respStream; } - /** - * Sends a message to the model and returns a stream of responses. - * - * @param content the content to be sent. - * @param generationConfig the generation config. - * @return an iterable in which each element is a GenerateContentResponse. Can be converted to - * stream by stream() method. - * @deprecated use {@link #sendMessageStream(Content, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public ResponseStream sendMessageStream( - Content content, GenerationConfig generationConfig) - throws IOException, IllegalArgumentException { - return sendMessageStream(content, generationConfig, null); - } - - /** - * Sends a message to the model and returns a stream of responses. - * - * @param content the content to be sent. - * @param safetySettings the safety settings. - * @return an iterable in which each element is a GenerateContentResponse. Can be converted to - * stream by stream() method. - * @deprecated use {@link #sendMessageStream(Content, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream sendMessageStream( - Content content, List safetySettings) - throws IOException, IllegalArgumentException { - return sendMessageStream(content, null, safetySettings); - } - - /** - * Sends a message to the model and returns a stream of responses. - * - * @param content the content to be sent. - * @param generationConfig the generation config. - * @param safetySettings the safety settings. - * @return an iterable in which each element is a GenerateContentResponse. Can be converted to - * stream by stream() method. - * @deprecated use {@link #sendMessageStream(Content, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream sendMessageStream( - Content content, GenerationConfig generationConfig, List safetySettings) - throws IOException { - checkLastResponseAndEditHistory(); - - history.add(content); - ResponseStream respStream = - model.generateContentStream(history, generationConfig, safetySettings); - currentResponseStream = respStream; - currentResponse = null; - return respStream; - } - /** * Sends a message to the model and returns a response. * @@ -232,7 +113,7 @@ public ResponseStream sendMessageStream( */ @BetaApi public GenerateContentResponse sendMessage(String text) throws IOException { - return sendMessage(text, null, null); + return sendMessage(text, GenerateContentConfig.newBuilder().build()); } /** @@ -249,60 +130,6 @@ public GenerateContentResponse sendMessage(String text, GenerateContentConfig co return sendMessage(ContentMaker.fromString(text), config); } - /** - * Sends a message to the model and returns a response. - * - * @param text the message to be sent. - * @param generationConfig the generation config. - * @return a response. - * @deprecated use {@link #sendMessage(Content, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public GenerateContentResponse sendMessage(String text, GenerationConfig generationConfig) - throws IOException { - return sendMessage(text, generationConfig, null); - } - - /** - * Sends a message to the model and returns a response. - * - * @param text the message to be sent. - * @param safetySettings the safety settings. - * @return a response. - * @deprecated use {@link #sendMessage(String, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public GenerateContentResponse sendMessage(String text, List safetySettings) - throws IOException { - return sendMessage(text, null, safetySettings); - } - - /** - * Sends a message to the model and returns a response. - * - * @param text the message to be sent. - * @param generationConfig the generation config. - * @param safetySettings the safety settings. - * @return a response. - * @deprecated use {@link #sendMessage(String, GenerateContentConfig)} instead - */ - @BetaApi("Both sendMessage and safetySettings are preview features.") - @Deprecated - public GenerateContentResponse sendMessage( - String text, GenerationConfig generationConfig, List safetySettings) - throws IOException { - - checkLastResponseAndEditHistory(); - history.add(ContentMaker.fromString(text)); - GenerateContentResponse response = - model.generateContent(history, generationConfig, safetySettings); - currentResponse = response; - currentResponseStream = null; - return response; - } - /** * Sends a message to the model and returns a response. * @@ -311,7 +138,7 @@ public GenerateContentResponse sendMessage( */ @BetaApi public GenerateContentResponse sendMessage(Content content) throws IOException { - return sendMessage(content, null, null); + return sendMessage(content, GenerateContentConfig.newBuilder().build()); } /** @@ -333,59 +160,6 @@ public GenerateContentResponse sendMessage(Content content, GenerateContentConfi return response; } - /** - * Sends a message to the model and returns a response. - * - * @param content the content to be sent. - * @param generationConfig the generation config. - * @return a response. - * @deprecated use {@link #sendMessage(Content, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public GenerateContentResponse sendMessage(Content content, GenerationConfig generationConfig) - throws IOException { - return sendMessage(content, generationConfig, null); - } - - /** - * Sends a message to the model and returns a response. - * - * @param content the content to be sent. - * @param safetySettings the safety settings. - * @return a response. - * @deprecated use {@link #sendMessage(Content, GenerateContentConfig)} instead - */ - @BetaApi("Both sendMessage and safetySettings are preview features.") - @Deprecated - public GenerateContentResponse sendMessage(Content content, List safetySettings) - throws IOException { - return sendMessage(content, null, safetySettings); - } - - /** - * Sends a message to the model and returns a response. - * - * @param content the content to be sent. - * @param generationConfig the generation config. - * @param safetySettings the safety settings. - * @return a response. - * @deprecated use {@link #sendMessage(Content, GenerateContentConfig)} instead - */ - @BetaApi("Both sendMessage and safetySettings are preview features.") - @Deprecated - public GenerateContentResponse sendMessage( - Content content, GenerationConfig generationConfig, List safetySettings) - throws IOException { - checkLastResponseAndEditHistory(); - history.add(content); - GenerateContentResponse response = - model.generateContent(history, generationConfig, safetySettings); - currentResponse = response; - currentResponseStream = null; - return response; - } - private void removeLastContent() { int lastIndex = history.size() - 1; history.remove(lastIndex); 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 60379fb8f87c..2c9b230f31c9 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 @@ -24,7 +24,6 @@ import com.google.cloud.vertexai.api.GenerateContentRequest; import com.google.cloud.vertexai.api.GenerateContentResponse; import com.google.cloud.vertexai.api.GenerationConfig; -import com.google.cloud.vertexai.api.Part; import com.google.cloud.vertexai.api.SafetySetting; import com.google.cloud.vertexai.api.Tool; import java.io.IOException; @@ -301,7 +300,7 @@ private CountTokensResponse countTokensFromRequest(CountTokensRequest request) */ @BetaApi public GenerateContentResponse generateContent(String text) throws IOException { - return generateContent(text, null, null); + return generateContent(text, GenerateContentConfig.newBuilder().build()); } /** @@ -321,63 +320,32 @@ public GenerateContentResponse generateContent(String text, GenerateContentConfi } /** - * Generates content from generative model given a text and generation config. - * - * @param text a text message to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(String, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public GenerateContentResponse generateContent(String text, GenerationConfig generationConfig) - throws IOException { - return generateContent(text, generationConfig, null); - } - - /** - * Generates content from generative model given a text and safety settings. + * Generates content from this model given a single content. * - * @param text a text message to send to the generative model - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set + * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains * response contents and other metadata * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(String, GenerateContentConfig)} instead */ - @BetaApi("Both generateContent and safetySettings are preview features.") - @Deprecated - public GenerateContentResponse generateContent(String text, List safetySettings) - throws IOException { - return generateContent(text, null, safetySettings); + @BetaApi("generateContent is a preview feature.") + public GenerateContentResponse generateContent(Content content) throws IOException { + return generateContent(content, GenerateContentConfig.newBuilder().build()); } /** - * Generates content from generative model given a text, generation config, and safety settings. + * Generates content from generative model given a single content and configs. * - * @param text a text message to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set + * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model + * @param config a {@link GenerateContentConfig} that contains all the configs in making a + * generate content api call * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains * response contents and other metadata * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(String, GenerateContentConfig)} instead */ - @BetaApi("Both generateContent and safetySettings are preview features.") - @Deprecated - public GenerateContentResponse generateContent( - String text, GenerationConfig generationConfig, List safetySettings) + @BetaApi + public GenerateContentResponse generateContent(Content content, GenerateContentConfig config) throws IOException { - Part part = Part.newBuilder().setText(text).build(); - Content content = Content.newBuilder().addParts(part).setRole("user").build(); - List contents = Arrays.asList(content); - return generateContent(contents, generationConfig, safetySettings); + return generateContent(Arrays.asList(content), config); } /** @@ -391,45 +359,7 @@ public GenerateContentResponse generateContent( */ @BetaApi("generateContent is a preview feature.") public GenerateContentResponse generateContent(List contents) throws IOException { - return generateContent(contents, null, null); - } - - /** - * Generates content from this model given a list of contents and generation config. - * - * @param contents a list of {@link com.google.cloud.vertexai.api.Content} to send to the - * generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(List, GenerateContentConfig)} instead - */ - @BetaApi("generateContent is a preview feature.") - @Deprecated - public GenerateContentResponse generateContent( - List contents, GenerationConfig generationConfig) throws IOException { - return generateContent(contents, generationConfig, null); - } - - /** - * Generates content from this model given a list of contents and safety settings. - * - * @param contents a list of {@link com.google.cloud.vertexai.api.Content} to send to the - * generative model - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(List, GenerateContentConfig)} instead - */ - @BetaApi("Both generateContent and safetySettings are preview features") - @Deprecated - public GenerateContentResponse generateContent( - List contents, List safetySettings) throws IOException { - return generateContent(contents, null, safetySettings); + return generateContent(contents, GenerateContentConfig.newBuilder().build()); } /** @@ -467,44 +397,6 @@ public GenerateContentResponse generateContent( return generateContent(requestBuilder.build()); } - /** - * Generates content from generative model given a list of contents, generation config, and safety - * settings. - * - * @param contents a list of {@link com.google.cloud.vertexai.api.Content} to send to the - * generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(List, GenerateContentConfig)} instead - */ - @BetaApi("Both generateContent and safetySettings are preview features") - @Deprecated - public GenerateContentResponse generateContent( - List contents, GenerationConfig generationConfig, List safetySettings) - throws IOException { - GenerateContentRequest.Builder requestBuilder = - GenerateContentRequest.newBuilder().setModel(this.resourceName).addAllContents(contents); - if (generationConfig != null) { - requestBuilder.setGenerationConfig(generationConfig); - } else if (this.generationConfig != null) { - requestBuilder.setGenerationConfig(this.generationConfig); - } - if (safetySettings != null) { - requestBuilder.addAllSafetySettings(safetySettings); - } else if (this.safetySettings != null) { - requestBuilder.addAllSafetySettings(this.safetySettings); - } - if (this.tools != null) { - requestBuilder.addAllTools(this.tools); - } - return generateContent(requestBuilder.build()); - } - /** * A base generateContent method that will be used internally. * @@ -518,93 +410,6 @@ private GenerateContentResponse generateContent(GenerateContentRequest request) return vertexAi.getPredictionServiceClient().generateContentCallable().call(request); } - /** - * Generates content from this model given a single content. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - */ - @BetaApi("generateContent is a preview feature.") - public GenerateContentResponse generateContent(Content content) throws IOException { - return generateContent(content, null, null); - } - - /** - * Generates content from generative model given a single content and configs. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @param config a {@link GenerateContentConfig} that contains all the configs in making a - * generate content api call - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - */ - @BetaApi - public GenerateContentResponse generateContent(Content content, GenerateContentConfig config) - throws IOException { - return generateContent(Arrays.asList(content), config); - } - - /** - * Generates content from this model given a single content and generation config. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(Content, GenerateContentConfig)} instead - */ - @BetaApi("generateContent is a preview feature.") - @Deprecated - public GenerateContentResponse generateContent(Content content, GenerationConfig generationConfig) - throws IOException { - return generateContent(content, generationConfig, null); - } - - /** - * Generates content from this model given a single content and safety settings. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(Content, GenerateContentConfig)} instead - */ - @BetaApi("generateContent is a preview feature.") - @Deprecated - public GenerateContentResponse generateContent( - Content content, List safetySettings) throws IOException { - return generateContent(content, null, safetySettings); - } - - /** - * Generates content from generative model given a single content, generation config, and safety - * settings. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance that contains - * response contents and other metadata - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContent(Content, GenerateContentConfig)} instead - */ - @BetaApi("Both generateContent and safetySettings are preview features.") - @Deprecated - public GenerateContentResponse generateContent( - Content content, GenerationConfig generationConfig, List safetySettings) - throws IOException { - return generateContent(Arrays.asList(content), generationConfig, safetySettings); - } - /** * Generates content with streaming support from generative model given a text. * @@ -615,7 +420,7 @@ public GenerateContentResponse generateContent( */ public ResponseStream generateContentStream(String text) throws IOException { - return generateContentStream(text, null, null); + return generateContentStream(text, GenerateContentConfig.newBuilder().build()); } /** @@ -633,69 +438,6 @@ public ResponseStream generateContentStream( return generateContentStream(ContentMaker.fromString(text), config); } - /** - * Generates content with streaming support from generative model given a text and generation - * config. - * - * @param text a text message to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(String, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public ResponseStream generateContentStream( - String text, GenerationConfig generationConfig) throws IOException { - return generateContentStream(text, generationConfig, null); - } - - /** - * Generates content with streaming support from generative model given a text and safety - * settings. - * - * @param text a text message to send to the generative model - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(String, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream generateContentStream( - String text, List safetySettings) throws IOException { - return generateContentStream(text, null, safetySettings); - } - - /** - * Generates content with streaming support from generative model given a text, generation config, - * and safety settings. - * - * @param text a text message to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(String, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream generateContentStream( - String text, GenerationConfig generationConfig, List safetySettings) - throws IOException { - Part part = Part.newBuilder().setText(text).build(); - Content content = Content.newBuilder().addParts(part).setRole("user").build(); - List contents = Arrays.asList(content); - return generateContentStream(contents, generationConfig, safetySettings); - } - /** * Generates content with streaming support from generative model given a single Content. * @@ -707,7 +449,7 @@ public ResponseStream generateContentStream( */ public ResponseStream generateContentStream(Content content) throws IOException { - return generateContentStream(content, null, null); + return generateContentStream(content, GenerateContentConfig.newBuilder().build()); } /** @@ -726,66 +468,6 @@ public ResponseStream generateContentStream( return generateContentStream(Arrays.asList(content), config); } - /** - * Generates content with streaming support from generative model given a single Content and - * generation config. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(Content, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public ResponseStream generateContentStream( - Content content, GenerationConfig generationConfig) throws IOException { - return generateContentStream(content, generationConfig, null); - } - - /** - * Generates content with streaming support from generative model given a single content and - * safety settings. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(Content, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream generateContentStream( - Content content, List safetySettings) throws IOException { - return generateContentStream(content, null, safetySettings); - } - - /** - * Generates content with streaming support from generative model given a single content, - * generation config, and safety settings. - * - * @param content a {@link com.google.cloud.vertexai.api.Content} to send to the generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(Content, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream generateContentStream( - Content content, GenerationConfig generationConfig, List safetySettings) - throws IOException { - return generateContentStream(Arrays.asList(content), generationConfig, safetySettings); - } - /** * Generates content with streaming support from generative model given a list of contents. * @@ -797,85 +479,7 @@ public ResponseStream generateContentStream( */ public ResponseStream generateContentStream(List contents) throws IOException { - return generateContentStream(contents, null, null); - } - - /** - * Generates content with streaming support from generative model given a list of contents and - * generation config. - * - * @param contents a list of {@link com.google.cloud.vertexai.api.Content} to send to the - * generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(List, GenerateContentConfig)} instead - */ - @BetaApi - @Deprecated - public ResponseStream generateContentStream( - List contents, GenerationConfig generationConfig) throws IOException { - return generateContentStream(contents, generationConfig, null); - } - - /** - * Generates content with streaming support from generative model given a list of contents and - * safety settings. - * - * @param contents a list of {@link com.google.cloud.vertexai.api.Content} to send to the - * generative model - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(List, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream generateContentStream( - List contents, List safetySettings) throws IOException { - return generateContentStream(contents, null, safetySettings); - } - - /** - * Generates content with streaming support from generative model given a list of contents, - * generation config, and safety settings. - * - * @param contents a list of {@link com.google.cloud.vertexai.api.Content} to send to the - * generative model - * @param generationConfig a {@link com.google.cloud.vertexai.api.GenerationConfig} instance for - * generating response. {@link #getGenerationConfig} will not be used if this is set - * @param safetySettings a list of {@link com.google.cloud.vertexai.api.SafetySetting} for - * generating response. {@link #getSafetySettings} will not be used if this is set - * @return a {@link ResponseStream} that contains a streaming of {@link - * com.google.cloud.vertexai.api.GenerateContentResponse} - * @throws IOException if an I/O error occurs while making the API call - * @deprecated use {@link #generateContentStream(List, GenerateContentConfig)} instead - */ - @BetaApi("safetySettings is a preview feature.") - @Deprecated - public ResponseStream generateContentStream( - List contents, GenerationConfig generationConfig, List safetySettings) - throws IOException { - GenerateContentRequest.Builder requestBuilder = - GenerateContentRequest.newBuilder().setModel(this.resourceName).addAllContents(contents); - if (generationConfig != null) { - requestBuilder.setGenerationConfig(generationConfig); - } else if (this.generationConfig != null) { - requestBuilder.setGenerationConfig(this.generationConfig); - } - if (safetySettings != null) { - requestBuilder.addAllSafetySettings(safetySettings); - } else if (this.safetySettings != null) { - requestBuilder.addAllSafetySettings(this.safetySettings); - } - if (this.tools != null) { - requestBuilder.addAllTools(this.tools); - } - return generateContentStream(requestBuilder.build()); + return generateContentStream(contents, GenerateContentConfig.newBuilder().build()); } /** diff --git a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ChatSessionTest.java b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ChatSessionTest.java index 28d8fd5d970e..5410904e1984 100644 --- a/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ChatSessionTest.java +++ b/java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ChatSessionTest.java @@ -18,6 +18,8 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import com.google.cloud.vertexai.api.Candidate; @@ -113,7 +115,8 @@ public void sendMessageStreamWithText_historyContainsTwoTurns() throws IOExcepti // (Arrange) Set up the return value of the generateContentStream when(mockGenerativeModel.generateContentStream( - Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1)), null, null)) + eq(Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))), + any(GenerateContentConfig.class))) .thenReturn(responseStream); // (Act) send request, consume response and get history @@ -138,7 +141,8 @@ public void sendMessageStreamWithText_throwsIllegalStateExceptionIfResponseNotCo // (Arrange) Set up the return value of the generateContentStream when(mockGenerativeModel.generateContentStream( - Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1)), null, null)) + eq(Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))), + any(GenerateContentConfig.class))) .thenReturn(responseStream); // (Act & Assert) Send request, consume response and get history, but not consume the response @@ -156,7 +160,8 @@ public void sendMessageWithText_historyContainsTwoTurns() throws IOException { // (Arrange) Set up the return value of the generateContent when(mockGenerativeModel.generateContent( - Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1)), null, null)) + eq(Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))), + any(GenerateContentConfig.class))) .thenReturn(RESPONSE_FROM_UNARY_CALL); // (Act) Send text message via sendMessage and get the history. @@ -174,7 +179,8 @@ public void sendMessageWithTextThenModifyHistory_historyChangedToNewContentList( // (Arrange) Set up the return value of the generateContent when(mockGenerativeModel.generateContent( - Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1)), null, null)) + eq(Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))), + any(GenerateContentConfig.class))) .thenReturn(RESPONSE_FROM_UNARY_CALL); // (Act) Send text message via sendMessage and get the history. @@ -204,7 +210,8 @@ public void sendMessageStreamWithText_throwsIllegalStateExceptionWhenFinishReaso // (Arrange) Set up the return value of the generateContentStream when(mockGenerativeModel.generateContentStream( - Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1)), null, null)) + eq(Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))), + any(GenerateContentConfig.class))) .thenReturn(responseStream); // (Act) send request, consume response @@ -228,7 +235,8 @@ public void sendMessageWithText_throwsIllegalStateExceptionWhenFinishReasonIsNot throws IOException { // (Arrange) Set up the return value of the generateContent when(mockGenerativeModel.generateContent( - Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1)), null, null)) + eq(Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))), + any(GenerateContentConfig.class))) .thenReturn(RESPONSE_FROM_UNARY_CALL_WITH_OTHER_FINISH_REASON); // (Act) Send text message via sendMessage 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 740934aae8ce..14864a378d1e 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 @@ -357,27 +357,6 @@ public void testGenerateContentwithContents() throws Exception { assertThat(request.getValue().getContents(0).getParts(0).getText()).isEqualTo(TEXT); } - @Test - public void testGenerateContentwithGenerationConfig() throws Exception { - model = new GenerativeModel(MODEL_NAME, vertexAi); - - Field field = VertexAI.class.getDeclaredField("predictionServiceClient"); - field.setAccessible(true); - field.set(vertexAi, mockPredictionServiceClient); - - when(mockPredictionServiceClient.generateContentCallable()).thenReturn(mockUnaryCallable); - when(mockUnaryCallable.call(any(GenerateContentRequest.class))) - .thenReturn(mockGenerateContentResponse); - - GenerateContentResponse unused = model.generateContent(TEXT, GENERATION_CONFIG); - - ArgumentCaptor request = - ArgumentCaptor.forClass(GenerateContentRequest.class); - verify(mockUnaryCallable).call(request.capture()); - assertThat(request.getValue().getContents(0).getParts(0).getText()).isEqualTo(TEXT); - assertThat(request.getValue().getGenerationConfig()).isEqualTo(GENERATION_CONFIG); - } - @Test public void testGenerateContentwithDefaultGenerationConfig() throws Exception { model = new GenerativeModel(MODEL_NAME, DEFAULT_GENERATION_CONFIG, vertexAi); @@ -399,27 +378,6 @@ public void testGenerateContentwithDefaultGenerationConfig() throws Exception { assertThat(request.getValue().getGenerationConfig()).isEqualTo(DEFAULT_GENERATION_CONFIG); } - @Test - public void testGenerateContentwithSafetySettings() throws Exception { - model = new GenerativeModel(MODEL_NAME, vertexAi); - - Field field = VertexAI.class.getDeclaredField("predictionServiceClient"); - field.setAccessible(true); - field.set(vertexAi, mockPredictionServiceClient); - - when(mockPredictionServiceClient.generateContentCallable()).thenReturn(mockUnaryCallable); - when(mockUnaryCallable.call(any(GenerateContentRequest.class))) - .thenReturn(mockGenerateContentResponse); - - GenerateContentResponse unused = model.generateContent(TEXT, safetySettings); - - ArgumentCaptor request = - ArgumentCaptor.forClass(GenerateContentRequest.class); - verify(mockUnaryCallable).call(request.capture()); - assertThat(request.getValue().getContents(0).getParts(0).getText()).isEqualTo(TEXT); - assertThat(request.getValue().getSafetySettings(0)).isEqualTo(SAFETY_SETTING); - } - @Test public void testGenerateContentwithDefaultSafetySettings() throws Exception { model = new GenerativeModel(MODEL_NAME, defaultSafetySettings, vertexAi); @@ -570,28 +528,6 @@ public void testGenerateContentStreamwithContents() throws Exception { .isEqualTo("What is your name?"); } - @Test - public void testGenerateContentStreamwithGenerationConfig() throws Exception { - model = new GenerativeModel(MODEL_NAME, vertexAi); - - Field field = VertexAI.class.getDeclaredField("predictionServiceClient"); - field.setAccessible(true); - field.set(vertexAi, mockPredictionServiceClient); - - when(mockPredictionServiceClient.streamGenerateContentCallable()) - .thenReturn(mockServerStreamCallable); - when(mockServerStreamCallable.call(any(GenerateContentRequest.class))) - .thenReturn(mockServerStream); - when(mockServerStream.iterator()).thenReturn(mockServerStreamIterator); - - ResponseStream unused = model.generateContentStream(TEXT, GENERATION_CONFIG); - - ArgumentCaptor request = - ArgumentCaptor.forClass(GenerateContentRequest.class); - verify(mockServerStreamCallable).call(request.capture()); - assertThat(request.getValue().getGenerationConfig()).isEqualTo(GENERATION_CONFIG); - } - @Test public void testGenerateContentStreamwithDefaultGenerationConfig() throws Exception { model = new GenerativeModel(MODEL_NAME, DEFAULT_GENERATION_CONFIG, vertexAi); @@ -614,28 +550,6 @@ public void testGenerateContentStreamwithDefaultGenerationConfig() throws Except assertThat(request.getValue().getGenerationConfig()).isEqualTo(DEFAULT_GENERATION_CONFIG); } - @Test - public void testGenerateContentStreamwithSafetySettings() throws Exception { - model = new GenerativeModel(MODEL_NAME, vertexAi); - - Field field = VertexAI.class.getDeclaredField("predictionServiceClient"); - field.setAccessible(true); - field.set(vertexAi, mockPredictionServiceClient); - - when(mockPredictionServiceClient.streamGenerateContentCallable()) - .thenReturn(mockServerStreamCallable); - when(mockServerStreamCallable.call(any(GenerateContentRequest.class))) - .thenReturn(mockServerStream); - when(mockServerStream.iterator()).thenReturn(mockServerStreamIterator); - - ResponseStream unused = model.generateContentStream(TEXT, safetySettings); - - ArgumentCaptor request = - ArgumentCaptor.forClass(GenerateContentRequest.class); - verify(mockServerStreamCallable).call(request.capture()); - assertThat(request.getValue().getSafetySettings(0)).isEqualTo(SAFETY_SETTING); - } - @Test public void testGenerateContentStreamwithDefaultSafetySettings() throws Exception { model = new GenerativeModel(MODEL_NAME, defaultSafetySettings, vertexAi);