speech() / /audio/speech

speech() / /audio/speech

텍스트를 음성으로 변환(TTS)하는 litellm.speech() 함수와 프록시 /audio/speech 엔드포인트 문서예요. OpenAI, Azure, Vertex AI, AWS Polly, ElevenLabs, MiniMax 등을 지원해요.

출처: 문서

본문

개요

기능 지원 비고
비용 추적 모든 지원 모델과 동작
로깅 모든 통합에서 동작
최종 사용자 추적
폴백 지원 모델 간 동작
로드밸런싱 지원 모델 간 동작
가드레일 입력 텍스트에 적용 (비스트리밍 전용)
지원 제공자 OpenAI, Azure OpenAI, Vertex AI, AWS Polly, ElevenLabs, MiniMax

LiteLLM Python SDK 사용법

Quick Start

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 프록시 사용법

LiteLLM은 텍스트-음성 호출을 위한 openai 호환 /audio/speech 엔드포인트를 제공해요.

curl http://0.0.0.0:4000/v1/audio/speech \
  -H "Authorization: Bearer ***" \
  -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

지원 제공자

제공자 사용법 링크
OpenAI Usage
Azure OpenAI Usage
Azure AI Speech Service (AVA) Usage
AWS Polly Usage
Vertex AI Usage
Gemini Usage
ElevenLabs Usage
MiniMax Usage

/audio/speech에서 /chat/completions로의 브리지

LiteLLM을 사용하면 /audio/speech 엔드포인트를 통해 /chat/completions 모델로 음성을 생성할 수 있어요. /chat/completions로만 접근 가능한 Gemini의 TTS 지원 모델 같은 경우에 유용해요.

Gemini 텍스트-음성

Python SDK 사용법

Gemini Text-to-Speech SDK Usage

import litellm
import os

# Set your Gemini API key
os.environ["GEMINI_API_KEY"] = "your-gemini-api-key"

def test_audio_speech_gemini():
    result = litellm.speech(
        model="gemini/gemini-2.5-flash-preview-tts",
        input="the quick brown fox jumped over the lazy dogs",
        api_key=os.getenv("GEMINI_API_KEY"),
    )
    
    # Save to file
    from pathlib import Path
    speech_file_path = Path(__file__).parent / "gemini_speech.mp3"
    result.stream_to_file(speech_file_path)
    print(f"Audio saved to {speech_file_path}")

test_audio_speech_gemini()

비동기 사용법

Gemini Text-to-Speech Async Usage

import litellm
import asyncio
import os
from pathlib import Path

os.environ["GEMINI_API_KEY"] = "your-gemini-api-key"

async def test_async_gemini_speech():
    speech_file_path = Path(__file__).parent / "gemini_speech.mp3"
    response = await litellm.aspeech(
        model="gemini/gemini-2.5-flash-preview-tts",
        input="the quick brown fox jumped over the lazy dogs",
        api_key=os.getenv("GEMINI_API_KEY"),
    )
    response.stream_to_file(speech_file_path)
    print(f"Audio saved to {speech_file_path}")

asyncio.run(test_async_gemini_speech())

LiteLLM 프록시 사용법

Config 설정: Gemini Proxy Configuration

model_list:
- model_name: gemini-tts
  litellm_params:
    model: gemini/gemini-2.5-flash-preview-tts
    api_key: os.environ/GEMINI_API_KEY

프록시 시작: Start LiteLLM Proxy

litellm --config /path/to/config.yaml

# RUNNING on http://0.0.0.0:4000

요청 보내기: Gemini TTS Request

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

Vertex AI 텍스트-음성

Python SDK 사용법

Vertex AI Text-to-Speech SDK Usage

import litellm
import os
from pathlib import Path

# Set your Google credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/service-account.json"

def test_audio_speech_vertex():
    result = litellm.speech(
        model="vertex_ai/gemini-2.5-flash-preview-tts",
        input="the quick brown fox jumped over the lazy dogs",
    )
    
    # Save to file
    speech_file_path = Path(__file__).parent / "vertex_speech.mp3"
    result.stream_to_file(speech_file_path)
    print(f"Audio saved to {speech_file_path}")

test_audio_speech_vertex()

LiteLLM 프록시 사용법

Config 설정: Vertex AI Proxy Configuration

model_list:
- model_name: vertex-tts
  litellm_params:
    model: vertex_ai/gemini-2.5-flash-preview-tts
    vertex_project: your-project-id
    vertex_location: us-central1

요청 보내기: Vertex AI TTS Request

curl http://0.0.0.0:4000/v1/audio/speech \
  -H "Authorization: Bearer ***" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "vertex-tts",
    "input": "The quick brown fox jumped over the lazy dog.",
    "voice": "en-US-Wavenet-D"
  }' \
  --output vertex_speech.mp3

AWS Polly 텍스트-음성

AWS Polly는 신경망 및 표준 텍스트-음성 엔진을 제공하며 여러 음성과 언어를 지원해요. 자세한 사용 예시는 AWS Polly 제공자 문서를 참고하세요.

✨ 엔터프라이즈 LiteLLM 프록시 - 요청 파일 크기 최대값 설정

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 ***" \
--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}}%  

더 알아보기 (Learn more)