Gemini Live API memproses aliran audio atau teks berkelanjutan yang disebut sesi. Anda dapat mengelola siklus proses sesi, mulai dari handshake awal hingga penghentian yang lancar.
Batas untuk sesi
Untuk Live API, sesi mengacu pada koneksi persisten tempat input dan output di-streaming secara berkelanjutan melalui koneksi yang sama.
Jika sesi melebihi salah satu batas berikut, koneksi akan dihentikan.
Panjang koneksi dibatasi hingga sekitar 10 menit.
Panjang sesi bergantung pada modalitas input:
- Sesi input hanya audio dibatasi hingga 15 menit.
- Input video + audio dibatasi hingga 2 menit.
Jendela konteks sesi dibatasi hingga 128 ribu token.
Anda akan menerima notifikasi going away sebelum koneksi berakhir, sehingga Anda dapat mengambil tindakan lebih lanjut.
Memulai sesi
Lihat panduan memulai untuk Live API guna melihat cuplikan lengkap yang menunjukkan cara memulai sesi.
Memperbarui di tengah sesi
Model Live API mendukung kemampuan lanjutan berikut untuk pembaruan di tengah sesi:
Memperbarui petunjuk sistem (untuk Vertex AI Gemini API saja)
Menambahkan pembaruan konten inkremental
Anda dapat menambahkan pembaruan inkremental selama sesi aktif. Gunakan fitur ini untuk mengirim input teks, menetapkan konteks sesi, atau memulihkan konteks sesi.
Untuk konteks yang lebih panjang, sebaiknya berikan ringkasan pesan tunggal untuk mengosongkan jendela konteks untuk interaksi berikutnya.
Untuk konteks singkat, Anda dapat mengirim interaksi langkah demi langkah untuk merepresentasikan urutan peristiwa yang tepat, seperti cuplikan di bawah.
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
);
Memperbarui petunjuk sistem di tengah sesi
| Hanya tersedia saat menggunakan Vertex AI Gemini API sebagai penyedia API Anda. |
Anda dapat memperbarui petunjuk sistem selama sesi aktif. Gunakan fitur ini untuk menyesuaikan respons model, misalnya mengubah bahasa respons atau mengubah nada.
Untuk memperbarui petunjuk sistem di tengah sesi, Anda dapat mengirim konten teks dengan peran system. Petunjuk sistem yang diperbarui akan tetap berlaku selama sisa sesi.
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}");
}
Mendeteksi kapan sesi akan berakhir
Notifikasi going away akan dikirim ke klien
Contoh berikut menunjukkan cara mendeteksi penghentian sesi yang akan terjadi dengan memproses notifikasi 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}");
}
}