#!/usr/bin/env python3
import json, time, urllib.request
from pathlib import Path

base = 'http://127.0.0.1:8130'
with urllib.request.urlopen(base + '/v1/models', timeout=15) as r:
    models=json.load(r)
assert any(m['id']=='ornith-sglang' for m in models['data'])
request={
    'model':'ornith-sglang',
    'messages':[{'role':'user','content':'Responde exactamente: SGLANG_OK'}],
    'temperature':0, 'max_tokens':128,
    'chat_template_kwargs':{'enable_thinking':False},
}
t=time.monotonic()
req=urllib.request.Request(base+'/v1/chat/completions',data=json.dumps(request).encode(),headers={'Content-Type':'application/json'})
with urllib.request.urlopen(req,timeout=180) as r:
    result=json.load(r)
elapsed=time.monotonic()-t
content=result['choices'][0]['message'].get('content','')
print(json.dumps({'models':models,'elapsed_seconds':round(elapsed,3),'content':content,'usage':result.get('usage'),'finish_reason':result['choices'][0]['finish_reason']},ensure_ascii=False,indent=2))
assert 'SGLANG_OK' in content, 'Expected final response missing'
Path('/home/lab/bench/ornith/sglang/smoke-result.json').write_text(json.dumps(result,ensure_ascii=False,indent=2))
print('PASS: quantified SGLang chat API completed.')
