419f5e3a89
RAG: switch Postgres to pgvector, chunk and embed the three D&D rulebooks locally via sentence-transformers, and retrieve relevant excerpts per DM turn (query = latest player message) to ground the system prompt. Retrieval runs off the event loop and is capped by a relevance threshold and a max character budget so it can't blow up context size or cost. Game setup wizard: creating a game now opens a short chat where the DM asks about genre, length, and the player's experience level, then proposes a name and description via a tool call. The player can edit both before creating the game. Stateless endpoint — the frontend carries the conversation, no DB needed since the game doesn't exist yet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
import json
|
|
import logging
|
|
import uuid
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.config import settings
|
|
from app.llm.client import get_dm_system_prompt, get_llm_client
|
|
from app.llm.context import build_context
|
|
from app.llm.tools import character_sheet, dice
|
|
from app.models.message import Message
|
|
from app.rag.retrieval import build_rag_block
|
|
|
|
logger = logging.getLogger("app.llm.orchestrator")
|
|
|
|
MAX_TOOL_ROUNDS = 5
|
|
MAX_TOKENS = 4096
|
|
|
|
TOOLS = [
|
|
{"type": "function", "function": dice.TOOL_SCHEMA},
|
|
{"type": "function", "function": character_sheet.TOOL_SCHEMA},
|
|
]
|
|
|
|
|
|
async def _execute_tool_call(
|
|
session: AsyncSession, game_id: uuid.UUID, tool_name: str, tool_input: dict
|
|
) -> dict:
|
|
"""Runs one tool call. Errors are returned as a payload (not raised), so the DM sees them
|
|
as a tool result and can recover instead of the whole turn crashing."""
|
|
try:
|
|
if tool_name == "roll_dice":
|
|
return dice.roll(tool_input["notation"])
|
|
if tool_name == "upsert_character_sheet":
|
|
return await character_sheet.upsert(session, game_id, tool_input)
|
|
return {"error": f"Unknown tool {tool_name!r}"}
|
|
except Exception as exc: # noqa: BLE001
|
|
logger.warning("Tool call %s failed: %s", tool_name, exc)
|
|
await session.rollback()
|
|
return {"error": str(exc)}
|
|
|
|
|
|
async def run_dm_turn(
|
|
session: AsyncSession, game_id: uuid.UUID, latest_player_message: str | None = None
|
|
) -> Message:
|
|
client = get_llm_client()
|
|
system_prompt = get_dm_system_prompt()
|
|
|
|
if latest_player_message:
|
|
rag_block = await build_rag_block(session, latest_player_message)
|
|
if rag_block:
|
|
system_prompt = f"{system_prompt}\n\n{rag_block}"
|
|
|
|
messages = [{"role": "system", "content": system_prompt}]
|
|
messages.extend(await build_context(session, game_id))
|
|
|
|
final_text = ""
|
|
for _ in range(MAX_TOOL_ROUNDS):
|
|
response = await client.chat.completions.create(
|
|
model=settings.dm_model,
|
|
max_tokens=MAX_TOKENS,
|
|
messages=messages,
|
|
tools=TOOLS,
|
|
)
|
|
choice = response.choices[0]
|
|
message = choice.message
|
|
final_text = message.content or ""
|
|
|
|
if not message.tool_calls:
|
|
break
|
|
|
|
messages.append(message.model_dump(exclude_unset=True))
|
|
|
|
for tool_call in message.tool_calls:
|
|
tool_input = json.loads(tool_call.function.arguments)
|
|
result = await _execute_tool_call(session, game_id, tool_call.function.name, tool_input)
|
|
messages.append(
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": tool_call.id,
|
|
"content": json.dumps(result),
|
|
}
|
|
)
|
|
else:
|
|
if not final_text:
|
|
final_text = "(Der Dungeon Master braucht einen Moment länger als erwartet — bitte versuche es erneut.)"
|
|
|
|
dm_message = Message(game_id=game_id, sender_type="dm", content=final_text)
|
|
session.add(dm_message)
|
|
await session.commit()
|
|
await session.refresh(dm_message)
|
|
return dm_message
|