跳至主要內容

OpenAI - 文字轉語音

總覽

功能支援備註
成本追蹤適用於所有支援的模型
記錄可跨所有整合運作
終端使用者追蹤
備援可在支援的模型之間運作
負載平衡可在支援的模型之間運作
防護欄套用於輸入文字
支援的模型tts-1, tts-1-hd, gpt-4o-mini-tts

LiteLLM Python SDK 使用方式

快速開始

from pathlib import Path
from litellm import speech
import os

os.environ["OPENAI_API_KEY"] = "sk-.."

speech_file_path = Path(__file__).parent / "speech.mp3"
response = speech(
model="openai/tts-1",
voice="alloy",
input="the quick brown fox jumped over the lazy dogs",
)
response.stream_to_file(speech_file_path)

非同步使用方式

from litellm import aspeech
from pathlib import Path
import os, asyncio

os.environ["OPENAI_API_KEY"] = "sk-.."

async def test_async_speech():
speech_file_path = Path(__file__).parent / "speech.mp3"
response = await aspeech(
model="openai/tts-1",
voice="alloy",
input="the quick brown fox jumped over the lazy dogs",
api_base=None,
api_key=None,
organization=None,
project=None,
max_retries=1,
timeout=600,
client=None,
optional_params={},
)
response.stream_to_file(speech_file_path)

asyncio.run(test_async_speech())

LiteLLM Proxy 使用方式

LiteLLM 提供一個與 openai 相容的 /audio/speech 端點,用於文字轉語音請求。

curl http://0.0.0.0:4000/v1/audio/speech \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1",
"input": "The quick brown fox jumped over the lazy dog.",
"voice": "alloy"
}' \
--output speech.mp3

設定

- model_name: tts
litellm_params:
model: openai/tts-1
api_key: os.environ/OPENAI_API_KEY
litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000

支援的模型

模型範例
tts-1speech(model="tts-1", voice="alloy", input="Hello, world!")
tts-1-hdspeech(model="tts-1-hd", voice="alloy", input="Hello, world!")
gpt-4o-mini-ttsspeech(model="gpt-4o-mini-tts", voice="alloy", input="Hello, world!")

✨ Enterprise LiteLLM Proxy - 設定最大請求檔案大小

當您想限制傳送至 audio/transcriptions 的請求檔案大小時,請使用此設定

- model_name: whisper
litellm_params:
model: whisper-1
api_key: sk-*******
max_file_size_mb: 0.00001 # 👈 max file size in MB (Set this intentionally very small for testing)
model_info:
mode: audio_transcription

使用有效檔案進行測試請求

curl --location 'http://localhost:4000/v1/audio/transcriptions' \
--header 'Authorization: Bearer sk-1234' \
--form 'file=@"/Users/ishaanjaffer/Github/litellm/tests/gettysburg.wav"' \
--form 'model="whisper"'

預期會看到以下回應

{"error":{"message":"File size is too large. Please check your file size. Passed file size: 0.7392807006835938 MB. Max file size: 0.0001 MB","type":"bad_request","param":"file","code":500}}%