Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. Tutorials
  4. ChatGPT AI: An Improved Chat App (Difficulty: 3)

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

Scheduled Pinned Locked Moved Tutorials
6 Posts 6 Posters 17.9k Views
  • 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.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by info-creaticode
    #1

    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. In addition, we will ask ChatGPT to say “hi” to the user. If we do not add this, it sometimes would say a lot more than necessary.

    64cafc55-9d33-42a3-b869-25657d5ee20b-image.png

     
     

    Step 4 - Enhance the System Request

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

    A smart user can still easily make ChatGPT fail its task like this:

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

     

    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

     
    Try to test with different instructions until your chatbot will stay in its character all the time.

     
     

    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
    0
    • CreatiCodeI CreatiCode pinned this topic on
    • E Offline
      E Offline
      Elam Gobelwana
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • I Offline
        I Offline
        Ismael Grellier
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • K Offline
          K Offline
          Khanh
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mia Peterson
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • T Offline
              T Offline
              Salvatore Nicholson
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • CreatiCode