feat(voice_worker): the v2 brain answers in Korean unless asked for another language
The brain passes default_language (worker --reply-language, default Korean) to the reply engine; unlike the output_language lock it gives way when the user explicitly asks for a language. Tools get the ko code (--tool-language). Eval evals/test_reply_language_default.py (Qwen3.6 on Strata): 6/6, English questions answered in Korean, an explicit English request in English, and the control without the default answers English in English. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -981,7 +981,8 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
text: str, dialogue_memory: "DialogueMemory",
|
||||
language: Optional[str] = None,
|
||||
history: Optional[Any] = None,
|
||||
legacy_memory: bool = True) -> Optional[str]:
|
||||
legacy_memory: bool = True,
|
||||
default_language: Optional[str] = None) -> Optional[str]:
|
||||
"""
|
||||
Main entry point for reply generation.
|
||||
|
||||
@@ -1004,6 +1005,9 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
enrichment) belongs to a single user. False skips all of it, for
|
||||
the v2 brain where several people share one assistant and only
|
||||
the scoped ``history`` may reach the prompt.
|
||||
default_language: v2 only. The language to reply in unless the user
|
||||
explicitly asks for another one (e.g. "Korean"). An
|
||||
``output_language`` lock still wins. None: the user's language.
|
||||
|
||||
Returns:
|
||||
Generated reply text or None
|
||||
@@ -1713,7 +1717,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
# _resolve_output_language). Building the persona from the raw env var while
|
||||
# the directive used the config value made the two contradict each other.
|
||||
_output_language = _resolve_output_language()
|
||||
_persona_prompt = build_system_prompt(_assistant_name, _output_language)
|
||||
_persona_prompt = build_system_prompt(_assistant_name, _output_language, default_language)
|
||||
# File-based operator instructions: every *.md in AGENTS_DIR (default
|
||||
# /app/agents, bind-mounted from ./agents). Read once per turn so edits in
|
||||
# the folder apply on the next reply without a restart; fail-open to "".
|
||||
@@ -1738,6 +1742,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
|
||||
_lang_directive = reply_language_directive(
|
||||
_output_language,
|
||||
getattr(cfg, "tts_engine", "piper"),
|
||||
default_language,
|
||||
)
|
||||
if _lang_directive:
|
||||
guidance.insert(1, _lang_directive)
|
||||
|
||||
@@ -180,7 +180,8 @@ def tool_honesty_reminder(tool_names) -> str:
|
||||
|
||||
|
||||
def build_system_prompt(
|
||||
assistant_name: str = "Jarvis", output_language: Optional[str] = None
|
||||
assistant_name: str = "Jarvis", output_language: Optional[str] = None,
|
||||
default_language: Optional[str] = None,
|
||||
) -> str:
|
||||
"""Render the persona prompt with the configured assistant name.
|
||||
|
||||
@@ -198,6 +199,9 @@ def build_system_prompt(
|
||||
lang = (output_language or "").strip()
|
||||
if lang:
|
||||
prompt = prompt.replace("in the user's language", f"in {lang}")
|
||||
elif (default_language or "").strip():
|
||||
# The reply-language directive decides (default, or what the user asked for).
|
||||
prompt = prompt.replace("in the user's language", "in the reply language")
|
||||
return prompt
|
||||
|
||||
|
||||
@@ -240,8 +244,30 @@ ENGLISH_ONLY_DIRECTIVE = (
|
||||
)
|
||||
|
||||
|
||||
def default_language_directive(language: Optional[str]) -> Optional[str]:
|
||||
"""Return a 'reply in <language> unless asked otherwise' instruction, or None when unset.
|
||||
|
||||
Unlike the ``output_language`` lock, a default gives way when the user explicitly asks for
|
||||
another language ("answer in English"). The v2 brain uses it so replies come in the
|
||||
deployment's language (Korean) whatever language the question was heard or written in.
|
||||
Names whatever language it is given, so it works for any default.
|
||||
"""
|
||||
lang = (language or "").strip()
|
||||
if not lang:
|
||||
return None
|
||||
return (
|
||||
f"REPLY LANGUAGE: write your reply in {lang}, whatever language the user's message or "
|
||||
f"any reference text is in. Use another language only when the user explicitly asks you "
|
||||
f"to answer or speak in it; then follow what they asked (just this reply, or from now "
|
||||
f"on in this conversation). Do not mix in other scripts: no Chinese/Hanja characters, "
|
||||
f"Japanese kana or Cyrillic letters inside a {lang} reply. (Numerals and unavoidable "
|
||||
f"proper-noun brand names are fine.)"
|
||||
)
|
||||
|
||||
|
||||
def reply_language_directive(
|
||||
output_language: Optional[str], tts_engine: Optional[str]
|
||||
output_language: Optional[str], tts_engine: Optional[str],
|
||||
default_language: Optional[str] = None,
|
||||
) -> Optional[str]:
|
||||
"""Resolve the reply-language instruction for the chat loop, or None.
|
||||
|
||||
@@ -249,14 +275,19 @@ def reply_language_directive(
|
||||
1. An explicit ``output_language`` lock wins — the deployment serves a
|
||||
single language and owns a TTS voice that can speak it (e.g. Korean
|
||||
MeloTTS). This intentionally overrides the English-only fallback.
|
||||
2. Otherwise, a Piper/Chatterbox TTS engine can only synthesise English,
|
||||
2. Otherwise, a ``default_language`` (the v2 brain: Korean) is used unless
|
||||
the user explicitly asks for another language.
|
||||
3. Otherwise, a Piper/Chatterbox TTS engine can only synthesise English,
|
||||
so force English to avoid garbled audio.
|
||||
3. Otherwise (multilingual TTS, no lock) → None: the assistant replies in
|
||||
4. Otherwise (multilingual TTS, no lock) → None: the assistant replies in
|
||||
the user's own language.
|
||||
"""
|
||||
forced = output_language_directive(output_language)
|
||||
if forced:
|
||||
return forced
|
||||
default = default_language_directive(default_language)
|
||||
if default:
|
||||
return default
|
||||
if (tts_engine or "piper").strip().lower() in _TTS_ENGLISH_ONLY:
|
||||
return ENGLISH_ONLY_DIRECTIVE
|
||||
return None
|
||||
|
||||
@@ -24,6 +24,9 @@ def main() -> int:
|
||||
ap.add_argument("--history", default=None, help="conversation history SQLite file; no history without it")
|
||||
ap.add_argument("--brain", action="store_true",
|
||||
help="answer reply requests with the jarvis reply engine (needs --history)")
|
||||
ap.add_argument("--reply-language", default="Korean",
|
||||
help="answers come in this language unless the asker asks for another; '' for the asker's")
|
||||
ap.add_argument("--tool-language", default="ko", help="ISO code tools get (e.g. the Wikipedia host)")
|
||||
args = ap.parse_args()
|
||||
if args.brain and not args.history:
|
||||
print("--brain needs --history", file=sys.stderr)
|
||||
@@ -56,7 +59,7 @@ def main() -> int:
|
||||
history = HistoryStore(args.history)
|
||||
if args.brain:
|
||||
from .brain import JarvisBrain
|
||||
brain = JarvisBrain()
|
||||
brain = JarvisBrain(language=args.tool_language or None, reply_language=args.reply_language or None)
|
||||
|
||||
async def run() -> None:
|
||||
worker = VoiceWorker(token, transcriber, vad.stream, tts, history=history, brain=brain)
|
||||
|
||||
@@ -118,16 +118,19 @@ class JarvisBrain:
|
||||
tool carry-over follows the person who asked.
|
||||
"""
|
||||
|
||||
def __init__(self, language: Optional[str] = None):
|
||||
def __init__(self, language: Optional[str] = "ko", reply_language: Optional[str] = "Korean"):
|
||||
from jarvis.config import load_settings
|
||||
from jarvis.memory.db import Database
|
||||
|
||||
# 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, so the brain answers in the asker's language (an OUTPUT_LANGUAGE lock still wins).
|
||||
# not spoken; the brain answers in ``reply_language`` (an OUTPUT_LANGUAGE lock still wins).
|
||||
self.cfg = dataclasses.replace(load_settings(), tts_engine="melo")
|
||||
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._lock = threading.Lock()
|
||||
debug_log(f"brain ready: llm={self.cfg.ollama_base_url} model={self.cfg.ollama_chat_model}", "voice_worker")
|
||||
@@ -156,7 +159,8 @@ class JarvisBrain:
|
||||
turn_state.set_chat(chat)
|
||||
try:
|
||||
return run_reply_engine(self.db, self.cfg, None, text, self._memory(user),
|
||||
language=self.language, history=history, legacy_memory=False)
|
||||
language=self.language, history=history, legacy_memory=False,
|
||||
default_language=self.reply_language)
|
||||
finally:
|
||||
turn_state.reset()
|
||||
|
||||
|
||||
@@ -233,10 +233,14 @@ which carry Discord's own time.
|
||||
`cancelled: true` and no audio. Each user keeps their own short-term dialogue
|
||||
memory (tool carry-over) across replies. The brain runs the reply engine with v1's single-user
|
||||
memory off (`legacy_memory=False`: no warm profile, diary or graph), because several people share
|
||||
this assistant and only the asker's scoped history may reach the prompt. It answers in the
|
||||
asker's language: v1's `tts_engine` setting (an English-only Piper voice by default) does not
|
||||
lock replies to English, because v2 speaks through the worker's TTS and chat answers are not
|
||||
spoken. An `output_language` lock in the jarvis settings still wins.
|
||||
this assistant and only the asker's scoped history may reach the prompt. It answers in Korean
|
||||
by default (`--reply-language`, default `Korean`; `''` answers in the asker's language), whatever
|
||||
language the question was heard or written in, and in another language only when the asker
|
||||
explicitly asks for it (just that answer, or from then on, as they said). Tools get the ISO code
|
||||
`--tool-language` (default `ko`). v1's `tts_engine` setting (an English-only Piper voice by
|
||||
default) does not lock replies to English, because v2 speaks through the worker's TTS. An
|
||||
`output_language` lock in the jarvis settings still wins. Eval: `evals/test_reply_language_default.py`.
|
||||
STT (`language` `"ko"`) and TTS (`--melo-language KR`) are Korean by default too.
|
||||
The asker is set in `jarvis.reply.turn_state` for the turn, so tools that keep personal data
|
||||
(meals) store and read it under that person only.
|
||||
- `addressed` and `rank` are the conversation's questions (`app/src/conversation.spec.md`), answered
|
||||
|
||||
Reference in New Issue
Block a user