Inspiration
As a long-time Dungeon Master for tabletop RPGs, I love the storytelling but struggle with the immense "cognitive load." I found myself having to narrate the story, act out NPCs, referee complex rules, and manually track every inventory item, quest update, and world change in a messy notebook. I asked myself: What if an AI could handle the bookkeeping, allowing me to focus entirely on the creativity? I didn't just want a chatbot; I wanted a "World Steward"—an agent that listens to my story and silently updates a structured database of the world state in the background, visualizes the scene, and prepares the recap for the next session automatically.
What it does
LoreForge is an AI-powered campaign companion that transforms unstructured storytelling into structured data and cinematic visuals.
- Automated State Tracking: It listens to the gameplay text and maintains a live, structured JSON database of the "World State" (NPCs, Factions, Active Quests) and "Inventory." I don't need to write down that the party picked up the Sword of Omens; LoreForge updates the inventory automatically.
- Cinematic Visualization: It detects when I am describing a scene and uses Imagen to generate atmospheric fantasy illustrations in real-time, strictly guard-railed to avoid text artifacts (like signposts or HUDs) to maintain immersion.
- Session Recaps on Autopilot: At the end of a session, it reads the entire event log and generates a fully styled Reveal.js slide deck. It creates an outline, summarizes key events, and generates custom background art for every slide, giving my group an instant "Previously on..." presentation for our next meetup. ## How we built it I built the backend using Python (FastAPI) and Google Cloud Firestore, heavily leveraging the Google GenAI SDK.
- The "Steward" Engine: I implemented a "State Derivation" pattern. Instead of just appending text to a history, I periodically feed the recent event log into Gemini Pro with a complex system instruction. I force a strict JSON output schema that represents the campaign's truth.
- Robust JSON Parsing: To make the system reliable, I wrote a custom heuristic parser (in utils.py) that strips Markdown fences, fixes trailing commas, and balances braces. This ensures the AI's output is always valid Python dictionaries, even if the model produces slightly invalid JSON syntax.
- Multimodal Generation: I used a dual-pipeline approach. The primary stream handles text via Server-Sent Events (SSE) for low latency. Parallel threads analyze the text for visual opportunities and trigger Imagen (via the Vertex AI/Gemini API) to generate scene art, which I push to the client asynchronously.
- Slide Generation: For the presentation service, I built a multi-step agentic workflow: i. Ask Gemini to generate a JSON outline based on the session log. ii. Iterate through the slides and generate specific image prompts for the background of each slide. iii. Render the final structure into HTML/Jinja2 templates. ## Challenges we ran into
- The "Wall of Text" Problem: RPG sessions generate massive amounts of text. Simply feeding the whole history into the context window became expensive and slow. I had to implement logic to curate "Recent Events" and rely on the "Canonical World State" (the JSON object) as the long-term memory, effectively compressing the history into state.
- Structured Output Consistency: Getting an LLM to update a complex nested JSON object (managing relationships between Factions and NPCs) without breaking the format was tough. I solved this by providing a "Schema Hint" in the prompt and implementing a retry loop that feeds error messages back to Gemini if the JSON fails to parse.
- Text Artifacts in Images: Early image generations kept including RPG UI elements (like health bars) or illegible text on signposts. I had to heavily tune the negative prompts (my _text_artifact_guardrail function) to force "clean" cinematic views. ## Accomplishments that we're proud of
- The Recap Deck Generator: It feels magical to click one button after a long game session and watch a beautiful, 10-slide presentation appear with custom art and accurate bullet points summarizing what just happened.
- State Persistence: Seeing the "Inventory" update automatically in the UI after I simply narrate "You find a potion in the chest" proves that the AI is actually understanding the game mechanics, not just predicting the next word. ## What we learned
- Context vs. State: I learned that for long-running applications, you cannot rely solely on the context window. You must transmute conversation into structured State, which acts as a compressed, queryable memory.
- Agentic Workflows: Breaking tasks down (Text > State > Visuals > Presentation) yielded far better results than trying to ask a single "super-prompt" to do everything at once. ## What's next for Lore Forger
- Voice Integration: Integrating Gemini Live to allow me to simply speak to the room, with LoreForge listening via microphone as a silent participant.
- Virtual Tabletop (VTT) Integration: Exporting the generated World State JSON directly into platforms like FoundryVTT or Roll20 to auto-populate NPC sheets.
- Multi-Model Orchestration: Using Gemini Flash for quick state updates and Gemini Pro/Ultra for complex narrative generation.
Built With
- css
- fastapi
- firestore
- gcp
- gemini
- html
- javascript
- pydantic
- python
Log in or sign up for Devpost to join the conversation.