@@ -237,7 +237,6 @@ import com.google.cloud.vertexai.generativeai.GenerativeModel;
237
237
import com.google.cloud.vertexai.generativeai.ChatSession;
238
238
import com.google.cloud.vertexai.generativeai.ResponseStream;
239
239
import com.google.cloud.vertexai.api.GenerateContentResponse;
240
- import com.google.cloud.vertexai.api.GenerationConfig;
241
240
import java.io.IOException;
242
241
import java.util.Arrays;
243
242
import java.util.List;
@@ -295,23 +294,57 @@ public class Main {
295
294
public static void main(String[] args) throws IOException {
296
295
try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION); ) {
297
296
// Declare a function to be used in a request.
298
- // More convinient method to simplify this declaration will be coming :)
297
+ // We construct a jsonString that corresponds to the following function
298
+ // declaration.
299
+ // {
300
+ // " name" : " getCurrentWeather" ,
301
+ // " description" : " Get the current weather in a given location" ,
302
+ // " parameters" : {
303
+ // " type" : " OBJECT " ,
304
+ // " properties" : {
305
+ // " location" : {
306
+ // " type" : " STRING " ,
307
+ // " description" : " location"
308
+ // }
309
+ // }
310
+ // }
311
+ // }
312
+ // With JDK 15 and above, you can do
313
+ //
314
+ // String jsonString = " " "
315
+ // {
316
+ // "name": "getCurrentWeather",
317
+ // "description": "Get the current weather in a given location",
318
+ // "parameters": {
319
+ // "type": "OBJECT",
320
+ // "properties": {
321
+ // "location": {
322
+ // "type": "STRING",
323
+ // "description": "location"
324
+ // }
325
+ // }
326
+ // }
327
+ // }
328
+ // """
329
+ String jsonString =
330
+ " {\n "
331
+ + " \" name\" : \" getCurrentWeather\" ,\n "
332
+ + " \" description\" : \" Get the current weather in a given location\" ,\n "
333
+ + " \" parameters\" : {\n "
334
+ + " \" type\" : \" OBJECT\" , \n "
335
+ + " \" properties\" : {\n "
336
+ + " \" location\" : {\n "
337
+ + " \" type\" : \" STRING\" ,\n "
338
+ + " \" description\" : \" location\"\n "
339
+ + " }\n "
340
+ + " }\n "
341
+ + " }\n "
342
+ + " }" ;
299
343
Tool tool =
300
344
Tool . newBuilder()
301
345
.addFunctionDeclarations(
302
- FunctionDeclaration.newBuilder()
303
- .setName(" getCurrentWeather" )
304
- .setDescription(" Get the current weather in a given location" )
305
- .setParameters(
306
- Schema.newBuilder()
307
- .setType(Type.OBJECT)
308
- .putProperties(
309
- " location" ,
310
- Schema.newBuilder()
311
- .setType(Type.STRING)
312
- .setDescription(" location" )
313
- .build())
314
- .addRequired(" location" )))
346
+ FunctionDeclarationMaker . fromJsonString(jsonString)
347
+ )
315
348
.build();
316
349
317
350
// Start a chat session from a model, with the use of the declared
@@ -361,7 +394,8 @@ See the [Vertex AI SDK docs][javadocs] to learn more about how to use this Verte
361
394
To get help, follow the instructions in the [ shared Troubleshooting document] [ troubleshooting ] .
362
395
363
396
## Other Configurations
364
- ### Transport
397
+ ### Vertex-scoped Configurations
398
+ #### Transport
365
399
366
400
Vertex AI uses gRPC and rest for the transport layer. By default, we use gRPC transport. To use rest, passing a ` Transport.REST ` to the ` VertexAI ` constructor as the example below:
367
401
@@ -379,7 +413,10 @@ public class Main {
379
413
private static final String LOCATION = < location> ;
380
414
381
415
public static void main (String [] args ) throws IOException {
382
- try (VertexAI vertexAi = new VertexAI (PROJECT_ID , LOCATION , Transport . REST );) {
416
+ try (VertexAI vertexAi = new VertexAI .Builder ()
417
+ .setProjectId(PROJECT_ID )
418
+ .setLocation(LOCATION )
419
+ .setTransport(Transport . REST );) {
383
420
384
421
GenerativeModel model = new GenerativeModel (" gemini-pro" , vertexAi);
385
422
@@ -390,6 +427,40 @@ public class Main {
390
427
}
391
428
```
392
429
430
+ #### ApiEndpoint
431
+ To use a different API endpoint, one can set it when instantiating ` VertexAI ` .
432
+
433
+ ``` java
434
+ package <your package name>
435
+
436
+ import com.google.cloud.vertexai.VertexAI ;
437
+ import com.google.cloud.vertexai.generativeai.GenerativeModel ;
438
+ import com.google.cloud.vertexai.api.GenerateContentResponse ;
439
+ import java.io.IOException ;
440
+
441
+ public class Main {
442
+ private static final String PROJECT_ID = < your project id> ;
443
+ private static final String LOCATION = < location> ;
444
+
445
+ public static void main (String [] args ) throws IOException {
446
+ try (VertexAI vertexAi = new VertexAI .Builder ()
447
+ .setProjectId(PROJECT_ID )
448
+ .setLocation(LOCATION )
449
+ .setApiEndpoint(< new_endpoint> );) {
450
+
451
+ GenerativeModel model = new GenerativeModel (" gemini-pro" , vertexAi);
452
+
453
+ GenerateContentResponse response = model. generateContent(" How are you?" );
454
+ // Do something with the response
455
+ }
456
+ }
457
+ }
458
+ ```
459
+
460
+ #### Model/Chat-level configurations
461
+
462
+ TODO(jayceeli)
463
+
393
464
## Supported Java Versions
394
465
395
466
Java 8 or above is required for using this client.
0 commit comments