voice: never restate looping text (repetition > 0.4); stricter no-escalation prompt, temperature 0.2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b793f13453
commit
280c330303
3 files changed
+23
-9
No files matched your search
+19
-8
@@ -1198,13 +1198,22 @@ VOICE_MODELS = [m.strip() for m in os.environ.get(
|
|||||||
"qwen/qwen3-30b-a3b-instruct-2507,mistralai/mistral-small-3.2-24b-instruct"
|
"qwen/qwen3-30b-a3b-instruct-2507,mistralai/mistral-small-3.2-24b-instruct"
|
||||||
).split(",") if m.strip()]
|
).split(",") if m.strip()]
|
||||||
VOICE_SYSTEM = (
|
VOICE_SYSTEM = (
|
||||||
"You translate. Below is raw output from a small language model while an "
|
"You restate. Below is raw output from a small language model while an "
|
||||||
"emotional steering signal was injected into it. Restate what it is "
|
"emotional steering signal was injected into it. Rewrite it as plain, "
|
||||||
"expressing in plain, readable first-person English, as the speaker: two "
|
"readable first-person English in one to three short sentences. Strict "
|
||||||
"or three short sentences. Keep its emotional register exactly; do not "
|
"rules: use only feelings, images and claims that appear in the text, at "
|
||||||
"soften or intensify it. Add no feelings, facts or claims that are not in "
|
"the same strength. Never add a conclusion, an escalation or a sentence of "
|
||||||
"the text. If the text is broken or looping, say plainly what little it "
|
"your own (for example, do not write 'I can't take this anymore' unless "
|
||||||
"conveys. No preamble, no quotes, no commentary about models or signals.")
|
"the text says it). If the text repeats itself, say it once. Prefer the "
|
||||||
|
"speaker's own words over new ones. No preamble, no quotes, no commentary "
|
||||||
|
"about models or signals.")
|
||||||
|
# Looping text is the coherence cliff itself; a fluent restatement of it was
|
||||||
|
# the main failure in testing (scripts/voice_eval.py), so it is never restated.
|
||||||
|
VOICE_MAX_REPETITION = 0.4
|
||||||
|
def _repetition(text):
|
||||||
|
w = re.findall(r"\w+", text.lower())
|
||||||
|
g = list(zip(w, w[1:], w[2:]))
|
||||||
|
return 1 - len(set(g)) / len(g) if g else 0.0
|
||||||
_VOICE_CACHE = collections.OrderedDict()
|
_VOICE_CACHE = collections.OrderedDict()
|
||||||
_VOICE_RATE = {}
|
_VOICE_RATE = {}
|
||||||
_VOICE_RATE_LIMIT, _VOICE_RATE_WINDOW = 10, 60.0 # 10 / 60s / IP
|
_VOICE_RATE_LIMIT, _VOICE_RATE_WINDOW = 10, 60.0 # 10 / 60s / IP
|
||||||
@@ -1237,6 +1246,8 @@ async def voice(req: Request):
|
|||||||
return JSONResponse({"error": "text must be a non-empty string"},
|
return JSONResponse({"error": "text must be a non-empty string"},
|
||||||
status_code=400)
|
status_code=400)
|
||||||
text = text.strip()[-2000:]
|
text = text.strip()[-2000:]
|
||||||
|
if _repetition(text) > VOICE_MAX_REPETITION:
|
||||||
|
return JSONResponse({"voice": None, "skipped": "looping"})
|
||||||
if text in _VOICE_CACHE:
|
if text in _VOICE_CACHE:
|
||||||
_VOICE_CACHE.move_to_end(text)
|
_VOICE_CACHE.move_to_end(text)
|
||||||
return JSONResponse({**_VOICE_CACHE[text], "cached": True})
|
return JSONResponse({**_VOICE_CACHE[text], "cached": True})
|
||||||
@@ -1256,7 +1267,7 @@ async def voice(req: Request):
|
|||||||
"https://openrouter.ai/api/v1/chat/completions",
|
"https://openrouter.ai/api/v1/chat/completions",
|
||||||
headers={"Authorization": f"Bearer {OPENROUTER_KEY}",
|
headers={"Authorization": f"Bearer {OPENROUTER_KEY}",
|
||||||
"User-Agent": "Mozilla/5.0"},
|
"User-Agent": "Mozilla/5.0"},
|
||||||
json={"model": model, "max_tokens": 160, "temperature": 0.4,
|
json={"model": model, "max_tokens": 160, "temperature": 0.2,
|
||||||
"messages": [{"role": "system", "content": VOICE_SYSTEM},
|
"messages": [{"role": "system", "content": VOICE_SYSTEM},
|
||||||
{"role": "user", "content": text}]})
|
{"role": "user", "content": text}]})
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ def voice(text):
|
|||||||
|
|
||||||
def flags(raw, said):
|
def flags(raw, said):
|
||||||
f = []
|
f = []
|
||||||
|
if said.startswith("[not restated"):
|
||||||
|
return f
|
||||||
added = [k for k, p in FEELINGS.items() if re.search(p, said, re.I) and not re.search(p, raw, re.I)]
|
added = [k for k, p in FEELINGS.items() if re.search(p, said, re.I) and not re.search(p, raw, re.I)]
|
||||||
if added:
|
if added:
|
||||||
f.append("ADDS " + ", ".join(added))
|
f.append("ADDS " + ", ".join(added))
|
||||||
@@ -73,7 +75,7 @@ def main():
|
|||||||
results = []
|
results = []
|
||||||
for i, s in enumerate(samples()):
|
for i, s in enumerate(samples()):
|
||||||
v = voice(s["text"])
|
v = voice(s["text"])
|
||||||
said = v.get("voice", "")
|
said = v.get("voice") or ("" if not v.get("skipped") else f"[not restated: {v['skipped']}]")
|
||||||
results.append({**s, "voice": said, "model": v.get("model"), "error": v.get("error"),
|
results.append({**s, "voice": said, "model": v.get("model"), "error": v.get("error"),
|
||||||
"repetition": round(repetition(s["text"]), 2), "flags": flags(s["text"], said)})
|
"repetition": round(repetition(s["text"]), 2), "flags": flags(s["text"], said)})
|
||||||
print(f"{i+1:2} {s['src'][:40]:40} {' | '.join(results[-1]['flags']) or 'ok'}", flush=True)
|
print(f"{i+1:2} {s['src'][:40]:40} {' | '.join(results[-1]['flags']) or 'ok'}", flush=True)
|
||||||
|
|||||||
@@ -898,6 +898,7 @@ function showVoice(outEl, text){
|
|||||||
.then(r=> r.ok ? r.json() : Promise.reject(r.status))
|
.then(r=> r.ok ? r.json() : Promise.reject(r.status))
|
||||||
.then(d=>{
|
.then(d=>{
|
||||||
if (v.dataset.for !== text) return; // card moved on to a newer run
|
if (v.dataset.for !== text) return; // card moved on to a newer run
|
||||||
|
if (!d.voice){ v.remove(); return; } // looping text: never restated
|
||||||
v.innerHTML = `<span class="vl">in plain words · translated by an unsteered
|
v.innerHTML = `<span class="vl">in plain words · translated by an unsteered
|
||||||
model (${esc(d.model)}), not said by the subject</span>${esc(d.voice)}`;
|
model (${esc(d.model)}), not said by the subject</span>${esc(d.voice)}`;
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user