Gemini Live API, ऑडियो या टेक्स्ट की लगातार स्ट्रीम को प्रोसेस करता है. इन्हें सेशन कहा जाता है. आपके पास सेशन के लाइफ़साइकल को मैनेज करने का विकल्प होता है. इसमें शुरुआती हैंडशेक से लेकर, ग्रेसफ़ुल टर्मिनेशन तक शामिल है.
सेशन की सीमाएं
Live API के लिए, सेशन का मतलब एक ऐसे कनेक्शन से है जो लगातार बना रहता है. इसमें इनपुट और आउटपुट, एक ही कनेक्शन पर लगातार स्ट्रीम किए जाते हैं.
अगर सेशन में, यहां दी गई सीमाओं में से किसी का उल्लंघन होता है, तो कनेक्शन बंद कर दिया जाता है.
कनेक्शन की अवधि करीब 10 मिनट तक सीमित होती है.
सेशन की अवधि, इनपुट के तरीकों पर निर्भर करती है:
- सिर्फ़ ऑडियो वाले इनपुट सेशन, 15 मिनट तक ही किए जा सकते हैं.
- वीडियो और ऑडियो इनपुट की अवधि दो मिनट से ज़्यादा नहीं होनी चाहिए.
सेशन कॉन्टेक्स्ट विंडो में ज़्यादा से ज़्यादा 1,28,000 टोकन इस्तेमाल किए जा सकते हैं.
कनेक्शन बंद होने से पहले, आपको बंद होने वाली सुविधा की सूचना मिलेगी. इससे आपको आगे की कार्रवाई करने का मौका मिलेगा.
सेशन शुरू करना
सेशन शुरू करने का तरीका दिखाने वाले पूरे स्निपेट के लिए, Live API के लिए शुरुआती गाइड पर जाएं.
सेशन के बीच में अपडेट करना
Live API मॉडल, मिड-सेशन अपडेट के लिए ये बेहतर सुविधाएं उपलब्ध कराते हैं:
सिस्टम के निर्देशों को अपडेट करना (सिर्फ़ Vertex AI Gemini API के लिए)
कॉन्टेंट में हुए बदलावों को जोड़ना
चालू सेशन के दौरान, इंक्रीमेंटल अपडेट जोड़े जा सकते हैं. इसका इस्तेमाल, टेक्स्ट इनपुट भेजने, सेशन का कॉन्टेक्स्ट सेट अप करने या सेशन का कॉन्टेक्स्ट वापस लाने के लिए किया जाता है.
ज़्यादा बड़े कॉन्टेक्स्ट के लिए, हमारा सुझाव है कि एक ही मैसेज की खास जानकारी दी जाए, ताकि बाद के इंटरैक्शन के लिए कॉन्टेक्स्ट विंडो खाली हो जाए.
छोटे कॉन्टेक्स्ट के लिए, बारी-बारी से होने वाली बातचीत भेजी जा सकती है, ताकि इवेंट के सही क्रम को दिखाया जा सके. जैसे, यहां दिया गया स्निपेट.
Swift
// Define initial turns (history/context).
let turns: [ModelContent] = [
ModelContent(role: "user", parts: [TextPart("What is the capital of France?")]),
ModelContent(role: "model", parts: [TextPart("Paris")]),
]
// Send history, keeping the conversational turn OPEN (false).
await session.sendContent(turns, turnComplete: false)
// Define the new user query.
let newTurn: [ModelContent] = [
ModelContent(role: "user", parts: [TextPart("What is the capital of Germany?")]),
]
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.sendContent(newTurn, turnComplete: true)
Kotlin
Not yet supported for Android apps - check back soon!
Java
Not yet supported for Android apps - check back soon!
Web
const turns = [{ text: "Hello from the user!" }];
await session.send(
turns,
false // turnComplete: false
);
console.log("Sent history. Waiting for next input...");
// Define the new user query.
const newTurn [{ text: "And what is the capital of Germany?" }];
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
newTurn,
true // turnComplete: true
);
console.log("Sent final query. Model response expected now.");
Dart
// Define initial turns (history/context).
final List turns = [
Content(
"user",
[Part.text("What is the capital of France?")],
),
Content(
"model",
[Part.text("Paris")],
),
];
// Send history, keeping the conversational turn OPEN (false).
await session.send(
input: turns,
turnComplete: false,
);
// Define the new user query.
final List newTurn = [
Content(
"user",
[Part.text("What is the capital of Germany?")],
),
];
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.send(
input: newTurn,
turnComplete: true,
);
Unity
// Define initial turns (history/context).
List turns = new List {
new ModelContent("user", new ModelContent.TextPart("What is the capital of France?") ),
new ModelContent("model", new ModelContent.TextPart("Paris") ),
};
// Send history, keeping the conversational turn OPEN (false).
foreach (ModelContent turn in turns)
{
await session.SendAsync(
content: turn,
turnComplete: false
);
}
// Define the new user query.
ModelContent newTurn = ModelContent.Text("What is the capital of Germany?");
// Send the final query, CLOSING the turn (true) to trigger the model response.
await session.SendAsync(
content: newTurn,
turnComplete: true
);
सेशन के बीच में सिस्टम के निर्देशों को अपडेट करना
| यह सुविधा सिर्फ़ तब उपलब्ध होती है, जब Vertex AI Gemini API को एपीआई उपलब्ध कराने वाली कंपनी के तौर पर इस्तेमाल किया जा रहा हो. |
चालू सेशन के दौरान, सिस्टम के निर्देशों को अपडेट किया जा सकता है. इसका इस्तेमाल, मॉडल के जवाबों को अपनी ज़रूरत के हिसाब से बनाने के लिए किया जाता है. उदाहरण के लिए, जवाब की भाषा बदलने या टोन में बदलाव करने के लिए.
सेशन के बीच में सिस्टम के निर्देशों को अपडेट करने के लिए, system भूमिका के साथ टेक्स्ट कॉन्टेंट भेजा जा सकता है. सिस्टम के लिए अपडेट किए गए निर्देश, सेशन के बाकी समय तक लागू रहेंगे.
Swift
await session.sendContent(
[ModelContent(
role: "system",
parts: [TextPart("new system instruction")]
)],
turnComplete: false
)
Kotlin
Not yet supported for Android apps - check back soon!
Java
Not yet supported for Android apps - check back soon!
Web
Not yet supported for Web apps - check back soon!
Dart
try {
await _session.send(
input: Content(
'system',
[Part.text('new system instruction')],
),
turnComplete: false,
);
} catch (e) {
print('Failed to update system instructions: $e');
}
Unity
try
{
await session.SendAsync(
content: new ModelContent(
"system",
new ModelContent.TextPart("new system instruction")
),
turnComplete: false
);
}
catch (Exception e)
{
Debug.LogError($"Failed to update system instructions: {e.Message}");
}
यह कुकी, सेशन के खत्म होने का पता लगाती है
सेशन खत्म होने से
यहां दिए गए उदाहरण में, going away सूचना को सुनकर, सेशन के खत्म होने का पता लगाने का तरीका बताया गया है:
Swift
for try await response in session.responses {
switch response.payload {
case .goingAwayNotice(let goingAwayNotice):
// Prepare for the session to close soon
if let timeLeft = goingAwayNotice.timeLeft {
print("Server going away in \(timeLeft) seconds")
}
}
}
Kotlin
for (response in session.responses) {
when (val message = response.payload) {
is LiveServerGoAway -> {
// Prepare for the session to close soon
val remaining = message.timeLeft
logger.info("Server going away in $remaining")
}
}
}
Java
session.getResponses().forEach(response -> {
if (response.getPayload() instanceof LiveServerResponse.GoingAwayNotice) {
LiveServerResponse.GoingAwayNotice notice = (LiveServerResponse.GoingAwayNotice) response.getPayload();
// Prepare for the session to close soon
Duration timeLeft = notice.getTimeLeft();
}
});
Web
for await (const message of session.receive()) {
switch (message.type) {
...
case "goingAwayNotice":
console.log("Server going away. Time left:", message.timeLeft);
break;
}
}
Dart
Future _handleLiveServerMessage(LiveServerResponse response) async {
final message = response.message;
if (message is GoingAwayNotice) {
// Prepare for the session to close soon
developer.log('Server going away. Time left: ${message.timeLeft}');
}
}
Unity
foreach (var response in session.Responses) {
if (response.Payload is LiveSessionGoingAway notice) {
// Prepare for the session to close soon
TimeSpan timeLeft = notice.TimeLeft;
Debug.Log($"Server going away notice received. Remaining: {timeLeft}");
}
}