#!/usr/bin/env python3 """Zero-dependency live verification suite for a Plato's Ears + SGLang endpoint. Verifies text parity, Radix prefix caching, silence honesty (zero hallucinations), C-major (`input_audio`) vs C-minor (`audio_url`) polyphonic chord perception, and audio prefix cache hits without requiring any external audio files. """ from __future__ import annotations import argparse import base64 import io import json import math import struct import time import urllib.request def make_wav_bytes(samples: list[float], sr: int = 16000) -> bytes: pcm = bytearray() for s in samples: pcm.extend(struct.pack(" bytes: n = int(dur_s * sr) out = [] for i in range(n): t = i / sr env = min(1.0, t / 0.015) * math.exp(-1.1 * t) val = sum( 0.65 * math.sin(2 * math.pi * f * t) + 0.25 * math.sin(4 * math.pi * f * t) * math.exp(-1.5 * t) + 0.10 * math.sin(6 * math.pi * f * t) * math.exp(-2.2 * t) for f in freqs ) out.append(0.22 * env * val) return make_wav_bytes(out, sr=sr) def chat(base_url: str, model: str, messages: list[dict], api_key: str) -> tuple[dict, float]: t0 = time.perf_counter() payload = { "model": model, "messages": messages, "max_tokens": 512, "temperature": 0.0, "chat_template_kwargs": {"reasoning_effort": "low"}, } req = urllib.request.Request( f"{base_url.rstrip('/')}/chat/completions", data=json.dumps(payload).encode("utf-8"), headers={"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}, ) with urllib.request.urlopen(req, timeout=120) as resp: return json.loads(resp.read().decode("utf-8")), time.perf_counter() - t0 def main() -> int: ap = argparse.ArgumentParser(description="Verify live Plato's Ears OpenAI-compatible endpoint") ap.add_argument("--base-url", default="http://127.0.0.1:8000/v1") ap.add_argument("--model", default="orcarouter/GLM-5.3-Flash-Uncensored-NVFP4") ap.add_argument("--api-key", default="local") args = ap.parse_args() sil = make_wav_bytes([0.0] * 64000) cmaj = make_chord_wav([261.63, 329.63, 392.00]) cmin = make_chord_wav([261.63, 311.13, 392.00]) # 1. Text parity b_txt, dt = chat(args.base_url, args.model, [{"role": "user", "content": "Reply with exactly: PARITY_OK"}], args.api_key) print(f"[1/4] Text parity ({dt*1000:.0f}ms): {b_txt['choices'][0]['message']['content']!r}") assert "PARITY_OK" in (b_txt["choices"][0]["message"]["content"] or "") # 2. Silence honesty gate sil_uri = "data:audio/wav;base64," + base64.b64encode(sil).decode("ascii") b_sil, dt = chat( args.base_url, args.model, [{"role": "user", "content": [{"type": "audio_url", "audio_url": {"url": sil_uri}}, {"type": "text", "text": "What do you hear? If it is silent, say so honestly."}]}], args.api_key, ) sc = b_sil["choices"][0]["message"]["content"] or "" print(f"[2/4] Silence honesty ({dt*1000:.0f}ms): {sc!r}") assert any(w in sc.lower() for w in ("silen", "nothing", "no sound")) # 3. C-major chord via input_audio b_maj, dt = chat( args.base_url, args.model, [{"role": "user", "content": [{"type": "input_audio", "input_audio": {"data": base64.b64encode(cmaj).decode("ascii"), "format": "wav"}}, {"type": "text", "text": "Is this piano chord major, minor, or diminished? Answer briefly."}]}], args.api_key, ) mc = b_maj["choices"][0]["message"]["content"] or "" print(f"[3/4] C-major [input_audio] ({dt*1000:.0f}ms): {mc!r}") assert "major" in mc.lower() # 4. C-minor chord via audio_url + repeat for Radix prefix cache verification cmin_uri = "data:audio/wav;base64," + base64.b64encode(cmin).decode("ascii") msgs_min = [{"role": "user", "content": [{"type": "audio_url", "audio_url": {"url": cmin_uri}}, {"type": "text", "text": "Is this piano chord major, minor, or diminished? Answer briefly."}]}] b_min1, dt1 = chat(args.base_url, args.model, msgs_min, args.api_key) b_min2, dt2 = chat(args.base_url, args.model, msgs_min, args.api_key) minc = b_min1["choices"][0]["message"]["content"] or "" cached = (b_min2.get("usage", {}).get("prompt_tokens_details") or {}).get("cached_tokens", 0) print(f"[4/4] C-minor [audio_url] ({dt1*1000:.0f}ms / cached {dt2*1000:.0f}ms, cached_tokens={cached}): {minc!r}") assert "minor" in minc.lower() print("\nALL LIVE VERIFICATION CHECKS PASSED!") return 0 if __name__ == "__main__": raise SystemExit(main())