Skip to content

Commit 2925c2d

Browse files
committed
feat: add ToolFunctionCallingConfigWithText sample and update ToolFunctionDescriptionWithText test case
1 parent 993f138 commit 2925c2d

3 files changed

Lines changed: 162 additions & 52 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2026 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 genai.tools;
18+
19+
// [START googlegenaisdk_tools_func_call_config_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.FunctionCallingConfig;
23+
import com.google.genai.types.FunctionCallingConfigMode;
24+
import com.google.genai.types.FunctionDeclaration;
25+
import com.google.genai.types.GenerateContentConfig;
26+
import com.google.genai.types.GenerateContentResponse;
27+
import com.google.genai.types.HttpOptions;
28+
import com.google.genai.types.Schema;
29+
import com.google.genai.types.Tool;
30+
import com.google.genai.types.ToolConfig;
31+
import com.google.genai.types.Type;
32+
import java.util.List;
33+
import java.util.Map;
34+
35+
public class ToolFunctionCallingConfigWithText {
36+
37+
public static void main(String[] args) {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String modelId = "gemini-2.5-flash";
40+
41+
generateContent(modelId);
42+
}
43+
44+
// Generates content using function calling config to force a specific function call
45+
public static String generateContent(String modelId) {
46+
String contents =
47+
"At Stellar Sounds, a music label, 2024 was a rollercoaster. \"Echoes of the Night,\""
48+
+ " a debut synth-pop album, \n surprisingly sold 350,000 copies, while veteran"
49+
+ " rock band \"Crimson Tide's\" latest, \"Reckless Hearts,\" \n lagged at"
50+
+ " 120,000. Their up-and-coming indie artist, \"Luna Bloom's\" EP, \"Whispers "
51+
+ "of Dawn,\" \n secured 75,000 sales. The biggest disappointment was the "
52+
+ "highly-anticipated rap album \"Street Symphony\" \n only reaching 100,000"
53+
+ " units. Overall, Stellar Sounds moved over 645,000 units this year, revealing"
54+
+ " unexpected \n trends in music consumption.";
55+
56+
return generateContent(modelId, contents);
57+
}
58+
59+
// Generates content using function calling config to force a specific function call
60+
public static String generateContent(String modelId, String contents) {
61+
// Initialize client that will be used to send requests. This client only needs to be created
62+
// once, and can be reused for multiple requests.
63+
try (Client client =
64+
Client.builder()
65+
.location("global")
66+
.vertexAI(true)
67+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
68+
.build()) {
69+
70+
FunctionDeclaration getAlbumSales =
71+
FunctionDeclaration.builder()
72+
.name("get_album_sales")
73+
.description("Gets the number of albums sold")
74+
// Function parameters are specified in schema format
75+
.parameters(
76+
Schema.builder()
77+
.type(Type.Known.OBJECT)
78+
.properties(
79+
Map.of(
80+
"albums",
81+
Schema.builder()
82+
.type(Type.Known.ARRAY)
83+
.description("List of albums")
84+
.items(
85+
Schema.builder()
86+
.description("Album and its sales")
87+
.type(Type.Known.OBJECT)
88+
.properties(
89+
Map.of(
90+
"album_name",
91+
Schema.builder()
92+
.type(Type.Known.STRING)
93+
.description("Name of the music album")
94+
.build(),
95+
"copies_sold",
96+
Schema.builder()
97+
.type(Type.Known.INTEGER)
98+
.description("Number of copies sold")
99+
.build()))
100+
.build()) // End items schema for albums
101+
.build() // End "albums" property schema
102+
))
103+
.build()) // End parameters schema
104+
.build(); // End function declaration
105+
106+
Tool salesTool = Tool.builder().functionDeclarations(getAlbumSales).build();
107+
108+
ToolConfig toolConfig =
109+
ToolConfig.builder()
110+
.functionCallingConfig(
111+
FunctionCallingConfig.builder()
112+
.mode(FunctionCallingConfigMode.Known.ANY)
113+
.allowedFunctionNames(List.of("get_album_sales"))
114+
.build())
115+
.build();
116+
117+
GenerateContentConfig config =
118+
GenerateContentConfig.builder()
119+
.tools(salesTool)
120+
.toolConfig(toolConfig)
121+
.temperature(0.0f)
122+
.build();
123+
124+
GenerateContentResponse response = client.models.generateContent(modelId, contents, config);
125+
126+
// response.functionCalls() returns an ImmutableList<FunctionCall>.
127+
System.out.println(response.functionCalls().get(0));
128+
129+
return response.functionCalls().toString();
130+
// Example response:
131+
// [FunctionCall{args=Optional[{albums=[{album_name=Echoes of the Night, copies_sold=350000},
132+
// {album_name=Reckless Hearts, copies_sold=120000}, {album_name=Whispers of Dawn, copies_sold=75000},
133+
// {album_name=Street Symphony, copies_sold=100000}]}], name=Optional[get_album_sales]}]
134+
}
135+
}
136+
}
137+
// [END googlegenaisdk_tools_func_call_config_with_txt]

genai/snippets/src/main/java/genai/tools/ToolFunctionDescriptionWithText.java

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,15 @@
2626
import com.google.genai.types.Schema;
2727
import com.google.genai.types.Tool;
2828
import com.google.genai.types.Type;
29+
import java.util.List;
2930
import java.util.Map;
3031

3132
public class ToolFunctionDescriptionWithText {
3233

3334
public static void main(String[] args) {
3435
// TODO(developer): Replace these variables before running the sample.
3536
String modelId = "gemini-2.5-flash";
36-
String contents =
37-
"At Stellar Sounds, a music label, 2024 was a rollercoaster. \"Echoes of the Night,\""
38-
+ " a debut synth-pop album, \n surprisingly sold 350,000 copies, while veteran"
39-
+ " rock band \"Crimson Tide's\" latest, \"Reckless Hearts,\" \n lagged at"
40-
+ " 120,000. Their up-and-coming indie artist, \"Luna Bloom's\" EP, \"Whispers "
41-
+ "of Dawn,\" \n secured 75,000 sales. The biggest disappointment was the "
42-
+ "highly-anticipated rap album \"Street Symphony\" \n only reaching 100,000"
43-
+ " units. Overall, Stellar Sounds moved over 645,000 units this year, revealing"
44-
+ " unexpected \n trends in music consumption.";
37+
String contents = "What is the weather like in Boston?";
4538

4639
generateContent(modelId, contents);
4740
}
@@ -58,59 +51,39 @@ public static String generateContent(String modelId, String contents) {
5851
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
5952
.build()) {
6053

61-
FunctionDeclaration getAlbumSales =
54+
FunctionDeclaration getCurrentWeather =
6255
FunctionDeclaration.builder()
63-
.name("get_album_sales")
64-
.description("Gets the number of albums sold")
56+
.name("get_current_weather")
57+
.description("Get the current weather in a given location")
6558
// Function parameters are specified in schema format
6659
.parameters(
6760
Schema.builder()
6861
.type(Type.Known.OBJECT)
6962
.properties(
7063
Map.of(
71-
"albums",
64+
"location",
7265
Schema.builder()
73-
.type(Type.Known.ARRAY)
74-
.description("List of albums")
75-
.items(
76-
Schema.builder()
77-
.description("Album and its sales")
78-
.type(Type.Known.OBJECT)
79-
.properties(
80-
Map.of(
81-
"album_name",
82-
Schema.builder()
83-
.type(Type.Known.STRING)
84-
.description("Name of the music album")
85-
.build(),
86-
"copies_sold",
87-
Schema.builder()
88-
.type(Type.Known.INTEGER)
89-
.description("Number of copies sold")
90-
.build()))
91-
.build()) // End items schema for albums
92-
.build() // End "albums" property schema
93-
))
66+
.type(Type.Known.STRING)
67+
.description(
68+
"The city name of the location for which to get the weather.")
69+
.build()))
70+
.required(List.of("location"))
9471
.build()) // End parameters schema
9572
.build(); // End function declaration
9673

97-
Tool salesTool = Tool.builder().functionDeclarations(getAlbumSales).build();
74+
Tool weatherTool = Tool.builder().functionDeclarations(getCurrentWeather).build();
9875

9976
GenerateContentConfig config =
100-
GenerateContentConfig.builder().tools(salesTool).temperature(0.0f).build();
77+
GenerateContentConfig.builder().tools(weatherTool).temperature(0.0f).build();
10178

10279
GenerateContentResponse response = client.models.generateContent(modelId, contents, config);
10380

104-
// response.functionCalls() returns an Immutable<FunctionCall>.
81+
// response.functionCalls() returns an ImmutableList<FunctionCall>.
10582
System.out.println(response.functionCalls().get(0));
10683

10784
return response.functionCalls().toString();
10885
// Example response:
109-
// FunctionCall{id=Optional.empty, args=Optional[{albums=[{copies_sold=350000,
110-
// album_name=Echoes of the Night},
111-
// {copies_sold=120000, album_name=Reckless Hearts}, {copies_sold=75000, album_name=Whispers
112-
// of Dawn},
113-
// {album_name=Street Symphony, copies_sold=100000}]}], name=Optional[get_album_sales]}
86+
// [FunctionCall{args=Optional[{location=Boston, MA}], name=Optional[get_current_weather]}]
11487
}
11588
}
11689
}

genai/snippets/src/test/java/genai/tools/ToolsIT.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ public void tearDown() {
7878

7979
@Test
8080
public void testGenerateContentWithFunctionDescription() {
81-
82-
String prompt =
83-
"At Stellar Sounds, a music label, 2024 was a rollercoaster. \"Echoes of the Night,\""
84-
+ " a debut synth-pop album, \n surprisingly sold 350,000 copies, while veteran"
85-
+ " rock band \"Crimson Tide's\" latest, \"Reckless Hearts,\" \n lagged at"
86-
+ " 120,000. Their up-and-coming indie artist, \"Luna Bloom's\" EP, \"Whispers "
87-
+ "of Dawn,\" \n secured 75,000 sales. The biggest disappointment was the "
88-
+ "highly-anticipated rap album \"Street Symphony\" \n only reaching 100,000"
89-
+ " units. Overall, Stellar Sounds moved over 645,000 units this year, revealing"
90-
+ " unexpected \n trends in music consumption.";
81+
String prompt = "What is the weather like in Boston?";
9182

9283
String response = ToolFunctionDescriptionWithText.generateContent(GEMINI_FLASH, prompt);
9384

85+
assertThat(response).isNotEmpty();
86+
assertThat(response).contains("get_current_weather");
87+
assertThat(response).contains("location=Boston");
88+
}
89+
90+
@Test
91+
public void testGenerateContentWithFunctionCallingConfig() {
92+
String response = ToolFunctionCallingConfigWithText.generateContent(GEMINI_FLASH);
93+
9494
assertThat(response).isNotEmpty();
9595
assertThat(response).contains("get_album_sales");
9696
assertThat(response).contains("copies_sold=350000");

0 commit comments

Comments
 (0)