Gemini の機能を構成する

このドキュメントでは、Live API を使用する際に Gemini モデルのさまざまな機能を構成する方法について説明します。思考、関数呼び出しやグラウンディングなどのツールの使用、感情的なダイアログやプロアクティブな音声などのネイティブ音声機能を構成できます。

思考を構成する

Gemini モデルは思考機能をサポートしており、動的思考がデフォルトで有効になっています。thinking_budget パラメータは、使用する思考トークンの数に関するガイダンスをモデルに提供します。思考を無効にするには、thinking_budget0 に設定します。

config = {
    "response_modalities": ["audio"],
    "thinking_config": {
        "thinking_budget": 256,
    },
}

ツールの使用を構成する

さまざまなツールが Live API 対応モデルのさまざまなバージョンと互換性があります。次に例を示します。

返されたレスポンスで使用する特定のツールを有効にするには、モデルを初期化するときに tools リストにツールの名前を含めます。以降のセクションでは、コードで各組み込みツールを使用する方法の例を示します。

関数呼び出し

関数呼び出しを使用して関数の説明を作成し、その説明をリクエストでモデルに渡します。モデルからのレスポンスには、説明に対応する関数の名前と、その関数を呼び出す引数が含まれます。

すべての関数は、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())
  

システム指示で関数呼び出しを使用する例については、ベスト プラクティスの例をご覧ください。

設定メッセージの 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 を使用するをご覧ください。

ネイティブ オーディオ機能を構成する

ネイティブの音声機能を備えたモデルは、次の機能をサポートしています。

アフェクティブ ダイアログを構成する

アフェクティブ ダイアログが有効になっている場合、モデルはユーザーの口調や感情表現に基づいて理解し、応答しようとします。

アフェクティブ ダイアログを有効にするには、セットアップ メッセージで enable_affective_dialogtrue に設定します。

Python

config = LiveConnectConfig(
    response_modalities=["AUDIO"],
    enable_affective_dialog=True,
)
  

プロアクティブ音声を構成する

プロアクティブ音声を使用すると、モデルが応答するタイミングを制御できます。たとえば、Gemini に、質問されたときや特定のトピックが議論されたときにのみ応答するようリクエストできます。プロアクティブ音声の動画デモについては、Gemini LiveAPI ネイティブ音声プレビューをご覧ください。

プロアクティブ音声を有効にするには、セットアップ メッセージの proactivity フィールドを構成し、proactive_audiotrue に設定します。

Python

config = LiveConnectConfig(
    response_modalities=["AUDIO"],
    proactivity=ProactivityConfig(proactive_audio=True),
)
  

会話の例

たとえば、料理について Gemini と会話する例を次に示します。

Prompt: "You are an AI assistant in Italian cooking; only chime in when the topic is about Italian cooking."

Speaker A: "I really love cooking!" (No response from Gemini.)

Speaker B: "Oh yes, me too! My favorite is French cuisine." (No response from
Gemini.)

Speaker A: "I really like Italian food; do you know how to make a pizza?"

(Italian cooking topic will trigger response from Gemini.)
Live API: "I'd be happy to help! Here's a recipe for a pizza."

一般的なユースケース

プロアクティブ音声を使用すると、Gemini は次のように動作します。

  • 最小限のレイテンシで応答する: ユーザーが話し終えた後に Gemini が応答するため、中断が減り、中断が発生した場合でも Gemini が文脈を見失うのを防ぐことができます。
  • 中断を回避する: プロアクティブ音声は、Gemini が周囲の雑音や外部の会話によって中断されるのを防ぎ、会話中に外部の会話が聞こえてきた場合に Gemini が応答しないようにします。
  • 割り込みを処理する: ユーザーが Gemini の回答中に割り込む必要がある場合、プロアクティブ音声を使用すると、ユーザーが「えー」や「あー」などのつなぎ言葉を使用するよりも、Gemini が適切にバックチャネル(適切な割り込みが処理される)を処理しやすくなります。
  • 音声を一緒に聴く: Gemini は、発言者の音声ではない音声ファイルを一緒に聴き、会話の後半でその音声ファイルに関する質問に答えることができます。

課金

Gemini が会話をリッスンしている間は、入力音声トークンが課金されます。

出力音声トークンについては、Gemini が応答した場合にのみ課金されます。Gemini が応答しない場合や無音状態が続く場合、出力音声トークンに対する課金は発生しません。

詳細については、Vertex AI の料金をご覧ください。

次のステップ

Live API の使用について詳しくは、以下をご覧ください。