AI Novelist

repository·main·Indexed 42 days ago

https://github.com/kele-tao/ai_novel

A content-generation workbench for co-writing novels and courseware using OpenAI-compatible LLM APIs. It features a live console for real-time writing, a dynamic worldview system for narrative consistency, and sliding-window context management to handle long-form content without exceeding token limits.

Tokens
5.3K
Snippets
11
Records
27
Agent score
51%

What's inside AI Novelist

  1. How Sliding Window Context Management works

    main

    To prevent context window overflow (common in LLMs at 4K-32K tokens) as a novel grows to 50+ chapters, AI Novelist automatically manages the prompt context:

    • Recent Chapters: Full summaries of the last 3 chapters are preserved.
    • Older Chapters: Earlier chapters are automatically compressed into a 200-word "long-term arc" summary.
    • Worldview: The worldview context is automatically truncated at the head and tail if it exceeds 4000 words.

    This ensures that the prompt size for Chapter 50 remains similar to Chapter 5, maintaining high generation quality without crashing the LLM.

  2. How Sliding-window context manages long novels

    main

    To prevent prompts from exceeding the LLM's context window as a novel grows, AI Novelist uses a sliding-window context strategy:

    1. Recent Context: The most recent 3 chapters are kept as full summaries.
    2. Distant Context: Earlier chapters are compressed into a single ~200-word "distant arc" summary.
    3. Worldview: The Worldview (lore) is truncated to include only the head and tail, capped at 4000 characters.

    This ensures that the prompt size remains constant regardless of whether you are writing chapter 5 or chapter 50.

  3. How the Command Center (指挥台) works

    main

    The Command Center is a real-time streaming interface for monitoring AI generation. Instead of waiting for a loading spinner, you see text being generated token-by-token.

    Key capabilities include:

    • Real-time Monitoring: Watch the AI's thought process and writing progress.
    • Task Control: Pause a generation task to resume later.
    • Dynamic Worldview Editing: If the AI makes a mistake (e.g., wrong character name), you can edit the worldview immediately. The updated settings will be used for the next chapter.
    • Thought Library: View the plot outline the AI has planned for the current chapter.
    • Pomodoro Timer: Includes a focus timer (25/5 pattern) for writing sessions.
  4. How the Console and Worldview work together

    main

    The Console is the real-time interface for watching the AI write. It provides several interactive capabilities:

    • Live Streaming: Watch the AI's prose being generated token-by-token.
    • Pause/Resume: Stop the generation process and continue later.
    • Worldview Editing: You can edit the 'Worldview' in the sidebar mid-stream. If the AI makes a mistake (e.g., misnaming a character), updating the Worldview ensures all subsequent chapters follow the corrected lore.
    • Regeneration: You can click the ↻ icon next to any specific chapter to regenerate only that chapter without affecting the rest of the book.

    This interaction loop allows for a highly collaborative 'co-writing' experience where the user manages the high-level lore and the AI manages the prose generation.

  5. Configure AI Novelist via environment variables

    main

    AI Novelist is configured exclusively through environment variables. Do not edit the source code for configuration.

    Required Variables

    • AI_NOVEL_LLM_URL: The OpenAI-compatible /v1/chat/completions endpoint.
    • AI_NOVEL_MODEL: The name of the model to use.

    Cloud API Variables

    If using a provider like OpenAI or DeepSeek, you must provide:

    • AI_NOVEL_API_KEY: Your provider's bearer token.

    To see estimated costs in the UI, set:

    • AI_NOVEL_PRICE_PROMPT: USD per 1K prompt tokens.
    • AI_NOVEL_PRICE_COMPLETION: USD per 1K completion tokens.
    • AI_NOVEL_CURRENCY: The currency symbol (e.g., $, ¥).

    Windows PowerShell Syntax

    On Windows, use the $env: prefix:

    $env:AI_NOVEL_LLM_URL = "http://localhost:8000/v1/chat/completions"
    # Required
    export AI_NOVEL_LLM_URL="http://localhost:8000/v1/chat/completions"
    export AI_NOVEL_MODEL="meta-llama/Llama-3-8B-Instruct"
    
    # If using a cloud API
    export AI_NOVEL_API_KEY="sk-xxxxxxxxxxxx"
    
    # Strongly recommended: enable cost tracking
    export AI_NOVEL_PRICE_PROMPT="0.0001"
    export AI_NOVEL_PRICE_COMPLETION="0.0002"
    export AI_NOVEL_CURRENCY="$"
  6. Run AI Novelist

    main

    After installing and configuring your environment variables, start the server using the following command:

    python main.py

    Once running, access the web interface at http://127.0.0.1:5000 (or the address/port specified by AI_NOVEL_HOST and AI_NOVEL_PORT).

  7. Install AI Novelist

    main

    To install AI Novelist, clone the repository and install the required Python dependencies. This requires Python 3.8+.

    git clone https://github.com/yourname/AI_Novel.git
    cd AI_Novel
    pip install -r requirements.txt
  8. Configure Nginx as a reverse proxy for AI Novelist

    main

    If you are running AI Novelist behind an Nginx reverse proxy, you must disable proxy buffering to allow the Server-Sent Events (SSE) used by the Live Console to function correctly.

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffering off;  # REQUIRED for SSE
        proxy_http_version 1.1;
    }
    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffering off;  # required for SSE
        proxy_http_version 1.1;
    }
  9. How to customize writing prompts

    main

    If you want to modify the underlying instructions used by the AI, you can edit the prompt templates directly in the source code:

    1. Open app/prompts/templates.py.
    2. Modify the desired template.
    3. Restart the server for changes to take effect.
    4. In the Works Library, use the ↻ (rewrite) button on a chapter to re-run it with the new prompt.
  10. How to back up your work

    main

    All generated content is stored locally in the works_library/ directory (or the path specified by WORKS_LIBRARY_PATH_OVERRIDE).

    To back up your novels, simply copy the works_library/ folder. The content is stored as plain Markdown files.

  11. How to write your first novel

    main

    Follow these steps to start generating content:

    1. Create Project: Enter a theme (e.g., "A story about a time-traveling detective"), select a template (e.g., "Web Novel Hit"), and click Generate Outline.
    2. Confirm Outline: You can edit the generated outline. Note that the title of the first chapter (formatted as Book Title: xxx) determines the final novel name.
    3. Start Generation: Click Confirm and Start Generating to enter the Command Center (指挥台).
    4. Use the Command Center: Watch the AI write in real-time. You can:
      • Pause/Resume the task.
      • Edit the Worldview (via the "Edit" button in the sidebar) to correct character names or settings; changes apply to subsequent chapters.
      • Rewrite a specific chapter by clicking the ↻ icon in the chapter directory.
    5. Manage Works: Access generated content in the Works Library (作品库), where chapters can be edited individually.
    6. Export: Download your project as a ZIP file using the ↓ ZIP button at the top.
  12. How to share your work via Public Showroom links

    main

    You can share your completed works with others in a read-only mode:

    1. Go to the Works Library (作品库).
    2. Select your novel.
    3. Click the 🔗 Share (分享) button at the top.
    4. The link is automatically copied to your clipboard.

    Note: Friends viewing the link see a pure reading mode without sidebars or tools. Links automatically expire after 7 days.