An AI-powered book generator that creates comprehensive books on any topic using Firebase Genkit and OpenRouter API. This tool generates a complete book with multiple chapters, organizing the content into well-structured markdown files.
- Go 1.25.1 or higher
- An API key from OpenRouter or compatible OpenAI API endpoint
- Clone the repository:
git clone <repository-url>
cd ai-book-generator-go- Install dependencies:
go mod download- Create your configuration file:
cp config.example.json config.json- Edit
config.jsonwith your API credentials:
{
"apiKey": "your-actual-api-key-here",
"baseUrl": "https://openrouter.ai/api/v1",
"defaultModel": "openai/google/gemini-2.5-flash"
}Note: You need to append openai/ to the defaultModel since this Go GenKit requires it when using the OpenAI compatible library
The config.json file contains the following settings:
- apiKey: Your API key for the AI service (required)
- baseUrl: The base URL for the API endpoint (default: OpenRouter)
- defaultModel: The AI model to use for generation (default: openai/z-ai/glm-4.5)
Important: The config.json file is ignored by git to protect your API key. Never commit your actual API key to version control.
The application supports two modes: CLI Mode (command-line) and Web Mode (browser interface).
Start the web server:
go run main.go -webOr specify a custom port:
go run main.go -web -port 3000Then open your browser and navigate to:
http://localhost:8080(default)http://localhost:3000(if using custom port)
Generate a book directly from the command line:
go run main.go -topic "How to Learn Programming" -chapters 10| Flag | Description | Default | Required |
|---|---|---|---|
-web |
Run in web server mode | false | No |
-port |
Port for web server | 8080 | No |
-topic |
The topic of the book to generate (CLI mode) | - | Yes (CLI mode) |
-chapters |
Number of chapters to generate | 5 | No |
-config |
Path to configuration file | config.json | No |
Web Mode:
# Start web server on default port (8080)
go run main.go -web
# Start web server on custom port
go run main.go -web -port 3000CLI Mode:
# Generate a 5-chapter book (default)
go run main.go -topic "Introduction to Go Programming"
# Generate a comprehensive 15-chapter guide
go run main.go -topic "Complete Guide to Web Development" -chapters 15
# Use custom config file
go run main.go -topic "AI Ethics" -chapters 7 -config ./configs/alternative.jsonThe generator creates a new directory with the sanitized book title containing:
README.md- Table of contents with links to all chapters01_Chapter_Title.md- First chapter02_Chapter_Title.md- Second chapter- ... (one file per chapter)
Example output structure:
Introduction_to_Go_Programming/
├── README.md
├── 01_Getting_Started_with_Go.md
├── 02_Basic_Syntax_and_Types.md
├── 03_Control_Flow.md
├── 04_Functions_and_Methods.md
└── 05_Concurrency_in_Go.md
To build a standalone binary:
go build -o book-generatorThen run in web mode:
./book-generator -webOr in CLI mode:
./book-generator -topic "Your Topic" -chapters 10-
Start the server:
go run main.go -web
-
Open your browser to
http://localhost:8080
Make sure you've created config.json from config.example.json and placed it in the project root.
Your config.json file is missing the API key. Make sure all required fields are filled in.
This could indicate:
- Invalid API key
- Network connectivity issues
- API rate limiting
- Model availability issues
Check your API key and try again. You can also try a different model in the config.
- Ensure the server is running (
go run main.go -web) - Check that port 8080 is not in use, or specify a different port with
-port - Verify firewall settings aren't blocking the connection
- Check browser console for JavaScript errors
- The WebSocket connection requires the HTTP server to be running
- Check browser console for connection errors
- Try refreshing the page
- Ensure no proxy/firewall is blocking WebSocket connections
Book generation can take several minutes depending on:
- Number of chapters
- API response time
- Model speed
The tool generates all chapters concurrently to minimize total time.
You can modify the generation parameters in generator/book.go:
chapterTimeout: Maximum time to wait for a chapter (default: 10 minutes)maxTokens: Maximum tokens per generation (default: 3000)temperature: AI creativity level (default: 0.6)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is provided as-is for educational and personal use.
- Built with Firebase Genkit
- Powered by OpenRouter
- OpenAI Go SDK for API compatibility
For issues, questions, or suggestions, please open an issue on the repository.

