feat: solution for 'текстовая игра на основе llm + interrupt'

This commit is contained in:
2026-06-24 14:56:51 +03:00
parent f7e3f2a9fa
commit 67ab81df8f
4 changed files with 171 additions and 225 deletions
+26 -48
View File
@@ -1,68 +1,46 @@
```
# Interactive ChooseYourOwnAdventure
# Текстовая игра на основе LLM + interrupt
This project demonstrates a simple text adventure game that uses an LLM (OpenAI) to generate a story opening, presents the user with three choices, and then continues the story based on the chosen option.
The flow is built with **LangGraph** and uses **interrupts** to pause the graph and wait for user input in the console.
## Описание
## Features
Это простая консольная игра «Выбери свою историю», где генеративная модель (LLM) создаёт начало истории и варианты действий, а пользователь выбирает один из них. После выбора модель генерирует короткую концовку.
- Generates a short opening and three distinct choices using an LLM.
- Pauses execution with an interrupt and presents the choices via a console menu.
- Resumes the graph with the users selection and generates a short ending.
- Stores the entire story (topic, opening, choice, ending) in a JSON file.
## Prerequisites
## Требования
- Python 3.10+
- An OpenAI API key. Set it in your environment:
- OpenAI API ключ (или другая модель, настроенная через LangChain)
## Установка
```bash
export OPENAI_API_KEY="sk-..."
```
## Installation
```bash
# Clone the repository
git clone https://github.com/yourusername/interactive-adventure.git
cd interactive-adventure
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
git clone https://git.brojs.ru/kuzakhmetovartur/tekstovaya-igra-na-osnove-llm-interrupt
cd tekstovaya-igra-na-osnove-llm-interrupt
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
```
## Usage
Создайте файл `.env` в корне проекта и добавьте ваш ключ:
```
OPENAI_API_KEY=sk-...
```
## Запуск
```bash
python src/main.py
```
You will be prompted to enter a topic for the adventure.
After the LLM generates the opening and choices, a menu will appear.
Select an option, and the story will continue with a short ending.
The final story will be printed to the console and saved to `adventure_story.json`.
Игра начнётся в консоли. После появления вопросов выберите вариант, используя клавиши со стрелками, и нажмите `Enter`.
## Project Structure
## Как работает
```
interactive-adventure/
├── src/
│ └── main.py # Main application logic
├── requirements.txt # Python dependencies
└── README.md # Documentation
```
1. **intro_node** – генерирует завязку и три варианта действий.
2. **interrupt** – приостанавливает выполнение и передаёт варианты пользователю.
3. **ending_node** – после выбора пользователя генерирует короткую концовку.
## Customization
Граф реализован с помощью `langgraph`, а прерывание обрабатывается через `interrupt(...)`. Состояние сохраняется в `InMemorySaver`, поэтому можно приостановить и возобновить игру в любой момент.
- **LLM Model**: Change the `model` parameter in `ChatOpenAI` inside `src/main.py` to use a different model.
- **Prompt Templates**: Modify the prompt strings in `generate_scene_and_choices` and `generate_ending` to tweak the story style.
- **Number of Choices**: Adjust the parsing logic if you want more or fewer options.
## Лицензия
## License
MIT License
```
MIT