Gemini Live API xử lý các luồng âm thanh hoặc văn bản liên tục được gọi là phiên. Bạn có thể quản lý vòng đời của phiên, từ quá trình bắt tay ban đầu đến quá trình chấm dứt một cách suôn sẻ.
Giới hạn cho phiên
Đối với Live API, một phiên đề cập đến một kết nối liên tục, trong đó dữ liệu đầu vào và đầu ra được truyền trực tuyến liên tục qua cùng một kết nối.
Nếu phiên vượt quá bất kỳ giới hạn nào sau đây, kết nối sẽ bị chấm dứt.
Thời lượng kết nối bị giới hạn ở khoảng 10 phút.
Thời lượng phiên phụ thuộc vào phương thức nhập:
- Phiên chỉ nhập âm thanh bị giới hạn ở 15 phút.
- Phiên nhập video + âm thanh bị giới hạn ở 2 phút.
Cửa sổ ngữ cảnh của phiên bị giới hạn ở 128 nghìn token.
Bạn sẽ nhận được thông báo sắp kết thúc trước khi kết nối kết thúc, cho phép bạn thực hiện các hành động khác.
Bắt đầu phiên
Hãy truy cập vào hướng dẫn bắt đầu sử dụng Live API để xem đoạn mã đầy đủ cho biết cách bắt đầu phiên.
Cập nhật giữa phiên
Các mô hình Live API hỗ trợ các tính năng nâng cao sau đây cho bản cập nhật giữa phiên:
Cập nhật hướng dẫn hệ thống (chỉ dành cho Vertex AI Gemini API)
Thêm bản cập nhật nội dung gia tăng
Bạn có thể thêm bản cập nhật gia tăng trong phiên đang hoạt động. Hãy sử dụng tính năng này để gửi dữ liệu đầu vào văn bản, thiết lập ngữ cảnh phiên hoặc khôi phục ngữ cảnh phiên.
Đối với ngữ cảnh dài hơn, bạn nên cung cấp một bản tóm tắt thông báo duy nhất để giải phóng cửa sổ ngữ cảnh cho các lượt tương tác tiếp theo.
Đối với ngữ cảnh ngắn, bạn có thể gửi lượt tương tác theo lượt để biểu thị chính xác trình tự sự kiện, chẳng hạn như đoạn mã bên dưới.
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
);
Cập nhật hướng dẫn hệ thống giữa phiên
| Chỉ dùng được khi bạn sử dụng Vertex AI Gemini API làm nhà cung cấp API. |
Bạn có thể cập nhật hướng dẫn hệ thống trong phiên đang hoạt động. Hãy sử dụng tính năng này để điều chỉnh phản hồi của mô hình, chẳng hạn như thay đổi ngôn ngữ phản hồi hoặc sửa đổi giọng điệu.
Để cập nhật hướng dẫn hệ thống giữa phiên, bạn có thể gửi nội dung văn bản có vai trò system. Hướng dẫn hệ thống đã cập nhật sẽ vẫn có hiệu lực trong thời gian còn lại của phiên.
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}");
}
Phát hiện thời điểm phiên sắp kết thúc
Thông báo sắp kết thúc sẽ được gửi đến ứng dụng
Ví dụ sau đây cho biết cách phát hiện phiên sắp kết thúc bằng cách theo dõi thông báo sắp kết thúc:
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}");
}
}