นี่คือคำแนะนำที่ครอบคลุมความสามารถและการกำหนดค่า ที่ใช้ได้กับ Live API ดูหน้าเริ่มต้นใช้งาน Live API เพื่อดูภาพรวมและโค้ดตัวอย่างสำหรับ Use Case ที่พบบ่อย
ก่อนเริ่มต้น
- ทำความคุ้นเคยกับแนวคิดหลัก: หากยังไม่ได้ทำ โปรดอ่านหน้าเริ่มต้นใช้งาน Live API ก่อน ซึ่งจะแนะนำหลักการพื้นฐานของ Live API วิธีการทำงาน และแนวทางการใช้งานต่างๆ
- ลองใช้ Live API ใน AI Studio: คุณอาจพบว่าการลองใช้ Live API ใน Google AI Studio มีประโยชน์ก่อนที่จะเริ่มสร้าง หากต้องการใช้ Live API ใน Google AI Studio ให้เลือกสตรีม
การเปรียบเทียบรูปแบบ
ตารางต่อไปนี้จะสรุปความแตกต่างที่สำคัญระหว่างโมเดลตัวอย่างแบบเรียลไทม์ของ Gemini 3.1 Flash กับตัวอย่างแบบเรียลไทม์ของ Gemini 2.5 Flash
| ฟีเจอร์ | เวอร์ชันตัวอย่างของ Gemini 3.1 Flash Live | เวอร์ชันตัวอย่างของ Gemini 2.5 Flash |
|---|---|---|
| การคิด | ใช้ thinkingLevel เพื่อควบคุมความลึกในการคิดด้วยการตั้งค่าต่างๆ เช่น minimal, low, medium และ high ค่าเริ่มต้นคือ minimal เพื่อเพิ่มประสิทธิภาพให้มีเวลาในการตอบสนองต่ำที่สุด ดูระดับและงบประมาณที่ควรพิจารณา |
ใช้ thinkingBudget เพื่อตั้งค่าจำนวนโทเค็นการคิด ระบบจะเปิดใช้การคิดแบบไดนามิกโดยค่าเริ่มต้น ตั้งค่า thinkingBudget เป็น 0 เพื่อปิดใช้ ดูระดับและงบประมาณที่ควรพิจารณา |
| การรับคำตอบ | เหตุการณ์ฝั่งเซิร์ฟเวอร์เดียวอาจมีเนื้อหาหลายส่วนพร้อมกัน (เช่น inlineDataและข้อความถอดเสียง) ตรวจสอบว่าโค้ดประมวลผลทุกส่วนในแต่ละเหตุการณ์เพื่อไม่ให้พลาดเนื้อหา |
เหตุการณ์ฝั่งเซิร์ฟเวอร์แต่ละรายการจะมีชิ้นส่วนเนื้อหาเพียงรายการเดียว โดยระบบจะส่งมอบชิ้นส่วนในเหตุการณ์แยกกัน |
| เนื้อหาของลูกค้า | send_client_content รองรับเฉพาะการเริ่มต้นประวัติบริบทเริ่มต้น (ต้องตั้งค่า initial_history_in_client_content ในการกำหนดค่าเซสชัน) หากต้องการส่งข้อความอัปเดตระหว่างการสนทนา ให้ใช้ send_realtime_input แทน |
send_client_content รองรับตลอดการสนทนาสำหรับการส่งการอัปเดตเนื้อหาแบบเพิ่มทีละรายการและการสร้างบริบท |
| เปิดความครอบคลุม | ค่าเริ่มต้นคือ TURN_INCLUDES_AUDIO_ACTIVITY_AND_ALL_VIDEO เทิร์นของโมเดลประกอบด้วยกิจกรรมเสียงที่ตรวจพบและเฟรมวิดีโอทั้งหมด |
ค่าเริ่มต้นคือ TURN_INCLUDES_ONLY_ACTIVITY เทิร์นของโมเดลจะรวมเฉพาะกิจกรรมที่ตรวจพบ |
VAD ที่กำหนดเอง (activity_start/activity_end) |
รองรับ ปิดใช้ VAD อัตโนมัติและส่งข้อความ activityStart และ activityEnd ด้วยตนเองเพื่อควบคุมขอบเขตการพูด |
รองรับ ปิดใช้ VAD อัตโนมัติและส่งข้อความ activityStart และ activityEnd ด้วยตนเองเพื่อควบคุมขอบเขตการพูด |
| การกำหนดค่า VAD อัตโนมัติ | รองรับ กำหนดค่าพารามิเตอร์ เช่น start_of_speech_sensitivity, end_of_speech_sensitivity, prefix_padding_ms และ silence_duration_ms |
รองรับ กำหนดค่าพารามิเตอร์ เช่น start_of_speech_sensitivity, end_of_speech_sensitivity, prefix_padding_ms และ silence_duration_ms |
การเรียกใช้ฟังก์ชันแบบไม่พร้อมกัน (behavior: NON_BLOCKING) |
ไม่รองรับ การเรียกใช้ฟังก์ชันจะทำได้ตามลำดับเท่านั้น โมเดลจะไม่เริ่มตอบจนกว่าคุณจะส่งการตอบกลับของเครื่องมือ | รองรับ ตั้งค่า behavior เป็น NON_BLOCKING ในการประกาศฟังก์ชันเพื่อให้โมเดลโต้ตอบต่อไปได้ขณะที่ฟังก์ชันทำงาน ควบคุมวิธีที่โมเดลจัดการการตอบกลับด้วยพารามิเตอร์ scheduling (INTERRUPT, WHEN_IDLE หรือ SILENT) |
| เสียงแบบเชิงรุก | สิ่งที่ทำไม่ได้ | รองรับ เมื่อเปิดใช้ โมเดลจะตัดสินใจไม่ตอบสนองได้โดยอัตโนมัติหากเนื้อหาอินพุตไม่เกี่ยวข้อง ตั้งค่า proactive_audio เป็น true ในการกำหนดค่า proactivity (ต้องใช้ v1alpha) |
| การโต้ตอบที่สะท้อนถึงความรู้สึก | สิ่งที่ทำไม่ได้ | รองรับ โมเดลจะปรับรูปแบบการตอบกลับให้ตรงกับการแสดงออกและน้ำเสียงของอินพุต ตั้งค่า enable_affective_dialog เป็น true ในการกำหนดค่าเซสชัน (ต้องใช้ v1alpha) |
หากต้องการย้ายข้อมูลจาก Gemini 2.5 Flash Live ไปยัง Gemini 3.1 Flash Live โปรดดูคำแนะนำในการย้ายข้อมูล
สร้างการเชื่อมต่อ
ตัวอย่างต่อไปนี้แสดงวิธีสร้างการเชื่อมต่อด้วยคีย์ API
Python
import asyncio
from google import genai
client = genai.Client()
model = "gemini-3.1-flash-live-preview"
config = {"response_modalities": ["AUDIO"]}
async def main():
async with client.aio.live.connect(model=model, config=config) as session:
print("Session started")
# Send content...
if __name__ == "__main__":
asyncio.run(main())
JavaScript
import { GoogleGenAI, Modality } from '@google/genai';
const ai = new GoogleGenAI({});
const model = 'gemini-3.1-flash-live-preview';
const config = { responseModalities: [Modality.AUDIO] };
async function main() {
const session = await ai.live.connect({
model: model,
callbacks: {
onopen: function () {
console.debug('Opened');
},
onmessage: function (message) {
console.debug(message);
},
onerror: function (e) {
console.debug('Error:', e.message);
},
onclose: function (e) {
console.debug('Close:', e.reason);
},
},
config: config,
});
console.debug("Session started");
// Send content...
session.close();
}
main();
รูปแบบการโต้ตอบ
ส่วนต่อไปนี้มีตัวอย่างและบริบทที่สนับสนุนสำหรับรูปแบบอินพุตและเอาต์พุตต่างๆ ที่มีใน Live API
การส่งเสียง
ต้องส่งเสียงเป็นข้อมูล PCM ดิบ (เสียง PCM ดิบ 16 บิต, 16 kHz, little-endian)
Python
# Assuming 'chunk' is your raw PCM audio bytes
await session.send_realtime_input(
audio=types.Blob(
data=chunk,
mime_type="audio/pcm;rate=16000"
)
)
JavaScript
// Assuming 'chunk' is a Buffer of raw PCM audio
session.sendRealtimeInput({
audio: {
data: chunk.toString('base64'),
mimeType: 'audio/pcm;rate=16000'
}
});
รูปแบบเสียง
ข้อมูลเสียงใน Live API จะเป็นแบบดิบ, Little-Endian,
PCM 16 บิต เสมอ เอาต์พุตเสียงจะใช้อัตราการสุ่มตัวอย่าง 24kHz เสมอ เสียงอินพุต
มีค่า 16kHz โดยค่าเริ่มต้น แต่ Live API จะทำการสุ่มตัวอย่างใหม่หากจำเป็น
จึงส่งอัตราการสุ่มตัวอย่างใดก็ได้ หากต้องการระบุอัตราการสุ่มตัวอย่างของเสียงที่ป้อน ให้ตั้งค่า
ประเภท MIME ของ Blob ที่มีเสียงแต่ละรายการเป็นค่า
เช่น audio/pcm;rate=16000
การรับเสียง
ระบบจะรับคำตอบเสียงของโมเดลเป็นกลุ่มข้อมูล
Python
async for response in session.receive():
if response.server_content and response.server_content.model_turn:
for part in response.server_content.model_turn.parts:
if part.inline_data:
audio_data = part.inline_data.data
# Process or play the audio data
JavaScript
// Inside the onmessage callback
const content = response.serverContent;
if (content?.modelTurn?.parts) {
for (const part of content.modelTurn.parts) {
if (part.inlineData) {
const audioData = part.inlineData.data;
// Process or play audioData (base64 encoded string)
}
}
}
กำลังส่งข้อความ
คุณส่งข้อความได้โดยใช้ send_realtime_input (Python) หรือ sendRealtimeInput (JavaScript)
Python
await session.send_realtime_input(text="Hello, how are you?")
JavaScript
session.sendRealtimeInput({
text: 'Hello, how are you?'
});
กำลังส่งวิดีโอ
ระบบจะส่งเฟรมวิดีโอเป็นรูปภาพแต่ละรูป (เช่น JPEG หรือ PNG) ที่อัตราเฟรมที่เฉพาะเจาะจง (สูงสุด 1 เฟรมต่อวินาที)
Python
# Assuming 'frame' is your JPEG-encoded image bytes
await session.send_realtime_input(
video=types.Blob(
data=frame,
mime_type="image/jpeg"
)
)
JavaScript
// Assuming 'frame' is a Buffer of JPEG-encoded image data
session.sendRealtimeInput({
video: {
data: frame.toString('base64'),
mimeType: 'image/jpeg'
}
});
การอัปเดตเนื้อหาแบบเพิ่มทีละรายการ
ใช้การอัปเดตแบบเพิ่มทีละรายการเพื่อส่งอินพุตข้อความ สร้างบริบทของเซสชัน หรือ กู้คืนบริบทของเซสชัน สำหรับบริบทสั้นๆ คุณสามารถส่งการโต้ตอบแบบเลี้ยวต่อเลี้ยว เพื่อแสดงลำดับเหตุการณ์ที่แน่นอนได้โดยทำดังนี้
Python
turns = [
{"role": "user", "parts": [{"text": "What is the capital of France?"}]},
{"role": "model", "parts": [{"text": "Paris"}]},
]
await session.send_client_content(turns=turns, turn_complete=False)
turns = [{"role": "user", "parts": [{"text": "What is the capital of Germany?"}]}]
await session.send_client_content(turns=turns, turn_complete=True)
JavaScript
let inputTurns = [
{ "role": "user", "parts": [{ "text": "What is the capital of France?" }] },
{ "role": "model", "parts": [{ "text": "Paris" }] },
]
session.sendClientContent({ turns: inputTurns, turnComplete: false })
inputTurns = [{ "role": "user", "parts": [{ "text": "What is the capital of Germany?" }] }]
session.sendClientContent({ turns: inputTurns, turnComplete: true })
สำหรับบริบทที่ยาวขึ้น เราขอแนะนำให้สรุปข้อความเดียวเพื่อเพิ่ม พื้นที่ในหน้าต่างบริบทสำหรับการโต้ตอบในภายหลัง ดูอีกวิธีในการโหลดบริบทของเซสชันได้ที่การกลับมาใช้เซสชันต่อ
การถอดเสียงเป็นคำ
นอกจากคำตอบของโมเดลแล้ว คุณยังรับข้อความถอดเสียงของทั้งเอาต์พุตเสียงและอินพุตเสียงได้ด้วย
หากต้องการเปิดใช้การถอดเสียงเอาต์พุตเสียงของโมเดล ให้ส่ง
output_audio_transcription ในการกำหนดค่าการตั้งค่า ระบบจะอนุมานภาษาในการถอดเสียงเป็นคำจากคำตอบของโมเดล
Python
import asyncio
from google import genai
from google.genai import types
client = genai.Client()
model = "gemini-3.1-flash-live-preview"
config = {
"response_modalities": ["AUDIO"],
"output_audio_transcription": {}
}
async def main():
async with client.aio.live.connect(model=model, config=config) as session:
message = "Hello? Gemini are you there?"
await session.send_client_content(
turns={"role": "user", "parts": [{"text": message}]}, turn_complete=True
)
async for response in session.receive():
if response.server_content.model_turn:
print("Model turn:", response.server_content.model_turn)
if response.server_content.output_transcription:
print("Transcript:", response.server_content.output_transcription.text)
if __name__ == "__main__":
asyncio.run(main())
JavaScript
import { GoogleGenAI, Modality } from '@google/genai';
const ai = new GoogleGenAI({});
const model = 'gemini-3.1-flash-live-preview';
const config = {
responseModalities: [Modality.AUDIO],
outputAudioTranscription: {}
};
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;
}
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: config,
});
const inputTurns = 'Hello how are you?';
session.sendClientContent({ turns: inputTurns });
const turns = await handleTurn();
for (const turn of turns) {
if (turn.serverContent && turn.serverContent.outputTranscription) {
console.debug('Received output transcription: %s\n', turn.serverContent.outputTranscription.text);
}
}
session.close();
}
async function main() {
await live().catch((e) => console.error('got error', e));
}
main();
หากต้องการเปิดใช้การถอดเสียงเป็นคำของอินพุตเสียงของโมเดล ให้ส่ง
input_audio_transcription ในการกำหนดค่าการตั้งค่า
Python
import asyncio
from pathlib import Path
from google import genai
from google.genai import types
client = genai.Client()
model = "gemini-3.1-flash-live-preview"
config = {
"response_modalities": ["AUDIO"],
"input_audio_transcription": {},
}
async def main():
async with client.aio.live.connect(model=model, config=config) as session:
audio_data = Path("16000.pcm").read_bytes()
await session.send_realtime_input(
audio=types.Blob(data=audio_data, mime_type='audio/pcm;rate=16000')
)
async for msg in session.receive():
if msg.server_content.input_transcription:
print('Transcript:', msg.server_content.input_transcription.text)
if __name__ == "__main__":
asyncio.run(main())
JavaScript
import { GoogleGenAI, Modality } from '@google/genai';
import * as fs from "node:fs";
import pkg from 'wavefile';
const { WaveFile } = pkg;
const ai = new GoogleGenAI({});
const model = 'gemini-3.1-flash-live-preview';
const config = {
responseModalities: [Modality.AUDIO],
inputAudioTranscription: {}
};
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;
}
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: config,
});
// Send Audio Chunk
const fileBuffer = fs.readFileSync("16000.wav");
// Ensure audio conforms to API requirements (16-bit PCM, 16kHz, mono)
const wav = new WaveFile();
wav.fromBuffer(fileBuffer);
wav.toSampleRate(16000);
wav.toBitDepth("16");
const base64Audio = wav.toBase64();
// If already in correct format, you can use this:
// const fileBuffer = fs.readFileSync("sample.pcm");
// const base64Audio = Buffer.from(fileBuffer).toString('base64');
session.sendRealtimeInput(
{
audio: {
data: base64Audio,
mimeType: "audio/pcm;rate=16000"
}
}
);
const turns = await handleTurn();
for (const turn of turns) {
if (turn.text) {
console.debug('Received text: %s\n', turn.text);
}
else if (turn.data) {
console.debug('Received inline data: %s\n', turn.data);
}
else if (turn.serverContent && turn.serverContent.inputTranscription) {
console.debug('Received input transcription: %s\n', turn.serverContent.inputTranscription.text);
}
}
session.close();
}
async function main() {
await live().catch((e) => console.error('got error', e));
}
main();
เปลี่ยนเสียงและภาษา
โมเดลเอาต์พุตเสียงเนทีฟรองรับเสียงใดก็ได้ ที่พร้อมใช้งานสำหรับโมเดลการอ่านออกเสียงข้อความ (TTS) คุณฟังเสียงทั้งหมดได้ใน AI Studio
หากต้องการระบุเสียง ให้ตั้งชื่อเสียงภายในออบเจ็กต์ speechConfig เป็นส่วนหนึ่ง
ของการกำหนดค่าเซสชัน
Python
config = {
"response_modalities": ["AUDIO"],
"speech_config": {
"voice_config": {"prebuilt_voice_config": {"voice_name": "Kore"}}
},
}
JavaScript
const config = {
responseModalities: [Modality.AUDIO],
speechConfig: { voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } } }
};
Live API รองรับหลายภาษา โมเดลเอาต์พุตเสียงดั้งเดิมจะเลือกภาษาที่เหมาะสมโดยอัตโนมัติ และไม่รองรับการตั้งค่ารหัสภาษาอย่างชัดเจน
ความสามารถด้านเสียงแบบเนทีฟ
โมเดลล่าสุดของเรามีเอาต์พุตเสียงในตัว ซึ่งให้เสียงพูดที่เป็นธรรมชาติและสมจริง รวมถึงประสิทธิภาพแบบหลายภาษาที่ดียิ่งขึ้น
กำลังคิด
โมเดล Gemini 3.1 ใช้ thinkingLevel เพื่อควบคุมความลึกในการคิด โดยมีการตั้งค่า
เช่น minimal, low, medium และ high ค่าเริ่มต้นคือ minimal เพื่อ
เพิ่มประสิทธิภาพให้มีเวลาในการตอบสนองต่ำที่สุด โมเดล Gemini 2.5 ใช้
thinkingBudget เพื่อตั้งค่าจำนวนโทเค็นการคิดแทน ดูรายละเอียดเพิ่มเติม
เกี่ยวกับระดับเทียบกับงบประมาณได้ที่
การพิจารณาระดับและงบประมาณ
Python
model = "gemini-3.1-flash-live-preview"
config = types.LiveConnectConfig(
response_modalities=["AUDIO"]
thinking_config=types.ThinkingConfig(
thinking_level="low",
)
)
async with client.aio.live.connect(model=model, config=config) as session:
# Send audio input and receive audio
JavaScript
const model = 'gemini-3.1-flash-live-preview';
const config = {
responseModalities: [Modality.AUDIO],
thinkingConfig: {
thinkingLevel: 'low',
},
};
async function main() {
const session = await ai.live.connect({
model: model,
config: config,
callbacks: ...,
});
// Send audio input and receive audio
session.close();
}
main();
นอกจากนี้ คุณยังเปิดใช้สรุปความคิดได้โดยตั้งค่า includeThoughts เป็น
true ในการกำหนดค่า ดูข้อมูลเพิ่มเติมได้ที่สรุปความคิด
Python
model = "gemini-3.1-flash-live-preview"
config = types.LiveConnectConfig(
response_modalities=["AUDIO"]
thinking_config=types.ThinkingConfig(
thinking_level="low",
include_thoughts=True
)
)
JavaScript
const model = 'gemini-3.1-flash-live-preview';
const config = {
responseModalities: [Modality.AUDIO],
thinkingConfig: {
thinkingLevel: 'low',
includeThoughts: true,
},
};
การโต้ตอบที่สะท้อนถึงความรู้สึก
ฟีเจอร์นี้ช่วยให้ Gemini ปรับรูปแบบการตอบกลับให้เข้ากับรูปแบบการป้อนข้อมูลและโทนเสียงได้
หากต้องการใช้การโต้ตอบที่สะท้อนถึงความรู้สึก ให้ตั้งค่าเวอร์ชัน API เป็น v1alpha และตั้งค่า
enable_affective_dialog เป็น true ในข้อความการตั้งค่า
Python
client = genai.Client(http_options={"api_version": "v1alpha"})
config = types.LiveConnectConfig(
response_modalities=["AUDIO"],
enable_affective_dialog=True
)
JavaScript
const ai = new GoogleGenAI({ httpOptions: {"apiVersion": "v1alpha"} });
const config = {
responseModalities: [Modality.AUDIO],
enableAffectiveDialog: true
};
เสียงเชิงรุก
เมื่อเปิดใช้ฟีเจอร์นี้ Gemini จะตัดสินใจไม่ตอบได้ หากเนื้อหาไม่เกี่ยวข้อง
หากต้องการใช้ ให้ตั้งค่าเวอร์ชัน API เป็น v1alpha และกำหนดค่าฟิลด์ proactivity
ในข้อความการตั้งค่า แล้วตั้งค่า proactive_audio เป็น true ดังนี้
Python
client = genai.Client(http_options={"api_version": "v1alpha"})
config = types.LiveConnectConfig(
response_modalities=["AUDIO"],
proactivity={'proactive_audio': True}
)
JavaScript
const ai = new GoogleGenAI({ httpOptions: {"apiVersion": "v1alpha"} });
const config = {
responseModalities: [Modality.AUDIO],
proactivity: { proactiveAudio: true }
}
การตรวจจับกิจกรรมเสียง (VAD)
การตรวจจับกิจกรรมเสียง (VAD) ช่วยให้โมเดลจดจำได้เมื่อมีคน พูด ซึ่งเป็นสิ่งสำคัญในการสร้างบทสนทนาที่เป็นธรรมชาติ เนื่องจากช่วยให้ผู้ใช้ขัดจังหวะโมเดลได้ทุกเมื่อ
เมื่อ VAD ตรวจพบการขัดจังหวะ ระบบจะยกเลิกและทิ้งการสร้างที่กำลังดำเนินการอยู่ ระบบจะเก็บเฉพาะข้อมูลที่ส่งไปยังไคลเอ็นต์แล้วไว้ใน
ประวัติเซสชัน จากนั้นเซิร์ฟเวอร์จะส่งข้อความ BidiGenerateContentServerContent เพื่อรายงานการหยุดชะงัก
จากนั้นเซิร์ฟเวอร์ Gemini จะทิ้งการเรียกใช้ฟังก์ชันที่รอดำเนินการและส่งBidiGenerateContentServerContentข้อความพร้อมรหัสของการเรียกที่ยกเลิก
Python
async for response in session.receive():
if response.server_content.interrupted is True:
# The generation was interrupted
# If realtime playback is implemented in your application,
# you should stop playing audio and clear queued playback here.
JavaScript
const turns = await handleTurn();
for (const turn of turns) {
if (turn.serverContent && turn.serverContent.interrupted) {
// The generation was interrupted
// If realtime playback is implemented in your application,
// you should stop playing audio and clear queued playback here.
}
}
VAD อัตโนมัติ
โดยค่าเริ่มต้น โมเดลจะดำเนินการ VAD โดยอัตโนมัติใน
สตรีมอินพุตเสียงอย่างต่อเนื่อง คุณกำหนดค่า VAD ได้ด้วยฟิลด์
realtimeInputConfig.automaticActivityDetection
ของการกำหนดค่าการตั้งค่า
เมื่อหยุดสตรีมเสียงชั่วคราวเป็นเวลานานกว่า 1 วินาที (เช่น เนื่องจากผู้ใช้ปิดไมโครโฟน) ควรส่งเหตุการณ์
audioStreamEnd
เพื่อล้างเสียงที่แคชไว้ ไคลเอ็นต์สามารถกลับมาส่งข้อมูลเสียงได้ทุกเมื่อ
Python
# example audio file to try:
# URL = "https://storage.googleapis.com/generativeai-downloads/data/hello_are_you_there.pcm"
# !wget -q $URL -O sample.pcm
import asyncio
from pathlib import Path
from google import genai
from google.genai import types
client = genai.Client()
model = "gemini-3.1-flash-live-preview"
config = {"response_modalities": ["AUDIO"]}
async def main():
async with client.aio.live.connect(model=model, config=config) as session:
audio_bytes = Path("sample.pcm").read_bytes()
await session.send_realtime_input(
audio=types.Blob(data=audio_bytes, mime_type="audio/pcm;rate=16000")
)
# if stream gets paused, send:
# await session.send_realtime_input(audio_stream_end=True)
async for response in session.receive():
if response.text is not None:
print(response.text)
if __name__ == "__main__":
asyncio.run(main())
JavaScript
// example audio file to try:
// URL = "https://storage.googleapis.com/generativeai-downloads/data/hello_are_you_there.pcm"
// !wget -q $URL -O sample.pcm
import { GoogleGenAI, Modality } from '@google/genai';
import * as fs from "node:fs";
const ai = new GoogleGenAI({});
const model = 'gemini-3.1-flash-live-preview';
const config = { responseModalities: [Modality.AUDIO] };
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;
}
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: config,
});
// Send Audio Chunk
const fileBuffer = fs.readFileSync("sample.pcm");
const base64Audio = Buffer.from(fileBuffer).toString('base64');
session.sendRealtimeInput(
{
audio: {
data: base64Audio,
mimeType: "audio/pcm;rate=16000"
}
}
);
// if stream gets paused, send:
// session.sendRealtimeInput({ audioStreamEnd: true })
const turns = await handleTurn();
for (const turn of turns) {
if (turn.text) {
console.debug('Received text: %s\n', turn.text);
}
else if (turn.data) {
console.debug('Received inline data: %s\n', turn.data);
}
}
session.close();
}
async function main() {
await live().catch((e) => console.error('got error', e));
}
main();
เมื่อใช้ send_realtime_input API จะตอบกลับเสียงโดยอัตโนมัติตาม VAD
แม้ว่า send_client_content จะเพิ่มข้อความลงในบริบทของโมเดลตามลำดับ แต่ send_realtime_input ได้รับการเพิ่มประสิทธิภาพเพื่อการตอบสนองโดยแลกกับการเรียงลำดับที่แน่นอน
การกำหนดค่า VAD อัตโนมัติ
หากต้องการควบคุมกิจกรรม VAD เพิ่มเติม คุณสามารถกำหนดค่าพารามิเตอร์ต่อไปนี้ได้ ดูข้อมูลเพิ่มเติมได้ที่เอกสารอ้างอิง API
Python
from google.genai import types
config = {
"response_modalities": ["AUDIO"],
"realtime_input_config": {
"automatic_activity_detection": {
"disabled": False, # default
"start_of_speech_sensitivity": types.StartSensitivity.START_SENSITIVITY_LOW,
"end_of_speech_sensitivity": types.EndSensitivity.END_SENSITIVITY_LOW,
"prefix_padding_ms": 20,
"silence_duration_ms": 100,
}
}
}
JavaScript
import { GoogleGenAI, Modality, StartSensitivity, EndSensitivity } from '@google/genai';
const config = {
responseModalities: [Modality.AUDIO],
realtimeInputConfig: {
automaticActivityDetection: {
disabled: false, // default
startOfSpeechSensitivity: StartSensitivity.START_SENSITIVITY_LOW,
endOfSpeechSensitivity: EndSensitivity.END_SENSITIVITY_LOW,
prefixPaddingMs: 20,
silenceDurationMs: 100,
}
}
};
ปิดใช้ VAD อัตโนมัติ
หรือจะปิดใช้ VAD อัตโนมัติก็ได้โดยตั้งค่า
realtimeInputConfig.automaticActivityDetection.disabled เป็น true ในข้อความการตั้งค่า
ในการกำหนดค่านี้ ไคลเอ็นต์มีหน้าที่ตรวจหาคำพูดของผู้ใช้และส่งข้อความ activityStart
และ activityEnd
ในเวลาที่เหมาะสม ระบบจะไม่ส่ง audioStreamEnd ในการกำหนดค่านี้ แต่จะมีการทำเครื่องหมายการหยุดชะงักของสตรีมด้วยactivityEndข้อความแทน
Python
config = {
"response_modalities": ["AUDIO"],
"realtime_input_config": {"automatic_activity_detection": {"disabled": True}},
}
async with client.aio.live.connect(model=model, config=config) as session:
# ...
await session.send_realtime_input(activity_start=types.ActivityStart())
await session.send_realtime_input(
audio=types.Blob(data=audio_bytes, mime_type="audio/pcm;rate=16000")
)
await session.send_realtime_input(activity_end=types.ActivityEnd())
# ...
JavaScript
const config = {
responseModalities: [Modality.AUDIO],
realtimeInputConfig: {
automaticActivityDetection: {
disabled: true,
}
}
};
session.sendRealtimeInput({ activityStart: {} })
session.sendRealtimeInput(
{
audio: {
data: base64Audio,
mimeType: "audio/pcm;rate=16000"
}
}
);
session.sendRealtimeInput({ activityEnd: {} })
จำนวนโทเค็น
คุณดูจำนวนโทเค็นที่ใช้ทั้งหมดได้ในฟิลด์ usageMetadata ของข้อความเซิร์ฟเวอร์ที่ส่งคืน
Python
async for message in session.receive():
# The server will periodically send messages that include UsageMetadata.
if message.usage_metadata:
usage = message.usage_metadata
print(
f"Used {usage.total_token_count} tokens in total. Response token breakdown:"
)
for detail in usage.response_tokens_details:
match detail:
case types.ModalityTokenCount(modality=modality, token_count=count):
print(f"{modality}: {count}")
JavaScript
const turns = await handleTurn();
for (const turn of turns) {
if (turn.usageMetadata) {
console.debug('Used %s tokens in total. Response token breakdown:\n', turn.usageMetadata.totalTokenCount);
for (const detail of turn.usageMetadata.responseTokensDetails) {
console.debug('%s\n', detail);
}
}
}
ความละเอียดของสื่อ
คุณระบุความละเอียดของสื่อสำหรับสื่ออินพุตได้โดยตั้งค่าฟิลด์
mediaResolution เป็นส่วนหนึ่งของการกำหนดค่าเซสชัน ดังนี้
Python
from google.genai import types
config = {
"response_modalities": ["AUDIO"],
"media_resolution": types.MediaResolution.MEDIA_RESOLUTION_LOW,
}
JavaScript
import { GoogleGenAI, Modality, MediaResolution } from '@google/genai';
const config = {
responseModalities: [Modality.AUDIO],
mediaResolution: MediaResolution.MEDIA_RESOLUTION_LOW,
};
ข้อจำกัด
โปรดคำนึงถึงข้อจำกัดต่อไปนี้ของ Live API เมื่อวางแผนโปรเจ็กต์
รูปแบบการตอบกลับ
โมเดลเสียงดั้งเดิมรองรับเฉพาะ `AUDIO` response modality เท่านั้น หากต้องการคำตอบของโมเดลเป็นข้อความ ให้ใช้ฟีเจอร์การถอดเสียงเป็นคำจากเอาต์พุตเสียง
การตรวจสอบสิทธิ์ไคลเอ็นต์
โดยค่าเริ่มต้น Live API จะให้การตรวจสอบสิทธิ์แบบเซิร์ฟเวอร์ต่อเซิร์ฟเวอร์เท่านั้น หากคุณใช้แอปพลิเคชัน Live API โดยใช้แนวทางไคลเอ็นต์ต่อเซิร์ฟเวอร์ คุณต้องใช้โทเค็นชั่วคราวเพื่อลดความเสี่ยงด้านความปลอดภัย
ระยะเวลาเซสชัน
เซสชันเสียงอย่างเดียวจะจำกัดไว้ที่ 15 นาที และเซสชันเสียงและวิดีโอจะจำกัดไว้ที่ 2 นาที อย่างไรก็ตาม คุณสามารถกำหนดค่าเทคนิคการจัดการเซสชันที่แตกต่างกันเพื่อขยายระยะเวลาเซสชันได้ไม่จำกัด
หน้าต่างบริบท
เซสชันมีขีดจํากัดหน้าต่างบริบทดังนี้
- โทเค็น 128,000 รายการสำหรับโมเดลเอาต์พุตเสียงดั้งเดิม
- โทเค็น 32,000 รายการสำหรับโมเดล Live API อื่นๆ
ภาษาที่รองรับ
Live API รองรับ 97 ภาษาต่อไปนี้
| ภาษา | รหัส BCP-47 | ภาษา | รหัส BCP-47 |
|---|---|---|---|
| แอฟริคานส์ | af |
ลัตเวีย | lv |
| อะคัน | ak |
ลิทัวเนีย | lt |
| แอลเบเนีย | sq |
มาซีโดเนีย | mk |
| อัมฮาริก | am |
มาเลย์ | ms |
| อาหรับ | ar |
มาลายาลัม | ml |
| อาร์เมเนีย | hy |
มอลตา | mt |
| อัสสัม | as |
เมารี | mi |
| อาร์เซอร์ไบจัน | az |
มราฐี | mr |
| บาสก์ | eu |
มองโกเลีย | mn |
| เบลารุส | be |
เนปาล | ne |
| เบงกอล | bn |
นอร์เวย์ | no |
| บอสเนีย | bs |
โอเดีย | or |
| บัลแกเรีย | bg |
โอโรโม | om |
| พม่า | my |
พาชตู | ps |
| คาตาลัน | ca |
เปอร์เซีย | fa |
| เซบู | ceb |
โปแลนด์ | pl |
| จีน | zh |
โปรตุเกส | pt |
| โครเอเชีย | hr |
ปัญจาบ | pa |
| เช็ก | cs |
เคชัว | qu |
| เดนมาร์ก | da |
โรมาเนีย | ro |
| ดัตช์ | nl |
โรมานช์ | rm |
| อังกฤษ | en |
รัสเซีย | ru |
| เอสโตเนีย | et |
เซอร์เบีย | sr |
| แฟโร | fo |
สินธี | sd |
| ฟิลิปปินส์ | fil |
สิงหล | si |
| ฟินแลนด์ | fi |
สโลวัก | sk |
| ฝรั่งเศส | fr |
สโลวีเนีย | sl |
| กาลิเชียน | gl |
โซมาลี | so |
| จอร์เจีย | ka |
โซโทใต้ | st |
| เยอรมัน | de |
สเปน | es |
| กรีก | el |
สวาฮิลี | sw |
| คุชราต | gu |
สวีเดน | sv |
| เฮาซา | ha |
ทาจิก | tg |
| ฮีบรู | iw |
ทมิฬ | ta |
| ฮินดี | hi |
เตลูกู | te |
| ฮังการี | hu |
ไทย | th |
| ไอซ์แลนด์ | is |
ซวานา | tn |
| อินโดนีเซีย | id |
ตุรกี | tr |
| ไอริช | ga |
เติร์กเมน | tk |
| อิตาลี | it |
ยูเครน | uk |
| ญี่ปุ่น | ja |
อูรดู | ur |
| กันนาดา | kn |
อุซเบก | uz |
| คาซัค | kk |
เวียดนาม | vi |
| เขมร | km |
เวลส์ | cy |
| คินยารวันดา | rw |
ฟริเซียนตะวันตก | fy |
| เกาหลี | ko |
วูลอฟ | wo |
| เคิร์ด | ku |
โยรูบา | yo |
| คีร์กิซ | ky |
ซูลู | zu |
| ภาษาลาว | lo |
ขั้นตอนถัดไป
- อ่านคำแนะนำเกี่ยวกับการใช้เครื่องมือและ การจัดการเซสชันเพื่อดูข้อมูลสำคัญ เกี่ยวกับการใช้ Live API อย่างมีประสิทธิภาพ
- ลองใช้ Live API ใน Google AI Studio
- ดูข้อมูลเพิ่มเติมเกี่ยวกับโมเดล Live API ได้ที่เสียงเนทีฟของ Gemini 2.5 Flash ในหน้าโมเดล
- ลองดูตัวอย่างเพิ่มเติมในตำรา Live API ตำราเครื่องมือ Live API และสคริปต์เริ่มต้นใช้งาน Live API