|
| 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] |
0 commit comments