AI Interactive Fiction Generator Builds Stories You Actually Control

7月17日 Published inAI Writing Tools

AI Interactive Fiction Generator: Storytelling with Tangible Consequences

This narrative engine uses OpenAI’s GPT models to generate dynamic, choice-driven stories. By selecting a genre and literary style, users trigger the system to construct original plots, characters, and branching endings in real time. The experience moves away from pre-written scripts and recycled tropes, ensuring every playthrough is unique.

Before the first scene begins, the backend generates a comprehensive story map. This structure is rendered in-browser via Mermaid.js, allowing you to visualize the branching paths and potential conclusions before making your first move. To deepen the immersion, every session also creates a fictional author and a book title specifically tailored to your chosen style.

How It Works

  1. Dynamic Story Assembly The AI generates opening scenes, rising actions, and resolutions based on your selected mode—such as Eastern fantasy or Western arcana. Every request produces fresh prose through live inference rather than pulling from a static database.

  2. Randomized Authorship When you start a game, the system assigns a plausible pen name and title. This turns a technical demo into a more cohesive experience, like picking a unique paperback off a shelf.

  3. Visible Narrative Mapping After the initial call, the backend provides a JSON-based story structure. The frontend then converts this into a flowchart. You can see the narrative's potential destination, even if the path to get there remains a mystery.

  4. Meaningful Branching Every choice you make forks the narrative. The AI tracks your previous decisions and steers the story toward one of the pre-defined endings on the map. This ensures the narrative structure remains coherent and your choices have real weight.

  5. Adaptive Literary Styles The model receives specific style directives for every game. Whether it is Gothic horror or Wuxia, the prose adjusts its tone and vocabulary without requiring manual prompt tuning from the user.

Tech Stack

Category Technology
Backend FastAPI (Python), SQLAlchemy & SQLModel (ORM), Redis (Caching & SSE queues)
Frontend Vanilla JavaScript, Tailwind CSS, Mermaid.js (Diagrams)
AI OpenAI GPT models
Deployment Docker & Docker Compose

Local Deployment

Ensure you have Docker and Docker Compose installed. Clone the repository and follow these steps:

  1. Copy the environment template: cp .env.example .env (macOS/Linux) or copy .env.example .env (Windows).
  2. Enter your OpenAI API key into the .env file.
  3. Build and launch the containers: docker-compose up --build.
  4. (Optional) If modifying Tailwind styles, run npm run build in a separate terminal.
  5. Access the application at http://localhost:1888.

API Endpoints

Method Path Description
POST /api/v1/game Initializes a new session; returns the opening scene and story map.
POST /api/v1/game/{game_id}/choice Submits a player decision and retrieves the subsequent scene.
GET /api/v1/game/{game_id} Retrieves the current state of an ongoing game.
DELETE /api/v1/game/{game_id} Terminates the current session.

The Initialization Process

  1. The frontend sends a POST request to /api/v1/game.
  2. The backend triggers the generate_initial_scene() function.
  3. The story service requests a full story graph in JSON format from the LLM.
  4. The LLM returns the structure, including nodes, edges, and all possible endings.
  5. The service saves the session metadata (author, title, map) to the database.
  6. The backend sends the game_id, scene text, and story map back to the frontend.
  7. Mermaid.js renders the visual map.
  8. The first scene is displayed, and the system waits for the player's first decision.