Gemini 可以回覆語音提示。舉例來說,Gemini 可以:
- 說明、總結或回答音訊內容相關問題。
- 提供音訊的轉錄稿。
- 提供有關音訊特定片段的解答或轉錄稿。
本指南將說明幾種操作方法:
- 將音訊傳送至 Gemini 模型。
- 輸入 Gemini 模型的音訊。
支援的音訊格式
Gemini 支援下列音訊格式 MIME 類型:
- WAV - 音訊/WAV
- MP3 - 音訊/mp3
- AIFF - 音訊/AI
- AAC - 音訊/AAC
- OGG Vorbis - 音訊/ogg
- FLAC - 音訊/flac
音訊的相關技術詳細資料
Gemini 對音訊有下列規則:
- Gemini 以 25 個符記表示的每一秒音訊;例如 1,500 個符記表示 1 分鐘的音訊。
- Gemini 只能推論英語的回覆。
- Gemini 可以「理解」例如鳥鳴或警笛聲等
- 單一提示支援的音訊資料長度上限為 9.5 小時。 Gemini 不會在單次提示中限制音訊檔案數量;不過 在單一提示中,所有音訊檔案的總長度不得超過 9.5 小時。
- Gemini 會將音訊檔案取樣至 16 Kbps 的資料解析度,
- 如果音訊來源含有多個聲道,Gemini 會合併這些聲道 明確轉換為單一管道
事前準備:設定專案和 API 金鑰
呼叫 Gemini API 前,請先設定專案並設定 您的 API 金鑰。
將音訊檔案提供給 Gemini
您可以透過下列任一方式將音訊檔案提供給 Gemini 兩種方式:
使用 File API 上傳音訊檔案
使用 File API 上傳任何大小的音訊檔案。一律使用 File API 要求總數 (包括檔案、文字提示、系統) 指令等) 大於 20 MB。
呼叫 media.upload
協助您使用 File API 上傳檔案。
以下程式碼示範呼叫這個方法的一種方式。
首先,請將音訊檔案從遠端伺服器下載到本機目錄。
curl -o sample.mp3 https://storage.googleapis.com/generativeai-downloads/data/State_of_the_Union_Address_30_January_1961.mp3
現在,上傳音訊檔案:
# Upload the file.
audio_file = genai.upload_file(path='sample.mp3')
將上傳檔案的提示傳送給 Gemini
上傳完成後,您可以呼叫
generateContent
方法,增加圍繞地圖邊緣的邊框間距。
例如:
# Initialize a Gemini model appropriate for your use case.
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
# Create the prompt.
prompt = "Summarize the speech."
# Pass the prompt and the audio file to Gemini.
response = model.generate_content([prompt, audio_file])
# Print the response.
print(response.text)
列出上傳的檔案
你可以上傳多個音訊檔案 (以及其他類型的檔案)。 以下程式碼會產生所有上傳檔案的清單:
# List all uploaded files.
for file in genai.list_files():
print(f"{file.display_name}, URI: {file.uri}')
刪除上傳的檔案
系統會在 48 小時後自動刪除檔案。您也可以選擇 刪除上傳的檔案。例如:
# Delete an uploaded file.
genai.delete_file(audio_file.name)
print(f'Deleted file {audio_file.uri}')
在要求中以內嵌資料的形式提供音訊檔案
除了上傳音訊檔案,您可以直接在要求中傳送音訊資料, 包含提示的相同呼叫請先下載小型音訊 檔案:
# Download an audio file from a remote server to a cache directory
# on your local host.
curl -o samplesmall.mp3 https://storage.googleapis.com/generativeai-downloads/data/Apollo-11_Day-01-Highlights-10s.mp3
接著,將已下載的小型音訊檔案與提示傳送至 Gemini:
# Initialize a Gemini model appropriate for your use case.
model = genai.GenerativeModel('models/gemini-1.5-flash')
# Create the prompt.
prompt = "Please summarize the audio."
# Load the samplesmall.mp3 file into a Python Blob object containing the audio
# file's bytes and then pass the prompt and the audio to Gemini.
response = model.generate_content([
prompt,
{
"mime_type": "audio/mp3",
"data": pathlib.Path('samplesmall.mp3').read_bytes()
}
])
# Output Gemini's response to the prompt and the inline audio.
print(response.text)
以內嵌資料形式提供音訊的相關注意事項如下:
- 要求大小上限為 20 MB,當中包含文字提示。 系統指示和內嵌檔案如果您的檔案的 會讓要求總大小超過 20 MB, 使用 File API 上傳要用於要求的檔案。
- 如果多次使用音訊樣本,會更有效率 使用 File API。
更多音訊處理方式
本節另外提供幾種進一步發揮音訊效益的方法。
取得音訊檔案的轉錄稿
如要取得轉錄稿,只要在提示中提問即可。例如:
# Initialize a Gemini model appropriate for your use case.
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
# Create the prompt.
prompt = "Generate a transcript of the speech."
# Pass the prompt and the audio file to Gemini.
response = model.generate_content([prompt, audio_file])
# Print the transcript.
print(response.text)
參考音訊檔案中的時間戳記
提示可以指定 MM:SS
格式的時間戳記來參照特定內容
片段。例如,下列提示要求
以下內容的轉錄稿:
- 從檔案開頭的 2 分 30 秒開始。
- 結束時間為檔案開頭的 3 分 29 秒。
# Create a prompt containing timestamps.
prompt = "Provide a transcript of the speech from 02:30 to 03:29."
計算符記數量
呼叫 countTokens
方法,
音訊檔案中的分詞數量。例如:
model.count_tokens([audio_file])