Files
DeepTutor/deeptutor/book/blocks/_language.py
Bingxi Zhao (Frank) 2745e8bde9 feat(book): introduce book capability and restructure guide/co_writer modules
- Add new `deeptutor/book/` module with multi-agent pipeline
  (ideation, spine, page planner, source explorer, synthesizer)
  and rich block system (text, code, figure, quiz, flash cards,
  callout, deep dive, timeline, animation, concept graph, etc.)
- Add `/api/routers/book.py` and `deeptutor_cli/book.py` entry points
- Add web UI under `web/app/(workspace)/book/` with sidebar,
  chat panel, page reader, spine editor, progress timeline, and
  block renderers
- Move `deeptutor/agents/co_writer` -> `deeptutor/co_writer` and
  `deeptutor/agents/guide` -> `deeptutor/guide` (then remove guide
  entirely now superseded by book)
- Remove legacy guide UI, hooks, types, and API router
- Update visualize agents/prompts, notebook service, prompt manager,
  session turn runtime, and shared services to support new flows
- Refresh chat composer, sidebar, notebook selector, and locales

Made-with: Cursor
2026-04-20 00:33:33 +08:00

75 lines
2.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Shared language directive for BookEngine LLM prompts.
Every block generator and agent that writes user-facing prose should append
``language_directive(language)`` to its **system prompt**. This guarantees the
output stays in the book's chosen language even when the rest of the prompt
contains English token names (block types, role hints, JSON keys, etc.) or
when the underlying source material is in a different language.
"""
from __future__ import annotations
_LANGUAGE_LABELS: dict[str, str] = {
"zh": "中文(简体)",
"zh-cn": "中文(简体)",
"zh-tw": "繁體中文",
"en": "English",
"ja": "日本語",
"ko": "한국어",
"es": "Español",
"fr": "Français",
"de": "Deutsch",
"ru": "Русский",
"pt": "Português",
"it": "Italiano",
}
def normalize_language(language: str | None) -> str:
return (language or "en").strip().lower() or "en"
def language_label(language: str | None) -> str:
code = normalize_language(language)
if code in _LANGUAGE_LABELS:
return _LANGUAGE_LABELS[code]
base = code.split("-", 1)[0]
return _LANGUAGE_LABELS.get(base, language or "English")
def language_directive(language: str | None) -> str:
"""Strict instruction that forces the LLM to respond in *language*.
The directive is intentionally repetitive — empirically that's what gets
LLMs to stop drifting back to English when the prompt mixes languages.
"""
code = normalize_language(language)
label = language_label(code)
if code.startswith("zh"):
return (
"\n\n[语言要求 / Language] "
f"请严格使用{label}撰写所有面向读者的文本(标题、正文、解释、提示、过渡句、"
"题干、选项等),即使参考资料、JSON 字段名或英文术语出现在 prompt 中也"
"不得切换语言;保留必要的专有名词原文(如人名、产品名、公式中的变量符号"
f"等)即可,其余一律使用{label}。"
)
if code == "en":
return (
"\n\n[Language] Write ALL reader-facing text (titles, prose, "
"explanations, hints, transitions, quiz stems, options, etc.) in "
"English. Do NOT switch languages even if the source material, "
"JSON keys, or examples in this prompt are in another language. "
"Keep proper nouns (people, products, formula symbols) in their "
"original form."
)
return (
f"\n\n[Language] Write ALL reader-facing text strictly in {label}. "
"Do NOT switch languages even if the source material, JSON keys, or "
"examples in this prompt are in a different language. Keep proper "
"nouns (people, products, formula symbols) in their original form."
)
__all__ = ["language_directive", "language_label", "normalize_language"]