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.
openrouter/free) for content generation.uv): This project uses uv for environment management. Install it if you haven't already (e.g., pip install --user uv).pytest, tenacity, requests) will be installed into a virtual environment.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.
Create Virtual Environment: Navigate to the project root directory in your terminal and create a uv environment:
python -m uv venv .venv
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
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:
--title (str, required): The title of the book.--toc-prompt (str, optional): A specific prompt for generating the Table of Contents. If omitted, a default prompt is used.--model (str, optional): The OpenRouter model to use (e.g., 'openrouter/free'). If omitted, the app will use the contents of ~/.model-openrouter when present, otherwise fallback to openrouter/free.--api-key-file (str, optional, default='~/.api-openrouter'): Path to the file containing the OpenRouter API key (fallback if env var not set).--output-dir (str, optional, default='books'): The directory to save the generated book files.--interactive-toc (flag, optional): Pause execution after TOC generation to allow manual editing of the <book_title>.json file before proceeding.--browse-after-toc (flag, optional): Display the current book content (if any) after TOC generation/loading and before generating chapters.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.
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\"]}]
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).
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.
This project is licensed under the MIT-0 License.
Files in books
Files in src/tests
Back-translation, a GAN-style prompt loop, an NMT model and a human vs AI classifier.
Spatial reasoning tests for LLMs, with a multi-model leaderboard.
CLI chatbot with swappable personalities such as tsundere and deredere.
GPU-accelerated text processing pipeline.
Scores AI conversations by their effect on neurochemistry.
Generates function call graphs for Python code.