Gemini Live API מעבד זרמים רציפים של אודיו או טקסט שנקראים סשנים. אתם יכולים לנהל את מחזור החיים של הסשן, מהלחיצה הראשונית ועד לסיום תקין.
מגבלות על סשנים
במקרה של Live API, סשן הוא חיבור מתמשך שבו הקלט והפלט מועברים בסטרימינג באופן רציף דרך אותו חיבור.
אם הסשן חורג מכל אחת מהמגבלות הבאות, החיבור מסתיים.
אורך החיבור מוגבל לכ-10 דקות.
אורך הסשן תלוי במודאליות של הקלט:
- משך הזמן של סשנים עם קלט אודיו בלבד מוגבל ל-15 דקות.
- הקלט של וידאו ואודיו מוגבל ל-2 דקות.
חלון ההקשר של הסשן מוגבל ל-128,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 כספק ה-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}");
}
}