@@ -144,6 +144,40 @@ public class Main {
144144}
145145```
146146
147+ #### Text Generation with Async
148+ To get a future response, you can use the ` generateContentAsync ` method
149+
150+ ``` java
151+ package <your package name>
152+
153+ import com.google.api.core.ApiFuture ;
154+ import com.google.cloud.vertexai.VertexAI ;
155+ import com.google.cloud.vertexai.api.GenerateContentResponse ;
156+ import com.google.cloud.vertexai.generativeai.GenerativeModel ;
157+ import java.io.IOException ;
158+
159+ public class Main {
160+ private static final String PROJECT_ID = < your project id> ;
161+ private static final String LOCATION = < location> ;
162+
163+ public static void main (String [] args ) throws IOException {
164+ try (VertexAI vertexAi = new VertexAI (PROJECT_ID , LOCATION );) {
165+
166+ GenerativeModel model = new GenerativeModel (" gemini-pro" , vertexAi);
167+
168+ ApiFuture<GenerateContentResponse > future = model. generateContentAsync(" How are you?" );
169+
170+ // Do something else.
171+
172+ // Get the response from Future
173+ GenerateContentResponse response = future. get();
174+
175+ // Do something with the response.
176+ }
177+ }
178+ }
179+ ```
180+
147181#### Text Generation from Multi-modal Input
148182To generate text based on data of multiple modalities, one needs to make a ` Content ` , which is made easier by ` ContentMaker ` :
149183
@@ -271,6 +305,151 @@ public class Main {
271305}
272306```
273307
308+ #### Generation with customized configurations
309+
310+ The Vertex AI SDK for Java provides configurations for customizing content
311+ generation. You can configure options like
312+ [GenerationConfig][generationconfig-ref] and [SafetySetting][safetysetting-ref],
313+ or add [Tool][tool-ref] for function calling.
314+
315+ You can choose between two configuration approaches: set configs during model
316+ instantiation for consistency across all text generations, or adjust them on a
317+ per-request basis for fine-grained control.
318+
319+ ##### Model level configurations
320+
321+ ```java
322+ package <PACKAGE_NAME>
323+
324+ import com.google.cloud.vertexai.VertexAI;
325+ import com.google.cloud.vertexai.api.GenerateContentResponse;
326+ import com.google.cloud.vertexai.api.GenerationConfig;
327+ import com.google.cloud.vertexai.generativeai.GenerativeModel;
328+ import com.google.cloud.vertexai.generativeai.ResponseHandler;
329+ import java.io.IOException;
330+
331+ public class Main {
332+ private static final String PROJECT_ID = <PROJECT_ID>;
333+ private static final String LOCATION = <LOCATION>;
334+
335+ public static void main(String[] args) throws IOException {
336+ try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION);) {
337+ // Build a GenerationConfig instance.
338+ GenerationConfig generationConfig =
339+ GenerationConfig.newBuilder().setMaxOutputTokens(50).build();
340+
341+ // Use the builder to instantialize the model with the configuration.
342+ GenerativeModel model =
343+ new GenerativeModel.Builder()
344+ .setModelName("gemino-pro")
345+ .setVertexAi(vertexAi)
346+ .setGenerationConfig(generationConfig)
347+ .build();
348+
349+ // Generate the response.
350+ GenerateContentResponse response = model.generateContent("Please explain LLM?");
351+
352+ // Do something with the response.
353+ }
354+ }
355+ }
356+ ```
357+
358+ ##### Request level configurations
359+
360+ Our SDK provides fluent APIs to control request level configurations.
361+
362+ ```java
363+ package <PACKAGE_NAME>
364+
365+ import com.google.cloud.vertexai.VertexAI;
366+ import com.google.cloud.vertexai.api.GenerateContentResponse;
367+ import com.google.cloud.vertexai.api.HarmCategory;
368+ import com.google.cloud.vertexai.api.SafetySetting;
369+ import com.google.cloud.vertexai.api.SafetySetting.HarmBlockThreshold;
370+ import com.google.cloud.vertexai.generativeai.GenerateContentConfig;
371+ import com.google.cloud.vertexai.generativeai.GenerativeModel;
372+ import java.io.IOException;
373+ import java.util.Arrays;
374+
375+ public class Main {
376+ private static final String PROJECT_ID = <PROJECT_ID>;
377+ private static final String LOCATION = <LOCATION>;
378+
379+ public static void main(String[] args) throws IOException {
380+ try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION); ) {
381+ // Build a SafetySetting instance.
382+ SafetySetting safetySetting =
383+ SafetySetting.newBuilder()
384+ .setCategory(HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT)
385+ .setThreshold(HarmBlockThreshold.BLOCK_LOW_AND_ABOVE)
386+ .build();
387+
388+ // Generate the response with the fluent API `withSafetySetting`.
389+ GenerateContentResponse response =
390+ model
391+ .withSafetySetting(Arrays.asList(SafetySetting))
392+ .generateContent("Please explain LLM?");
393+
394+ // Do something with the response.
395+ }
396+ }
397+ }
398+ ```
399+
400+ #### Configurations for ChatSession
401+
402+ When a chat session is started (`ChatSesson chat = model.startChat()`),
403+ it inherits all configurations from the model. You can also use fluent APIs
404+ to update these settings during the chat.
405+
406+ ```java
407+ package <PACKAGE_NAME>
408+
409+ import com.google.cloud.vertexai.VertexAI;
410+ import com.google.cloud.vertexai.api.GenerateContentResponse;
411+ import com.google.cloud.vertexai.api.GenerationConfig;
412+ import com.google.cloud.vertexai.generativeai.ChatSession;
413+ import com.google.cloud.vertexai.generativeai.GenerativeModel;
414+ import com.google.cloud.vertexai.generativeai.ResponseHandler;
415+ import java.io.IOException;
416+
417+ public class Main {
418+ private static final String PROJECT_ID = <PROJECT_ID>;
419+ private static final String LOCATION = <LOCATION>;
420+
421+ public static void main(String[] args) throws IOException {
422+ try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION);) {
423+ // Instantiate a model with GenerationConfig
424+ GenerationConfig generationConfig =
425+ GenerationConfig.newBuilder().setMaxOutputTokens(50).build();
426+ GenerativeModel model =
427+ new GenerativeModel.Builder()
428+ .setModelName("gemino-pro")
429+ .setVertexAi(vertexAi)
430+ .setGenerationConfig(generationConfig)
431+ .build();
432+
433+ // Start a chat session
434+ ChatSession chat = model.startChat();
435+
436+ // Send a message. The model level GenerationConfig will be applied here
437+ GenerateContentResponse response = chat.sendMessage("Please explain LLM?");
438+
439+ // Do something with the response
440+
441+ // Send another message, using Fluent API to update the GenerationConfig
442+ response =
443+ chat.withGenerationConfig(GenerationConfig.getDefaultInstance())
444+ .sendMessage("Tell me more about what you can do.");
445+
446+ // Do something with the response
447+ }
448+ }
449+ }
450+ ```
451+
452+
274453#### Using ChatSession for Function-calling
275454In a chat, we can also do function calling.
276455
@@ -350,7 +529,7 @@ public class Main {
350529 // Start a chat session from a model, with the use of the declared
351530 // function.
352531 GenerativeModel model =
353- GenerativeModel . newBuilder ()
532+ new GenerativeModel .Builder ()
354533 .setModelName(MODEL_NAME )
355534 .setVertexAi(vertexAi)
356535 .setTools(Arrays . asList(tool))
@@ -457,9 +636,6 @@ public class Main {
457636}
458637```
459638
460- #### Model/Chat-level configurations
461-
462- TODO(jayceeli)
463639
464640## Supported Java Versions
465641
@@ -576,4 +752,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
576752[ apilibs ] : https://cloud.google.com/apis/docs/client-libraries-explained#google_api_client_libraries
577753[ oracle ] : https://www.oracle.com/java/technologies/java-se-support-roadmap.html
578754[ g-c-j ] : http://github.com/googleapis/google-cloud-java
579- [ generative-ai-studio ] : https://cloud.google.com/generative-ai-studio?hl=en
755+ [ generative-ai-studio ] : https://cloud.google.com/generative-ai-studio?hl=en
756+ [ generationconfig-ref ] : https://cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.api.GenerationConfig.Builder
757+ [ safetysetting-ref ] : https://cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.api.SafetySetting.Builder
758+ [ tool-ref ] : https://cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.api.Tool.Builder
0 commit comments