51 lines
2.4 KiB
Python
51 lines
2.4 KiB
Python
"""Scoped Vertex dictation settings; ordinary transcription calls stay unchanged."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
VOCABULARY = Path('/credentials/personal-vocabulary.json')
|
|
|
|
|
|
def vocabulary(path=VOCABULARY):
|
|
words = json.loads(path.read_text())
|
|
if not isinstance(words, list) or not 1 <= len(words) <= 100:
|
|
raise ValueError('Personal vocabulary must contain 1-100 terms')
|
|
if any(not isinstance(word, str) or not word.strip() or len(word) > 100 for word in words):
|
|
raise ValueError('Invalid personal vocabulary term')
|
|
return list(dict.fromkeys(word.strip() for word in words))
|
|
|
|
|
|
def install():
|
|
from litellm.llms.vertex_ai.audio_transcription.gemini_transcribe_transformation import (
|
|
VertexGeminiAudioTranscriptionConfig,
|
|
)
|
|
original = VertexGeminiAudioTranscriptionConfig.transform_audio_transcription_request
|
|
if getattr(original, '_personal_dictation_patch', False):
|
|
return
|
|
|
|
def transform(self, model, audio_file, optional_params, litellm_params):
|
|
if optional_params.get('personal_dictation') is not True:
|
|
return original(self, model, audio_file, optional_params, litellm_params)
|
|
if model.removeprefix('vertex_ai/') != 'gemini-3.5-transcribe':
|
|
raise ValueError('Personal dictation is restricted to Vertex Gemini 3.5 Transcribe')
|
|
if optional_params.get('timestamp_granularities') or optional_params.get('response_format') not in (None, 'json', 'text'):
|
|
raise ValueError('Personal dictation supports json/text without timestamps')
|
|
clean = {key: val for key, val in optional_params.items() if key != 'personal_dictation'}
|
|
result = original(self, model, audio_file, clean, litellm_params)
|
|
config = result.data.setdefault('generationConfig', {}).setdefault('audioTranscriptionConfig', {})
|
|
if set(config) - {'mode', 'languageCodes'} or config.get('mode') not in (None, 'VERBATIM'):
|
|
raise ValueError('Unexpected Vertex transcription defaults; review adapter compatibility')
|
|
config['mode'] = 'SMART'
|
|
config['customVocabulary'] = vocabulary()
|
|
return result
|
|
|
|
transform._personal_dictation_patch = True
|
|
VertexGeminiAudioTranscriptionConfig.transform_audio_transcription_request = transform
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
from litellm import run_server
|
|
install()
|
|
sys.argv[0] = 'litellm'
|
|
sys.exit(run_server())
|