Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    An AI Book Writer (Difficulty: 3)

    Tutorials
    2
    2
    1118
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      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!

      chapter123.gif

       

      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.

      374cddee-0216-4c77-9655-fe02a1abaa23-image.png

       
       

      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.

      positionlabel1.gif

       
       

      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:

      b8a6fdeb-be8e-41d8-925c-29546415203f-image.png

       
       
       
      Here is the example code:

      1bd213a4-a863-41e9-9917-f438a3d4d9aa-image.png

       
       

      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:

      23666a6c-bef3-43fb-8158-80e587e891a4-image.png

       
      Here are the blocks to do that:

      344fa59f-5589-4f28-b03d-edf505322195-image.png

       
       

      Step 6 - Add the Dropdown Menu

       
      Next, we will add a dropdown to allow the user to pick a number between 3 and 10:

      4b0fd679-14f7-4ac3-87b8-681e504a7026-image.png
       
      To do that, we first need to add a new list named “numbers”, and insert the numbers 3 to 10 into it:

      7d7c5edb-30f0-4c2e-89f3-1cd7794defaa-image.png

       
      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.

      6caad46a-2c06-4b39-be37-184d5b1427c4-image.png

       
       

      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:

      8647e3ad-b455-49b2-97f0-525446875fb4-image.png

       
      Here are the new blocks to add the buttons. Feel free to update their styles if you like.

      89433247-50b5-451d-be39-347c959654df-image.png

       
       

      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:

      d58dfdb2-285f-40f1-b835-290bbdef0c15-image.png

       
      Here is the new block to add the rich textbox:

      6959f13f-6fb9-4550-91ce-310eedcc97bc-image.png

       
       

      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:

      e1b25aad-5f96-484f-b672-5653644a31c8-image.png

       
       

      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:

      910abadb-7e92-4f7a-a276-e3174d355a6b-image.png

      d50a2405-2023-480c-b050-013ae3299deb-image.png

       
      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:

      34fe14ad-1803-425e-88fb-70f53f885ebd-image.png

       
       

      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:

      2e9a0bd8-b748-4409-8927-d33c7b057d00-image.png

       
      Here is what we will get:

      positionlabel1.gif

       
       

      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:

      46cdf0aa-0e30-4609-be72-c4d5a78043b3-image.png

       
       

      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.

      920fb47f-3422-41c5-8e65-205c81041511-image.png

      a0fb9de1-4fcb-4509-b1f9-a137cbb8ce13-image.png

       
      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:

      chapter1.gif

       
       

      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:

      93285312-9610-4ae7-b025-37fb33bdcc4e-image.png

       
      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:

      3944865e-ad2a-4490-ba91-237f07532066-image.png

       
      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:

      chapter123.gif

       
       

      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.

      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • T
        Termaine Pittman last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • First post
          Last post