Back to projects

AI Game Master

PythonAILLMsStreamlitDockerOllamaLangChain

Overview

Finding a good Dungeon Master is hard. Building one that runs entirely on your laptop turned out to be considerably more achievable.

AI Game Master is a fully local, self-hosted RPG experience. Choose your campaign setting, create a character, and let a local language model narrate an adaptive story — handling combat, tracking quests, managing your inventory, and maintaining a persistent world across sessions. Everything runs on your machine via Ollama and Docker. No subscriptions, no API keys, no data leaving your computer.

What it does

  • 5 campaign settings — High Fantasy, Dark Fantasy, Sci-Fi, Post-Apocalyptic, Cyberpunk
  • 6+ character classes — Warrior, Mage, Rogue, Ranger, Cleric, Paladin, and Sci-Fi variants, each with distinct stat profiles
  • Full combat system — initiative rolls, attack dice, damage resolution, and enemy AI turns all handled in Python before being passed to the GM for narration
  • Persistent save states — full game state serialised to JSON with multiple save slots, stored in a named Docker volume
  • Level up system — XP thresholds, stat growth, HP increases, and narrative level-up scenes
  • Quest tracker — active and completed quests with objectives
  • World map — location graph built with NetworkX and rendered with Matplotlib
  • Voice narration — the GM reads its own narration aloud via edge-tts
  • Multiplayer support — multiple players can connect to the same session

Tech stack

  • Streamlit — UI and session management
  • LangChain + langchain-ollama — LLM orchestration and agent logic
  • Ollama (llama3.2) — local AI model, runs on CPU, no API key required
  • NetworkX + Matplotlib — world map as a location graph
  • edge-tts — GM voice narration
  • Pillow — scene image handling
  • Docker — single-command setup and isolated runtime

Key design decisions

Dice rolls are resolved entirely in Python before the LLM ever sees the result. This was deliberate — letting the model roll its own dice produces inconsistent outcomes and a strong bias toward dramatic results. By handling randomness in code and passing the resolved numbers to the GM as context, the game stays fair and the LLM's job is purely narrative.

The world map uses NetworkX to model locations as a graph — nodes are places, edges are connections. This gives the GM structured spatial information to work with rather than relying on it to keep geography consistent across a long session.

Save state is serialised to JSON and stored in a named Docker volume, which means game saves persist across container restarts without any external database.

Results

A fully playable, locally-hosted RPG that runs on average consumer hardware. Responses take 10–30 seconds on a CPU-only machine — slow enough to notice, fast enough not to break immersion when you're waiting for the GM to describe what happens next.