AI - OpenAI ChatGPT
-
Introduction
ChatGPT is an AI model built by OpenAI. It can chat with us like a human. People use ChatGPT for many tasks, such as searching for information or writing new articles.
In this article, you will learn how ChatGPT works, and how you can use it to in your projects on the CreatiCode playground.
How does ChatGPT work?
Although ChatGPT can pretend to be a human when it talks to us, it does not work the same way as human brains.
Strictly speaking, ChatGPT does not truly understand what we are saying. Instead, it tries to guess the next word to say based on the previous words. For example, when we say “United States of”, it will guess that the next word is most likely “America”. That’s why we often say ChatGPT is a “completion” model, because it tries to complete a sentence or a converstaion so that it makes sense.
Althought this method seems very simple, it works really well most of the time, because ChatGPT has “read” a vast amount of books, articles and websites, much more than any human can ever do.
ChatGPT can give wrong answers
Although ChatGPT works well in many cases, it can still make silly mistakes. This problem is called “hallucination”. This is mostly due to 2 reasons:
- Wrong Data: We often say “garbage in, garbage out”. If the data it reads is wrong or not up to date, then it will give some wrong answers.
- Misunderstanding: Because ChatGPT doesn’t really understand the logic and meaning of what we are saying, it can give wrong answers. For example, as of March 2023, it would give a wrong answer like this:
As a programmer, you need to help avoid or fix such mistakes. For example, if you feed ChatGPT some more accurate or up-to-date facts related to the question, then it will be able to give a better response.
Write Accurate and Detailed Prompts
The answers we get from ChatGPT mostly depend on what we say to it. This is called a “prompt”.
When we search on Google, the keywords we type in determine which websites are returned by Google. The more accurate our keywords, the more likely we get the websites we need.
Similarly, the quality of the prompts we give ChatGPT determine the quality of the response we get from it. The more acurate and detailed they are, the more likely we get the response we are looking for.
For example, here is the response we get when we use a general prompt:
And this is what we get when we give much more details in our prompt:
When we create projects using ChatGPT, instead of using the users’ input directly, we should try to enhance it to create better prompts.
Role-Playing Prompts
Another great way to improve the quality of the prompt is “role-playing”. We can tell ChatGPT to play a certain role, such as a “teacher” or a “doctor”, and this often lead to much better results. It would often help if we tell it who we are as well.
For example, without role-playing, we can get a fairly complex answer:
However, if we tell ChatGPT to pretend to be a teacher, we can get an answer that’s easier to understand:
The ChatGPT Request Block
There are many ways for us to add ChatGPT to our projects. For example, in game projects, we can allow the player to chat with the non-player characters for fun or information. If the project is a tool, we can offer a chatbot to answer user questions or write articles.
To send a request to the OpenAI ChatGPT server and get the response back, you can use the following block in the “AI” category. Note that you have to sign in as a user on the CreatiCode playground to use it, but you do not need the premium subscription.
It accepts the following input parameters:-
Request: This is the prompt you are sending to the OpenAI ChatGPT server.
-
Result: You need to select a variable from this dropdown. You can use an existing variable or create a new variable. It will contain the response you get from ChatGPT.
-
Mode: This dropdown controls whether this block will wait for all the response to come back before continuing to the next block.
- If it is set to “waiting”, then the program will pause at this block, and when we get the full response from OpenAI, it will be stored in the result variable, and then the program will continue to the next block. For example, this program make the dog say the response after waiting:
- If it is set to “streaming”, then the program will continue to the next block below this block right away. At this point, the result variable will still be empty. However, as we get more and more response from the OpenAI server, the variable’s content will be updated continuously. This mode allows us to show partial response to the users as soon as possible, so it is more suitable if the response would be long. For example, this program will repeatedly update what the dog says using the response:
Note that to find out when we have received the entire response from ChatGPT, you can look for the “check mark emoji” at the end (you can copy that emoji here). If the result variable contains this symbol, it means we have reached the end of the response.
- If it is set to “waiting”, then the program will pause at this block, and when we get the full response from OpenAI, it will be stored in the result variable, and then the program will continue to the next block. For example, this program make the dog say the response after waiting:
-
Length: This is the maximum number of tokens for the response. You can think of a token as a word or a symbol. ChatGPT will try to make sure its response does not exceed this limit, but it may not be able to give a complete response if the limit is too small.
-
Temperature: This is a number between 0 and 2. A higher value will make the response more random and creative; a lower value will make the response deterministic and focused.
-
Session: This dropdown controls whether we are continuing the previous conversation, or we are starting a new chat. If it is a new chat, the ChatGPT robot will not remember anything from the previous conversation (the “context”).
- For example, if we try to ask 2 questions that are related using “new chats”, ChatGPT does not know what we want it to do for the second question, because it does not “remember” the first question is about calculating squares:
- However, if we change the second question to “continue” the previous conversation, ChatGPT will “remember” we are talking about “square of numbers”:
- Note that whenever the green flag button is clicked, the chat history is cleared, so even if you specify “continue”, ChatGPT will still start a fresh new chat.
- For example, if we try to ask 2 questions that are related using “new chats”, ChatGPT does not know what we want it to do for the second question, because it does not “remember” the first question is about calculating squares:
You can not “continue” the session for too long
You might be thinking about using the ChatGPT block this way: start a session of type “new chat” on the first user message, then keep using the “continue” mode after. There is one problem with this approach. The ChatGPT service has a limit on how many words it can receive and send back, which is about 5000.
When you use the “continue” session type for a new message from the user, all the previous messages are sent back by the CreatiCode server on your behalf to ChatGPT along with this new message. As a result, as the conversation continues, the chat history grows bigger and bigger. So when the entire conversation has reached about 5000 words, the chat has to restart as a “new chat” type, and all the chat history will be lost. You will notice this when the response ends with the special phrase “TOKEN LIMIT REACHED”.
To deal with this limitation of the ChatGPT service, you should try to keep the request and response short, which will allow you to send and receive more messages.
System Requests
We can also send another type of request to ChatGPT called “system requests”. ChatGPT will treat such requests as coming from the programmer rather than the end user, and it will try its best to fulfill such requests. You can use the following block to send a system request:
OpenAI ChatGPT: system request [ ] session [new chat v]
The first input is the request itself. The second input specifies if we should start a new chat session or continue the current session.
Since the system requests are very powerful, you should not send user messages as system requests. Instead, only use this block for setting up the conversation and give ChatGPT high-priority rules to follow. For example, if you want ChatGPT to play a certain role no matter what the user says, you should specify such a role using a system request.
Cancel a ChatGPT Request
Sometimes the user may want to cancel an ongoing request. For example, the chatbot may take too long to write the response, or the response is not what the user is looking for. Also, the user can not send in another request until ChatGPT finishes responding to the current request.
To cancel the request, you can run the following block:
OpenAI ChatGPT: cancel request
Safety for Use in Schools
Unlike the publicly available ChatGPT API from OpenAI, it is perfectly safe for school-age students to use the block in the CreatiCode playground. A strong content moderation process is applied to guarantee that only appropriate response is returned. If the user request contains any content that is not appropriate for school settings, it will be rejected with a response like this:
The chat messages will not be shared with any third party. However, a teacher can request to review the chat content of students in his/her classes.
Usage Limit
The ChatGPT block is publicly available to any user registered to use the CreatiCode playground. However, at time of high demand, usage of the block may be subject to a rate limit.