mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 16:30:35 +08:00
`parse_language()` ended in a bare `return "zh"`, so ja/ko/fr/de/ru/ar all resolved to Chinese. Book prose normalized its code correctly while quiz, Deep Research and Mastery Path went through this parser — which is why a Japanese book came back with Chinese quizzes. Delegate to the existing `normalize_language()` instead of keeping a second, weaker implementation, and pass unknown codes through. What makes the model answer in a language is the directive built from the code, not the prompt file, so no new locale resources are needed. Two fallback chains assumed the result was zh or en and had to follow: * `PromptManager` now tries `[code, base locale, "en"]`, so "zh-tw" reuses the Chinese files and "ja" lands on English rather than Chinese. The chains for zh and en are unchanged. * Mastery Path's `[lang, "zh" if lang != "zh" else "en"]` gets the same treatment, and its notebook prompts now append the shared language directive — without it a Japanese learner would only have traded Chinese output for English. An unconfigured language still defaults to "zh", so deployments that never set one keep the behaviour they shipped with. Reported with an exact root-cause analysis by @takeshi-o.
102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
"""Mastery Path LLM prompt templates.
|
|
|
|
The prompt text lives in ``deeptutor/learning/prompts/{en,zh}.yaml`` so the
|
|
capability and API can follow the active UI language. The module-level constants
|
|
remain as the Chinese defaults for older tests/imports.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import yaml
|
|
|
|
from deeptutor.services.config import parse_language
|
|
from deeptutor.services.prompt.language import append_language_directive
|
|
|
|
_PROMPT_DIR = Path(__file__).with_name("prompts")
|
|
|
|
|
|
def _get_nested(data: dict[str, Any], path: str, default: str = "") -> str:
|
|
value: Any = data
|
|
for part in path.split("."):
|
|
if not isinstance(value, dict):
|
|
return default
|
|
value = value.get(part)
|
|
return value if isinstance(value, str) else default
|
|
|
|
|
|
@lru_cache(maxsize=8)
|
|
def get_learning_prompts(language: str = "zh") -> dict[str, Any]:
|
|
"""Load localized Mastery Path LLM prompts."""
|
|
lang = parse_language(language)
|
|
# Regional codes reuse their base locale's file ("zh-tw" -> zh.yaml); a
|
|
# language with no file of its own lands on English, not Chinese (#712).
|
|
candidates = dict.fromkeys([lang, lang.split("-", 1)[0], "en", "zh"])
|
|
for candidate in candidates:
|
|
path = _PROMPT_DIR / f"{candidate}.yaml"
|
|
if path.exists():
|
|
return yaml.safe_load(path.read_text(encoding="utf-8")) or {}
|
|
return {}
|
|
|
|
|
|
def prompt_text(language: str, path: str, default: str = "") -> str:
|
|
return _get_nested(get_learning_prompts(language), path, default)
|
|
|
|
|
|
def notebook_generation_prompts(language: str, records_json: str) -> tuple[str, str]:
|
|
prompts = get_learning_prompts(language)
|
|
system_prompt = _get_nested(prompts, "notebook.system", NOTEBOOK_SYSTEM)
|
|
user_template = _get_nested(prompts, "notebook.user", NOTEBOOK_USER)
|
|
# Only en/zh ship prompt files, so a Japanese learner is handed English
|
|
# scaffolding. The directive — the same one book, quiz and Deep Research
|
|
# already append — is what makes the answer come back in the language that
|
|
# was actually asked for (#712).
|
|
system_prompt = append_language_directive(system_prompt, parse_language(language))
|
|
return system_prompt, user_template.format(records_json=records_json)
|
|
|
|
|
|
def default_module_name(language: str, index: int) -> str:
|
|
template = prompt_text(language, "notebook.default_module_name", "模块 {index}")
|
|
return template.format(index=index)
|
|
|
|
|
|
DIAGNOSTIC_SYSTEM = prompt_text("zh", "diagnostic.system")
|
|
DIAGNOSTIC_USER = prompt_text("zh", "diagnostic.user")
|
|
EXPLAIN_SYSTEM = prompt_text("zh", "explain.system")
|
|
EXPLAIN_USER = prompt_text("zh", "explain.user")
|
|
FEYNMAN_SYSTEM = prompt_text("zh", "feynman.system")
|
|
FEYNMAN_USER = prompt_text("zh", "feynman.user")
|
|
PRACTICE_SYSTEM = prompt_text("zh", "practice.system")
|
|
PRACTICE_USER = prompt_text("zh", "practice.user")
|
|
ERROR_DIAGNOSIS_SYSTEM = prompt_text("zh", "error_diagnosis.system")
|
|
ERROR_DIAGNOSIS_USER = prompt_text("zh", "error_diagnosis.user")
|
|
REVIEW_SYSTEM = prompt_text("zh", "review.system")
|
|
REVIEW_USER = prompt_text("zh", "review.user")
|
|
NOTEBOOK_SYSTEM = prompt_text("zh", "notebook.system")
|
|
NOTEBOOK_USER = prompt_text("zh", "notebook.user")
|
|
|
|
|
|
__all__ = [
|
|
"DIAGNOSTIC_SYSTEM",
|
|
"DIAGNOSTIC_USER",
|
|
"ERROR_DIAGNOSIS_SYSTEM",
|
|
"ERROR_DIAGNOSIS_USER",
|
|
"EXPLAIN_SYSTEM",
|
|
"EXPLAIN_USER",
|
|
"FEYNMAN_SYSTEM",
|
|
"FEYNMAN_USER",
|
|
"NOTEBOOK_SYSTEM",
|
|
"NOTEBOOK_USER",
|
|
"PRACTICE_SYSTEM",
|
|
"PRACTICE_USER",
|
|
"REVIEW_SYSTEM",
|
|
"REVIEW_USER",
|
|
"default_module_name",
|
|
"get_learning_prompts",
|
|
"notebook_generation_prompts",
|
|
"prompt_text",
|
|
]
|