1. Gemini Developer में आपका स्वागत है!

इस कोडलैब में, कस्टम Gemini Interactions SDK का इस्तेमाल करके, Java में अगली पीढ़ी के जनरेटिव एआई ऐप्लिकेशन बनाने का तरीका बताया गया है.
Gemini Interactions API क्या है?
पारंपरिक एलएलएम एपीआई, स्टेटलेस होते हैं. साथ ही, ये अनुरोध और जवाब के आधार पर काम करते हैं. मल्टी-टर्न चैट असिस्टेंट या कॉम्प्लेक्स एजेंटिक लूप बनाने के लिए, डेवलपर को बातचीत की स्थिति, इतिहास को छोटा करना, टूल कॉल ऑर्केस्ट्रेशन, और एक्ज़ीक्यूशन लूप को पूरी तरह से क्लाइंट-साइड ऐप्लिकेशन कोड में मैनेज करना पड़ता था.
Gemini Interactions API इस जटिलता को सर्वर पर शिफ़्ट कर देता है. यह एक स्टेटफ़ुल, सेशन पर आधारित एपीआई है. इसमें Google का इन्फ़्रास्ट्रक्चर, बातचीत के ग्राफ़ को होस्ट और मैनेज करता है. एक इंटरैक्शन, स्टेटफ़ुल सेशन को दिखाता है. इसके साथ इंटरैक्ट करने पर, एपीआई एक बेहतर और स्ट्रक्चर्ड टाइमलाइन दिखाता है. इसमें कई तरह के चरण शामिल होते हैं. जैसे:
ThoughtStep: मॉडल की इंटरनल रीज़निंग प्रोसेस.ModelOutputStep: मॉडल से जनरेट किया गया टेक्स्ट, ऑडियो या इमेज कॉन्टेंट.ToolCallStepऔरToolResultStep: सिस्टम या मॉडल की ओर से शुरू किए गए टूल के इस्तेमाल.UserInteractionStep: वे पॉइंट जहां सिस्टम, मैन्युअल इनपुट या मंज़ूरी का अनुरोध करने के लिए रुकता है.
मैनेज किए जा रहे एजेंट क्या होते हैं?
ऑटोनॉमस एजेंट को मैनेज करना बहुत मुश्किल होता है. जैसे, लूप को मैनेज करना, फिर से कोशिश करने का लॉजिक, टूल को चलाने के लिए एनवायरमेंट, और स्टेट मैनेजमेंट.
मैनेज किए जा रहे एजेंट, Gemini Interactions API से मिलने वाला प्लैटफ़ॉर्म-लेवल का समाधान है. एजेंट लूप को स्थानीय तौर पर चलाने के बजाय, खास एजेंट को सीधे Google के इन्फ़्रास्ट्रक्चर पर उपलब्ध कराया जा सकता है:
- पहले से मौजूद एजेंट: इस्तेमाल के लिए तैयार खास एजेंट, जैसे कि Deep Research एजेंट. यह एजेंट, वेब पर कई चरणों में रिसर्च करता है, नतीजों को इकट्ठा करता है, और पूरी जानकारी वाली रिपोर्ट जनरेट करता है.
- कस्टम मैनेज्ड एजेंट: ये अपने-आप काम करने वाली ऐसी इकाइयां होती हैं जिन्हें आपने तय किया है. सिस्टम से जुड़े निर्देश दिए जाते हैं, टूल अटैच किए जाते हैं (जैसे, Google Search या Bash एक्ज़ीक्यूशन एनवायरमेंट), और Cloud Sandbox को कॉन्फ़िगर किया जाता है. Cloud Sandbox, एक सुरक्षित, अलग, और कंटेनर में मौजूद रनटाइम एनवायरमेंट है. इसमें नेटवर्क इग्रेस के नियमों को पसंद के मुताबिक बनाया जा सकता है. उदाहरण के लिए, GitHub जैसे चुनिंदा डोमेन को ही ऐक्सेस करने की अनुमति देना.
Gemini Interactions Java SDK का इस्तेमाल करके, स्टैंडर्ड Java ऐप्लिकेशन में मैनेज किए जा रहे इन एजेंट को आसानी से बूटस्ट्रैप किया जा सकता है, उनके साथ मिलकर काम किया जा सकता है, और उन्हें मैनेज किया जा सकता है.
आपको क्या सीखने को मिलेगा
- पॉलीमॉर्फ़िक
Stepपर आधारित नए आर्किटेक्चर को नेविगेट करने का तरीका. - एक्सप्रेसिव टीटीएस ऑडियो को सीधे स्पीकर पर स्ट्रीम करने का तरीका.
- Lyria की मदद से संगीत (एमपी3 + गाने के बोल) जनरेट करने का तरीका.
- Gemini 3 Pro Image की मदद से, विज़ुअल स्केचनोट जनरेट करने का तरीका.
- Collaborative Planning का इस्तेमाल करके, Deep Research एजेंट को कैसे कंट्रोल करें.
- नेटवर्क इग्रेस डेटा ट्रैफ़िक के नियमों और टूल की मदद से, कस्टम एजेंट को कैसे तैयार करें.
आपको किन चीज़ों की ज़रूरत होगी
- Java 21 या इसके बाद का वर्शन.
- Apache Maven.
- कोई टेक्स्ट एडिटर या IDE (IntelliJ IDEA, VS Code वगैरह).
- Gemini API पासकोड (Google AI Studio से).
2. सेटअप: प्रोजेक्ट और एपीआई पासकोड
Maven प्रोजेक्ट बनाना
अपने टर्मिनल से नया Maven प्रोजेक्ट बूटस्ट्रैप करने के लिए, इस निर्देश का इस्तेमाल करें:
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=gemini-interactions-demo \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.5 \
-DinteractiveMode=false
हाल ही में बनाई गई प्रोजेक्ट डायरेक्ट्री पर जाएं:
cd gemini-interactions-demo
अपनी pom.xml फ़ाइल खोलें और उसे कॉन्फ़िगर करें:
- Java 21 को टारगेट करने के लिए, Java वर्शन की प्रॉपर्टी अपडेट करें:
<properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> </properties> ब्लॉक में एसडीके डिपेंडेंसी जोड़ें:<dependency> <groupId>io.github.glaforge</groupId> <artifactId>gemini-interactions-api-sdk</artifactId> <version>0.10.1</version> </dependency>
एपीआई पासकोड कॉन्फ़िगर करना
Google AI Studio से Gemini API पासकोड पाएं.
अपने टर्मिनल में, कुंजी को एनवायरमेंट वैरिएबल के तौर पर सेट करें:
macOS / Linux:
export GEMINI_API_KEY="your_actual_api_key"
Windows (कमांड प्रॉम्प्ट):
set GEMINI_API_KEY="your_actual_api_key"
3. नमस्ते: स्टेप आर्किटेक्चर को समझना
Interactions API में, पॉलीमॉर्फ़िक और स्टेप-आधारित टाइमलाइन आर्किटेक्चर को शामिल किया गया है. एपीआई, आउटपुट की फ़्लैट सूची दिखाने के बजाय, टाइप किए गए Step ऑब्जेक्ट का क्रम दिखाता है. जैसे, ModelOutputStep, ThoughtStep, FunctionCallStep.
इस चरण में, आपको एक आसान इंटरैक्शन लिखना होगा, ताकि यह समझा जा सके कि इस स्ट्रक्चर से फ़ाइनल मॉडल आउटपुट कैसे निकाला जाता है.
HelloInteractions.java बनाएं
यहां दिए गए कॉन्टेंट के साथ src/main/java/com/example/HelloInteractions.java फ़ाइल बनाएं:
package com.example;
import io.github.glaforge.gemini.interactions.GeminiInteractionsClient;
import io.github.glaforge.gemini.interactions.model.*;
import io.github.glaforge.gemini.interactions.model.InteractionParams.ModelInteractionParams;
public class HelloInteractions {
public static void main(String[] args) {
// 1. Initialize the client
GeminiInteractionsClient client = GeminiInteractionsClient.builder()
.apiKey(System.getenv("GEMINI_API_KEY"))
.build();
// 2. Build the request
ModelInteractionParams request = ModelInteractionParams.builder()
.model("gemini-3.5-flash")
.input("Explain the difference between a library and a framework in one sentence.")
.build();
// 3. Send request
Interaction response = client.create(request);
// 4. Navigate the step-based architecture to get the output
response.steps().stream()
.filter(step -> step instanceof Step.ModelOutputStep)
.map(step -> (Step.ModelOutputStep) step)
.findFirst()
.ifPresent(step -> System.out.println(step.content().get(0)));
}
}
कोड को रन करना
क्लास को कंपाइल करें और चलाएं:
mvn compile exec:java -Dexec.mainClass=com.example.HelloInteractions
4. Steerable Audio: Streaming Expressive TTS
Gemini 3.1 Flash में, लिखे गए शब्दों को सुनने की सुविधा (टीटीएस) को कंट्रोल करने की सुविधा जोड़ी गई है. प्रॉम्प्ट का इस्तेमाल करके, आवाज़ की रफ़्तार, टोन, और माहौल को कंट्रोल किया जा सकता है. साथ ही, वाक्य के बीच में इमोशन टैग (जैसे, [excitedly] या [whispers]) इस्तेमाल किए जा सकते हैं.
इस चरण में, आपको एक्सप्रेसिव ऑडियो जनरेट करना होगा और उसे सीधे अपने स्पीकर पर स्ट्रीम करना होगा.
StreamingDJ.java बनाएं
यहां दिए गए कॉन्टेंट के साथ src/main/java/com/example/StreamingDJ.java फ़ाइल बनाएं:
package com.example;
import io.github.glaforge.gemini.interactions.GeminiInteractionsClient;
import io.github.glaforge.gemini.interactions.model.*;
import io.github.glaforge.gemini.interactions.model.Config.SpeechConfig;
import io.github.glaforge.gemini.interactions.model.InteractionParams.ModelInteractionParams;
import javax.sound.sampled.*;
import java.util.Base64;
import java.util.stream.Stream;
public class StreamingDJ {
public static void main(String[] args) throws Exception {
GeminiInteractionsClient client = GeminiInteractionsClient.builder()
.apiKey(System.getenv("GEMINI_API_KEY"))
.build();
// Prompt defining the voice profile and emotional tags
String prompt = """
# AUDIO PROFILE: Jaz R.
## THE SCENE: London Studio
### DIRECTOR'S NOTES
Accent: Jaz is a DJ from Brixton, London.
Style: Bouncy, energetic, high-speed delivery.
#### TRANSCRIPT
[excitedly] Yes, massive vibes in the studio!
[whispers] But keep it down, the boss is coming...
[shouting] Turn this up! Let's go!
""";
ModelInteractionParams request = ModelInteractionParams.builder()
.model("gemini-3.1-flash-tts-preview")
.input(prompt)
.responseModalities(Interaction.Modality.AUDIO)
.speechConfig(new SpeechConfig("Algenib", "en-GB"))
.stream(true) // Enable streaming
.build();
System.out.println("Streaming audio from Gemini...");
try (Stream<Events> eventStream = client.stream(request)) {
// Configure the Java Audio System for 24kHz Mono 16-bit PCM
AudioFormat format = new AudioFormat(24000, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try (SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) {
line.open(format);
line.start();
// Process the stream and play audio chunks as they arrive
eventStream.forEach(event -> {
if (event instanceof Events.StepDelta cd && cd.delta() instanceof Events.AudioDelta audioDelta) {
byte[] audioData = Base64.getDecoder().decode(audioDelta.data());
line.write(audioData, 0, audioData.length);
}
});
line.drain();
}
}
}
}
कोड को रन करना
mvn compile exec:java -Dexec.mainClass=com.example.StreamingDJ
आउटपुट सुनना
यहां एक ऑडियो उदाहरण दिया गया है. इसमें बताया गया है कि कोड को चलाने पर आपको क्या सुनाई देगा. इसमें इमोशन टैग के साथ Algenib की आवाज़ का इस्तेमाल किया गया है:
5. Lyria 3 की मदद से संगीत जनरेट करने की सुविधा
DeepMind Lyria 3 मॉडल का इस्तेमाल करके, संगीत और जिंगल जनरेट किए जा सकते हैं. जवाब के लिए दो मोड (AUDIO और TEXT) का अनुरोध करके, जनरेट किया गया ऑडियो (MP3) और गाने के बोल, दोनों को वापस पाया जा सकता है.
MusicGenerator.java बनाएं
यहां दिए गए कॉन्टेंट के साथ src/main/java/com/example/MusicGenerator.java फ़ाइल बनाएं:
package com.example;
import io.github.glaforge.gemini.interactions.GeminiInteractionsClient;
import io.github.glaforge.gemini.interactions.model.*;
import io.github.glaforge.gemini.interactions.model.InteractionParams.ModelInteractionParams;
import io.github.glaforge.gemini.interactions.model.Content.AudioContent;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MusicGenerator {
public static void main(String[] args) throws Exception {
GeminiInteractionsClient client = GeminiInteractionsClient.builder()
.apiKey(System.getenv("GEMINI_API_KEY"))
.build();
ModelInteractionParams request = ModelInteractionParams.builder()
.model("models/lyria-3-clip-preview") // 30-second clip
.input("An uplifting rock song with acoustic guitars about coding in Java.")
.responseModalities(
Interaction.Modality.AUDIO,
Interaction.Modality.TEXT) // Request both MP3 and Lyrics
.build();
System.out.println("Generating music (this might take a moment)...");
Interaction response = client.create(request);
// 1. Print the lyrics (TEXT output)
System.out.println("\n--- Generated Lyrics ---");
response.steps().stream()
.filter(step -> step instanceof Step.ModelOutputStep)
.flatMap(step -> ((Step.ModelOutputStep) step).content().stream())
.filter(content -> content instanceof Content.TextContent)
.forEach(content -> System.out.println(((Content.TextContent) content).text()));
// 2. Save the MP3 (AUDIO output)
response.steps().stream()
.filter(step -> step instanceof Step.ModelOutputStep)
.flatMap(step -> ((Step.ModelOutputStep) step).content().stream())
.filter(content -> content instanceof AudioContent)
.map(content -> (AudioContent) content)
.findFirst()
.ifPresent(audio -> {
try {
Files.write(Paths.get("coding_song.mp3"), audio.data());
System.out.println("\nSuccess: Song saved to coding_song.mp3");
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
कोड को रन करना
mvn compile exec:java -Dexec.mainClass=com.example.MusicGenerator
जनरेट किया गया गाना सुनना
यहां जनरेट की गई MP3 फ़ाइल (coding_song.mp3) दी गई है. इसमें संगीत और गाने के बोल शामिल हैं:
6. Sketchnotes (Nano Banana Pro) की मदद से विज़ुअलाइज़ करना
Gemini 3 Pro Image (इसे Nano Banana Pro भी कहा जाता है) इमेज जनरेट कर सकता है. IMAGE मोडैलिटी का अनुरोध करके, टेक्स्ट इनपुट के आधार पर इन्फ़ोग्राफ़िक, डायग्राम या स्केचनोट जनरेट किए जा सकते हैं.
इस चरण में, मैनेज किए जा रहे एजेंट के बारे में लिखे गए लेख की खास जानकारी को स्केचनोट के तौर पर जनरेट किया जाएगा. इसके बाद, उसे PNG फ़ाइल के तौर पर सेव किया जाएगा.
ImageGenerator.java बनाएं
यहां दिए गए कॉन्टेंट के साथ src/main/java/com/example/ImageGenerator.java फ़ाइल बनाएं:
package com.example;
import io.github.glaforge.gemini.interactions.GeminiInteractionsClient;
import io.github.glaforge.gemini.interactions.model.*;
import io.github.glaforge.gemini.interactions.model.InteractionParams.ModelInteractionParams;
import io.github.glaforge.gemini.interactions.model.Content.ImageContent;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ImageGenerator {
public static void main(String[] args) throws Exception {
GeminiInteractionsClient client = GeminiInteractionsClient.builder()
.apiKey(System.getenv("GEMINI_API_KEY"))
.build();
String articleSummary = """
Managed Agents in the Gemini API allow developers to run autonomous agents
that reason, plan, use tools, and execute code inside isolated cloud sandboxes.
The Gemini API handles the infrastructure (containers, network, runtime).
It is powered by the Antigravity agent running on Gemini 3.5 Flash.
The Java Interactions SDK supports these capabilities, utilizing a Step-based
architecture to model the execution timeline.
""";
ModelInteractionParams request = ModelInteractionParams.builder()
.model("gemini-3-pro-image-preview")
.input(String.format("""
Create a hand-drawn and hand-written sketchnote
style summary infographic, with a pure white background,
about the following information:
%s
""", articleSummary))
.responseModalities(Interaction.Modality.IMAGE) // Request IMAGE modality
.build();
System.out.println("Generating sketchnote (this might take a moment)...");
Interaction response = client.create(request);
// Save the generated image
response.steps().stream()
.filter(step -> step instanceof Step.ModelOutputStep)
.flatMap(step -> ((Step.ModelOutputStep) step).content().stream())
.filter(content -> content instanceof ImageContent)
.map(content -> (ImageContent) content)
.findFirst()
.ifPresent(image -> {
try {
Files.write(Paths.get("sketchnote.png"), image.data());
System.out.println("Success: Sketchnote saved to sketchnote.png");
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
कोड को रन करना
mvn compile exec:java -Dexec.mainClass=com.example.ImageGenerator
जनरेट किया गया स्केचनोट
मॉडल ने यह स्केचनोट (sketchnote.png) जनरेट किया है:

7. एजेंट को निर्देश देना: मिलकर Deep Research करना
Deep Research एक बेहतरीन एजेंट है, जो कई चरणों वाले रिसर्च टास्क पूरे कर सकता है. हालांकि, रिसर्च को तुरंत शुरू करने के बजाय, साथ मिलकर प्लान बनाने की सुविधा का इस्तेमाल किया जा सकता है. इससे एजेंट के डेटा इकट्ठा करने से पहले, रिसर्च के प्लान की समीक्षा की जा सकती है, उसमें बदलाव किया जा सकता है, और उसे बेहतर बनाया जा सकता है.
आपको एक से ज़्यादा बार बातचीत करने की सुविधा लागू करनी होगी. इसके लिए, सर्वर-साइड स्टेट (previousInteractionId) का इस्तेमाल किया जाएगा, ताकि प्लान को बेहतर बनाया जा सके.
CollaborativeResearch.java बनाएं
यहां दिए गए कॉन्टेंट के साथ src/main/java/com/example/CollaborativeResearch.java फ़ाइल बनाएं:
package com.example;
import io.github.glaforge.gemini.interactions.GeminiInteractionsClient;
import io.github.glaforge.gemini.interactions.model.*;
import io.github.glaforge.gemini.interactions.model.InteractionParams.AgentInteractionParams;
import io.github.glaforge.gemini.interactions.model.Config.DeepResearchAgentConfig;
import io.github.glaforge.gemini.interactions.model.Config.ThinkingSummaries;
import io.github.glaforge.gemini.interactions.model.Config.Visualization;
public class CollaborativeResearch {
public static void main(String[] args) throws Exception {
GeminiInteractionsClient client = GeminiInteractionsClient.builder()
.apiKey(System.getenv("GEMINI_API_KEY"))
.build();
String agentModel = "deep-research-preview-04-2026";
// --- Phase 1: Request a Plan ---
System.out.println("Phase 1: Requesting research plan...");
AgentInteractionParams planParams = AgentInteractionParams.builder()
.agent(agentModel)
.input("Research the latest generations of Google Cloud TPUs (TPU7x and the 8th generation TPU 8t and TPU 8i).")
.agentConfig(new DeepResearchAgentConfig(
"deep-research",
ThinkingSummaries.AUTO,
Visualization.AUTO,
true // TRUE enables collaborative planning
))
.background(true)
.store(true)
.build();
Interaction planInteraction = client.create(planParams);
planInteraction = waitForCompletion(client, planInteraction.id());
System.out.println("\n--- Proposed Plan ---");
printOutputText(planInteraction);
// --- Phase 2: Refine the Plan ---
System.out.println("\nPhase 2: Refining research plan...");
AgentInteractionParams refineParams = AgentInteractionParams.builder()
.agent(agentModel)
.input("Focus on comparing the architectural, performance, and scaling differences between the TPU7x generation and the two flavors of the eighth generation: TPU 8t (optimized for training at scale) and TPU 8i (optimized for low-latency reasoning and inference).")
.agentConfig(new DeepResearchAgentConfig(
"deep-research",
ThinkingSummaries.AUTO,
Visualization.AUTO,
true // Keep collaborative planning TRUE to iterate
))
.previousInteractionId(planInteraction.id()) // Resume session
.background(true)
.store(true)
.build();
Interaction refinedInteraction = client.create(refineParams);
refinedInteraction = waitForCompletion(client, refinedInteraction.id());
System.out.println("\n--- Refined Plan ---");
printOutputText(refinedInteraction);
// --- Phase 3: Approve and Execute ---
System.out.println("\nPhase 3: Approving plan and starting deep research (this will take a few minutes)...");
AgentInteractionParams executeParams = AgentInteractionParams.builder()
.agent(agentModel)
.input("Plan looks good, execute!")
.agentConfig(new DeepResearchAgentConfig(
"deep-research",
ThinkingSummaries.AUTO,
Visualization.AUTO,
false // FALSE approves the plan and executes the research
))
.previousInteractionId(refinedInteraction.id()) // Resume session
.background(true)
.store(true)
.build();
Interaction finalReport = client.create(executeParams);
finalReport = waitForCompletion(client, finalReport.id());
System.out.println("\n--- Final Research Report ---");
printOutputText(finalReport);
}
private static Interaction waitForCompletion(GeminiInteractionsClient client, String id) throws Exception {
Interaction interaction = client.get(id);
while (interaction.status() != Interaction.Status.COMPLETED && interaction.status() != Interaction.Status.FAILED) {
Thread.sleep(5000);
interaction = client.get(id);
}
if (interaction.status() == Interaction.Status.FAILED) {
throw new RuntimeException("Interaction failed. Status: " + interaction.status());
}
return interaction;
}
private static void printOutputText(Interaction interaction) {
interaction.steps().stream()
.filter(step -> step instanceof Step.ModelOutputStep)
.flatMap(step -> ((Step.ModelOutputStep) step).content().stream())
.filter(content -> content instanceof Content.TextContent)
.forEach(content -> System.out.println(((Content.TextContent) content).text()));
}
}
कोड को रन करना
mvn compile exec:java -Dexec.mainClass=com.example.CollaborativeResearch
जनरेट की गई रिपोर्ट का आउटपुट
Deep Research एजेंट, पूरी जानकारी वाली व्यवस्थित रिपोर्ट तैयार करेगा. उदाहरण के तौर पर जनरेट की गई पूरी रिपोर्ट यहां देखी जा सकती है:
Deep Research की मदद से जनरेट की गई रिपोर्ट (tpu_history_report.md) देखना
8. कस्टम एजेंट और क्लाउड सैंडबॉक्स
डेवलपर से जुड़े मुश्किल टास्क के लिए, कस्टम एजेंट बनाए जा सकते हैं. सिस्टम से जुड़े निर्देश तय करें, उन्हें कोड एक्ज़ीक्यूशन/बैश जैसे टूल उपलब्ध कराएं, और उनके रिमोट एनवायरमेंट को कॉन्फ़िगर करें. जैसे, नेटवर्क इग्रेस के नियम.
इस चरण में, आपको एक ऐसा एजेंट उपलब्ध कराना होगा जिसके पास github.com के लिए सुरक्षित इंटरनेट का ऐक्सेस हो. साथ ही, उसे रिपॉज़िटरी को क्लोन करने और अपने क्लाउड सैंडबॉक्स में उसकी कॉन्फ़िगरेशन फ़ाइलों का विश्लेषण करने का निर्देश देना होगा.
GitHubAnalyzer.java बनाएं
यहां दिए गए कॉन्टेंट के साथ src/main/java/com/example/GitHubAnalyzer.java फ़ाइल बनाएं:
package com.example;
import io.github.glaforge.gemini.interactions.GeminiInteractionsClient;
import io.github.glaforge.gemini.interactions.model.*;
import io.github.glaforge.gemini.interactions.model.InteractionParams.AgentInteractionParams;
import java.util.List;
public class GitHubAnalyzer {
public static void main(String[] args) throws Exception {
GeminiInteractionsClient client = GeminiInteractionsClient.builder()
.apiKey(System.getenv("GEMINI_API_KEY"))
.build();
String agentId = "github-analyzer-codelab";
// 1. Define the Custom Agent with Network Egress and Tools
Agent customAgent = Agent.builder()
.id(agentId)
.description("Clones and analyzes GitHub repos.")
.baseAgent("antigravity-preview-09-2026")
.baseEnvironment(new EnvironmentConfig(
new EnvironmentNetworkEgressAllowlist(List.of(
new AllowlistEntry("github.com") // Allow git clone over HTTPS
)),
List.of()
))
.systemInstruction("You are an architect. Clone the repo, inspect files, and write a summary.")
.tools(List.of(
new AgentTool.CodeExecution(), // Enables terminal bash execution in sandbox
new AgentTool.GoogleSearch()
))
.build();
// 2. Provision the Agent
System.out.println("Provisioning custom agent in the cloud...");
client.createAgent(customAgent);
try {
// 3. Start the Interaction
AgentInteractionParams params = AgentInteractionParams.builder()
.agent(agentId)
.input("Clone https://github.com/glaforge/gemini-interactions-api-sdk and explain its pom.xml structure.")
.environment("remote") // Crucial: Run in cloud sandbox
.build();
System.out.println("Starting clone and analysis (polling status)...");
Interaction interaction = client.create(params);
// 4. Poll for completion
while (interaction.status() != Interaction.Status.COMPLETED) {
System.out.println("Agent working... Status: " + interaction.status());
Thread.sleep(5000);
interaction = client.get(interaction.id());
}
// 5. Output the results
System.out.println("\n--- Architectural Analysis ---");
interaction.steps().stream()
.filter(step -> step instanceof Step.ModelOutputStep)
.flatMap(step -> ((Step.ModelOutputStep) step).content().stream())
.filter(content -> content instanceof Content.TextContent)
.forEach(content -> System.out.println(((Content.TextContent) content).text()));
} finally {
// 6. Clean up resources
client.deleteAgent(agentId);
System.out.println("\nCustom agent resource deleted from cloud.");
}
}
}
कोड को रन करना
mvn compile exec:java -Dexec.mainClass=com.example.GitHubAnalyzer
जनरेट किया गया विश्लेषण आउटपुट
रिपॉज़िटरी को क्लोन करने के बाद, कस्टम एजेंट से जनरेट की गई आर्किटेक्चरल विश्लेषण की पूरी रिपोर्ट यहां देखी जा सकती है:
9. बधाई हो!
आपने कोडलैब पूरा कर लिया है. साथ ही, Gemini Interactions SDK का इस्तेमाल करके, Java में जटिल, मल्टी-मोडल, और एजेंटिक वर्कफ़्लो बनाने का तरीका सीख लिया है.
आपने क्या हासिल किया:
- स्टेप आर्किटेक्चर का इस्तेमाल किया: स्टैंडर्ड मॉडल से क्वेरी करने के लिए, नए पॉलीमॉर्फ़िक स्टेप आर्किटेक्चर का इस्तेमाल किया.
- स्ट्रीम किया गया एक्सप्रेसिव टीटीएस: रीयल-टाइम में ऑडियो स्ट्रीम करने के लिए, डायरेक्टर के नोट और इनलाइन इमोशनल टैग का इस्तेमाल किया गया.
- जनरेट किया गया संगीत: Lyria 3 की मदद से जनरेट किए गए MP3 ट्रैक और गाने के बोल.
- जनरेट किए गए स्केचनोट: Gemini 3 Pro Image (Nano Banana Pro) का इस्तेमाल करके, विज़ुअल समरी बनाई गई.
- Steered Deep Research: रिसर्च का प्लान बेहतर बनाने के लिए, Collaborative Planning का इस्तेमाल किया गया.
- प्रोविज़न किए गए कस्टम एजेंट: कोड को सुरक्षित तरीके से चलाने के लिए, कस्टम नेटवर्क इग्रेस कंट्रोल के साथ सैंडबॉक्स किए गए एनवायरमेंट बनाए गए.
ज़्यादा जानें:
- GitHub पर SDK टूल का सोर्स कोड और ज़्यादा टेस्ट केस देखें: glaforge/gemini-interactions-api-sdk
- एजेंटिक डिज़ाइन पैटर्न के बारे में ज़्यादा जानने के लिए, Guillaume का ब्लॉग पढ़ें: glaforge.dev