ChatGPT AI: A Book Writer (Difficulty: 3)
-
Introduction
In this tutorial, you will learn to build a simple but useful app: a book writer. The user can specify any topic, then the program will use ChatGPT to write an entire book automatically.
We will first add some widgets to allow the user to specify what kind of book he/she wants, then use the ChatGPT block to write the book accordingly.
Step 0 - Book Writing Strategy Discussion
Before we start writing any code, we need to discuss the basic idea of how to use ChatGPT to write books. As we all know, there is a word limit on ChatGPT’s response: our input plus its output can not exceed 4096 tokens, which is about 3000 words. So if our book will be longer than 3000 words, we can not get the complete book from ChatGPT in a single request.
To solve this issue, there is a simple but clever solution. We will breakdown the process into 2 steps:
- we will first ask ChatGPT to write a table of contents, which includes the title of each chapter
- we then repeatedly ask ChatGPT to write each chapter.
Now let’s get started.
Step 1 - An Empty Project
Create a new project, and name it “ChatGPT- Book Writer”. Delete the sprite with the dog, and we will just use the “Empty1” sprite for coding.
Step 2 - The Title Label
First, we will add a label that says “Title:”. It will tell the user where to input the title of the book. As shown below, make sure you set the background of the label to 100% transparent, and set its border width to 0. You will get a label at the center of the stage.
Step 3 - Move the Title Label
We need to move the label to the left top. The easiest way is to use the “Widget Position” tool, which allows us to change the size of a widget and drag it to a new position. The new position and size will be stored in the block directly, so the next time the program runs, the widget will appear at the new position automatically.
Step 4 - Add the Title Input Textbox
Next, we will add a textbox for the user to input the book title. We will take the same 2 steps: add the widget, then adjust its size and position.
Step 5 - Add the Chapter Count Label
Next, we will need to allow the user to select the number of chapters from a dropdown menu. It will not only allow the user to control the length of the book, but also allow us to easily go through each chapter one by one.
We will first add a label that looks like this:
Here are the blocks to do that:
Step 6 - Add the Dropdown Menu
Next, we will add a dropdown to allow the user to pick a number between 3 and 10:
To do that, we first need to add a new list named “numbers”, and insert the numbers 3 to 10 into it:
Next, we can add the “dropdown menu” widget that uses the “numbers” list, and move it to the desired position:
Step 7 - Add the “TOC” Button and “Content” Button
Next, we need to add 2 more buttons:
- The “TOC” button will generate the table of contents.
- The “Content” button will generate the chapters of the book based on the table of contents.
They will look like this:
Here are the new blocks to add the buttons and update their styles:
Step 8 - Add the Textbox for the Output
Finally, the last widget we need is another textbox for displaying the book. It needs to have multiple rows and allow scrolling.
Here is the new block to add the textbox:
Step 9 - Add the Title to the Book
After the user inputs the book title (feel free to pick any title) and the number of chapters, he/she will first click the “TOC” button. When that happens, we first need to copy the book title from the input textbox to the output textbox:
To do that, we need to use the ‘when widget clicked’ block to detect when the user clicks the “TOC” button. When that happens, we need to set the value of the output textbox (“booktextbox”) to the value of the input textbox (“textbox1”). To show that this is the title, we can add “<” and “>” to the title using a join block:
Step 10 - Ask ChatGPT to Generate the Table of Contents
Now we are finally ready to make use of the ChatGPT API. To make it write the table of contents, we just need to tell it the book title and the number of chapters. For example, here is our first version of the prompt:
You are helping me to write a book. The title is <Time Travel 101>. It will have 4 chapters. Now write the table of contents.
Now let’s put this prompt into a variable named “request”, then send this request to ChatGPT, and store the response in a variable named “response”. Then we can “append” this response to the output textbox:
Here is what we will get:
This is a good start. We’ll fix the issues one by one.
Step 11 - Make ChatGPT Only Return the Table of Contents
Obviously, the biggest issue is that the response from ChatGPT is not “clean”. Since it is trained to “chat”, it naturally adds some sentences before and after the table of contents.
Now let’s try to improve our request so that it only returns the table of contents. For example, we can try to say it like this:
You are helping me to write a book. The title is <Time Travel 101>. It will have 4 chapters. Now return only the table of contents and nothing before or after it. I don't want anything other than the table of contents!
Unfortunately, we always get a “chatty” response:
Can you find a good prompt that always works?
Step 12 - Mark the Start and End of the Table of Contents
The bad news is that it’s almost impossible to find a prompt that would get us a clean TOC with the current version of ChatGPT. So we have to find another solution. The idea is that we accept there may be unwanted sentences before and after the table of contents, and we ask ChatGPT to tell us where is the table of contents. Specifically, we can ask ChatGPT to add ``` before and after the table of contents like this:
You are helping me to write a book. The title is <Time Travel 101>. It will have 4 chapters. Now write the table of contents. Add ``` before and after it.
Now we find ChatGPT would give us the table of contents between a pair of ``` almost all the time:
Now we just need to extract the part of the response between the 2 ``` marks. We can easily do that using the “part N of T by X” block:
The way it works is that it will first split the response by the ``` marks. Since there are 2 of them in the response, this step will split the response into 3 parts, and the middle part (part 2) will be the table of contents by itself.
This is what you should get after the change:
Step 13 - Compose the Request Using User Input
Another minor issue is that our prompt is “hardcoded”. It is not using the user’s input at all. To change that, we need to use the “join” block to compose the request using the value of the textbox and dropdown menu:
After that, we will always pick up the title and chapter count specified by the user:
Step 14 - Write the First Chapter
Now let’s try to ask ChatGPT to write the first chapter. We just need to tell it the book title and the table of contents, which happens to be the current value of the output textbox. So we just need to save that into a new variable “toc”, then compose the request like this:
Note that since we are developing the program, we can limit the word count to a small number like 100, so we don’t have to wait too long for the response to come back.The request2 variable would look like this if we print it to the console panel:
Since it looks good, now we just need to send this request2 to ChatGPT, and append the new response to the book textbox.
Now we can successfully generate Chapter 1:
Step 15 - Write All Chapters with a Loop
For the last step, we need to use a loop to write all the chapters one by one. We can use a for-loop to make a variable ‘chapter’ to go from 1 to the total number of chapters, then compose the request to ChatGPT using the ‘chapter’ variable in the loop:
Here is the final demo of the program:
Enhancements
This version of the book writer is very crude. There are many places where it can be enhanced. Here are some examples:
- Sometimes ChatGPT would not generate the table of contents properly. How can we improve the request to avoid this?
-
The book in the output textbox is not well formatted. Try to add some empty lines between the different parts of the book.
-
Sometimes the table of contents includes a “conclusion” after the given number of chapters, but our program does not generate any conclusion yet. You can try to add that at the end.
-
-
This post is deleted!