借助工具使用功能,Live API 不仅可以进行对话,还可以在保持实时连接的同时执行实际操作并提取外部上下文。 您可以使用 Live API 定义 函数调用 和 Google 搜索 等工具。
支持的工具概览
下面简要介绍了 Live API 模型可用的工具:
| 工具 | Gemini 3.1 Flash Live 预览版 | Gemini 2.5 Flash Live 预览版 |
|---|---|---|
| 搜索 | 支持 | 支持 |
| 函数调用 | 支持(仅限同步) | 支持(同步和异步) |
| Google 地图 | 不支持 | 不支持 |
| 代码执行 | 不支持 | 不支持 |
| 网址上下文 | 不支持 | 不支持 |
函数调用
Live API 支持函数调用,就像常规内容生成请求一样。借助函数调用,Live API 可以与外部数据和程序进行交互,从而大大提升应用的功能。
您可以将函数声明定义为会话配置的一部分。
收到工具调用后,客户端应使用 session.send_tool_response 方法响应 FunctionResponse 对象列表。
如需了解 详情,请参阅函数调用教程。
Python
import asyncio
import wave
from google import genai
from google.genai import types
client = genai.Client()
model = "gemini-3.1-flash-live-preview"
# 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": ["AUDIO"], "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}]})
wf = wave.open("audio.wav", "wb")
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(24000) # Output is 24kHz
async for response in session.receive():
if response.data is not None:
wf.writeframes(response.data)
elif response.tool_call:
print("The tool was called")
function_responses = []
for fc in response.tool_call.function_calls:
function_response = types.FunctionResponse(
id=fc.id,
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)
wf.close()
if __name__ == "__main__":
asyncio.run