Skip to content

donvito/ai-book-generator-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Book Generator

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.

Create a book

Sample book

Prerequisites

  • Go 1.25.1 or higher
  • An API key from OpenRouter or compatible OpenAI API endpoint

Installation

  1. Clone the repository:
git clone <repository-url>
cd ai-book-generator-go
  1. Install dependencies:
go mod download
  1. Create your configuration file:
cp config.example.json config.json
  1. Edit config.json with 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

Configuration

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.

Usage

The application supports two modes: CLI Mode (command-line) and Web Mode (browser interface).

Web Mode (Recommended)

Start the web server:

go run main.go -web

Or specify a custom port:

go run main.go -web -port 3000

Then open your browser and navigate to:

  • http://localhost:8080 (default)
  • http://localhost:3000 (if using custom port)

CLI Mode

Generate a book directly from the command line:

go run main.go -topic "How to Learn Programming" -chapters 10

CLI Flags

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

Examples

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 3000

CLI 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.json

Output

The generator creates a new directory with the sanitized book title containing:

  • README.md - Table of contents with links to all chapters
  • 01_Chapter_Title.md - First chapter
  • 02_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

Building

To build a standalone binary:

go build -o book-generator

Then run in web mode:

./book-generator -web

Or in CLI mode:

./book-generator -topic "Your Topic" -chapters 10

Web Interface Guide

Starting the Web Server

  1. Start the server:

    go run main.go -web
  2. Open your browser to http://localhost:8080

Troubleshooting

Error: "config.json not found"

Make sure you've created config.json from config.example.json and placed it in the project root.

Error: "apiKey is required"

Your config.json file is missing the API key. Make sure all required fields are filled in.

Error: "failed to generate outline"

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.

Web Interface Not Loading

  • 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

WebSocket Connection Failed

  • 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

Slow generation

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.

Customization

Adjusting Generation Parameters

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)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is provided as-is for educational and personal use.

Acknowledgments

Support

For issues, questions, or suggestions, please open an issue on the repository.


About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors