fix(gateway): constrain verified Qwen replies to emotion schema

This commit is contained in:
zenord
2026-10-12 12:18:18 +08:00
parent b521d368a5
commit 18ec4c2104
18 changed files with 351 additions and 60 deletions
+7 -5
View File
@@ -30,7 +30,7 @@ from protocol import WavStream, pack, unpack
from vad import Config, Silero, VadSession
ROOT = Path(__file__).resolve().parent
VERSION = '0.7.1'
VERSION = '0.7.2'
DEFAULT_VOICE_ID = '9a97272c-3919-5606-9c8f-9552ab602528'
ORIGINS = {'https://192.168.31.61:5011', 'https://localhost:5011', 'https://127.0.0.1:5011'}
@@ -39,6 +39,7 @@ ORIGINS = {'https://192.168.31.61:5011', 'https://localhost:5011', 'https://127.
async def lifespan(app):
token = os.environ.get('LAN_GATEWAY_TOKEN', '')
if len(token) < 32: raise RuntimeError('missing gateway token')
app.state.llm_structured_output = LLMProvider().structured_output
path = ROOT / 'models/silero_vad.onnx'
if hashlib.sha256(path.read_bytes()).hexdigest() != EXPECTED_HASH: raise RuntimeError('model checksum')
app.state.model = await asyncio.to_thread(Silero, path)
@@ -137,7 +138,7 @@ async def logout(request: Request):
@app.get('/health')
async def health():
return {'status': 'ok', 'version': VERSION, 'codec': 'pcm16', 'opus': False, 'busy': app.state.busy, 'stt': 'batch SenseVoice', 'tts': 'per-turn full text / single streaming generator VoxCPM2', 'voice_mode_default': 'continuous_voice', 'default_voice_id': DEFAULT_VOICE_ID}
return {'status': 'ok', 'version': VERSION, 'codec': 'pcm16', 'opus': False, 'busy': app.state.busy, 'llm_structured_output': getattr(app.state, 'llm_structured_output', 'prompt'), 'stt': 'batch SenseVoice', 'tts': 'per-turn full text / single streaming generator VoxCPM2', 'voice_mode_default': 'continuous_voice', 'default_voice_id': DEFAULT_VOICE_ID}
@app.post('/debug/log')
@@ -467,7 +468,7 @@ class Dialogue:
request_start = time.perf_counter()
metrics.update(voice_mode=self.voice_mode, voice_id=self.voice_id, tts_request_count=0)
# Exactly one initial system: Qwen's deployed template rejects a second system.
system = '以下角色设定只约束reply字段的口述内容;其中旧有不输出JSON等格式约定不约束本轮外层结构。\n' + self.persona_rules + ''.join(
system = '以下角色设定只约束reply字段的可朗读正文。\n' + self.persona_rules + ''.join(
'\n\n' + item['content'] for item in self.face_messages.get(turn, [])) + '\n\n' + SYSTEM_FRAGMENT
messages = [{'role': 'system', 'content': system}] + self.contexts.get(turn, []) + [{'role': 'user', 'content': text}]
url, headers, body = self.llm_provider.request(messages, max_tokens=160)
@@ -505,7 +506,8 @@ class Dialogue:
result = parse_reply(raw_body, finish_reason=finish_reason)
metrics.update(reply_emotion_reason=result.reason, reply_parse_branch=result.parse_branch,
reply_emotion=result.emotion, reply_intensity=result.intensity,
llm_finish_reason=finish_reason, llm_usage=usage, llm_max_tokens=body['max_tokens'])
llm_finish_reason=finish_reason, llm_usage=usage, llm_max_tokens=body['max_tokens'],
llm_structured_output=self.llm_provider.structured_output)
metrics['reply_parse_ms'] = round((time.perf_counter() - parse_start) * 1000, 2)
metrics['reply_emotion_source'] = result.source
metrics['llm_first_body_ms'] = round((time.perf_counter() - request_start) * 1000, 2)
@@ -652,7 +654,7 @@ async def dialogue(ws: WebSocket):
session.voice_id = voice_id
session.debug_store = app.state.debug
session.debug_enabled = hello.get('tts_debug', True) is True
await session.event('hello', context_face_cue_version=1, reply_emotion_version=1, version=VERSION, codec='pcm16', sample_rate=16000, output_sample_rate=48000, channels=1, frame_duration=20, header_version=1, conversation_id=cid, duplex_default=True, voice_mode=voice_mode, voice_id=voice_id, voices=app.state.voices)
await session.event('hello', context_face_cue_version=1, reply_emotion_version=1, llm_structured_output=session.llm_provider.structured_output, version=VERSION, codec='pcm16', sample_rate=16000, output_sample_rate=48000, channels=1, frame_duration=20, header_version=1, conversation_id=cid, duplex_default=True, voice_mode=voice_mode, voice_id=voice_id, voices=app.state.voices)
await session.send_history()
started = time.monotonic()
while time.monotonic() - started < 600: