Live API 対応モデルには、次のツールを使用する機能が組み込まれています。
返されたレスポンスで使用する特定のツールを有効にするには、モデルを初期化するときに tools
リストにツールの名前を含めます。以降のセクションでは、コードで各組み込みツールを使用する方法の例を示します。
サポートされているモデル
Live API は次のモデルで使用できます。
モデル バージョン | 可用性レベル |
---|---|
gemini-live-2.5-flash |
限定公開 GA* |
gemini-live-2.5-flash-preview-native-audio |
公開プレビュー版 |
* アクセス権をリクエストするには、Google アカウント チームの担当者にお問い合わせください。
関数呼び出し
関数呼び出しを使用して関数の説明を作成し、その説明をリクエストでモデルに渡します。モデルからのレスポンスには、説明に対応する関数の名前と、その関数を呼び出す引数が含まれます。
すべての関数は、LiveConnectConfig
メッセージの一部としてツール定義を送信することで、セッションの開始時に宣言する必要があります。
関数呼び出しを有効にするには、tools
リストに function_declarations
を含めます。
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())
Python
コードの実行
Live API でコード実行を使用すると、Python コードを直接生成して実行できます。レスポンスのコード実行を有効にするには、tools
リストに code_execution
を含めます。
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())
Google 検索によるグラウンディング
tools
リストに google_search
を含めることで、Live API で Google 検索によるグラウンディングを使用できます。
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())
Vertex AI RAG Engine によるグラウンディング(プレビュー)
Vertex AI RAG Engine を Live API と組み合わせて使用して、コンテキストのグラウンディング、保存、取得を行うことができます。
Python
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=# 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
詳細については、Gemini Live API で Vertex AI RAG Engine を使用するをご覧ください。
(公開プレビュー)ネイティブ音声
Gemini 2.5 Flash with Live API には、ネイティブ音声機能が導入され、標準の Live API 機能が強化されています。ネイティブ音声により、24 言語の 30 種類の HD 音声で、より豊かで自然な音声インタラクションを実現します。また、ネイティブ音声専用の 2 つの新機能( Proactive Audio と Affective Dialog)も含まれています。
コンテキストに応じた音声を使用する
プロアクティブ音声を使用すると、モデルは関連性がある場合にのみ応答します。有効にすると、モデルはテキストの文字起こしと音声の回答をプロアクティブに生成しますが、デバイス宛てのクエリに対してのみ行います。デバイスを対象としないクエリは無視されます。
プロアクティブ オーディオを使用するには、セットアップ メッセージの proactivity
フィールドを構成し、proactive_audio
を true
に設定します。
Python
config = LiveConnectConfig( response_modalities=["AUDIO"], proactivity=ProactivityConfig(proactive_audio=True), )
アフェクティブ ダイアログを使用する
アフェクティブ ダイアログを使用すると、Live API ネイティブ音声を使用するモデルがユーザーの感情表現をより適切に理解して応答できるようになり、より機微を捉えた会話が可能になります。
感情ダイアログを有効にするには、設定メッセージで enable_affective_dialog
を true
に設定します。
Python
config = LiveConnectConfig( response_modalities=["AUDIO"], enable_affective_dialog=True, )
詳細
Live API の使用について詳しくは、以下をご覧ください。