ใน Live API เซสชันหมายถึงการเชื่อมต่อแบบถาวร ซึ่งมีการสตรีมอินพุตและเอาต์พุตอย่างต่อเนื่องผ่านการเชื่อมต่อเดียวกัน (อ่านเพิ่มเติมเกี่ยวกับวิธีการทำงาน) การออกแบบเซสชันที่ไม่เหมือนใครนี้ช่วยให้เกิดเวลาในการตอบสนองที่ต่ำและรองรับฟีเจอร์ที่ไม่เหมือนใคร แต่ ก็อาจทำให้เกิดความท้าทายต่างๆ เช่น ขีดจำกัดเวลาของเซสชันและการสิ้นสุดก่อนเวลา คู่มือนี้ครอบคลุมกลยุทธ์ในการเอาชนะความท้าทายด้านการจัดการเซสชัน ที่อาจเกิดขึ้นเมื่อใช้ Live API
อายุของเซสชัน
หากไม่มีการบีบอัด เซสชันเสียงอย่างเดียวจะจำกัดไว้ที่ 15 นาที และเซสชันเสียงและวิดีโอจะจำกัดไว้ที่ 2 นาที การเกินขีดจำกัดเหล่านี้จะทำให้เซสชันสิ้นสุดลง (และทำให้การเชื่อมต่อสิ้นสุดลงด้วย) แต่คุณสามารถใช้การบีบอัดหน้าต่างบริบทเพื่อขยายเซสชันได้โดยไม่จำกัดเวลา
อายุการใช้งานของการเชื่อมต่อก็ถูกจำกัดไว้ที่ประมาณ 10 นาทีเช่นกัน เมื่อการเชื่อมต่อสิ้นสุดลง เซสชันก็จะสิ้นสุดลงด้วย ในกรณีนี้ คุณสามารถ กำหนดค่าเซสชันเดียวให้ทำงานอยู่ตลอดการเชื่อมต่อหลายครั้งได้โดยใช้การกลับมาใช้เซสชันต่อ นอกจากนี้ คุณจะได้รับข้อความ GoAway ก่อนที่การเชื่อมต่อจะสิ้นสุดลง ซึ่งจะช่วยให้คุณดำเนินการเพิ่มเติมได้
การบีบอัดหน้าต่างบริบท
หากต้องการเปิดใช้เซสชันที่ยาวขึ้นและหลีกเลี่ยงการสิ้นสุดการเชื่อมต่ออย่างกะทันหัน คุณสามารถ เปิดใช้การบีบอัดหน้าต่างบริบทได้โดยการตั้งค่าฟิลด์ contextWindowCompression เป็นส่วนหนึ่งของการกำหนดค่าเซสชัน
ใน ContextWindowCompressionConfig คุณสามารถกำหนดค่า กลไกหน้าต่างเลื่อน และจำนวนโทเค็น ที่ทริกเกอร์การบีบอัดได้
Python
from google.genai import types
config = types.LiveConnectConfig(
    response_modalities=["AUDIO"],
    context_window_compression=(
        # Configures compression with default parameters.
        types.ContextWindowCompressionConfig(
            sliding_window=types.SlidingWindow(),
        )
    ),
)
JavaScript
const config = {
  responseModalities: [Modality.AUDIO],
  contextWindowCompression: { slidingWindow: {} }
};
การกลับมาใช้เซสชันต่อ
หากต้องการป้องกันไม่ให้เซสชันสิ้นสุดเมื่อเซิร์ฟเวอร์รีเซ็ตการเชื่อมต่อ WebSocket เป็นระยะ ให้กำหนดค่าฟิลด์ sessionResumption ภายในการกำหนดค่าการตั้งค่า
การส่งการกำหนดค่านี้จะทำให้เซิร์ฟเวอร์ส่งข้อความ SessionResumptionUpdate
 ซึ่งใช้เพื่อกลับมาใช้เซสชันต่อได้โดยการส่งโทเค็นการกลับมาใช้ต่อล่าสุดเป็น SessionResumptionConfig.handle
 ของการเชื่อมต่อครั้งถัดไป
โทเค็นการกลับมาทำงานต่อจะมีอายุ 2 ชั่วโมงหลังจากเซสชันสุดท้ายสิ้นสุดลง
Python
import asyncio
from google import genai
from google.genai import types
client = genai.Client()
model = "gemini-live-2.5-flash-preview"
async def main():
    print(f"Connecting to the service with handle {previous_session_handle}...")
    async with client.aio.live.connect(
        model=model,
        config=types.LiveConnectConfig(
            response_modalities=["AUDIO"],
            session_resumption=types.SessionResumptionConfig(
                # The handle of the session to resume is passed here,
                # or else None to start a new session.
                handle=previous_session_handle
            ),
        ),
    ) as session:
        while True:
            await session.send_client_content(
                turns=types.Content(
                    role="user", parts=[types.Part(text="Hello world!")]
                )
            )
            async for message in session.receive():
                # Periodically, the server will send update messages that may
                # contain a handle for the current state of the session.
                if message.session_resumption_update:
                    update = message.session_resumption_update
                    if update.resumable and update.new_handle:
                        # The handle should be retained and linked to the session.
                        return update.new_handle
                # For the purposes of this example, placeholder input is continually fed
                # to the model. In non-sample code, the model inputs would come from
                # the user.
                if message.server_content and message.server_content.turn_complete:
                    break
if __name__ == "__main__":
    asyncio.run(main())
JavaScript
import { GoogleGenAI, Modality } from '@google/genai';
const ai = new GoogleGenAI({});
const model = 'gemini-live-2.5-flash-preview';
async function live() {
  const responseQueue = [];
  async function waitMessage() {
    let done = false;
    let message = undefined;
    while (!done) {
      message = responseQueue.shift();
      if (message) {
        done = true;
      } else {
        await new Promise((resolve) => setTimeout(resolve, 100));
      }
    }
    return message;
  }
  async function handleTurn() {
    const turns = [];
    let done = false;
    while (!done) {
      const message = await waitMessage();
      turns.push(message);
      if (message.serverContent && message.serverContent.turnComplete) {
        done = true;
      }
    }
    return turns;
  }
console.debug('Connecting to the service with handle %s...', previousSessionHandle)
const session = await ai.live.connect({
  model: model,
  callbacks: {
    onopen: function () {
      console.debug('Opened');
    },
    onmessage: function (message) {
      responseQueue.push(message);
    },
    onerror: function (e) {
      console.debug('Error:', e.message);
    },
    onclose: function (e) {
      console.debug('Close:', e.reason);
    },
  },
  config: {
    responseModalities: [Modality.TEXT],
    sessionResumption: { handle: previousSessionHandle }
    // The handle of the session to resume is passed here, or else null to start a new session.
  }
});
const inputTurns = 'Hello how are you?';
session.sendClientContent({ turns: inputTurns });
const turns = await handleTurn();
for (const turn of turns) {
  if (turn.sessionResumptionUpdate) {
    if (turn.sessionResumptionUpdate.resumable && turn.sessionResumptionUpdate.newHandle) {
      let newHandle = turn.sessionResumptionUpdate.newHandle
      // ...Store newHandle and start new session with this handle here
    }
  }
}
  session.close();
}
async function main() {
  await live().catch((e) => console.error('got error', e));
}
main();
การรับข้อความก่อนที่เซสชันจะถูกตัดการเชื่อมต่อ
เซิร์ฟเวอร์จะส่งข้อความ GoAway ซึ่งส่งสัญญาณว่าการเชื่อมต่อปัจจุบันจะสิ้นสุดในเร็วๆ นี้ ข้อความนี้มี timeLeft ซึ่งระบุเวลาที่เหลืออยู่และช่วยให้คุณดำเนินการเพิ่มเติมได้ก่อนที่ระบบจะสิ้นสุดการเชื่อมต่อเป็น ABORTED
Python
async for response in session.receive():
    if response.go_away is not None:
        # The connection will soon be terminated
        print(response.go_away.time_left)
JavaScript
const turns = await handleTurn();
for (const turn of turns) {
  if (turn.goAway) {
    console.debug('Time left: %s\n', turn.goAway.timeLeft);
  }
}
รับข้อความเมื่อการสร้างเสร็จสมบูรณ์
เซิร์ฟเวอร์จะส่งข้อความ generationComplete ซึ่งเป็นสัญญาณว่าโมเดลสร้างคำตอบเสร็จแล้ว
Python
async for response in session.receive():
    if response.server_content.generation_complete is True:
        # The generation is complete
JavaScript
const turns = await handleTurn();
for (const turn of turns) {
  if (turn.serverContent && turn.serverContent.generationComplete) {
    // The generation is complete
  }
}
ขั้นตอนถัดไป
ดูวิธีอื่นๆ ในการทำงานกับ Live API ได้ในคู่มือความสามารถฉบับเต็ม หน้าการใช้เครื่องมือ หรือ สูตรการใช้ Live API