Skip to content

Commit 05275be

Browse files
authored
docs: [vertexai] Update README (#10607)
1 parent 42e806e commit 05275be

File tree

1 file changed

+88
-17
lines changed

1 file changed

+88
-17
lines changed

‎java-vertexai/README.md‎

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ import com.google.cloud.vertexai.generativeai.GenerativeModel;
237237
import com.google.cloud.vertexai.generativeai.ChatSession;
238238
import com.google.cloud.vertexai.generativeai.ResponseStream;
239239
import com.google.cloud.vertexai.api.GenerateContentResponse;
240-
import com.google.cloud.vertexai.api.GenerationConfig;
241240
import java.io.IOException;
242241
import java.util.Arrays;
243242
import java.util.List;
@@ -295,23 +294,57 @@ public class Main {
295294
public static void main(String[] args) throws IOException {
296295
try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION); ) {
297296
// 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+
+ "}";
299343
Tool tool =
300344
Tool.newBuilder()
301345
.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+
)
315348
.build();
316349

317350
// 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
361394
To get help, follow the instructions in the [shared Troubleshooting document][troubleshooting].
362395

363396
## Other Configurations
364-
### Transport
397+
### Vertex-scoped Configurations
398+
#### Transport
365399

366400
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:
367401

@@ -379,7 +413,10 @@ public class Main {
379413
private static final String LOCATION = <location>;
380414

381415
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);) {
383420

384421
GenerativeModel model = new GenerativeModel("gemini-pro", vertexAi);
385422

@@ -390,6 +427,40 @@ public class Main {
390427
}
391428
```
392429

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+
393464
## Supported Java Versions
394465

395466
Java 8 or above is required for using this client.

0 commit comments

Comments
 (0)