Model yang didukung Live API dilengkapi dengan kemampuan bawaan untuk menggunakan alat berikut:
- Pemanggilan fungsi
- Eksekusi kode
- Grounding dengan Google Penelusuran
- Grounding dengan Vertex AI RAG Engine (Pratinjau)
Untuk mengaktifkan alat tertentu agar dapat digunakan dalam respons yang ditampilkan, sertakan nama alat dalam daftar tools
saat Anda menginisialisasi model. Bagian berikut memberikan contoh cara menggunakan setiap alat bawaan dalam kode Anda.
Model yang didukung
Anda dapat menggunakan Live API dengan model berikut:
Versi model | Tingkat ketersediaan |
---|---|
gemini-live-2.5-flash |
GA Pribadi* |
gemini-live-2.5-flash-preview-native-audio |
Pratinjau publik |
* Hubungi perwakilan tim akun Google Anda untuk meminta akses.
Panggilan fungsi
Gunakan panggilan fungsi untuk membuat deskripsi fungsi, lalu teruskan deskripsi tersebut ke model dalam permintaan. Respons dari model mencakup nama fungsi yang sesuai dengan deskripsi dan argumen yang digunakan untuk memanggilnya.
Semua fungsi harus dideklarasikan di awal sesi dengan mengirimkan definisi alat sebagai bagian dari pesan LiveConnectConfig
.
Untuk mengaktifkan panggilan fungsi, sertakan function_declarations
dalam daftar tools
:
Gen AI SDK untuk Python
import asyncio from google import genai from google.genai import types client = genai.Client( vertexai=True, project=GOOGLE_CLOUD_PROJECT, location=GOOGLE_CLOUD_LOCATION, ) model = "gemini-live-2.5-flash" # Simple function definitions turn_on_the_lights = {"name": "turn_on_the_lights"} turn_off_the_lights = {"name": "turn_off_the_lights"} tools = [{"function_declarations": [turn_on_the_lights, turn_off_the_lights]}] config = {"response_modalities": ["TEXT"], "tools": tools} async def main(): async with client.aio.live.connect(model=model, config=config) as session: prompt = "Turn on the lights please" await session.send_client_content(turns={"parts": [{"text": prompt}]}) async for chunk in session.receive(): if chunk.server_content: if chunk.text is not None: print(chunk.text) elif chunk.tool_call: function_responses = [] for fc in tool_call.function_calls: function_response = types.FunctionResponse( name=fc.name, response={ "result": "ok" } # simple, hard-coded function response ) function_responses.append(function_response) await session.send_tool_response(function_responses=function_responses) if __name__ == "__main__": asyncio.run(main())
WebSockets
Eksekusi kode
Anda dapat menggunakan eksekusi kode dengan Live API
untuk membuat dan mengeksekusi kode Python secara langsung. Untuk mengaktifkan eksekusi kode untuk respons Anda, sertakan code_execution
dalam daftar tools
:
Gen AI SDK untuk Python
import asyncio from google import genai from google.genai import types client = genai.Client( vertexai=True, project=GOOGLE_CLOUD_PROJECT, location=GOOGLE_CLOUD_LOCATION, ) model = "gemini-live-2.5-flash" tools = [{'code_execution': {}}] config = {"response_modalities": ["TEXT"], "tools": tools} async def main(): async with client.aio.live.connect(model=model, config=config) as session: prompt = "Compute the largest prime palindrome under 100000." await session.send_client_content(turns={"parts": [{"text": prompt}]}) async for chunk in session.receive(): if chunk.server_content: if chunk.text is not None: print(chunk.text) model_turn = chunk.server_content.model_turn if model_turn: for part in model_turn.parts: if part.executable_code is not None: print(part.executable_code.code) if part.code_execution_result is not None: print(part.code_execution_result.output) if __name__ == "__main__": asyncio.run(main())
Grounding dengan Google Penelusuran
Anda dapat menggunakan Perujukan dengan Google Penelusuran dengan Live API dengan menyertakan google_search
dalam daftar tools
:
Gen AI SDK untuk Python
import asyncio from google import genai from google.genai import types client = genai.Client( vertexai=True, project=GOOGLE_CLOUD_PROJECT, location=GOOGLE_CLOUD_LOCATION, ) model = "gemini-live-2.5-flash" tools = [{'google_search': {}}] config = {"response_modalities": ["TEXT"], "tools": tools} async def main(): async with client.aio.live.connect(model=model, config=config) as session: prompt = "When did the last Brazil vs. Argentina soccer match happen?" await session.send_client_content(turns={"parts": [{"text": prompt}]}) async for chunk in session.receive(): if chunk.server_content: if chunk.text is not None: print(chunk.text) # The model might generate and execute Python code to use Search model_turn = chunk.server_content.model_turn if model_turn: for part in model_turn.parts: if part.executable_code is not None: print(part.executable_code.code) if part.code_execution_result is not None: print(part.code_execution_result.output) if __name__ == "__main__": asyncio.run(main())
Penyelarasan dengan Mesin RAG Vertex AI (Pratinjau)
Anda dapat menggunakan Vertex AI RAG Engine dengan Live API untuk merujuk, menyimpan, dan mengambil konteks:
from google import genai
from google.genai import types
from google.genai.types import (Content, LiveConnectConfig, HttpOptions, Modality, Part)
from IPython import display
PROJECT_ID=YOUR_PROJECT_ID
LOCATION=YOUR_LOCATION
TEXT_INPUT=YOUR_TEXT_INPUT
MODEL_NAME="gemini-live-2.5-flash"
client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location=LOCATION,
)
rag_store=types.VertexRagStore(
rag_resources=[
types.VertexRagStoreRagResource(
rag_corpus=<Your corpus resource name> # Use memory corpus if you want to store context.
)
],
# Set `store_context` to true to allow Live API sink context into your memory corpus.
store_context=True
)
async with client.aio.live.connect(
model=MODEL_NAME,
config=LiveConnectConfig(response_modalities=[Modality.TEXT],
tools=[types.Tool(
retrieval=types.Retrieval(
vertex_rag_store=rag_store))]),
) as session:
text_input=TEXT_INPUT
print("> ", text_input, "\n")
await session.send_client_content(
turns=Content(role="user", parts=[Part(text=text_input)])
)
async for message in session.receive():
if message.text:
display.display(display.Markdown(message.text))
continue
Untuk mengetahui informasi selengkapnya, lihat Menggunakan Vertex AI RAG Engine di Gemini Live API.
(Pratinjau publik) Audio native
Gemini 2.5 Flash dengan Live API memperkenalkan kemampuan audio native, yang meningkatkan fitur Live API standar. Audio native memberikan interaksi suara yang lebih kaya dan alami melalui 30 suara HD dalam 24 bahasa. Aplikasi ini juga menyertakan dua fitur baru yang eksklusif untuk audio native: Audio Proaktif dan Dialog Afektif.
Menggunakan Audio Proaktif
Audio Proaktif memungkinkan model merespons hanya jika relevan. Jika diaktifkan, model akan membuat transkrip teks dan respons audio secara proaktif, tetapi hanya untuk kueri yang ditujukan ke perangkat. Kueri yang tidak ditujukan ke perangkat akan diabaikan.
Untuk menggunakan Audio Proaktif, konfigurasi kolom proactivity
di
pesan penyiapan dan tetapkan proactive_audio
ke true
:
Gen AI SDK untuk Python
config = LiveConnectConfig( response_modalities=["AUDIO"], proactivity=ProactivityConfig(proactive_audio=True), )
Menggunakan Dialog Afektif
Dialog Afektif memungkinkan model yang menggunakan audio native Live API lebih memahami dan merespons ekspresi emosional pengguna dengan tepat, sehingga menghasilkan percakapan yang lebih bernuansa.
Untuk mengaktifkan Affective Dialog, tetapkan enable_affective_dialog
ke
true
dalam pesan penyiapan:
Gen AI SDK untuk Python
config = LiveConnectConfig( response_modalities=["AUDIO"], enable_affective_dialog=True, )
Informasi selengkapnya
Untuk mengetahui informasi selengkapnya tentang penggunaan Live API, lihat: