10000 feat(gemini sdk): add generativeaionvertexai complex function callin… · suztomo/java-docs-samples@140f01e · GitHub
[go: up one dir, main page]

Skip to content

Commit 140f01e

Browse files
feat(gemini sdk): add generativeaionvertexai complex function calling (GoogleCloudPlatform#9436)
* Implemented generativeaionvertexai_function_calling_advanced sample, created test * Fixed response order * Fixed test * Fixed request and response * Fixed implementation. Added JSON as external response * Fixed END tag * Changed tag to be consistent * Changed tag and naming to be consistent * Fixed indentation --------- Co-authored-by: Sita Lakshmi Sangameswaran <sita1996@gmail.com>
1 parent a6f99be commit 140f01e

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package vertexai.gemini;
18+
19+
//[START generativeaionvertexai_gemini_complex_function_calling]
20+
21+
import com.google.cloud.vertexai.VertexAI;
22+
import com.google.cloud.vertexai.api.Content;
23+
import com.google.cloud.vertexai.api.FunctionDeclaration;
24+
import com.google.cloud.vertexai.api.GenerateContentResponse;
25+
import com.google.cloud.vertexai.api.Schema;
26+
import com.google.cloud.vertexai.api.Tool;
27+
import com.google.cloud.vertexai.api.Type;
28+
import com.google.cloud.vertexai.generativeai.ChatSession;
29+
import com.google.cloud.vertexai.generativeai.ContentMaker;
30+
import com.google.cloud.vertexai.generativeai.GenerativeModel;
31+
import com.google.cloud.vertexai.generativeai.PartMaker;
32+
import com.google.cloud.vertexai.generativeai.ResponseHandler;
33+
import java.io.IOException;
34+
import java.util.Collections;
35+
import java.util.List;
36+
37+
public class ComplexFunctionCalling {
38+
public static void main(String[] args) throws IOException {
39+
// TODO(developer): Replace these variables before running the sample.
40+
String projectId = "your-google-cloud-project-id";
41+
String location = "us-central1";
42+
String modelName = "gemini-1.5-flash-001";
43+
44+
String promptText = "What is the weather like in Boston?";
45+
46+
complexFunctionCalling(projectId, location, modelName, promptText);
47+
}
48+
49+
// A request involving the interaction with an external tool
50+
public static String complexFunctionCalling(String projectId, String location,
51+
String modelName, String promptText)
52+
throws IOException {
53+
//In this example, we'll use synthetic data to simulate a response payload from an external API
54+
String jsonString = "{ \"location\": \"Boston, MA\", \"temperature\": 38, \"description\": "
55+
+ "\"Partly Cloudy\", \"icon\": \"partly-cloudy\", \"humidity\": 65, \"wind\": "
56+
+ "{ \"speed\": 10, \"direction\": \"NW\" } }";
57+
58+
// Initialize client that will be used to send requests.
59+
// This client only needs to be created once, and can be reused for multiple requests.
60+
try (VertexAI vertexAI = new VertexAI(projectId, location)) {
61+
62+
FunctionDeclaration functionDeclaration = FunctionDeclaration.newBuilder()
63+
.setName("getCurrentWeather")
64+
.setDescription("Get the current weather in a given location")
65+
.setParameters(
66+
Schema.newBuilder()
67+
.setType(Type.OBJECT)
68+
.putProperties("location", Schema.newBuilder()
69+
.setType(Type.STRING)
70+
.setDescription("location")
71+
.build()
72+
)
73+
.addRequired("location")
74+
.build()
75+
)
76+
.build();
77+
78+
System.out.println("Function declaration:");
79+
System.out.println(functionDeclaration);
80+
81+
// Add the function to a "tool"
82+
Tool tool = Tool.newBuilder()
83+
.addFunctionDeclarations(functionDeclaration)
84+
.build();
85+
86+
// Start a chat session from a model, with the use of the declared function.
87+
GenerativeModel model = new GenerativeModel(modelName, vertexAI)
88+
.withTools(List.of(tool));
89+
ChatSession chat = model.startChat();
90+
91+
System.out.printf("Ask the question: %s%n", promptText);
92+
GenerateContentResponse response = chat.sendMessage(promptText);
93+
94+
// The model will most likely return a function call to the declared
95+
// function `getCurrentWeather` with "Boston" as the value for the
96+
// argument `location`.
97+
System.out.println("\nPrint response: ");
98+
System.out.println(ResponseHandler.getContent(response));
99+
100+
// Provide an answer to the model so that it knows what the result
101+
// of a "function call" is.
102+
Content content =
103+
ContentMaker.fromMultiModalData(
104+
PartMaker.fromFunctionResponse(
105+
"getCurrentWeather",
106+
Collections.singletonMap("currentWeather", jsonString)));
107+
System.out.println("Provide the function response: ");
108+
System.out.println(content);
109+
response = chat.sendMessage(content);
110+
111+
// See what the model replies now
112+
System.out.println("Print response: ");
113+
String finalAnswer = ResponseHandler.getText(response);
114+
System.out.println(finalAnswer);
115+
116+
return finalAnswer;
117+
}
118+
}
119+
}
120+
//[END generativeaionvertexai_gemini_complex_function_calling]

vertexai/snippets/src/test/java/vertexai/gemini/SnippetsIT.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ public void testFunctionCalling() throws Exception {
237237
assertThat(answer).ignoringCase().contains("sunny");
238238
}
239239

240+
@Test
241+
public void testComplexFunctionCalling() throws Exception {
242+
String textPrompt = "What is the weather like in Boston?";
243+
244+
String answer =
245+
ComplexFunctionCalling.complexFunctionCalling(
246+
PROJECT_ID, LOCATION, GEMINI_FLASH, textPrompt);
247+
assertThat(answer).ignoringCase().contains("Boston");
248+
assertThat(answer).ignoringCase().contains("Partly Cloudy");
249+
assertThat(answer).ignoringCase().contains("temperature");
250+
assertThat(answer).ignoringCase().contains("65");
251+
}
252+
240253
@Test
241254
public void testAutomaticFunctionCalling() throws Exception {
242255
String textPrompt = "What's the weather in Paris?";

0 commit comments

Comments
 (0)
0