How maoxiaoyuz/long-novel-gpt works
Project Overview
• Long-Novel-GPT is a full-stack application that lets users generate long novels via a multi-phase creative workflow (大纲 → 章节 → 正文). It leverages GPT and other large language models to generate and refine story content.
• The architecture includes components for handling API calls to different LLM services, a rich collection of prompt templates (organized under the prompts folder), and both backend and frontend parts—all containerized via Docker (with dedicated Dockerfiles for overall, backend, and frontend).
Main Components
a. Frontend:
• Directory: “frontend”
• Features: Contains the UI assets (HTML, CSS, JavaScript), an Nginx config for serving the static content, and example novel data.
• Role: Acts as the client-facing web interface where users interact with the system (e.g., initiating creative requests, monitoring progress, configuring settings).
b. Backend:
• Directory: “backend”
• Features: Contains the main backend application (app.py, healthcheck.py, summary.py, xapp.py, etc.), utilities, and configuration (setting.py, requirements.txt).
• Role: Processes API requests coming from the frontend, manages workflow logic (creating chapters, outlines, etc.), and handles the "reflective loop" to refine story generation.
c. Core Library:
• Directory: “core”
• Features: Contains shared functionality (e.g., writer utilities, parsing, and functions to manage both frontend and backend operations).
• Role: Encapsulates business logic that is re-used across the application, enabling the transformation of prompts into usable story segments.
d. LLM API Integration:
• Directory: “llm_api”
• Features: Modules for different LLM providers (OpenAI, Baidu, SparkAI, etc.), integration with MongoDB-related caching (mongodb_cache, mongodb_cost) and price configurations.
• Role: Acts as the abstraction layer for communicating with external large language model services, managing API keys, endpoints, cost tracking, and caching.
e. Prompts and Templates:
• Directory: “prompts”
• Features: Rich assortment of prompt files (YAML, text, and Jinja2 templates) for different creative tasks (创作剧情、创作正文、审阅, etc.).
• Role: Provides the structured instructions that drive each stage of novel creation, allowing for modular and community-driven contributions.
f. Docker and Deployment:
• Files: Dockerfile’s at the root, in “backend”, “frontend”, docker_build.sh, docker_run.sh, .env.example, etc.
• Role: Containerizes different components to simplify deployment, with instructions for environment configuration and inter-container communication (note the special parameters like --add-host for local LLM services).
Relationships and Interaction Flow
• User/Client → Frontend: Browser accesses the frontend static assets served (often by Nginx as configured in nginx.conf) which handle user interactions.
• Frontend → Backend: UI interactions trigger AJAX calls to the backend, which provides APIs for initiating novel generation requests.
• Backend → Core & Prompts: The backend uses the core library to process user requests, utilizes prompt templates (from the prompts directory) to generate instructions for text creation phases.
• Backend → LLM API: Backend components then call corresponding LLM API modules (in llm_api) to interact with external language models for generating text.
• Data Feedback Loop: Results from external API calls are fed back into the workflow (using utilities in “core” and custom scripts in “custom”) to refine content iteratively.
• Docker Environment: The Docker configuration encapsulates both frontend and backend components, ensuring consistent deployment. Environment variables (.env) and scripts (docker_run.sh) mediate how components interact, including linking to local model services if needed.
Architectural Patterns and Design Principles
• Separation of Concerns: The clear division into frontend, backend, core utilities, prompt templates, and API integration layers.
• Modularity & Extensibility: The use of multiple directories (especially for prompts and llm_api) illustrates how the system can add new language models or creative templates with minimal interdependency.
• Containerization: Dockerfiles for backend and frontend indicate a microservices-like approach, although components remain tightly integrated through API calls.