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
+14
View File
@@ -1,6 +1,8 @@
import json
import os
from copy import deepcopy
from urllib.parse import urlsplit
from reply_emotion import REPLY_SCHEMA
class LLMProvider:
@@ -18,10 +20,22 @@ class LLMProvider:
self.template_kwargs = json.loads(raw) if raw is not None else ({'enable_thinking': False} if self.model.lower().startswith('qwen') else {})
if not isinstance(self.template_kwargs, dict):
raise ValueError('invalid LLM chat template kwargs')
# Only this exact local deployment has been verified with streaming schema decoding.
# Other endpoints/aliases require explicit opt-in after their own compatibility probe.
verified_local = (self.base_url == 'http://127.0.0.1:8888/v1'
and self.model == 'qwen3.8-27b'
and self.template_kwargs == {'enable_thinking': False})
self.structured_output = env.get('LAN_LLM_STRUCTURED_OUTPUT',
'json_schema' if verified_local else 'prompt')
if self.structured_output not in ('prompt', 'json_schema'):
raise ValueError('invalid LLM structured output mode')
def request(self, messages, *, max_tokens=64):
body = {'model': self.model, 'messages': messages, 'max_tokens': max_tokens, 'temperature': 0.3, 'stream': True}
if self.template_kwargs:
body['chat_template_kwargs'] = self.template_kwargs
if self.structured_output == 'json_schema':
body['response_format'] = {'type': 'json_schema', 'json_schema': {
'name': 'role_reply', 'strict': True, 'schema': deepcopy(REPLY_SCHEMA)}}
headers = {'Authorization': 'Bearer ' + self.api_key} if self.api_key else {}
return self.base_url + '/chat/completions', headers, body