Skip to content
Open
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,65 @@
/*
* 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
*
* https://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 com.google.genai.examples;

import com.google.genai.types.interactions.content.FileSearchCallContent;

/**
* Example demonstrating the usage of FileSearchCallContent.
*
* <p>FileSearchCallContent represents a file search operation call in the Interactions API.
* Unlike other tool calls, FileSearchCallContent contains only the type and call ID.
* All search configuration (stores, top_k, filters) is defined in the FileSearch.
*/
public class FileSearchCallContentExample {

public static void main(String[] args) {
// Example 1: Create FileSearchCallContent using builder
FileSearchCallContent content1 =
FileSearchCallContent.builder()
.id("file-search-call-123")
.build();

System.out.println("FileSearchCallContent (using builder) JSON:");
System.out.println(content1.toJson());
System.out.println();

// Example 2: Create FileSearchCallContent using convenience method
FileSearchCallContent content2 =
FileSearchCallContent.of("call-456");

System.out.println("FileSearchCallContent (using of() method) JSON:");
System.out.println(content2.toJson());
System.out.println();

// Example 3: Deserialize from JSON
String json = content2.toJson();
FileSearchCallContent deserialized = FileSearchCallContent.fromJson(json);

System.out.println("Deserialized FileSearchCallContent:");
System.out.println(" ID: " + deserialized.id());
System.out.println();

// Example 4: Create FileSearchCallContent without ID (optional)
FileSearchCallContent content3 =
FileSearchCallContent.builder()
.build();

System.out.println("FileSearchCallContent (no ID) JSON:");
System.out.println(content3.toJson());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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
*
* https://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 com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.types.interactions.CreateInteractionConfig;
import com.google.genai.types.interactions.Interaction;
import com.google.genai.types.interactions.content.Content;
import com.google.genai.types.interactions.content.TextContent;
import java.util.concurrent.CompletableFuture;

/**
* Example: Async Create with the Interactions API
*
* <p>Demonstrates async operations using CompletableFuture for non-blocking requests.
*
* <p>To run this example:
* <ol>
* <li>Set the GOOGLE_API_KEY environment variable: {@code export GOOGLE_API_KEY=YOUR_API_KEY}
* <li>Compile the examples: {@code mvn clean compile}
* <li>Run: {@code mvn exec:java -Dexec.mainClass="com.google.genai.examples.InteractionsAsyncCreate"}
* </ol>
*
* <p><b>Note:</b> The Interactions API is currently in beta.
*/
public final class InteractionsAsyncCreate {

public static void main(String[] args) {
Client client = new Client();

System.out.println("=== Interactions API: Async Create Example ===\n");

try {
// ===== Basic Async Create =====
System.out.println("--- Async Create with Callback ---\n");

CreateInteractionConfig config =
CreateInteractionConfig.builder()
.model("gemini-2.5-flash")
.input("What is the capital of France?")
.build();

System.out.println("=== REQUEST ===");
System.out.println(config.toJson());
System.out.println();

System.out.println("Sending async request...\n");

CompletableFuture<Interaction> future = client.async.interactions.create(config);

// Wait for completion and print response
Interaction interaction = future.join();

System.out.println("=== RESPONSE ===");
System.out.println(interaction.toJson());
System.out.println();

printResults(interaction);

// ===== Multiple Parallel Creates =====
System.out.println("\n--- Multiple Parallel Creates ---\n");

CompletableFuture<Interaction> p1 =
client.async.interactions.create(
CreateInteractionConfig.builder()
.model("gemini-2.5-flash")
.input("What is machine learning?")
.build());

CompletableFuture<Interaction> p2 =
client.async.interactions.create(
CreateInteractionConfig.builder()
.model("gemini-2.5-flash")
.input("What is quantum computing?")
.build());

p1.thenAccept(i -> System.out.println("Request 1 completed: " + i.id()));
p2.thenAccept(i -> System.out.println("Request 2 completed: " + i.id()));

CompletableFuture.allOf(p1, p2)
.thenRun(() -> System.out.println("\nAll parallel requests completed!"));

CompletableFuture.allOf(p1, p2).join();

System.out.println("\n=== Example completed ===");

} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}

private static void printResults(Interaction interaction) {
System.out.println("Results:");
System.out.println(" Interaction ID: " + interaction.id());
System.out.println(" Status: " + interaction.status());

if (interaction.outputs().isPresent() && !interaction.outputs().get().isEmpty()) {
for (Content output : interaction.outputs().get()) {
if (output instanceof TextContent) {
System.out.println(" Text: " + ((TextContent) output).text().orElse("(empty)"));
} else {
System.out.println(" " + output.getClass().getSimpleName());
}
}
}
}

private InteractionsAsyncCreate() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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
*
* https://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 com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.types.interactions.CreateInteractionConfig;
import com.google.genai.types.interactions.Interaction;
import com.google.genai.types.interactions.content.AudioContent;
import com.google.genai.types.interactions.content.Content;
import com.google.genai.types.interactions.content.TextContent;

/**
* Example demonstrating audio transcription and analysis using AudioContent with the Interactions
* API.
*
* <p>This example shows:
* <ul>
* <li>Using AudioContent.fromUri() to analyze audio from a URL
* <li>Using AudioContent.fromData() to analyze base64-encoded audio
* </ul>
*
* <p>To run this example:
* <ol>
* <li>Set the GOOGLE_API_KEY environment variable: {@code export GOOGLE_API_KEY=YOUR_API_KEY}
* <li>Compile the examples: {@code mvn clean compile}
* <li>Run: {@code mvn exec:java -Dexec.mainClass="com.google.genai.examples.InteractionsAudioContent"}
* </ol>
*
* <p><b>Note:</b> The Interactions API is currently in beta.
*/
public final class InteractionsAudioContent {

public static void main(String[] args) {
Client client = new Client();

System.out.println("=== Interactions API: Audio Content Example ===\n");

try {
// ===== PART 1: Audio from URI =====
System.out.println("--- PART 1: Audio from URI ---\n");

String audioUri = "https://storage.googleapis.com/cloud-samples-data/speech/brooklyn_bridge.mp3";
AudioContent audioContent = AudioContent.fromUri(audioUri, "audio/mp3");
TextContent textPrompt = TextContent.builder()
.text("Transcribe this audio.")
.build();

CreateInteractionConfig config =
CreateInteractionConfig.builder()
.model("gemini-2.5-flash")
.inputFromContents(textPrompt, audioContent)
.build();

System.out.println("=== REQUEST ===");
System.out.println(config.toJson());
System.out.println();

Interaction response = client.interactions.create(config);

System.out.println("=== RESPONSE ===");
System.out.println(response.toJson());
System.out.println();

printResults(response);

// ===== PART 2: Audio from Inline Data (base64) =====
System.out.println("\n--- PART 2: Audio from Inline Data (Base64) ---\n");

// Small WAV file header encoded as base64
String base64Data = "UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAAB9AAACABAAZGF0YQAAAAA=";
AudioContent audioFromData = AudioContent.fromData(base64Data, "audio/wav");
TextContent textPrompt2 = TextContent.builder()
.text("What do you hear in this audio?")
.build();

CreateInteractionConfig config2 =
CreateInteractionConfig.builder()
.model("gemini-2.5-flash")
.inputFromContents(textPrompt2, audioFromData)
.build();

System.out.println("=== REQUEST ===");
System.out.println(config2.toJson());
System.out.println();

Interaction response2 = client.interactions.create(config2);

System.out.println("=== RESPONSE ===");
System.out.println(response2.toJson());
System.out.println();

printResults(response2);

System.out.println("\n=== Example completed ===");

} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}

private static void printResults(Interaction interaction) {
System.out.println("Results:");
System.out.println(" Interaction ID: " + interaction.id());
System.out.println(" Status: " + interaction.status());

if (interaction.outputs().isPresent() && !interaction.outputs().get().isEmpty()) {
for (Content output : interaction.outputs().get()) {
if (output instanceof TextContent) {
System.out.println(" Text: " + ((TextContent) output).text().orElse("(empty)"));
} else {
System.out.println(" " + output.getClass().getSimpleName());
}
}
}
}

private InteractionsAudioContent() {}
}
Loading