The BookWriter script is a Node.js application that uses OpenAI's language model to generate a book chapter by chapter. It interacts with the user to define the book's theme, incorporate user inputs for each chapter, and maintains narrative continuity by summarizing previous chapters.
Before you begin, ensure you have the following installed on your system:
- Node.js (version 14 or higher)
- npm (Node Package Manager)
- TypeScript and ts-node (for running TypeScript files)
- OpenAI API Key
First, clone this repository to your local machine or download the book_writer.ts script.
git clone <repository_url>
cd <repository_directory>Install the necessary npm packages by running:
npm installIf you haven't installed TypeScript and ts-node globally, install them as development dependencies:
npm install typescript ts-node @types/node --save-devYou need an OpenAI API key to use the language model. Set up your API key as an environment variable:
For Unix-based systems (Linux/macOS):
export OPENAI_API_KEY='your-api-key-here'For Windows Command Prompt:
set OPENAI_API_KEY='your-api-key-here'For Windows PowerShell:
$env:OPENAI_API_KEY='your-api-key-here'Alternatively, you can create a .env file in the root directory of your project and add:
OPENAI_API_KEY='your-api-key-here'Make sure to replace 'your-api-key-here' with your actual OpenAI API key.
Ensure that the @langchain/openai package is installed. If not, install it using:
npm install @langchain/openaiYou can run the book_writer.ts script using ts-node:
ts-node book_writer.tsIf you prefer to compile the TypeScript file to JavaScript first, you can do so:
npx tsc book_writer.ts
node book_writer.jsWhen you run the script, it will guide you through the process of generating a book. Here's what to expect:
The script will prompt you:
Enter the theme or genre of the book:
Type in your desired theme or genre, such as "Science Fiction Adventure" or "Mystery Thriller," and press Enter.
The script enters a loop to generate chapters. For each chapter:
You will be prompted:
Enter instructions or plot points for Chapter X (optional):
- Optional Input: You can provide specific instructions, plot points, character developments, or leave it blank for the model to generate the content based on the theme and previous chapters.
- Examples:
- "Introduce a conflict between the protagonist and antagonist."
- "The main character discovers a hidden ability."
The script generates the content of the chapter using the OpenAI model, considering:
- The book's theme.
- Summaries of previous chapters.
- Any instructions you've provided.
The generated chapter is saved as a Markdown file in the book_output/chapters directory:
- File Name:
chapter_X.md(whereXis the chapter number)
The script creates a concise summary of the chapter to maintain context for future chapters.
- The summary is saved in the
book_output/summariesdirectory asX.txt.
You will be asked:
Do you want to write the next chapter? (yes/no):
- Type
yesory: Continues to the next chapter. - Type
noorn: Ends the writing session.
When you choose not to continue, the script will display:
Book writing session completed.
The script creates an output directory with the following structure:
book_output/
├── chapters/
│ ├── chapter_1.md
│ ├── chapter_2.md
│ └── ...
└── summaries/
├── 1.txt
├── 2.txt
└── ...
chapters/: Contains the full content of each chapter.summaries/: Contains summaries of each chapter for context in generating subsequent chapters.
You can adjust the model parameters by modifying the BookWriter class constructor in book_writer.ts:
this.model = new ChatOpenAI({
modelName: 'gpt-4o-mini',
temperature: 0.7,
maxTokens: 2000,
});modelName: Specifies the OpenAI model to use.temperature: Controls the creativity of the output (0.0 to 1.0).- Lower values produce more deterministic outputs.
- Higher values produce more creative outputs.
maxTokens: The maximum number of tokens in the generated response.
- Be Specific with Instructions: Providing clear instructions or plot points can guide the model to produce content that aligns with your vision.
- Review Generated Content: Read each chapter to ensure it meets your expectations. You can manually edit the Markdown files if needed.
- Monitor the Narrative: Keep an eye on the story's coherence, especially as it progresses over multiple chapters.
If you encounter authentication errors:
- Check Your API Key: Ensure that your OpenAI API key is correctly set as an environment variable.
- API Key Permissions: Verify that your API key has the necessary permissions to access the models.
If the script cannot find certain modules:
- Install Missing Packages: Run
npm installto install all dependencies. - Check Typings: Ensure that
@types/nodeis installed for TypeScript definitions.
If the script cannot read or write files:
- Check Directory Permissions: Ensure that you have read and write permissions for the project directory and subdirectories.
- Run as Administrator: On Windows, you might need to run the terminal as an administrator.
If the model returns unexpected responses or errors:
- Adjust Model Parameters: Try changing the
temperatureormaxTokenssettings. - Review Prompts: Ensure that the prompts provided to the model are well-structured.
- Extend the Script: You can modify the script to include character tracking, world-building details, or other features.
- Change Output Formats: If you prefer a different file format, adjust the
saveChaptermethod to save files with the desired extension. - Integrate a GUI: For a more user-friendly experience, consider integrating a graphical user interface.
If you'd like to contribute to the project, feel free to submit pull requests or report issues on the repository's issue tracker.
This project is licensed under the MIT License.