Files
DungeonsDragons/backend/app/llm/client.py
T
Thorsten 624644619c Add character detail, markdown rendering, wider layout, and LLM provider switch
- Character sheets gain structured combat_stats, abilities (name +
  plain-language explanation), and equipment fields, populated by the
  upsert_character_sheet tool; the detail page renders them as their
  own sections instead of one prose block, with a player-card-style
  attribute grid (full ability names + modifiers).
- Fix double-escaped unicode occasionally left in tool-call JSON
  arguments (a model quirk) and stop the app's own json.dumps calls
  from re-introducing it (ensure_ascii=False).
- Render chat messages as markdown (react-markdown + Tailwind
  typography) instead of raw text, and widen the games/characters/
  chat layout to use more of the screen on desktop.
- Sharpen the DM system prompt: state the ability + DC for each
  offered action option before the player commits, and narrate rolls
  as DC → result → pass/fail before the story consequence; also stop
  re-asking Session-0 questions already answered in the game's
  name/description.
- Make the DM's LLM provider configurable (xai default, lmstudio for
  a local OpenAI-compatible server via host.docker.internal) instead
  of hardcoded to x.ai.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-01 15:18:15 +02:00

29 lines
831 B
Python

from functools import lru_cache
from pathlib import Path
from openai import AsyncOpenAI
from app.config import settings
PROMPTS_DIR = Path(__file__).parent / "prompts"
@lru_cache
def get_dm_system_prompt() -> str:
return (PROMPTS_DIR / "dm_system_prompt.txt").read_text(encoding="utf-8")
@lru_cache
def get_game_setup_prompt() -> str:
return (PROMPTS_DIR / "game_setup_prompt.txt").read_text(encoding="utf-8")
@lru_cache
def get_llm_client() -> AsyncOpenAI:
if settings.llm_provider == "lmstudio":
# LM Studio's local server accepts any non-empty string when no key is configured.
return AsyncOpenAI(
api_key=settings.lmstudio_api_key or "lm-studio", base_url=settings.lmstudio_base_url
)
return AsyncOpenAI(api_key=settings.xai_api_key, base_url=settings.xai_base_url)