feat(voice): implement local S2S retained history and provider selection
Add pinned CPU engine dependencies, retained interrupted-response history, provider admission, offline evaluation and isolated browser tooling. Record Phase 2 acceptance and Phase 3/4 preparation, bounded paid-test accounting and interruption incident findings. Human interruption acceptance, paired OpenAI evaluation and rollback remain pending.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
"""Actual pinned handler + OpenAI SDK; only HTTP I/O is replaced with fixtures."""
|
||||
import json
|
||||
from queue import Queue
|
||||
from threading import Event
|
||||
from types import SimpleNamespace
|
||||
|
||||
import httpx
|
||||
from openai import OpenAI, APIConnectionError, APIStatusError
|
||||
import speech_to_speech.LLM.base_openai_compatible_language_model as base
|
||||
from speech_to_speech.LLM.chat_completions_language_model import ChatCompletionsApiModelHandler
|
||||
from services.local_s2s.request_policy import install_client_policy
|
||||
|
||||
original_factory, original_setup = base.OpenAI, base.BaseOpenAICompatibleHandler.setup
|
||||
results = []
|
||||
for limit in (17, 83):
|
||||
for outcome in ('ok', 'http503', 'connect'):
|
||||
calls, clients = [], []
|
||||
|
||||
def fixture(request):
|
||||
calls.append(json.loads(request.content))
|
||||
if outcome == 'connect':
|
||||
raise httpx.ConnectError('controlled fixture', request=request)
|
||||
if outcome == 'http503':
|
||||
return httpx.Response(503, json={'error': {'message': 'fixture'}})
|
||||
return httpx.Response(200, json={
|
||||
'id': 'fixture', 'object': 'chat.completion', 'created': 0,
|
||||
'model': 'fixture/model', 'choices': [{'index': 0,
|
||||
'message': {'role': 'assistant', 'content': 'fixture'},
|
||||
'finish_reason': 'stop'}]})
|
||||
|
||||
def factory(*args, **kwargs):
|
||||
client = httpx.Client(transport=httpx.MockTransport(fixture), trust_env=False)
|
||||
clients.append(client)
|
||||
return OpenAI(*args, **kwargs, http_client=client)
|
||||
|
||||
base.OpenAI = factory
|
||||
config = SimpleNamespace(local_s2s_request_timeout_seconds=7,
|
||||
local_s2s_max_output_tokens=limit)
|
||||
install_client_policy(base, config)
|
||||
try:
|
||||
try:
|
||||
handler = ChatCompletionsApiModelHandler(Event(), Queue(), Queue(),
|
||||
setup_kwargs=dict(model_name='fixture/model', device='cpu',
|
||||
base_url='http://127.0.0.1:9/v1', api_key='fixture', stream=False))
|
||||
except (APIConnectionError, APIStatusError):
|
||||
assert outcome != 'ok' and len(calls) == 1
|
||||
else:
|
||||
try:
|
||||
assert outcome == 'ok'
|
||||
assert handler.request_timeout_s == 7
|
||||
assert handler.client.max_retries == 0
|
||||
handler._request([{'role': 'user', 'content': 'fixture'}],
|
||||
{'max_tokens': 999, 'max_completion_tokens': 999})
|
||||
assert len(calls) == 2
|
||||
finally:
|
||||
handler.cleanup()
|
||||
assert calls and all(c['max_tokens'] == limit for c in calls)
|
||||
assert all('max_completion_tokens' not in c for c in calls)
|
||||
results.append(dict(limit=limit, outcome=outcome, attempts=len(calls), passed=True))
|
||||
finally:
|
||||
base.OpenAI = original_factory
|
||||
base.BaseOpenAICompatibleHandler.setup = original_setup
|
||||
for client in clients:
|
||||
client.close()
|
||||
print(json.dumps(dict(cases=results, paid_calls=0, http_io='controlled HTTPX fixtures')))
|
||||
Reference in New Issue
Block a user