my_voice_assistant_v3_jamulix/app/pipeline/decontextualizer.py

84 lines
3.8 KiB
Python
Raw Normal View History

"""Koreferenz-Vorstufe: löst Pronomen der letzten Äußerung anhand des Verlaufs auf.
Schließt die im Tool-Calling-Eval isolierte Restkante (nl + Pronomen-aus-History,
z. B. Leeft hij nog?" → „Leeft Rutger Hauer nog?"). Bewusst **gegated** (kurze
Folgefrage MIT Pronomen UND vorhandener History), damit nicht jeder Turn einen
Extra-Call kostet. Siehe Docs/weg2-tool-calling.md §5.3.
"""
import logging
import httpx
logger = logging.getLogger(__name__)
ENDPOINT = "https://openrouter.ai/api/v1/chat/completions"
# Pronomen/Referenzwörter (lowercase, mehrsprachig) für die Gate-Heuristik.
_PRONOUNS = {
"er", "sie", "es", "der", "die", "das", "den", "dem", "deren", "dessen",
"ihn", "ihm", "ihr", # de
"he", "she", "it", "they", "him", "her", "them", "that", "those", "these", # en
"hij", "ze", "zij", "het", "die", "dat", "hem", "haar", "hen", "hun", # nl
"il", "elle", "ils", "elles", "lui", "celui", "celle", # fr
"él", "ella", "ellos", "ese", "esa", "lei", "loro", "quello", # es/it
}
_MAX_WORDS = 8
_SYSTEM = (
"You are a coreference resolver. Given a short conversation and the user's "
"latest message, output ONLY that latest message rewritten so it stands on "
"its own: resolve pronouns and references to the concrete names or entities "
"mentioned earlier. Keep the original language and meaning. Do NOT answer it; "
"only rewrite. If it is already self-contained, output it unchanged."
)
def _words(text: str) -> list[str]:
return [w for w in "".join(c.lower() if (c.isalpha() or c == " ") else " "
for c in text).split() if w]
class Decontextualizer:
def __init__(self, api_key: str, model: str, timeout: float = 15.0):
self.api_key = (api_key or "").strip()
self.model = (model or "").strip()
self.timeout = timeout
def _gated(self, text: str, history) -> bool:
"""Nur kurze Folgefragen mit Pronomen und vorhandener History."""
if not history or not text:
return False
words = _words(text)
if not words or len(words) > _MAX_WORDS:
return False
return any(w in _PRONOUNS for w in words)
def _render(self, history: list[dict]) -> str:
lines = []
for m in history[-6:]:
who = "User" if m.get("role") == "user" else "Assistant"
lines.append(f"{who}: {m.get('content', '')}")
return "\n".join(lines)
async def run(self, text: str, history: list[dict] | None = None,
language: str | None = None) -> str:
if not self.api_key or not self._gated(text, history):
return text
user = (f"Conversation:\n{self._render(history)}\n\n"
f"Latest message: {text}\n\nRewritten self-contained message:")
payload = {"model": self.model, "temperature": 0.0,
"messages": [{"role": "system", "content": _SYSTEM},
{"role": "user", "content": user}]}
headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
try:
async with httpx.AsyncClient(timeout=httpx.Timeout(self.timeout)) as client:
resp = await client.post(ENDPOINT, json=payload, headers=headers)
resp.raise_for_status()
out = (resp.json()["choices"][0]["message"]["content"] or "").strip()
except Exception as exc: # noqa: BLE001 — best effort, bei Fehler Original behalten
logger.warning("Decontextualizer fehlgeschlagen: %s", exc)
return text
# Schutz vor Ausreißern (Erklärungen statt Rewrite): nur Plausibles übernehmen.
if not out or len(out) > len(text) + 200:
return text
return out.strip().strip('"')