An AI Book Writer (Difficulty: 3)
-
Introduction
In this tutorial, you will learn to build a simple but handy app: a book writer. The user can specify any topic, and the app will utilize AI to write an entire book automatically. The next time you need to learn something, you won’t need to buy a book, since you can write a book for yourself!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. You will learn a new trick to make the AI model generate a lot of content.
You will also learn to use a new AI block, which works similarly as the ChatGPT block.
Book Writing Strategy
Before we begin writing any code, we need to discuss the basic concept of using 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 tokens (words) they can generate in each response, 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 be disappointed after waiting a long time.
To address these issues, a simple yet clever solution exists. We will break down the process into a few steps:
- We will first ask the AI model to write a table of content, which includes the title of each chapter;
- We then repeatedly ask the AI model to write each chapter.
Now, let’s get started.
Step 1 - Create an Empty Project
Create a new project and name it “AI Book Writer.” Delete the sprite with the dog, as we will use the “Empty1” sprite for coding.
Step 2 - The Topic Label
First, we will add a label that says “Topic:”. It will instruct the user on where to enter the book’s topic. As shown below, ensure that the label’s background is set to 100% transparent and its border width is set 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 top left. 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 text box for the user to input the book’s topic. 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 generate 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 as its source, and move the dropdown 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 formats the text better for reading. 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 the 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: [Y] Now give me the title and table of contents.
and [Y] 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 the user input. Since it contains 5 parts joined together, this block is quite long, so we need to show it as 2 parts here:
As shown, we are reading the number of topics using the “value of widget menu1” and also reading the book title using the “value of widget textbox1”. We use “\n\n” to add some empty lines for better formatting.For testing purposes, we are printing the “request” variable into the console panel. When we input “time travel” as the book topic, these 2 blocks should print the following into the console panel:
Step 11 - Send the Prompt to AI
Next, let’s send this request to the 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 tasks.
Note that even the “small” model is still a bit smarter than the ChatGPT model, so you can use this block instead of the ChatGPT block in your projects.
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 Unnecessary Comments
One common (and kind of annoying) problem with ChatGPT and other AIs is that they often talk too much. When we ask them to write something, they usually add extra words before or after what we actually want. These extra parts are called introductory commentary and concluding commentary.Let’s look at an example. In the screenshot above, you can see that the AI started its response with something like, “Okay, here’s a title and table of contents for a book about time travel …” That’s an introductory comment.
These extra comments can be nice when the AI is having a conversation with a person, because they make it feel more friendly and natural. But when you’re using AI as a tool—to get a job done—those comments just get in the way.
So, how do we stop this from happening? Simple: we need to tell the AI not to add those extra comments. One way to do that is by changing the prompt (the instruction we give the AI). For example, we can rewrite the last part of our 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
Next, let’s ask AI to write chapter 1 when the second button is clicked. Later, we will ask it to write more chapters.This prompt will ask the AI to write chapter 1 and also avoid giving us any commentary:
Write Chapter 1 in 200 words with no introductory or concluding commentary.
Note that since we are still developing the program, we can limit the word count to a small number, such as 200, so we don’t have to wait too long for a response.After we receive the new response, we will first append two new empty lines using “
”, and then append the new response to the rich text box. The “LLM model” block is shown as 2 parts below since it is too long.
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.
These new changes will help avoid the issues most of the time.
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 the AI model using the ‘chapter’ variable in the loop:
Note that the chapter length doesn’t have to be only 200 words in real use. 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, when we input the topic, instead of simply saying “time travel”, we can use the following description:
time travel, an exciting fiction book for teenagers that talks about 2 middle-school students that travel back to the dinosaur age.
If you are curious, here is the complete book written by the AI for that topic:
Dino-Dash: A Middle School Time Warp
Additional Challenges
There are many places where this book writer program can be enhanced. Here are some examples for your inspiration:-
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.
-
Interactive Mode: Instead of writing all chapters at once, have the AI model write one chapter at a time, then pause to ask for user approval before continuing to the next chapter. If the user does not like it, he/she can input some comments, and the AI writer will rewrite the given chapter based on those comments.
-
info-creaticode
-
This post is deleted!