Skip to content

Commit 8efddc1

Browse files
Zhenyi Qicopybara-github
Zhenyi Qi
authored andcommitted
docs: [vertexai] Add README section about function-calling
PiperOrigin-RevId: 603727103
1 parent f1bca3c commit 8efddc1

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

java-vertexai/README.md

+89-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ to add `google-cloud-vertexai` as a dependency in your code.
8989
### Vertex AI SDK
9090
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.
9191

92+
#### Basic Text Generation
9293
Vertex AI SDK allows you to access the service programmatically. The following code snippet is the most basic usage of SDK
9394

9495
```java
@@ -115,6 +116,7 @@ public class Main {
115116
}
116117
```
117118

119+
#### Text Generation with Streaming
118120
To get a streamed output, you can use the `generateContentStream` method
119121

120122
```java
@@ -142,6 +144,7 @@ public class Main {
142144
}
143145
```
144146

147+
#### Text Generation from Multi-modal Input
145148
To generate text based on data of multiple modalities, one needs to make a `Content`, which is made easier by `ContentMaker`:
146149

147150
```java
@@ -176,6 +179,7 @@ public class Main {
176179
}
177180
```
178181

182+
#### Role Change for Multi-turn Conversation
179183
For a multi-turn conversation, one needs to make a `Content` list to represent the whole conversation between two roles: "user" and "model".
180184

181185
```java
@@ -222,6 +226,7 @@ public class Main {
222226
}
223227
```
224228

229+
#### Using ChatSession for Multi-turn Conversation
225230
Yeah, we know, that isn't the most intuitive and easy way to chat with a model. Therefore we provide a `Chat` class:
226231
227232
```java
@@ -267,13 +272,96 @@ public class Main {
267272
}
268273
```
269274
275+
#### Using ChatSession for Function-calling
276+
In a chat, we can also do function calling.
277+
278+
```java
279+
package <your package name>;
280+
import com.google.cloud.vertexai.VertexAI;
281+
import com.google.cloud.vertexai.generativeai.preview.ChatSession;
282+
import com.google.cloud.vertexai.generativeai.preview.GenerativeModel;
283+
import com.google.cloud.vertexai.generativeai.preview.ResponseHandler;
284+
import com.google.cloud.vertexai.generativeai.preview.ResponseStream;
285+
import com.google.cloud.vertexai.api.Content;
286+
import com.google.cloud.vertexai.api.GenerateContentResponse;
287+
import java.io.IOException;
288+
289+
public class Main {
290+
private static final String PROJECT_ID = "<your project>";
291+
private static final String LOCATION = "<location>";
292+
private static final String MODEL_NAME = "gemini-pro";
293+
private static final String TEXT = "What's the weather in Vancouver?";
294+
295+
public static void main(String[] args) throws IOException {
296+
try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION); ) {
297+
// Declare a function to be used in a request.
298+
// More convinient method to simplify this declaration will be coming :)
299+
Tool tool =
300+
Tool.newBuilder()
301+
.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")))
315+
.build();
316+
317+
// Start a chat session from a model, with the use of the declared
318+
// function.
319+
GenerativeModel model =
320+
GenerativeModel.newBuilder()
321+
.setModelName(MODEL_NAME)
322+
.setVertexAi(vertexAi)
323+
.setTools(Arrays.asList(tool))
324+
.build();
325+
ChatSession chat = model.startChat();
326+
327+
System.out.println(String.format("Ask the question: %s", TEXT));
328+
GenerateContentResponse response = chat.sendMessage(TEXT);
329+
330+
// The model will most likely return a function call to the declared
331+
// function `getCurrentWeather` with "Vancouver" as the value for the
332+
// argument `location`.
333+
System.out.println("\nPrint response: ");
334+
System.out.println(ResponseHandler.getContent(response));
335+
System.out.println("\n");
336+
337+
// Provide an answer to the model so that it knows what the result of a
338+
// "function call" is.
339+
Content content =
340+
ContentMaker.fromMultiModalData(
341+
PartMaker.fromFunctionResponse(
342+
"getCurrentWeather", Collections.singletonMap("currentWeather", "snowing")));
343+
System.out.println("Provide the function response: ");
344+
System.out.println(content);
345+
System.out.println("\n");
346+
response = chat.sendMessage(content);
347+
348+
// See what the model replies now
349+
System.out.println("\nPrint response: ");
350+
System.out.println(ResponseHandler.getText(response));
351+
System.out.println("\n");
352+
}
353+
}
354+
}
355+
```
356+
270357
See the [Vertex AI SDK docs][javadocs] to learn more about how to use this Vertex AI SDK in more advanced ways.
271358

272359
## Troubleshooting
273360

274361
To get help, follow the instructions in the [shared Troubleshooting document][troubleshooting].
275362

276-
## Transport
363+
## Other Configurations
364+
### Transport
277365

278366
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:
279367

0 commit comments

Comments
 (0)