GeminiのLive APIを試す
gemini-2.5-flash-native-audio-preview-12-2025
クライアントから直にWSでコネクションをはれる。
Ehemeral Tokenを発行できるので、中継のサーバーをかませない場合はそのTokenを使う
トークン発行時にVADの設定や音声合成を行うボイスの設定など色々な設定ができる
client.authTokens.create({
config: {
uses: 1,
expireTime,
liveConnectConstraints: {
model: LIVE_MODEL,
config: {
sessionResumption: {},
temperature: 0.7,
responseModalities: [Modality.AUDIO],
inputAudioTranscription: {},
outputAudioTranscription: {},
realtimeInputConfig: {
automaticActivityDetection: {
silenceDurationMs: xxx
},
},
...(systemInstruction && { systemInstruction }),
speechConfig: {
...(voiceName && {
voiceConfig: {
prebuiltVoiceConfig: { voiceName },
},
}),
},
},
},
},
})
```これToken発行側にvoiceConfigとか設定しないと、Client側で設定しても反映されなかった。
直API Tokenの場合は反映されたので上書きできないようにロックされているんだと思う。
日本語音声認識に関して
The Live API supports multiple languages. Native audio output models automatically choose the appropriate language and don't support explicitly setting the language code.
特に指定がなくても音声認識の段階で自動で応答音声の言語が変わるようになっているっぽい。
応答は空白とかのノイズが含まれてしまうのでユーザー側でよしなに正規化しないといけない。
Live APIとセッションを確立した後にどのように追加でプロンプトを注入するか。
・BidiGenerateContentというAPIがある
クライアント→サーバー
- BidiGenerateContentSetup
- セッション開始時に最初に1回だけ送信する設定メッセージ
- BidiGenerateContentClientContent
- 会話履歴やユーザー入力を追加するメッセージ
https://ai.google.dev/api/live#BidiGenerateContentClientContent
- 会話履歴やユーザー入力を追加するメッセージ
// コンテキストのみ追加(応答を待たない)
sendClientContent({
turns: { role: "user", parts: [{ text: "[指示] 深掘りしてください" }] },
turnComplete: false, // AI応答をトリガーしない
});
// 応答を要求
sendClientContent({
turns: { role: "user", parts: [{ text: "こんにちは" }] },
turnComplete: true, // AI応答をトリガー
});
-
BidiGenerateContentRealtimeInput
- リアルタイム音声・映像・テキストのストリーミング入力
https://ai.google.dev/api/live#BidiGenerateContentRealtimeInput
- リアルタイム音声・映像・テキストのストリーミング入力
-
BidiGenerateContentToolResponse
サーバー側でFunction Callingでのレスポンスを受けられる
https://ai.google.dev/api/live#BidiGenerateContentToolResponse
Audio inputs and audio outputs negatively impact the model's ability to use function calling
らしい
エフェメラルトークンでのTool設定もできる
client.authTokens.create({
config: {
uses: 1,
expireTime,
liveConnectConstraints: {
model: LIVE_MODEL,
config: {
tools: [
{
functionDeclarations: [
{
name: "get_weather",
description: "Get weather for a location",
parameters: { ... }
}
]
}
],
// ...その他の設定
},
},
},
});
ユーザー側のターンで発話での応答が遅延・欠落してしまう問題が。。。
inputTranscriptionを結合してみてみるとなんか、発話時間?が一定を超えたらめちゃくちゃバグってる。
抜粋
生産、生産、生産、生産、生産、生産、生産、SEEM、SEEM、SEEM、SEEM、SEEM、SE、SE、SE、SE、SE、SE、SEEM、SEEM、SEEM、SEEM、SEEM、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、ふ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、あ、そう、そう、そう、そう、そう、そう、そう、そう、そう、そう、そう、そう、そう、うん