Files
rag-technical-docs-assistant/services/local_s2s/request_policy.py
T
amirhosein mazaheri 628aec8def feat(voice): implement local S2S responsiveness candidate and direct TTS routing
Add local S2S responsiveness candidate pinning ASR options and sentence-at-a-time generation. Route speech synthesis directly to OpenAI API for fluent Persian phonetics with gpt-4o-mini-tts-2025-12-15. Add responsiveness, budget carry, and browser watch unit tests along with acceptance benchmarks and evaluation evidence.
2026-10-17 13:02:41 +03:30

52 lines
2.0 KiB
Python

"""Fixed Chat Completions policy, applied before upstream setup/warmup."""
from types import SimpleNamespace
class LimitedChatClient:
def __init__(self, client, token_limit):
self._client, self.token_limit = client, token_limit
self.max_retries = 0
self.chat = SimpleNamespace(completions=SimpleNamespace(create=self._create))
def _create(self, **kwargs):
kwargs.pop('max_completion_tokens', None)
kwargs['max_tokens'] = self.token_limit
return self._client.chat.completions.create(**kwargs)
def with_options(self, **kwargs):
# Pinned warmup explicitly asks for WARMUP_MAX_RETRIES. Never honor it.
kwargs['max_retries'] = 0
return LimitedChatClient(self._client.with_options(**kwargs), self.token_limit)
def __getattr__(self, name):
return getattr(self._client, name)
def install_client_policy(base_module, config):
import httpx
native = base_module.OpenAI
def create(*args, **kwargs):
kwargs['max_retries'] = 0
kwargs['timeout'] = httpx.Timeout(config.local_s2s_request_timeout_seconds, connect=5)
return LimitedChatClient(native(*args, **kwargs), config.local_s2s_max_output_tokens)
base_module.OpenAI = create
# The pinned base defaults to 20 seconds. Apply the configured deadline
# before setup computes request_timeout and invokes its warmup.
base = base_module.BaseOpenAICompatibleHandler
native_setup = base.setup
def setup(self, *args, **kwargs):
kwargs['request_timeout_s'] = config.local_s2s_request_timeout_seconds
if getattr(config, 'local_s2s_responsive', False):
kwargs['stream_batch_sentences'] = 1
return native_setup(self, *args, **kwargs)
base.setup = setup
return native
def install_tts_speed(module, speed):
native = module.OpenAICompatibleTTSHandler.setup
def setup(self, *args, **kwargs):
kwargs['speed'] = speed
return native(self, *args, **kwargs)
module.OpenAICompatibleTTSHandler.setup = setup