OpenAI 텍스트 음성 변환

OpenAI 텍스트 음성 변환 (Text-to-speech)

OpenAI의 TTS(text-to-speech) 기능을 LiteLLM에서 사용하는 방법을 알아봐요. speech() API와 Proxy의 /v1/audio/speech 엔드포인트를 통해 음성을 생성할 수 있어요.

출처: 문서

본문

개요

기능 지원 비고
Cost Tracking 지원되는 모든 모델에서 동작
Logging 모든 통합에서 동작
End-user Tracking
Fallbacks 지원되는 모델 간 동작
Loadbalancing 지원되는 모델 간 동작
Guardrails 입력 텍스트에 적용

지원 모델: 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은 TTS 호출을 위한 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

지원 모델

모델 예시
tts-1 speech(model="tts-1", voice="alloy", input="Hello, world!")
tts-1-hd speech(model="tts-1-hd", voice="alloy", input="Hello, world!")
gpt-4o-mini-tts speech(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 ***" \
--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)

  • OpenAI TTS 공식 문서
  • LiteLLM 오디오 스트리밍