summaryrefslogtreecommitdiff
path: root/src/ollama_web/prompts/__init__.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-06-27 14:49:12 +0900
committertanaka takashi <isazaka@gmail.com>2026-06-27 14:49:12 +0900
commitf93822cebf6258c3a31e03745183aa2c8bb56f4d (patch)
tree8edfba7593d7db039b117fc4d742fbde78b72da6 /src/ollama_web/prompts/__init__.py
parentb7943a7586fbd0bb0e19aad7732c11a62f72d7c1 (diff)
マルチ言語対応。MCP設定画面の言語切り替え表示にバグあり
Diffstat (limited to 'src/ollama_web/prompts/__init__.py')
-rw-r--r--src/ollama_web/prompts/__init__.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/ollama_web/prompts/__init__.py b/src/ollama_web/prompts/__init__.py
new file mode 100644
index 0000000..59bdb48
--- /dev/null
+++ b/src/ollama_web/prompts/__init__.py
@@ -0,0 +1,57 @@
+"""Localized prompt management for LLM prompts.
+
+Prompts live in JSON files under ``prompts/``. The default language is read from
+``config.settings.language`` and can be overridden per-call via the ``lang``
+argument.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+from typing import Any
+
+from ollama_web.config import settings
+
+_DIR = Path(__file__).resolve().parent
+
+_PROMPTS_CACHE: dict[str, dict[str, Any]] = {}
+
+
+def _load_prompts(lang: str) -> dict[str, Any]:
+ """Load prompts for a language, falling back to an empty dict on error."""
+ path = _DIR / f"{lang}.json"
+ try:
+ with path.open("r", encoding="utf-8") as f:
+ return json.load(f)
+ except (FileNotFoundError, json.JSONDecodeError):
+ return {}
+
+
+def get_prompts(lang: str | None = None) -> dict[str, Any]:
+ """Return the full prompt dictionary for the requested language."""
+ lang = lang or settings.language or "ja"
+ if lang not in _PROMPTS_CACHE:
+ _PROMPTS_CACHE[lang] = _load_prompts(lang)
+ return _PROMPTS_CACHE[lang]
+
+
+def get_prompt(key: str, lang: str | None = None, fallback: str | None = None) -> str:
+ """Return a localized LLM prompt string.
+
+ Args:
+ key: Dot-separated path, e.g. ``"tool_system"``.
+ lang: Optional language override. Uses ``settings.language`` when omitted.
+ fallback: Value returned when the key is missing. Defaults to the key itself.
+
+ Returns:
+ The localized prompt string, or the fallback value if missing.
+ """
+ current: Any = get_prompts(lang)
+ for part in key.split("."):
+ if not isinstance(current, dict) or part not in current:
+ return fallback if fallback is not None else key
+ current = current[part]
+ if isinstance(current, str):
+ return current
+ return fallback if fallback is not None else key