Skip to content

feat(genai): Add new GenAI SDK samples (WIP) #10095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package snippets;

// [START googlegenaisdk_counttoken_with_txt]

import com.google.genai.Client;
import com.google.genai.types.Content;
import com.google.genai.types.CountTokensResponse;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.Part;
import java.util.List;
import java.util.Optional;

public class CountTokensWithText {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String modelId = "gemini-2.0-flash";
countTokens(modelId);
}

public static Optional<Integer> countTokens(String modelId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (Client client = Client.builder()
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
.build()) {

Content content = Content.builder()
.parts(List.of(
Part.fromText("What's the highest mountain in Africa?")))
.build();

CountTokensResponse response =
client.models.countTokens(modelId, List.of(content), null);

System.out.print(response);
// Example response:
// CountTokensResponse{totalTokens=Optional[9], cachedContentTokenCount=Optional.empty}
return response.totalTokens();
}
}
}
// [END googlegenaisdk_counttoken_with_txt]



Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package snippets;

// [START googlegenaisdk_counttoken_with_txt_vid]

import com.google.genai.Client;
import com.google.genai.types.Content;
import com.google.genai.types.CountTokensResponse;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.Part;
import java.util.List;
import java.util.Optional;

public class CountTokensWithTextAndVideo {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String modelId = "gemini-2.0-flash";
countTokens(modelId);
}

public static Optional<Integer> countTokens(String modelId) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (Client client = Client.builder()
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
.build()) {

Content content = Content.builder()
.parts(List.of(
Part.fromText("Provide a description of this video"),
Part.fromUri("gs://cloud-samples-data/generative-ai/video/pixel8.mp4", "video/mp4")))
.build();

CountTokensResponse response =
client.models.countTokens(modelId, List.of(content),
null);

System.out.print(response);
// Example response:
// CountTokensResponse{totalTokens=Optional[16251], cachedContentTokenCount=Optional.empty}
return response.totalTokens();
}
}
}
// [END googlegenaisdk_counttoken_with_txt_vid]


Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package snippets;

// [START googlegenaisdk_textgen_with_txt_stream]

import com.google.genai.Client;
import com.google.genai.ResponseStream;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.HttpOptions;

public class GenerateContentStream {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String contents = "Why is the sky blue?";
String modelId = "gemini-2.0-flash";
generateContent(modelId, contents);
}

public static String generateContent(String modelId, String contents) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (Client client = Client.builder()
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
.build()) {

StringBuilder responseTextBuilder = new StringBuilder();
ResponseStream<GenerateContentResponse> responseStream =
client.models.generateContentStream(modelId, contents, null);

for (GenerateContentResponse chunk : responseStream) {
System.out.print(chunk.text());
responseTextBuilder.append(chunk.text());
}
// Example response:
// The sky appears blue due to a phenomenon called **Rayleigh scattering**. Here's
// a breakdown of why:
// ...
return responseTextBuilder.toString();
}
}
}
// [END googlegenaisdk_textgen_with_txt_stream]
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package snippets;

// [START googlegenaisdk_ctrlgen_with_enum_schema]

import com.google.genai.Client;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.Schema;
import com.google.genai.types.Type;
import java.util.List;

public class GenerateContentWithEnumSchema {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String contents = "What type of instrument is an oboe?";
String modelId = "gemini-2.0-flash";
generateContent(modelId, contents);
}

public static String generateContent(String modelId, String contents) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (Client client = Client.builder()
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
.build()) {

// Define the response schema with an enum.
Schema responseSchema =
Schema.builder()
.type(Type.Known.STRING)
.enum_(
List.of("Percussion", "String", "Woodwind", "Brass", "Keyboard"))
.build();

GenerateContentConfig config = GenerateContentConfig.builder()
.responseMimeType("text/x.enum")
.responseSchema(responseSchema)
.build();

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

System.out.print(response.text());
// Example response:
// Woodwind
return response.text();
}
}
}
// [END googlegenaisdk_ctrlgen_with_enum_schema]
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package snippets;

// [START googlegenaisdk_tools_func_desc_with_txt]

import com.google.genai.Client;
import com.google.genai.types.FunctionDeclaration;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.Schema;
import com.google.genai.types.Tool;
import com.google.genai.types.Type;
import java.util.List;
import java.util.Map;

public class GenerateContentWithFunctionDescription {

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

generateContent(modelId, contents);
}

public static String generateContent(String modelId, String contents) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (Client client = Client.builder()
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
.build()) {

FunctionDeclaration getAlbumSales = FunctionDeclaration.builder()
.name("get_album_sales")
.description("Gets the number of albums sold")
// Function parameters are specified in schema format
.parameters(Schema.builder()
.type(Type.Known.OBJECT)
.properties(Map.of(
"albums", Schema.builder()
.type(Type.Known.ARRAY)
.description("List of albums")
.items(Schema.builder()
.description("Album and its sales")
.type(Type.Known.OBJECT)
.properties(Map.of(
"album_name", Schema.builder()
.type(Type.Known.STRING)
.description("Name of the music album")
.build(),
"copies_sold", Schema.builder()
.type(Type.Known.INTEGER)
.description("Number of copies sold")
.build()
))
.build()) // End items schema for albums
.build() // End "albums" property schema
))
.build()) // End parameters schema
.build(); // End function declaration

Tool salesTool = Tool.builder()
.functionDeclarations(List.of(getAlbumSales))
.build();

GenerateContentConfig config = GenerateContentConfig.builder()
.tools(List.of(salesTool))
.temperature(0.0f)
.build();

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

// response.functionCalls() returns an Optional<List<FunctionCall>>.
// We get the list, then get the first FunctionCall from the list.
System.out.println(response.functionCalls().get(0));
return response.functionCalls().toString();
Comment on lines +100 to +101

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are a couple of concerns here:

  1. Unsafe Optional Access: response.functionCalls() returns an Optional<List<FunctionCall>>. Calling .get() without isPresent() check can lead to NoSuchElementException. Furthermore, if the list is present but empty, get(0) on the list will cause an IndexOutOfBoundsException.
  2. Meaningfulness of Return Value: response.functionCalls().toString() returns the string representation of the Optional object itself (e.g., "Optional[...]"), not the content of the function call. This might not be the most useful value if this method's result is to be used programmatically.

Consider checking for presence and emptiness before accessing elements, and returning a more direct representation of the function call data.

      Optional<List<FunctionCall>> functionCallsOptional = response.functionCalls();
      if (functionCallsOptional.isPresent() && !functionCallsOptional.get().isEmpty()) {
        FunctionCall firstCall = functionCallsOptional.get().get(0);
        System.out.println(firstCall);
        return firstCall.toString(); // Or a more specific part of the FunctionCall if needed
      } else {
        System.out.println("No function calls returned by the model or the list was empty.");
        return "No function calls returned"; // Indicate absence of function calls
      }

// Example response:
// FunctionCall{id=Optional.empty, args=Optional[{albums=[{copies_sold=350000, album_name=Echoes of the Night},
// {copies_sold=120000, album_name=Reckless Hearts}, {copies_sold=75000, album_name=Whispers of Dawn},
// {album_name=Street Symphony, copies_sold=100000}]}], name=Optional[get_album_sales]}
}
}
}
// [END googlegenaisdk_tools_func_desc_with_txt]

Loading