Skip to content

Commit 6c1087c

Browse files
authored
docs: [vertexai]Add README section for using system-instruction. (#10835)
1 parent 1e8df46 commit 6c1087c

File tree

1 file changed

+97
-37
lines changed

1 file changed

+97
-37
lines changed

java-vertexai/README.md

Lines changed: 97 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Java idiomatic SDK for [Vertex AI][product-docs].
88
- [Product Documentation][product-docs]
99

1010

11-
## Add Dependency
11+
## Add dependency
1212

1313
If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
1414

@@ -32,7 +32,7 @@ If you are using Maven with [BOM][libraries-bom], add this to your pom.xml file:
3232
</dependency>
3333
```
3434

35-
If you are using Maven without the BOM, add this to your dependencies:
35+
If you're using Maven without the BOM, add the following to your dependencies:
3636

3737
<!-- {x-version-update-start:google-cloud-vertexai:released} -->
3838

@@ -59,35 +59,48 @@ libraryDependencies += "com.google.cloud" % "google-cloud-vertexai" % "1.3.0"
5959

6060
## Authentication
6161

62-
See the [Authentication][authentication] section in the base directory's README.
62+
To learn how to authenticate to the API, see the [Authentication][authentication].
6363

6464
## Authorization
6565

66-
The client application making API calls must be granted [authorization scopes][auth-scopes] required for the desired Vertex AI APIs, and the authenticated principal must have the [IAM role(s)][predefined-iam-roles] required to access GCP resources using the Vertex AI API calls.
66+
When a client application makes a call to the Vertex AI API, the application
67+
must be granted the [authorization scopes][auth-scopes] that are required for
68+
the API. Additionally, the authenticated principal must have the
69+
[IAM role(s)][predefined-iam-roles] that are required to access the Google Cloud
70+
resources being called.
6771

6872
## Getting Started
6973

74+
Follow the instructions in this section to get started using the Vertex AI SDK for Java.
75+
7076
### Prerequisites
7177

72-
You will need a [Google Cloud Platform Console][developer-console] project with the Vertex AI [API enabled][enable-api].
73-
You will need to [enable billing][enable-billing] to use Google Vertex AI.
74-
[Follow these instructions][create-project] to get your project set up. You will also need to set up the local development environment by
75-
[installing the Google Cloud Command Line Interface][cloud-cli] and running the following commands in command line:
76-
`gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`.
78+
To use the Vertex AI SDK for Java, you must have completed the following:
79+
80+
* [Create a Google Cloud project][create-project].
81+
* [Enable the Vertex AI API][enable-api] for your project.
82+
* [Enable billing][enable-billing] for your project.
83+
* [Install the Google Cloud Command Line Interface][cloud-cli] and run the
84+
following commands in command line:
7785

78-
To acquire user credentials to use for [Application Default Credentials][adc], run `gcloud auth application-default login`.
86+
```sh
87+
gcloud auth login &&
88+
gcloud config set project <var>PROJECT_ID</var>
89+
```
7990

80-
### Installation and setup
91+
To acquire user credentials to use for [Application Default Credentials][adc],
92+
run `gcloud auth application-default login`.
8193

82-
You'll need to obtain the `google-cloud-vertexai` library. See the [Add Dependency](#add-dependency) section
83-
to add `google-cloud-vertexai` as a dependency in your code.
94+
### Install and setup the SDK
8495

85-
## About Vertex AI
96+
You must install the `google-cloud-vertexai` library. See the
97+
[Add Dependency](#add-dependency) section
98+
to learn how to add `google-cloud-vertexai` as a dependency in your code.
8699

87-
[Vertex AI][product-docs] is an integrated suite of machine learning tools and services for building and using ML models with AutoML or custom code. It was previously known as AI Platform. Vertex AI offers both novices and experts the best workbench for the entire machine learning development lifecycle. This SDK currently only supports Generative AI service on the Vertex AI platform. To access the full set of services on the Vertex AI, consider using the [`google-cloud-aiplatform` client libraries][aiplatform-client-libraries].
100+
### Use the Vertex AI SDK for Java
88101

89-
### Vertex AI SDK
90-
Vertex AI provides [Generative AI Studio](generative-ai-studio) that supports text generation from multi-modality input via a set of most advanced models from Google. This brings out a wide range of applications.
102+
The following sections show you how to perform common tasks by using the
103+
Vertex AI SDK for Java.
91104

92105
#### Basic Text Generation
93106
Vertex AI SDK allows you to access the service programmatically. The following code snippet is the most basic usage of SDK
@@ -116,8 +129,9 @@ public class Main {
116129
}
117130
```
118131

119-
#### Text Generation with Streaming
120-
To get a streamed output, you can use the `generateContentStream` method
132+
#### Stream generated output
133+
134+
To get a streamed output, you can use the `generateContentStream` method:
121135

122136
```java
123137
package <your package name>
@@ -178,8 +192,10 @@ public class Main {
178192
}
179193
```
180194

181-
#### Text Generation from Multi-modal Input
182-
To generate text based on data of multiple modalities, one needs to make a `Content`, which is made easier by `ContentMaker`:
195+
#### Generate text from multi-modal input
196+
197+
To generate text from a prompt that contains multiple modalities of data, use
198+
`ContentMaker` to make a `Content`:
183199

184200
```java
185201
package <your package name>;
@@ -260,8 +276,10 @@ public class Main {
260276
}
261277
```
262278

263-
#### Using ChatSession for Multi-turn Conversation
264-
Yeah, we know, that isn't the most intuitive and easy way to chat with a model. Therefore we provide a `Chat` class:
279+
#### Use `ChatSession` for multi-turn chat
280+
281+
The Vertex AI SDK for Java provides a `ChatSession` class that lets you easily
282+
chat with the model:
265283

266284
```java
267285
package <your package name>;
@@ -305,19 +323,21 @@ public class Main {
305323
}
306324
```
307325

308-
#### Generation with customized configurations
326+
#### Update configurations
309327

310328
The Vertex AI SDK for Java provides configurations for customizing content
311329
generation. You can configure options like
312-
[GenerationConfig][generationconfig-ref] and [SafetySetting][safetysetting-ref],
313-
or add [Tool][tool-ref] for function calling.
330+
[GenerationConfig][generationconfig-ref], [SafetySetting][safetysetting-ref], and
331+
[system instructions][system-instruction], or add [Tool][tool-ref] for function
332+
calling.
314333

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.
334+
You can choose between two configuration approaches: 1) set configurations
335+
during model instantiation for consistency across all text generations, or 2)
336+
adjust them on a per-request basis for fine-grained control.
318337

319338
##### Model level configurations
320339

340+
Below is an example of configure `GenerationConfig` for a `GenerativeModel`:
321341
```java
322342
package <PACKAGE_NAME>
323343
@@ -355,9 +375,45 @@ public class Main {
355375
}
356376
```
357377

358-
##### Request level configurations
378+
And an example of configuring preambles as a [system instruction][system-instruction].
379+
```java
380+
package <your package name>
359381
360-
Our SDK provides fluent APIs to control request level configurations.
382+
import com.google.cloud.vertexai.VertexAI;
383+
import com.google.cloud.vertexai.generativeai.ContentMaker;
384+
import com.google.cloud.vertexai.generativeai.GenerativeModel;
385+
import com.google.cloud.vertexai.api.GenerateContentResponse;
386+
import java.io.IOException;
387+
388+
public class Main {
389+
private static final String PROJECT_ID = <your project id>;
390+
private static final String LOCATION = <location>;
391+
392+
public static void main(String[] args) throws IOException {
393+
try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION);) {
394+
395+
GenerativeModel model =
396+
new GenerativeModel.Builder()
397+
.setModelName("gemino-pro")
398+
.setVertexAi(vertexAi)
399+
.setSystemInstruction(
400+
ContentMaker.fromString(
401+
"You're a helpful assistant that starts all its answers with: \"COOL\"")
402+
)
403+
.build();
404+
405+
GenerateContentResponse response = model.generateContent("How are you?");
406+
// Do something with the response
407+
}
408+
}
409+
}
410+
```
411+
412+
413+
##### Update request-level configurations
414+
415+
The Vertex AI SDK for Java provides fluent APIs to control request-level
416+
configurations.
361417

362418
```java
363419
package <PACKAGE_NAME>
@@ -450,8 +506,9 @@ public class Main {
450506
```
451507

452508

453-
#### Using ChatSession for Function-calling
454-
In a chat, we can also do function calling.
509+
#### Use ChatSession for function calling
510+
511+
You can perfrom a function call in a `ChatSession` as follows:
455512

456513
```java
457514
package <your package name>;
@@ -606,8 +663,10 @@ public class Main {
606663
}
607664
```
608665
609-
#### ApiEndpoint
610-
To use a different API endpoint, one can set it when instantiating `VertexAI`.
666+
#### Change API endpoints
667+
668+
To use a different API endpoint, specify the endpoint that you want to use when
669+
you instantiate `VertexAI`:
611670
612671
```java
613672
package <your package name>
@@ -688,7 +747,7 @@ This library follows [Semantic Versioning](http://semver.org/).
688747
689748
690749
691-
## Contributing
750+
## Contribute to this library
692751
693752
694753
Contributions to this library are always welcome and highly encouraged.
@@ -755,4 +814,5 @@ Java is a registered trademark of Oracle and/or its affiliates.
755814
[generative-ai-studio]: https://cloud.google.com/generative-ai-studio?hl=en
756815
[generationconfig-ref]: https://cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.api.GenerationConfig.Builder
757816
[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
817+
[tool-ref]: https://cloud.google.com/java/docs/reference/google-cloud-vertexai/latest/com.google.cloud.vertexai.api.Tool.Builder
818+
[system-instruction]: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions

0 commit comments

Comments
 (0)