ChatGPT AI: A Book Writer (Difficulty: 3)
-
Introduction
In this tutorial, you will learn to build a simple but very useful app: a book writer. The user can specify any topic, and the app will use AI 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 an AI block to write the book accordingly.
Book Writing Strategy Discussion
Before we start writing any code, we need to discuss the basic idea of how to use AI to write books. You might think it is as easy as telling the AI to “write an entire book for me”. However, this would not work well for 2 reasons:
- Output Length Limit: all AI models have limits on how many words they can generate each time, so they can not generate a book with many words.
- Output Quality: The user may not like the book written by AI, so they may wait a long time for nothing.
To solve these issues, there is a simple but clever solution. We will breakdown the process into 2 steps:
- we will first ask AI to write a table of content, which includes the title of each chapter
- we then repeatedly ask AI to write each chapter.
Now, let’s get started.
Step 1 - Create 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 Topic Label
First, we will add a label that says “Topic:”. It will tell the user where to input the topic 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 Topic 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 Topic Input Textbox
Next, we will add a textbox for the user to input the topic of the book. Please take the same two steps: add the textbox widget, and then adjust its size and position. It should look like this:
Here is the example code:
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. The items in the “numbers” list becomes the options in the dropdown automatically.
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. Feel free to update their styles if you like.
Step 8 - Add the Rich Textbox for the Output
Finally, the last widget we need is a rich textbox for displaying the book content. A rich textbox is better than a plain textbox because it can display text in markdown format, which makes it easier to read. So here is the complete stage:
Here is the new block to add the rich textbox:
Step 9 - Clear the Rich Textbox
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 clear any existing content in the rich 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 rich textbox (“richtextbox1”) to an empty value:
Step 10 - Compose the Initial Prompt
Now, let’s prepare the initial prompt for AI. To make it write the title and table of contents, we need to specify the book topic and the number of chapters. For example, the prompt may look like this:
You are helping me write a book with [X] chapters about this topic: [X] Now give me the title and table of contents.
for the number of chapters and the book topic.Let’s put this prompt into a variable named “request”, and use the “join” block to compose this prompt dynamically based on user input. Since it contains 5 parts joined together, the block is quite long, so we need 2 images to show it:
As shown, we are reading the number of topics using “value of widget menu1”, and also reading the book top using “value of widget textbox1”. We use “\n\n” to add some empty lines for better formatting. For testing, we can print the “request” variable into the console. Suppose we input “time travel” as the book topic, these 2 blocks should print the following into the console panel:
Step 11 - Send Prompt to AI
Next, let’s send this request to AI. Instead of using ChatGPT, we will be using the “LLM” AI block. It works almost the same way as the ChatGPT block. The only difference is that you need to specify which AI model to use:
- small: this is a small and fast model, which is good enough for most common use cases;
- large: this is a large and slow model, but it is smarter than the small model, so you can use it if you need AI to work on some very challenging task.
The specific model to be used may be ChatGPT or some other AI models, which may change over time as newer and better AI models emerge.
Here is the code to send our request to the small LLM AI, store the response in a variable named “response”, and then “append” this response to the rich textbox:
Here is what we will get:
Step 12 - Remove Extra Comments
A very common and annoying problem with ChatGPT and other AI models is that they tend to “chat” with us. So whenever we ask them to write something, they tend to say something before or after the exact content we need. Such sentences are often called “introductory commentary” and “concluding commentary”.
For example, in the screenshot above, we see that the AI model’s output contains some introductory commentary like “Okay, here’s a title and table of contents for a book about time travel …”, and some concluding commentary like “I hope it inspires you to write a great book …”.
Such comments make the AI sound more natural when it is chatting with a human, but they become unnecessary when we are using AI as a tool.
To fix this issue, we need to explicitly tell the AI model not to say these things. For example, we can update the last part of the prompt like this:
\n\nNow write the title and table of contents in 100 words with no introductory or concluding commentary.
Now we get the book title and TOC without any unnecessary comments:
Step 13 - Write the First Chapter
Now let’s try to ask AI to write chapter 1 when the second button is clicked. Later we will ask it to write more chapters.
The prompt will ask the AI to write chapter 1 and also avoid giving us any commentary:
Write chatper 1 in 200 words with no introductory or concluding commentary.
Note that since we are developing the program, we can limit the word count to a small number like 200, so we don’t have to wait too long for the response to come back.After we receive the new response, we will first add 2 new lines using “
”, and then append the new response to the rich textbox.
Note that we are using the “continue” session type, so that the AI model will remember the previous request we sent to it and its response, which contains the book topic, title and TOC.Now it can successfully generate Chapter 1:
Step 14 - 2 Improvements
If you test the program a few times, you may find that the AI model still adds some comments before the content we want. In addition, the book content is in plain text, so it is kind of hard to read:
To solve both issues, we can add some additional requirements in our second prompt like this:Write chatper 1 in 200 words in markdown format with absolutely no introductory or concluding commentary.
We are asking the AI to write in markdown format, which will lead to better formatting. We are also telling it to add absolutely no commentary.
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’ go from 1 to the total number of chapters, then compose the request to ChatGPT using the ‘chapter’ variable in the loop:
Note that each chapter doesn’t have to be only 200 words. Here is a demo of the program, where we set each chapter to be within 800 words:
Step 16 - Add Additional Details to the Topic
Since AI models can follow our instructions very well, we can give it additional details when we describe the book topic.
For example, we can expand the “time travel” topic to the following:
time travel, an exciting fiction book for teenagers that talks about 2 middle-school students that travel back to the dinosaur age.
Here is the book written by the AI:
Dino-Dash: A Middle School Time Warp
Challenges
There are many places where this book writer can be enhanced. Here are some examples:-
Streaming Mode: Instead of waiting for the entire chapter to come back, we can display its content as the AI writes it. You will need to make sure the entire chapter is complete before asking the AI to write the next chapter.
-
Rewriting a chapter: This means instead of writing all chapters automatically, only write one chapter at a time, then pause to ask the user’s approval before continuing to the next chapter. If the user does not like it, he/she can provide some additional comments, and the AI writer will rewrite the given chapter based on the comment.
-
-
This post is deleted!