Text Adventure with LLM and Interrupt
A lightweight “choose‑your‑own‑story” game that lets a large language model (LLM) generate the plot while the user makes decisions in real time via the console.
The project demonstrates how to combine LangGraph state management, LangChain for LLM calls, and questionary for interactive prompts.
Table of Contents
- What It Does
- Prerequisites
- Installation
- Running the Game
client.py– Interactive CLIagent.py– Stand‑alone demo (no user input)
- Example Session
- Project Structure
What It Does
- LLM generates a hook – the opening line of an adventure.
- The graph pauses (
interrupt) and presents three options to the player viaquestionary. - Player selects an option; the choice is fed back into the LLM.
- LLM writes a short continuation based on that choice.
- The cycle repeats until the story ends or the user quits.
The game showcases:
- LangGraph state nodes and checkpoints
- Human‑in‑the‑loop via custom interrupt handling
- Simple CLI integration with
questionary
Prerequisites
| Component | Minimum Version | Notes |
|---|---|---|
| Python | 3.10+ | Tested on 3.11 |
| Ollama | – | Optional if you use a local LLM; otherwise set OPENAI_API_KEY for OpenAI. |
| Qdrant | – | Not required for this demo (only used in advanced setups). |
Environment variables
# If using OpenAI
export OPENAI_API_KEY="sk-..."
# If using Ollama locally
export OLLAMA_HOST="http://localhost:11434"
Installation
git clone https://github.com/yourname/text-adventure.git
cd text-adventure
python -m venv .venv # optional but recommended
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
requirements.txt contains:
langchain-openai>=0.2.0
langgraph>=0.1.0
questionary>=1.10.0
python-dotenv>=1.0.0
Running the Game
1. Interactive CLI (client.py)
Launch the game and follow on‑screen prompts.
python client.py
What happens
- The program asks for a theme (e.g., “fantasy”, “sci‑fi”).
- LLM creates an opening hook.
- Three choices appear; pick one.
- LLM writes a short continuation.
- Repeat until you type
quitor the story ends.
2. Stand‑alone Demo (agent.py)
Runs the same logic but without user interaction – useful for quick tests or automated runs.
python agent.py
It will automatically generate a theme, hook, and options, then print the final story to stdout.
Example Session
$ python client.py
Enter a theme (or press Enter for random): mystery
LLM Hook:
"In the dim glow of the lantern, Detective Marlowe stared at the cryptic note left on the desk..."
Choose an action:
1. Inspect the note closely.
2. Call the forensic team.
3. Leave the office and investigate the alley.
> 1
LLM Continuation:
"She unfolded the brittle paper, revealing a series of numbers that seemed to map out the city’s underground tunnels..."
The game continues until you decide to quit or the story naturally concludes.
Project Structure
.
├── agent.py # Non‑interactive demo
├── client.py # Interactive CLI with questionary
├── requirements.txt # Dependencies
└── README.md # This file
Feel free to tweak agent.py or client.py to experiment with different LLM prompts, themes, or interrupt handling strategies.
Happy adventuring!