Files
DungeonsDragons/backend/app/alembic/versions/7c3f9a2e1d44_add_rulebook_chunks.py
Thorsten 419f5e3a89 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>
2026-08-31 20:03:05 +02:00

44 lines
1.4 KiB
Python

"""add rulebook_chunks (pgvector RAG)
Revision ID: 7c3f9a2e1d44
Revises: 48846ed4fe0e
Create Date: 2026-08-31 19:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from pgvector.sqlalchemy import Vector
# revision identifiers, used by Alembic.
revision: str = '7c3f9a2e1d44'
down_revision: Union[str, None] = '48846ed4fe0e'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.execute("CREATE EXTENSION IF NOT EXISTS vector")
op.create_table(
'rulebook_chunks',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('source_document', sa.String(length=200), nullable=False),
sa.Column('chunk_index', sa.Integer(), nullable=False),
sa.Column('content', sa.Text(), nullable=False),
sa.Column('embedding', Vector(384), nullable=False),
sa.Column('doc_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.PrimaryKeyConstraint('id'),
)
op.create_index(
op.f('ix_rulebook_chunks_source_document'), 'rulebook_chunks', ['source_document'], unique=False
)
def downgrade() -> None:
op.drop_index(op.f('ix_rulebook_chunks_source_document'), table_name='rulebook_chunks')
op.drop_table('rulebook_chunks')
op.execute("DROP EXTENSION IF EXISTS vector")