ChatGPT AI: An Improved Chat App (Difficulty: 3)
-
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.
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:
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.
Now, when we challenge ChatGPT as a user, it will stay in character:
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:
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:
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.
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:- Append a simple message like “…” to the chat window, which tells the user ChatGPT is still “thinking”.
- Repeatedly update this message using the “response” variable.
Now we are getting the response in the chat window as soon as they are received:
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
. You can copy it here https://emojiterra.com/white-heavy-check-mark/.
Another place you can easily find the emoji is this operator block:
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.
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.
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.
-
info-creaticode
-
This post is deleted! -
tikoooooooooooo
-
hello how are you
-
Wow, you must have not had phones or any of the new technology.
-
Pretty cool yet basic. I made sonic the hedgehog w/ this tutorial!