Files
homework-solutions/solutions/69b1a07c67bbf488a1177da4_текстовая_игра_на_основе_llm___interrupt

Text Adventure with LLM and Interrupt

A lightweight “chooseyourownstory” 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

  1. LLM generates a hook the opening line of an adventure.
  2. The graph pauses (interrupt) and presents three options to the player via questionary.
  3. Player selects an option; the choice is fed back into the LLM.
  4. LLM writes a short continuation based on that choice.
  5. The cycle repeats until the story ends or the user quits.

The game showcases:

  • LangGraph state nodes and checkpoints
  • Humanintheloop 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 onscreen prompts.

python client.py

What happens

  • The program asks for a theme (e.g., “fantasy”, “scifi”).
  • LLM creates an opening hook.
  • Three choices appear; pick one.
  • LLM writes a short continuation.
  • Repeat until you type quit or the story ends.

2. Standalone 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 citys underground tunnels..."

The game continues until you decide to quit or the story naturally concludes.


Project Structure

.
├── agent.py          # Noninteractive 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!