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}");
}
}