fix(transcription): normalize chat-style apiBase to audio endpoint (#3637)
This commit is contained in:
@@ -7,6 +7,25 @@ from pathlib import Path
|
||||
import httpx
|
||||
from loguru import logger
|
||||
|
||||
_TRANSCRIPTIONS_PATH = "audio/transcriptions"
|
||||
|
||||
|
||||
def _resolve_transcription_url(api_base: str | None, default_url: str) -> str:
|
||||
"""Resolve the full transcription endpoint URL.
|
||||
|
||||
Accepts either a chat-style base (e.g. ``https://api.groq.com/openai/v1``)
|
||||
or a complete URL already ending in ``/audio/transcriptions``. A chat-style
|
||||
base — the form users naturally copy from their LLM provider config — gets
|
||||
the path appended instead of being POSTed verbatim and 404ing (#3637).
|
||||
"""
|
||||
if not api_base:
|
||||
return default_url
|
||||
base = api_base.rstrip("/")
|
||||
if base.endswith(_TRANSCRIPTIONS_PATH):
|
||||
return base
|
||||
return f"{base}/{_TRANSCRIPTIONS_PATH}"
|
||||
|
||||
|
||||
# Up to 3 retries (4 attempts total) with exponential backoff on transient
|
||||
# failures. Whisper endpoints occasionally return 502/503 under load, and
|
||||
# mobile-network transcription callers hit sporadic connect/read errors.
|
||||
@@ -127,12 +146,12 @@ class OpenAITranscriptionProvider:
|
||||
language: str | None = None,
|
||||
):
|
||||
self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
|
||||
self.api_url = (
|
||||
api_base
|
||||
or os.environ.get("OPENAI_TRANSCRIPTION_BASE_URL")
|
||||
or "https://api.openai.com/v1/audio/transcriptions"
|
||||
self.api_url = _resolve_transcription_url(
|
||||
api_base or os.environ.get("OPENAI_TRANSCRIPTION_BASE_URL"),
|
||||
"https://api.openai.com/v1/audio/transcriptions",
|
||||
)
|
||||
self.language = language or None
|
||||
logger.debug("OpenAI transcription endpoint: {}", self.api_url)
|
||||
|
||||
async def transcribe(self, file_path: str | Path) -> str:
|
||||
if not self.api_key:
|
||||
@@ -166,12 +185,12 @@ class GroqTranscriptionProvider:
|
||||
language: str | None = None,
|
||||
):
|
||||
self.api_key = api_key or os.environ.get("GROQ_API_KEY")
|
||||
self.api_url = (
|
||||
api_base
|
||||
or os.environ.get("GROQ_BASE_URL")
|
||||
or "https://api.groq.com/openai/v1/audio/transcriptions"
|
||||
self.api_url = _resolve_transcription_url(
|
||||
api_base or os.environ.get("GROQ_BASE_URL"),
|
||||
"https://api.groq.com/openai/v1/audio/transcriptions",
|
||||
)
|
||||
self.language = language or None
|
||||
logger.debug("Groq transcription endpoint: {}", self.api_url)
|
||||
|
||||
async def transcribe(self, file_path: str | Path) -> str:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user