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
Expand Up @@ -44,6 +44,7 @@
import com.google.genai.interactions.models.interactions.CreateModelInteractionParams;
import com.google.genai.interactions.models.interactions.Interaction;
import com.google.genai.interactions.models.interactions.Model;
import com.google.genai.interactions.models.interactions.Step;

/** An example of using the Unified Gen AI Java SDK to create a basic interaction. */
public final class InteractionBasic {
Expand All @@ -70,11 +71,22 @@ public static void main(String[] args) {

// Print the text outputs from the interaction.
interaction
.outputs()
.steps()
.ifPresent(
outputs -> {
for (Content output : outputs) {
output.text().ifPresent(text -> System.out.println("Output: " + text.text()));
steps -> {
for (Step step : steps) {
if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content content : contents) {
content
.text()
.ifPresent(text -> System.out.println("Output: " + text.text()));
}
});
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.genai.interactions.models.interactions.CreateModelInteractionParams;
import com.google.genai.interactions.models.interactions.Interaction;
import com.google.genai.interactions.models.interactions.Model;
import com.google.genai.interactions.models.interactions.Step;

/** An example of using the Unified Gen AI Java SDK to create an interaction. */
public final class InteractionCreate {
Expand Down Expand Up @@ -77,11 +78,22 @@ public static void main(String[] args) {

// Print the text outputs from the interaction.
interaction
.outputs()
.steps()
.ifPresent(
outputs -> {
for (Content output : outputs) {
output.text().ifPresent(text -> System.out.println("Output: " + text.text()));
steps -> {
for (Step step : steps) {
if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content content : contents) {
content
.text()
.ifPresent(text -> System.out.println("Output: " + text.text()));
}
});
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.genai.interactions.models.interactions.CreateModelInteractionParams;
import com.google.genai.interactions.models.interactions.Interaction;
import com.google.genai.interactions.models.interactions.Model;
import com.google.genai.interactions.models.interactions.Step;
import java.util.concurrent.CompletableFuture;

/** An example of using the Unified Gen AI Java SDK to create an interaction asynchronously. */
Expand Down Expand Up @@ -82,13 +83,24 @@ public static void main(String[] args) {

// Print the text outputs from the interaction.
interaction
.outputs()
.steps()
.ifPresent(
outputs -> {
for (Content output : outputs) {
output
.text()
.ifPresent(text -> System.out.println("Output: " + text.text()));
steps -> {
for (Step step : steps) {
if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content content : contents) {
content
.text()
.ifPresent(
text ->
System.out.println("Output: " + text.text()));
}
});
}
}
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@
import com.google.genai.interactions.models.interactions.Content;
import com.google.genai.interactions.models.interactions.CreateModelInteractionParams;
import com.google.genai.interactions.models.interactions.Function;
import com.google.genai.interactions.models.interactions.FunctionCallContent;
import com.google.genai.interactions.models.interactions.FunctionResultContent;
import com.google.genai.interactions.models.interactions.FunctionCallStep;
import com.google.genai.interactions.models.interactions.FunctionResultStep;
import com.google.genai.interactions.models.interactions.Interaction;
import com.google.genai.interactions.models.interactions.Model;
import com.google.genai.interactions.models.interactions.Step;
import com.google.genai.interactions.models.interactions.TextContent;
import com.google.genai.interactions.models.interactions.Tool;
import com.google.genai.interactions.models.interactions.Turn;
import com.google.genai.interactions.models.interactions.UserInputStep;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -110,20 +112,25 @@ public static void main(String[] args) {
.build();

// 2. Initialize conversation history
List<Turn> conversationHistory = new ArrayList<>();
List<Step> conversationHistory = new ArrayList<>();
conversationHistory.add(
Turn.builder()
.role("user")
.content(
"Schedule a meeting for 2025-11-01 at 10 am with Peter and Amir about the Next Gen"
+ " API")
.build());
Step.ofUserInput(
UserInputStep.builder()
.content(
List.of(
Content.ofText(
TextContent.builder()
.text(
"Schedule a meeting for 2025-11-01 at 10 am with Peter and Amir"
+ " about the Next Gen API")
.build())))
.build()));

// 3. First turn: Model decides to call the function
CreateModelInteractionParams params =
CreateModelInteractionParams.builder()
.model(Model.GEMINI_2_5_FLASH)
.inputOfTurnList(conversationHistory)
.inputOfStepList(conversationHistory)
.tools(Arrays.asList(Tool.ofFunction(function)))
.build();

Expand All @@ -132,59 +139,76 @@ public static void main(String[] args) {
String functionCallId = null;
String functionName = null;

List<Content> outputs = response.outputs().orElse(null);
if (outputs != null) {
for (Content output : outputs) {
if (output.isFunctionCall()) {
FunctionCallContent functionCall = output.asFunctionCall();
List<Step> steps = response.steps().orElse(null);
if (steps != null) {
for (Step step : steps) {
if (step.isFunctionCall()) {
FunctionCallStep functionCall = step.asFunctionCall();
functionCallId = functionCall.id();
functionName = functionCall.name();
System.out.println("Model requested function call: " + functionName);
System.out.println("Arguments: " + functionCall.arguments());
} else if (output.text().isPresent()) {
System.out.println("Output Text: " + output.text().get().text());
} else if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content output : contents) {
output
.text()
.ifPresent(text -> System.out.println("Output Text: " + text.text()));
}
});
}
}

// Add model response back to history
conversationHistory.add(Turn.builder().role("model").contentOfContentList(outputs).build());
for (Step step : steps) {
conversationHistory.add(step);
}
}

// 4. Second turn: Send the function result back to the model
if (functionCallId != null) {
System.out.println("Sending function result back...");

FunctionResultContent functionResult =
FunctionResultContent.builder()
FunctionResultStep functionResult =
FunctionResultStep.builder()
.callId(functionCallId)
.name(functionName)
.result("Meeting scheduled successfully.")
.result(FunctionResultStep.Result.ofString("Meeting scheduled successfully."))
.build();

// Create a turn with function result
conversationHistory.add(
Turn.builder()
.role("user")
.contentOfContentList(Arrays.asList(Content.ofFunctionResult(functionResult)))
.build());
// Create a step with function result
conversationHistory.add(Step.ofFunctionResult(functionResult));

CreateModelInteractionParams followUpParams =
CreateModelInteractionParams.builder()
.model(Model.GEMINI_2_5_FLASH)
.inputOfTurnList(conversationHistory)
.inputOfStepList(conversationHistory)
.build();

Interaction followUpResponse = client.interactions.create(followUpParams);

System.out.println("Final response status: " + followUpResponse.status());
followUpResponse
.outputs()
.steps()
.ifPresent(
finalOutputs -> {
for (Content output : finalOutputs) {
output
.text()
.ifPresent(text -> System.out.println("Final Output: " + text.text()));
finalSteps -> {
for (Step step : finalSteps) {
if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content output : contents) {
output
.text()
.ifPresent(
text -> System.out.println("Final Output: " + text.text()));
}
});
}
}
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
import com.google.genai.interactions.models.interactions.Content;
import com.google.genai.interactions.models.interactions.CreateModelInteractionParams;
import com.google.genai.interactions.models.interactions.Function;
import com.google.genai.interactions.models.interactions.FunctionCallContent;
import com.google.genai.interactions.models.interactions.FunctionResultContent;
import com.google.genai.interactions.models.interactions.FunctionCallStep;
import com.google.genai.interactions.models.interactions.FunctionResultStep;
import com.google.genai.interactions.models.interactions.Interaction;
import com.google.genai.interactions.models.interactions.Model;
import com.google.genai.interactions.models.interactions.Step;
import com.google.genai.interactions.models.interactions.Tool;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -122,17 +123,26 @@ public static void main(String[] args) {
String functionCallId = null;
String functionName = null;

List<Content> outputs = response.outputs().orElse(null);
if (outputs != null) {
for (Content output : outputs) {
if (output.isFunctionCall()) {
FunctionCallContent functionCall = output.asFunctionCall();
List<Step> steps = response.steps().orElse(null);
if (steps != null) {
for (Step step : steps) {
if (step.isFunctionCall()) {
FunctionCallStep functionCall = step.asFunctionCall();
functionCallId = functionCall.id();
functionName = functionCall.name();
System.out.println("Model requested function call: " + functionName);
System.out.println("Arguments: " + functionCall.arguments());
} else if (output.text().isPresent()) {
System.out.println("Output Text: " + output.text().get().text());
} else if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content output : contents) {
output
.text()
.ifPresent(text -> System.out.println("Output Text: " + text.text()));
}
});
}
}
}
Expand All @@ -141,31 +151,43 @@ public static void main(String[] args) {
if (functionCallId != null) {
System.out.println("Sending function result back...");

FunctionResultContent functionResult =
FunctionResultContent.builder()
FunctionResultStep functionResult =
FunctionResultStep.builder()
.callId(functionCallId)
.name(functionName)
.result("Meeting scheduled successfully.")
.result(FunctionResultStep.Result.ofString("Meeting scheduled successfully."))
.build();

CreateModelInteractionParams followUpParams =
CreateModelInteractionParams.builder()
.model(Model.GEMINI_2_5_FLASH)
.previousInteractionId(response.id())
.input(CreateModelInteractionParams.Input.ofFunctionResultContent(functionResult))
.input(
CreateModelInteractionParams.Input.ofStepList(
java.util.Arrays.asList(Step.ofFunctionResult(functionResult))))
.build();

Interaction followUpResponse = client.interactions.create(followUpParams);

System.out.println("Final response status: " + followUpResponse.status());
followUpResponse
.outputs()
.steps()
.ifPresent(
finalOutputs -> {
for (Content output : finalOutputs) {
output
.text()
.ifPresent(text -> System.out.println("Final Output: " + text.text()));
finalSteps -> {
for (Step step : finalSteps) {
if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content output : contents) {
output
.text()
.ifPresent(
text -> System.out.println("Final Output: " + text.text()));
}
});
}
}
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.genai.interactions.models.interactions.CreateModelInteractionParams;
import com.google.genai.interactions.models.interactions.Interaction;
import com.google.genai.interactions.models.interactions.Model;
import com.google.genai.interactions.models.interactions.Step;

/** An example of using the Unified Gen AI Java SDK to retrieve an interaction. */
public final class InteractionGet {
Expand Down Expand Up @@ -75,11 +76,22 @@ public static void main(String[] args) {

// Print the text outputs from the retrieved interaction.
retrievedInteraction
.outputs()
.steps()
.ifPresent(
outputs -> {
for (Content output : outputs) {
output.text().ifPresent(text -> System.out.println("Output: " + text.text()));
steps -> {
for (Step step : steps) {
if (step.isModelOutput()) {
step.asModelOutput()
.content()
.ifPresent(
contents -> {
for (Content content : contents) {
content
.text()
.ifPresent(text -> System.out.println("Output: " + text.text()));
}
});
}
}
});
}
Expand Down
Loading
Loading