feat(voice_worker): a short voice prompt and one judgement before each v2 answer
- compact_prompt: the v2 persona in a tenth of v1's words, answers as short as possible, the full tool guidance only on turns with a data tool. A greeting's reply prompt went from about 4,500 to 1,400 tokens on Strata. - intent_judge: the tool router judges whether the turn needs tools; the planner runs only when it does. prepare_intent runs the pick ahead on a partial transcript (worker message prepare), and the reply reuses it. - Without stream_browser the screen-share tools are not offered. - The time context carries the 12-hour form (10:37 was read as pm). - A <tool_call> block written as the reply is parsed as a call, not spoken. Evals (Qwen3.6 on Strata, EVAL_COMPACT_PROMPT=1 EVAL_INTENT_JUDGE=1): 221 passed, 4 failed (known failures); new evals/test_voice_reply_length.py (v2 answers 234 chars against v1's 416) and test_reply_language_default.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,9 @@ interface, so the worker logic is tested with a fake (voice_worker.spec.md, "His
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import difflib
|
||||
import sys
|
||||
import unicodedata
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
@@ -28,6 +30,11 @@ class Brain(Protocol):
|
||||
"""Blocking: candidate ids (``{"id", "user", "text"}``) most important first; None on failure.
|
||||
Runs on its own thread, also while a reply runs."""
|
||||
|
||||
def prepare(self, user: str, text: str, chat: bool) -> None:
|
||||
"""Blocking: get ready to answer ``text`` (a partial transcript of a call still being said),
|
||||
so the reply to about the same text starts further along. ``chat``: chat channels are
|
||||
registered. Runs on its own thread, also while a reply runs. Optional."""
|
||||
|
||||
|
||||
@dataclass
|
||||
class MemorySettings:
|
||||
@@ -110,6 +117,32 @@ class Muted:
|
||||
cls._depth -= 1
|
||||
|
||||
|
||||
# A prepared intent is used for a reply whose text is at least this close to the text it was
|
||||
# prepared for (letters and digits only): the last partial often lacks a final syllable or two.
|
||||
PREPARED_SIMILARITY = 0.85
|
||||
# ... and only this soon after it was prepared, and at most this long is waited for one in flight.
|
||||
PREPARED_MAX_AGE_S = 30.0
|
||||
PREPARED_WAIT_S = 5.0
|
||||
|
||||
|
||||
def _letters(text: str) -> str:
|
||||
return "".join(c for c in unicodedata.normalize("NFKD", text.lower()) if c.isalnum())
|
||||
|
||||
|
||||
def similar(a: str, b: str) -> bool:
|
||||
x, y = _letters(a), _letters(b)
|
||||
return bool(x and y) and difflib.SequenceMatcher(None, x, y).ratio() >= PREPARED_SIMILARITY
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Prepared:
|
||||
text: str
|
||||
turns: int # the asker's dialogue memory size it was made with
|
||||
at: float
|
||||
done: threading.Event = dataclasses.field(default_factory=threading.Event)
|
||||
tools: Optional[list] = None # the intent judge's pick; None: none made
|
||||
|
||||
|
||||
class JarvisBrain:
|
||||
"""The existing jarvis reply engine with the user's history context.
|
||||
|
||||
@@ -125,13 +158,16 @@ class JarvisBrain:
|
||||
# v1's tts_engine names the v1 desktop voice (English-only Piper by default), which makes the
|
||||
# engine lock replies to English. v2 speaks through the worker's TTS and chat answers are
|
||||
# not spoken; the brain answers in ``reply_language`` (an OUTPUT_LANGUAGE lock still wins).
|
||||
self.cfg = dataclasses.replace(load_settings(), tts_engine="melo")
|
||||
# stream_browser: v1's screen-share browser (controlBrowser, browseAndPlay, setBroadcast and
|
||||
# browser-first web search) needs v1's Node bot; v2 has no on-screen browser until M6.
|
||||
self.cfg = dataclasses.replace(load_settings(), tts_engine="melo", stream_browser=False)
|
||||
self.db = Database(self.cfg.db_path, self.cfg.sqlite_vss_path)
|
||||
# ``language``: the ISO code tools get (e.g. the Wikipedia host); ``reply_language``: replies
|
||||
# come in it unless the asker explicitly asks for another language (None: the asker's).
|
||||
self.language = language
|
||||
self.reply_language = reply_language
|
||||
self._memories: dict = {}
|
||||
self._prepared: dict[str, _Prepared] = {}
|
||||
self._lock = threading.Lock()
|
||||
debug_log(f"brain ready: llm={self.cfg.ollama_base_url} model={self.cfg.ollama_chat_model}", "voice_worker")
|
||||
|
||||
@@ -145,11 +181,41 @@ class JarvisBrain:
|
||||
self._memories[user] = mem
|
||||
return mem
|
||||
|
||||
@staticmethod
|
||||
def _turns(mem) -> int:
|
||||
return len(mem.get_recent_messages()) if mem.has_recent_messages() else 0
|
||||
|
||||
def prepare(self, user: str, text: str, chat: bool) -> None:
|
||||
from jarvis.reply.engine import prepare_intent
|
||||
|
||||
mem = self._memory(user)
|
||||
p = _Prepared(text=text, turns=self._turns(mem), at=time.monotonic())
|
||||
with self._lock:
|
||||
self._prepared[user] = p
|
||||
t0 = time.perf_counter()
|
||||
try:
|
||||
p.tools = Muted.run(prepare_intent, self.cfg, text, mem, chat)
|
||||
finally:
|
||||
p.done.set()
|
||||
debug_log(f"brain prepare user={user} picked={p.tools is not None} "
|
||||
f"ms={(time.perf_counter() - t0) * 1000:.0f}", "voice_worker")
|
||||
|
||||
def _prepared_tools(self, user: str, text: str) -> Optional[list]:
|
||||
"""The pick prepared for about this text, waiting for one still being made; None if none fits."""
|
||||
with self._lock:
|
||||
p = self._prepared.pop(user, None)
|
||||
if p is None or not similar(p.text, text) or time.monotonic() - p.at > PREPARED_MAX_AGE_S:
|
||||
return None
|
||||
if not p.done.wait(PREPARED_WAIT_S) or p.turns != self._turns(self._memory(user)):
|
||||
return None
|
||||
return p.tools
|
||||
|
||||
def reply(self, user: str, text: str, history: HistoryContext, chat=None) -> Optional[str]:
|
||||
from jarvis.reply import turn_state
|
||||
from jarvis.reply.engine import run_reply_engine
|
||||
|
||||
t0 = time.perf_counter()
|
||||
prepared = self._prepared_tools(user, text)
|
||||
# legacy_memory=False: v1's profile, diary and graph belong to one user; several people
|
||||
# share this assistant, so only the asker's scoped history may reach the prompt.
|
||||
def turn():
|
||||
@@ -160,13 +226,14 @@ class JarvisBrain:
|
||||
try:
|
||||
return run_reply_engine(self.db, self.cfg, None, text, self._memory(user),
|
||||
language=self.language, history=history, legacy_memory=False,
|
||||
default_language=self.reply_language)
|
||||
default_language=self.reply_language, compact_prompt=True,
|
||||
intent_judge=True, prepared_tools=prepared)
|
||||
finally:
|
||||
turn_state.reset()
|
||||
|
||||
answer = Muted.run(turn)
|
||||
debug_log(f"brain reply user={user} ok={bool(answer)} ms={(time.perf_counter() - t0) * 1000:.0f}",
|
||||
"voice_worker")
|
||||
debug_log(f"brain reply user={user} ok={bool(answer)} prepared={prepared is not None} "
|
||||
f"ms={(time.perf_counter() - t0) * 1000:.0f}", "voice_worker")
|
||||
return answer
|
||||
|
||||
def addressed(self, user: str, text: str, names: list[str]) -> Optional[bool]:
|
||||
|
||||
Reference in New Issue
Block a user