Book Generator

Writes a full book from a title prompt using OpenRouter.

This Python script uses the OpenRouter API (specifically openrouter/free by default) to generate books based on a given title and a prompt for the table of contents. It dynamically creates chapters and subchapters based on the generated TOC, writing the content to a Markdown file.

Features

Prerequisites

Setup

  1. Set Credentials: Preferred: set OPENROUTER_API_KEY in your environment. Fallback: create a file named .api-openrouter in your home directory (~/.api-openrouter) and paste your OpenRouter API key into it. Ensure the file has restrictive permissions. You can override the path via --api-key-file.

  2. Create Virtual Environment: Navigate to the project root directory in your terminal and create a uv environment:

    python -m uv venv .venv
    
  3. Install Dependencies: Install the required packages into the virtual environment using uv. This command ensures pip is available, installs uv inside the venv, and then installs the project dependencies:

    .venv/Scripts/python.exe -m ensurepip ; .venv/Scripts/python.exe -m pip install uv ; .venv/Scripts/python.exe -m uv pip install pytest tenacity requests
    

Usage

The script is run from the command line using the Python interpreter within the virtual environment.

Basic Example (using default model and API key path):

.venv/Scripts/python.exe -m src.main --title "My AI Generated Book"

Example with a custom TOC prompt, specific model, and interactive editing:

.venv/Scripts/python.exe -m src.main --title "Interactive AI Book" --toc-prompt "Create a 5-chapter TOC about advanced AI..." --model "openrouter/free" --interactive-toc

Command-Line Arguments:

Customization

Most customization is handled via the command-line arguments described in the Usage section. You can change the book title, output directory, OpenRouter model, API key location, and control interactive steps without modifying the Python code.

Example TOC Prompt Structure

Ensure your --toc-prompt (if provided) instructs the AI to return valid JSON in the specified format: a list of dictionaries, where each dictionary represents a chapter and contains 'title' (string) and 'subchapters' (list of strings) keys. Instruct the model to output only the JSON list.

Example snippet for a prompt:

... Format the output as a valid JSON list of dictionaries. Each dictionary must have 'title' and 'subchapters' keys. 'subchapters' should be a list of strings. Output ONLY the JSON list, without any introductory text or code fences. For example:  [{\"title\": \"Chapter 1: ...\", \"subchapters\": [\"Sub 1.1\", \"Sub 1.2\"]}, {\"title\": \"Chapter 2: ...\", \"subchapters\": [\"Sub 2.1\"]}]

Output

The generated book will be saved as a Markdown file in the directory specified by --output-dir (defaulting to books). The filename will be the book title converted to lowercase with spaces replaced by underscores (e.g., my_ai_generated_book.md). A corresponding JSON file containing the final Table of Contents structure will also be saved (e.g., my_ai_generated_book.json).

Error Handling

The script includes error handling for various scenarios, such as invalid JSON responses, API errors (including rate limits and content blocking), file not found errors for the API key, and missing keys in the generated table of contents. Error messages are printed to the console and logged.

Future Improvements

License

This project is licensed under the MIT-0 License.

books (6)

Files in books

=

src/tests (1)

Files in src/tests

Related