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
+3 -3
View File
@@ -11,7 +11,7 @@ provider=LLMProvider();rows=[]
with httpx.Client(timeout=30,trust_env=False) as client:
for name,prompt in [('ordinary','一加一等于几?'),('happy','请用明显高兴的语气祝贺我完成公开练习。')]:
start=time.perf_counter();first=None;raw='';finish=None
messages=[{'role':'system','content':'以下角色设定只约束reply口述内容,旧有不输出JSON约定不约束外层结构。用简短中文直接回答。\n'+SYSTEM_FRAGMENT},{'role':'user','content':prompt}]
messages=[{'role':'system','content':'以下角色设定只约束reply字段的可朗读正文。用简短中文直接回答。\n'+SYSTEM_FRAGMENT},{'role':'user','content':prompt}]
url,headers,body=provider.request(messages,max_tokens=160)
row={'case':name}
try:
@@ -26,14 +26,14 @@ with httpx.Client(timeout=30,trust_env=False) as client:
if delta and first is None:first=round((time.perf_counter()-start)*1000,2)
raw+=delta;finish=choice.get('finish_reason') or finish
if len(raw)>8192:raise ValueError('reply bound')
parsed=parse_reply(raw)
parsed=parse_reply(raw,finish_reason=finish)
row.update(source=parsed.source,emotion=parsed.emotion,intensity=parsed.intensity,reply_chars=len(parsed.reply),finish_reason=finish,first_token_ms=first)
except Exception as error:row['error_type']=type(error).__name__
row['elapsed_ms']=round((time.perf_counter()-start)*1000,2)
if first is not None:row['structured_wait_ms']=round(row['elapsed_ms']-first,2)
rows.append(row)
passed=all(x.get('source')=='model' and x.get('finish_reason')=='stop' for x in rows)
print(json.dumps({'status':'passed' if passed else 'failed','base_url':provider.base_url,'model_alias':provider.model,'chat_template_kwargs':provider.template_kwargs,'key_present':bool(provider.api_key),'strategy':'prompt_only_json_single_system_single_request','results':rows}))
print(json.dumps({'status':'passed' if passed else 'failed','base_url':provider.base_url,'model_alias':provider.model,'chat_template_kwargs':provider.template_kwargs,'key_present':bool(provider.api_key),'strategy':provider.structured_output,'single_system_single_request':True,'results':rows}))
raise SystemExit(0 if passed else 1)
'''
REMOTE=r'''