fix(openai): prompt engineering and transcript model
This commit is contained in:
@@ -4,7 +4,9 @@ from urllib.parse import quote
|
||||
|
||||
from app.modules.knowledge.service import SearchInput
|
||||
from app.modules.voice.settings import RuntimeSettings
|
||||
from app.modules.voice.configuration import OpenAIInputSettings
|
||||
from app.modules.voice.serialization import bounded_json
|
||||
from app.modules.voice.openai_style import effective_instructions
|
||||
|
||||
class ProviderProtocolError(ValueError):
|
||||
pass
|
||||
@@ -35,10 +37,17 @@ def strict_json(raw):
|
||||
|
||||
class RealtimeAdapter:
|
||||
def __init__(self, transport, *, model="gpt-realtime-mini", voice="shimmer",
|
||||
no_active_classifier=None, settings=None):
|
||||
no_active_classifier=None, settings=None, input_settings=None):
|
||||
self.settings = settings or RuntimeSettings()
|
||||
if not isinstance(self.settings, RuntimeSettings):
|
||||
raise ValueError("Validated runtime settings required")
|
||||
input_settings = input_settings if input_settings is not None else OpenAIInputSettings()
|
||||
if not isinstance(input_settings, OpenAIInputSettings):
|
||||
raise ValueError('Validated OpenAI input settings required')
|
||||
# Revalidate copies too: Pydantic model_copy deliberately bypasses validation.
|
||||
self.input_settings = OpenAIInputSettings.model_validate({
|
||||
key: getattr(input_settings, key) for key in OpenAIInputSettings.model_fields})
|
||||
self.reasoning = self.input_settings.reasoning(model)
|
||||
self.transport, self.model, self.voice = transport, model, voice
|
||||
# The default classifier is restricted to the empirically verified
|
||||
# OpenAI profile below. Other profiles require separate acceptance.
|
||||
@@ -46,12 +55,10 @@ class RealtimeAdapter:
|
||||
|
||||
def setup(self):
|
||||
return dict(type="session.update", session=dict(
|
||||
type="realtime", model=self.model, instructions=POLICY,
|
||||
type="realtime", model=self.model, instructions=effective_instructions(POLICY),
|
||||
**({"reasoning": self.reasoning.copy()} if self.reasoning is not None else {}),
|
||||
output_modalities=["audio"], tool_choice="auto",
|
||||
audio=dict(input=dict(format=dict(type="audio/pcm", rate=24000),
|
||||
transcription=dict(model="whisper-1", language="fa"),
|
||||
turn_detection=dict(type="server_vad", create_response=False,
|
||||
interrupt_response=False)),
|
||||
audio=dict(input=self.input_settings.audio_input(),
|
||||
output=dict(format=dict(type="audio/pcm", rate=24000), voice=self.voice)),
|
||||
tools=[dict(type="function", name="search_knowledge",
|
||||
description="Retrieve evidence from authorized selected documents.",
|
||||
@@ -67,8 +74,22 @@ class RealtimeAdapter:
|
||||
if isinstance(wanted, list):
|
||||
return isinstance(actual, list) and len(actual) == len(wanted) and all(
|
||||
contains(a, w) for a, w in zip(actual, wanted))
|
||||
if type(wanted) is float:
|
||||
return type(actual) in (int, float) and actual == wanted
|
||||
return type(actual) is type(wanted) and actual == wanted
|
||||
return contains(session, expected)
|
||||
if not contains(session, expected):
|
||||
return False
|
||||
if self.reasoning is None and session.get('reasoning') is not None:
|
||||
return False
|
||||
if session['audio']['input']['turn_detection'].get('idle_timeout_ms') is not None:
|
||||
return False
|
||||
transcription = session['audio']['input']['transcription']
|
||||
# The provider must not silently enable a conflicting language mode.
|
||||
empty_defaults = dict(language=(None,), languages=(None, []),
|
||||
prompt=(None, ''), keywords=(None, []))
|
||||
return all(key not in transcription or transcription[key] in empty_defaults[key]
|
||||
for key in empty_defaults
|
||||
if key not in expected['audio']['input']['transcription'])
|
||||
|
||||
def serialize(self, event):
|
||||
return bounded_json(event, self.settings.max_provider_message_bytes, ensure_ascii=False)
|
||||
|
||||
Reference in New Issue
Block a user