Navigation

    CreatiCode Scratch Forum

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

    ChatGPT AI: An Improved Chat App (Difficulty: 3)

    Tutorials
    6
    6
    13198
    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 the previous tutorial (/topic/925/chatgpt-ai-chat-with-einstein-difficulty-3), you learned how to build a chat app where ChatGPT pretends to be Einstein.

      In this tutorial, you will learn a few tricks to improve that chat program.

      j3.gif

       
       

      Step 1 - Starting Project

       
      We’ll start with the chat project from our last lesson. If you didn’t make one yet, you can either follow the previous lesson or remix this ready-made project:

      play.creaticode.com/projects/1ca634e4193e2b27500be13e

       
       

      Step 2 - Test “Breaking Character”

       
      Right now, ChatGPT pretends to be Einstein, but users can easily trick it into admitting it’s just pretending.

      See what happens if you ask the following:

      b94202e0-7229-4996-9006-489cebcd9a43-image.png

       
      There’s nothing wrong with ChatGPT being honest, but in a game or quiz, this may be a problem.

      For example, suppose we are using ChatGPT to play an almighty wizard in an adventure game. It would spoil the fun if the wizard suddenly started to admit it was an “AI model”, right?

      For example, consider using ChatGPT to quiz a student. If the student somehow prompts ChatGPT to break its character, it may start to provide answers to the quiz.

       
       

      Step 3 - Use a “System Request” to Enforce ChatGPT’s Role

       
      To ensure ChatGPT remains in its assigned role, we can use a “system request.” ChatGPT treats all system requests as high priority and will do its best to fulfill them.

      Replace the original request block with a “system request” block, and clearly instruct ChatGPT to stay in its assigned role in all responses. Additionally, since this is just an instruction to ChatGPT, we will begin with a simple message of “Hi” instead of waiting for ChatGPT’s response.

      23c7bc92-6c78-4b62-beb7-19214c10aeb1-image.png

       
      Now, when we challenge ChatGPT as a user, it will stay in character:

      j2.gif

       
      In a sense, you can think of this as a 3-way conversation between ChatGPT, the user, and you (the system programmer). You start the conversation with some hidden system instructions that only ChatGPT receives, and then ChatGPT proceeds to chat with the user based on these instructions.

       
       

      Step 4 - Enhance the System Request

       
      Although we are using a system request to enforce the role, the solution is not perfect.

      First of all, the true Einstein does not really know what ChatGPT is, so it should not say “I’m not ChatGPT” or “I’m not a chatbot”.

      In addition, a smart user can still easily make ChatGPT fail its task like this:

      f6d2244e-3cfb-4913-9542-c15a6fd87e20-image.png

       

      If we truly want to keep our chatbot realistic, we need to enhance our system’s request to account for such user challenges. This is often referred to as “prompt engineering,” as we aim to find the optimal prompt to enhance ChatGPT’s responses.

      For example, we can add another sentence to the end of our system request: “You have never heard of AI models or ChatGPT.”. Here are the improved responses:

      14d16fae-85bd-44f0-b72d-78e50538a35e-image.png

       
       

      Step 5 - Switch to “Streaming” Mode

       
      Currently, ChatGPT is not very fast in generating its responses. If a response is long, the user may have to wait several seconds before seeing it.

      To solve this problem, most chatbot apps use the “streaming” mode, which displays the partial responses from ChatGPT as soon as they are generated. This will allow the user to start reading the response much earlier.

      bddf801d-f8c6-4e05-a062-9653f079364b-image.png

       
      Note that in streaming mode, this request block will finish immediately, but the content of the “response” variable will be empty at this moment.

       
       

      Step 6 - Repeatedly Refresh the Response

       
      In streaming mode, the value of the “response” variable is automatically updated as ChatGPT generates the response. Therefore, to display the response, we need to use the following approach:

      1. Append a simple message like “…” to the chat window, which tells the user ChatGPT is still “thinking”.
      2. Repeatedly update this message using the “response” variable.

       
      8dfb559e-b0d3-4e50-86bb-506e59dbba51-image.png

       
      Now we are getting the response in the chat window as soon as they are received:

      j3.gif

       
       

      Step 7 - Detect the End of the Response

       
      We need to refine the repeat loop slightly. We can not use a forever loop, since it will cause the program to get stuck in this loop.

      To find out whether we have received the entire response from ChatGPT, we can look for the emoji of a checkmark 0b3ff1fa-a891-44ec-92a8-2e318892ef5c-image.png . You can copy it here https://emojiterra.com/white-heavy-check-mark/.

       
      6dc68900-0061-4c6f-81c2-106888e414ce-image.png

       
      Another place you can easily find the emoji is this operator block:
      2b4fcfc5-7786-4ac4-a86e-eff8e6e38ea0-image.png

       
       

      Step 8 - Clean Up the Ending of the Message

       
      After we have received the checkmark in the response, we will exit the “repeat until” loop. At this point, we will still need to update the chat message again, as “response” may contain content that has not been displayed yet. We should also remove the checkmark emoji.

      ee87dfa5-6569-433b-98da-10f6f4ed4845-image.png

       
       

      Step 9 - Handle Chat Limit Gracefully

       
      Currently, there is a limit on the duration of the chat. If the chat is too long, the response will contain the special phrase “CHAT LIMIT REACHED”. When that happens, this chat can no longer continue, so we need to tell the user about it. At this point, the user cannot continue with the current chat, but they can start a new chat by clicking the green flag.

      785ff5c8-048c-468a-ab4a-b1f21a3169e2-image.png

       
       

      Further Enhancements

       
      Now, please try to further extend this chat app. You can try to enhance the prompt to make ChatGPT’s role more realistic by using the “system request” block. For example, you can try to pretend the year is 1945 and our Einstein knows nothing about what happened after that year.

      You can also try the same method with other roles or objects for ChatGPT, and then test if your chatbot can remain in its character no matter what you say.

      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • E
        Elam Gobelwana last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • I
          Ismael Grellier last edited by

          tikoooooooooooo

          1 Reply Last reply Reply Quote 0
          • K
            Khanh last edited by

            hello how are you

            1 Reply Last reply Reply Quote 0
            • M
              Mia Peterson last edited by

              Wow, you must have not had phones or any of the new technology.

              1 Reply Last reply Reply Quote 0
              • T
                Salvatore Nicholson last edited by

                Pretty cool yet basic. I made sonic the hedgehog w/ this tutorial!

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post