Gemini Live API جریانهای پیوستهای از صدا یا متن به نام جلسات (session) را پردازش میکند. شما میتوانید چرخه حیات جلسات را از اولین دستدهی (handshake) تا پایان دادن به آن مدیریت کنید.
محدودیت برای جلسات
برای Live API ، یک جلسه به یک اتصال پایدار اشاره دارد که در آن ورودی و خروجی به طور مداوم از طریق همان اتصال پخش میشوند.
اگر جلسه از هر یک از محدودیتهای زیر فراتر رود، اتصال خاتمه مییابد.
مدت زمان اتصال به حدود 10 دقیقه محدود شده است.
طول جلسه به روشهای ورودی بستگی دارد:
- جلسات ورودی فقط صوتی به ۱۵ دقیقه محدود میشوند.
- ورودی ویدیو + صدا به ۲ دقیقه محدود میشود.
پنجرهی زمینهی جلسه به ۱۲۸ هزار توکن محدود شده است.
قبل از پایان اتصال، یک اعلان مبنی بر قطع شدن دریافت خواهید کرد که به شما امکان میدهد اقدامات بیشتری انجام دهید.
شروع یک جلسه
برای مشاهدهی قطعه کد کامل که نحوهی شروع یک جلسه را نشان میدهد، به راهنمای شروع به کار با Live API مراجعه کنید.
بهروزرسانی اواسط جلسه
مدلهای Live API از قابلیتهای پیشرفته زیر برای بهروزرسانیهای میاندورهای پشتیبانی میکنند:
دستورالعملهای بهروزرسانی سیستم (فقط برای API Vertex AI Gemini )
بهروزرسانیهای تدریجی محتوا را اضافه کنید
شما میتوانید در طول یک جلسه فعال، بهروزرسانیهای افزایشی اضافه کنید. از این برای ارسال ورودی متن، ایجاد زمینه جلسه یا بازیابی زمینه جلسه استفاده کنید.
برای متنهای طولانیتر، توصیه میکنیم یک خلاصه پیام واحد ارائه دهید تا پنجره متن برای تعاملات بعدی آزاد شود.
برای متنهای کوتاه، میتوانید تعاملات نوبت به نوبت را برای نمایش توالی دقیق رویدادها، مانند قطعه کد زیر، ارسال کنید.
سویفت
// 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,
);
وحدت
// 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 ارسال کنید. دستورالعملهای بهروز شده سیستم تا پایان جلسه معتبر خواهند بود.
سویفت
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');
}
وحدت
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}");
}
تشخیص زمان پایان یک جلسه
مثال زیر نحوه تشخیص قریبالوقوع خاتمه جلسه را با گوش دادن به اعلان خروج نشان میدهد:
سویفت
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}');
}
}
وحدت
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}");
}
}