Navigation

    CreatiCode Scratch Forum

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

    ChatGPT AI: A Quiz Writer (Difficulty: 3)

    Tutorials
    1
    1
    1863
    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

       
      You have learned how to utilize ChatGPT to develop smart chatbots. However, it can be used in many other types of projects, and the interface does not have to be a chat window.

      In this tutorial, you will learn to build a smart “quiz writer”. The user can specify any topic, then our program will use ChatGPT to generate a quiz question on that topic, and then evaluate the user’s answer. You can even use this tool to help yourself prepare for an upcoming test!

       
      w5.gif

       
       

      Step 1 - An Empty Starting Project

       
      Create a new project, and name it “ChatGPT- Quiz Writer”. Delete the sprite with the dog, as we will only use the “Empty1” sprite for coding.

       
       

      Step 2 - The Topic Input

       
      First, let’s add a label that says “Topic”, and an input textbox for the user to write the topic. We’ll name these 2 widgets “topiclabel” and “topicinput”. Note that the Y position of the label is 100, so they won’t overlap on the stage.

      a5af3123-d1b0-4220-8ba3-ef3491c4d1b4-image.png

       
      They will look like this:
       

      0db0e06a-208a-4a87-a822-48bef4fefead-image.png

       
       

      Step 3 - Move Widgets Using the “Widget Position” Tool

       
      Now we need to move these two widgets to the top row of the stage window, so we can add other widgets below them. You can manually specify the position and size of each widget if you are comfortable with math.

      However, for a more visual solution, you can utilize the “Widget Position” tool like this:

      1. Run the project first so the widgets appear on the stage;
      2. Click the Widget Position tool button to turn it on;
      3. Drag/resize the widgets;
      4. Click the Widget Position tool button again to turn it off.

      w1.gif

       
      As you drag and resize a widget, the X/Y/Width/Height values in the corresponding block are automatically changed, so the next time you run the program, the widgets will directly appear in the new positions:

      4b2f9e30-3da0-4ed0-a044-f3229e983ba2-image.png

       
      Of course, you can still manually adjust the position and size numbers at any time. For example, if the Y positions of these two widgets are off by a small value, you can manually set them to be the same value.

       
       

      Step 4 - Add a “Go” Button

       
      Next, we will add another button that says “Go”, which will tell ChatGPT to generate the quiz.

      dc587fb9-9c90-4508-a29b-c8d4a5807335-image.png

       
      By default, the button will appear at the center. As a practice, please place it in the top right like this:

       
      45e9ce04-0717-45b9-8071-0526ec5b80b6-image.png

       
      Feel free to update the block directly or use the Widget Position tool. Try to complete the change before looking at the solution code below.
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
      Your program should be similar to this afterward:

      cb2440f4-2d55-4afe-aba3-e4476af77a4a-image.png

       
       

      Step 5 - Add the Question Box

       
      Now we need to add a large text box to display the quiz generated by ChatGPT. It should allow multiple lines of text, but does not allow any user input (so it is “read-only”), since the user should never be allowed to change the quiz question. Practice positioning this textbox yourself.

       

      76f05a5b-0931-4fea-821f-1dd5fa47ae86-image.png

       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
      Your program should be similar to this:
       
      d0bcc16c-9532-4457-994e-a4431357687e-image.png

       
       

      Step 6 - 2 New Custom Blocks

       
      Before we add more widgets, it looks like this stack of blocks may be getting too long. Therefore, it is a good time to define two new custom blocks to help organize the program.

      Please define two new blocks, “add question widgets” and “add answer widgets”, and place them right below the green flag block. Then, please move all the existing blocks for adding the widgets into the definition of “add question widgets”.

      129af87e-cdd1-477b-9c09-25d42d34cdfc-image.png

       
       

      Step 7 - 4 Answer Buttons

       
      Now we will add blocks to add 4 buttons in the definition of “add answer widgets”. Their names are simply A/B/C/D. Feel free to practice positioning all remaining widgets on your own.

       

      10e86166-dfe0-4749-a140-be9ea0baa091-image.png

       
      They look like this:

      f7b474e6-4b42-402d-8e51-aa973a280ba4-image.png

       
       

      Step 8 - The Feedback Box

       
      When the user clicks one of the 4 buttons, we will ask ChatGPT to check if the user’s answer is correct. Therefore, we need to add another text box at the bottom to display ChatGPT’s feedback. Please add it as a multi-line, read-only textbox.

      1b923cdd-1c6c-48d4-b63e-ff8e5ce905f1-image.png

       
      Now the interface is ready:

      b9152c3e-6ebc-4f19-a552-499d4901fd5b-image.png

       
       

      Step 9 - Compose the ChatGPT Request

       
      Suppose the user inputs a topic at the top, then clicks the “Go” button. At this point, we need to compose a request and send it to ChatGPT. The request should include the topic written by the user. We can write our request first, then join it with the user’s input.

      To store our request, we can define a variable “request”, and set its value when the “Go” button is clicked. The request will start with "write a quiz question with 4 choices of A/B/C/D. The topic is: ", which is joined by the value of the input box. Notice that we are forcing ChatGPT to always give us a question with exactly 4 choices.

      0c8680c9-2529-422f-9ed8-dd290dba8488-image.png

       
       

      Step 10 - Send Request to ChatGPT in Streaming Mode

       
      Now we are ready to let ChatGPT do the magic and give us a random question. To show the question as soon as possible, we will use the “streaming” mode. To allow a fairly complex question, we can set the max length to 400.

      Additionally, this should be a new chat, as each time we click “Go”, we do not need ChatGPT to recall the previous questions. If we use the continue mode, we will reach the token limit of ChatGPT after a few questions and have to restart.

      We will need to create a new variable called “question” to store the question generated by ChatGPT.

       
      54078a8c-cc62-4e0c-9cbf-55d5bf94c0f0-image.png

       
       

      Step 11 - Show the Question

       
      Now we need to show the question as ChatGPT generates it. We will use a “repeat until” loop to keep updating the question box with the value of the “question” variable. You can copy the emoji for the checkmark here (may look grey here but it will be green when pasted into the block): ✅. Note that we are using a “repeat until” block: the content of the textbox will be updated right before the checkmark emoji is appended to the question.

      781195d0-d748-4401-95d5-969ea7bee2d5-image.png

       
      Now we can test if ChatGPT will generate the question properly:

      w3.gif

       
       

      Step 12 - Handle the User Answer

       
      When the user clicks on one of the 4 answer buttons, we need to ask ChatGPT to check it. To do that, define a new custom block named “check answer”, which takes the “answer” as input:

       
      89d6225b-4340-4acd-ac3e-49ebc1f9d85f-image.png

       
      We will run this new block when the user clicks any of the 4 buttons:

      ba3a2799-3b9e-4119-8a90-da0bb158b5b5-image.png

       
       

      Step 13 - Compose the Feedback Request

       
      In the “check answer” block’s definition, we need to compose a new request to ChatGPT: we will inform ChatGPT of the question, as we are not asking it to remember the question. We also need to tell ChatGPT the user’s answer, then ask ChatGPT whether the answer is correct.

      We can compose a 4-part request using the “join” block like this:

      b5bf53b0-8176-4ce5-9de1-68b8e60fee35-image.png

       
       

      Step 14 - Display the Feedback from ChatGPT

       
      The remaining work is very similar to how we get the question from ChatGPT. We send the request, then display the result in the “feedback” textbox. You can duplicate the previous blocks and make some changes to them. Note that the session type is “new chat”, since we don’t need ChatGPT to remember the earlier messages about generating the question.

       

      64baa372-c2e0-4b22-840e-c28ca4080ce5-image.png

       

      Now we have a working version of the quiz writer!

      w4.gif

       
       

      Step 15 - Highlight the User’s Answer

       
      To make our quiz writer easier to use, we need to improve the user interface slightly. The most important change is to highlight the answer selected by the user.

      We can change the button’s background color to yellow when it is clicked:

      de953261-2909-4013-b770-28d4027a37b7-image.png

       
       

      Step 16 - Reset Button Colors

       
      When the user generates a new quiz question, we need to reset the color of all the answer buttons back to unselected. We can do it when the “Go” button is clicked:

      846cd626-cc7e-4391-95c3-a5bfeb38c0e0-image.png

       
       

      Step 17 - Testing

       
      Finally, you need to conduct further testing to ensure the user experience is seamless. Here is a final demo:

      w5.gif

       
       

      Additional Challenges

       
      This quiz writer is an elementary version. There are many areas where you can improve it. Here are some ideas for you to try:

      • Other Question Format: For example, you can change the question format from multiple-choice to short answers. The user would need to enter the answer in another text box.

      • Improve the Prompt: The current request we send to ChatGPT is straightforward, so we may sometimes receive unexpected answers from ChatGPT. For example, sometimes the question generated by ChatGPT also contains the answer. For another example, in this feedback, the answer is wrong, but ChatGPT still says “Great job!” because it is a “great question”. Try to improve the request to avoid such issues.

       

      • Better Styling: You can change the text style, color, and background color of all widgets to enhance the interface’s appearance.

      • Keeping Track of Scores: You can help the user keep track of how many questions were asked and how many were answered correctly.

      • Pre-generated Questions: Sometimes, you may not want to generate questions on the fly, since you have no control over what ChatGPT may ask. If you have a fixed topic, you can instruct ChatGPT to create 20 questions for you beforehand, and store these questions and their answers in a list or table. Then, when the user runs the program, you can randomly select a question from your pool of questions.

      1 Reply Last reply Reply Quote 1
      • Pinned by  info-creaticode info-creaticode 
      • First post
        Last post