Add rulebook RAG pipeline and LLM-driven game setup wizard

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>
This commit is contained in:
Thorsten
2026-08-31 20:03:05 +02:00
parent f37dc9fa76
commit 419f5e3a89
26 changed files with 82471 additions and 51 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
from app.models.character import Character
from app.models.game import Game, GameParticipant
from app.models.message import Message
from app.models.rulebook_chunk import RulebookChunk
from app.models.user import User
__all__ = ["User", "Game", "GameParticipant", "Character", "Message"]
__all__ = ["User", "Game", "GameParticipant", "Character", "Message", "RulebookChunk"]
+23
View File
@@ -0,0 +1,23 @@
import uuid
from pgvector.sqlalchemy import Vector
from sqlalchemy import Integer, String, Text
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.db import Base
EMBEDDING_DIM = 384
class RulebookChunk(Base):
__tablename__ = "rulebook_chunks"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
source_document: Mapped[str] = mapped_column(String(length=200), nullable=False, index=True)
chunk_index: Mapped[int] = mapped_column(Integer, nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False)
embedding: Mapped[list[float]] = mapped_column(Vector(EMBEDDING_DIM), nullable=False)
doc_metadata: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)