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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file added examples/resources/sample_audio.mp3
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2026 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.
*/

/**
* Usage:
*
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
*
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
*
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
*
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
*
* <p>export GOOGLE_GENAI_USE_VERTEXAI=true
*
* <p>1b. If you are using Gemini Developer API, set an API key environment variable. You can find a
* list of available API keys here: https://aistudio.google.com/app/apikey
*
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
*
* <p>2. Compile the java package and run the sample code.
*
* <p>mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.InteractionBasic"
*/
package com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.ModelOutputStep;
import com.google.genai.gaos.models.interactions.Step;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

/** An example of using the Unified Gen AI Java SDK to create a basic interaction. */
public final class InteractionBasic {
public static void main(String[] args) {
Client client = new Client();

if (client.vertexAI()) {
System.out.println("Interactions API is not yet supported on Vertex");
return;
}

System.out.println("Using Gemini Developer API");

CreateModelInteraction params =
CreateModelInteraction.builder()
.input(InteractionsInput.of("Why is the sky blue?"))
.model(Constants.GEMINI_MODEL_NAME)
.build();

Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params))
.interaction()
.orElseThrow(() -> new RuntimeException("Failed to create interaction"));

System.out.println("Interaction ID: " + interaction.id().orElse(""));
System.out.println("Status: " + interaction.status());

// Print the text outputs from the interaction.
interaction.steps().ifPresent(steps -> {
for (Step step : steps) {
if (step.value() instanceof ModelOutputStep) {
ModelOutputStep modelOutput = (ModelOutputStep) step.value();
modelOutput.content().ifPresent(contents -> {
for (Content content : contents) {
if (content.value() instanceof TextContent) {
TextContent text = (TextContent) content.value();
System.out.println("Output: " + text.text());
}
}
});
}
}
});
}

private InteractionBasic() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2026 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.
*/

/**
* Usage:
*
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
*
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
*
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
*
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
*
* <p>export GOOGLE_GENAI_USE_VERTEXAI=true
*
* <p>1b. If you are using Gemini Developer API, set an API key environment variable. You can find a
* list of available API keys here: https://aistudio.google.com/app/apikey
*
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
*
* <p>2. Compile the java package and run the sample code.
*
* <p>mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.InteractionCreate"
*/
package com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.ModelOutputStep;
import com.google.genai.gaos.models.interactions.Step;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

/** An example of using the Unified Gen AI Java SDK to create an interaction. */
public final class InteractionCreate {
public static void main(String[] args) {
// Instantiate the client. The client by default uses the Gemini Developer API. It gets the API
// key from the environment variable `GOOGLE_API_KEY`. Vertex AI API can be used by setting the
// environment variables `GOOGLE_CLOUD_LOCATION` and `GOOGLE_CLOUD_PROJECT`, as well as setting
// `GOOGLE_GENAI_USE_VERTEXAI` to "true".
//
// Note: Some services are only available in a specific API backend (Gemini or Vertex), you will
// get a `UnsupportedOperationException` if you try to use a service that is not available in
// the backend you are using.
Client client = new Client();

if (client.vertexAI()) {
System.out.println("Using Vertex AI");
} else {
System.out.println("Using Gemini Developer API");
}

CreateModelInteraction params =
CreateModelInteraction.builder()
.input(InteractionsInput.of("What is your name?"))
.model(Constants.GEMINI_MODEL_NAME)
.build();

Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params))
.interaction()
.orElseThrow(() -> new RuntimeException("Failed to create interaction"));

System.out.println("Interaction ID: " + interaction.id().orElse(""));
System.out.println("Status: " + interaction.status());

// Print the text outputs from the interaction.
interaction.steps().ifPresent(steps -> {
for (Step step : steps) {
if (step.value() instanceof ModelOutputStep) {
ModelOutputStep modelOutput = (ModelOutputStep) step.value();
modelOutput.content().ifPresent(contents -> {
for (Content content : contents) {
if (content.value() instanceof TextContent) {
TextContent text = (TextContent) content.value();
System.out.println("Output: " + text.text());
}
}
});
}
}
});
}

private InteractionCreate() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2026 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.
*/

/**
* Usage:
*
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
*
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
*
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
*
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
*
* <p>export GOOGLE_GENAI_USE_VERTEXAI=true
*
* <p>1b. If you are using Gemini Developer API, set an API key environment variable. You can find a
* list of available API keys here: https://aistudio.google.com/app/apikey
*
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
*
* <p>2. Compile the java package and run the sample code.
*
* <p>mvn clean compile exec:java
* -Dexec.mainClass="com.google.genai.examples.InteractionCreateAsync"
*/
package com.google.genai.examples;

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.Content;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.ModelOutputStep;
import com.google.genai.gaos.models.interactions.Step;
import com.google.genai.gaos.models.interactions.TextContent;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import java.util.concurrent.CompletableFuture;

/** An example of using the Unified Gen AI Java SDK to create an interaction asynchronously. */
public final class InteractionCreateAsync {
public static void main(String[] args) {
// Instantiate the client. The client by default uses the Gemini Developer API. It gets the API
// key from the environment variable `GOOGLE_API_KEY`. Vertex AI API can be used by setting the
// environment variables `GOOGLE_CLOUD_LOCATION` and `GOOGLE_CLOUD_PROJECT`, as well as setting
// `GOOGLE_GENAI_USE_VERTEXAI` to "true".
//
// Note: Some services are only available in a specific API backend (Gemini or Vertex), you will
// get a `UnsupportedOperationException` if you try to use a service that is not available in
// the backend you are using.
Client client = new Client();

if (client.vertexAI()) {
System.out.println("Using Vertex AI");
} else {
System.out.println("Using Gemini Developer API");
}

CreateModelInteraction params =
CreateModelInteraction.builder()
.input(InteractionsInput.of("What is your name?"))
.model(Constants.GEMINI_MODEL_NAME)
.build();

CompletableFuture<Interaction> interactionFuture =
client.async.interactions.create(CreateInteractionRequestBody.of(params))
.body()
.thenApply(
response ->
response
.interaction()
.orElseThrow(() -> new RuntimeException("Failed to create interaction")));

interactionFuture
.thenAccept(
interaction -> {
System.out.println("Interaction ID: " + interaction.id().orElse(""));
System.out.println("Status: " + interaction.status());

// Print the text outputs from the interaction.
interaction.steps().ifPresent(steps -> {
for (Step step : steps) {
if (step.value() instanceof ModelOutputStep) {
ModelOutputStep modelOutput = (ModelOutputStep) step.value();
modelOutput.content().ifPresent(contents -> {
for (Content content : contents) {
if (content.value() instanceof TextContent) {
TextContent text = (TextContent) content.value();
System.out.println("Output: " + text.text());
}
}
});
}
}
});
})
.join();
}

private InteractionCreateAsync() {}
}
Loading
Loading