текстовая игра на основе llm + interrupt: README.md

This commit is contained in:
2026-05-27 11:48:34 +00:00
parent d85a7a9c4a
commit 9da380f419
@@ -0,0 +1,108 @@
# Interactive Text Adventure with LLM and Interrupts
A minimal “Choose Your Own Story” game built on **LangGraph** that lets a large language model generate the narrative, pause for user input, and then continue based on the choice.
---
## 📖 Overview
- The graph starts by asking an LLM to write the opening of a story and propose several actions.
- It then *interrupts* the flow, presenting those options in the console via **questionary**.
- After the player selects an option, the graph resumes and asks the LLM to finish the scene with that choice.
This demonstrates:
- State management with `TypedDict`
- Custom interrupt nodes (`langgraph.types.interrupt`)
- Humanintheloop pattern
- Checkpointing (inmemory in this example)
---
## ⚙️ Installation
```bash
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install dependencies
pip install langgraph==0.1.9 \
langchain==1.2.10 \
langchain-openai==1.1.9 \
questionary
```
> **OpenAI API key**
> The script uses `ChatOpenAI` from LangChain, which requires an OpenAI key. Export it as an environment variable:
```bash
export OPENAI_API_KEY="sk-..."
# Windows: set OPENAI_API_KEY=sk-...
```
---
## 📂 Project Structure
```
.
├── solution.py # Main script the graph definition and runner
└── README.md # Youre reading it now
```
---
## 🚀 Running the Game
```bash
python solution.py
```
Youll see something like:
```
LLM: "You find yourself in a dimly lit tavern..."
Options:
1) Order a drink
2) Ask about the town's rumors
3) Leave immediately
Enter choice (1-3):
```
Type `1`, `2` or `3` and press **Enter**. The LLM will then generate a short continuation based on your selection.
---
## 📋 Example Session
```text
$ python solution.py
LLM: "You stand at the crossroads of destiny, with three paths before you..."
Options:
1) Take the left path into the forest.
2) Walk straight toward the looming castle.
3) Turn back to the village.
Enter choice (1-3): 2
LLM: "You stride confidently toward the castle gates, feeling the weight of history upon your shoulders..."
```
Feel free to modify `solution.py` to add more nodes, change prompts, or persist checkpoints with a different saver.
---
## 🔧 Customization Tips
| What | How |
|------|-----|
| **Different LLM** | Replace `ChatOpenAI()` with another LangChain model (e.g., Anthropic). |
| **Persisting state** | Swap `InMemorySaver` for `SQLiteSaver`, `RedisSaver`, etc. |
| **More complex choices** | Add additional interrupt nodes or loop back to earlier parts of the story. |
---
## 📜 License
This project is provided as a learning example and may be freely used, modified, and distributed.
---