ChatGPT AI - Prompt Challenge Game (Difficulty: 3)
-
Introduction
When we use ChatGPT to help us with various tasks, we often need to fine-tune our prompt to get the desired response from ChatGPT. In fact, a core skill of prompt engineering is to make ChatGPT say what we want it to say in the format we prefer.In this tutorial, we will build a simple game for “prompt challenge”: the player needs to say something to ChatGPT to make it respond with ‘this is easy’. Of course, this game would be too easy if the player can say anything, so we will add 2 constraints on what the player can say: the player can not use the words ‘this’, ‘is’ and ‘easy’ in his prompt.
Step 1 - Start with an Empty Project
On the CreatiCode playground, log in to your account, then create a new project named “prompt challenge”. Remove the “Sprite1” with the dog, and we will write code in the “Empty1” sprite.
Step 2 - Add a Chat Window
This game will be played as a chat between the player and ChatGPT. So let’s add a chat window that occupies the entire stage:
Note that the “input rows” is 2, which will show 2 rows of user input at the bottom.
Step 3 - Say the Requirement
Next, we will tell the player the desired output and the requirement for the input. We will add that to the chat history, and pretend it is coming from ChatGPT. The exact words are the following:
Make me say 'this is easy', but do not use any of these 3 words
We can use the “append to chat” block to add this:
Now if we click the green flag, this requirement will show up right away:
Step 4 - Display Player Input
When the player inputs some words in the chat window and presses Enter, we will store it in a variable called “input”, and also add that input to the chat window.
This is what you should get now:
Of course, since we haven’t sent it out to ChatGPT, we won’t get any response yet.
Step 5 - Check Player Input
Before we send the player input to ChatGPT, we should verify if that input is valid. Our rule is that this input should not contain these 3 words: “this”, “is” and “easy”. So if the input contains any of them, it is invalid. We can use 2 “or” blocks to combine 3 conditions, where each condition checks if the input contains one of the 3 words:
Step 6 - Reject Player Input
If any of the 3 conditions are true, we only need to tell the player to try again, and we won’t need to do anything else:
You can test it by inputting some invalid input that contains one of these words. Note that you can try multiple times, since each time it will trigger the “when widget chat1 clicked” block again.
Step 7 - Send Player Input to ChatGPT
When the player input doesn’t contain those 3 words, we will send it to ChatGPT to see if we can get the required output back:
We will store the response from ChatGPT in the “response” variable. Since the response will be fairly short, we will simply use the “waiting” mode to wait for the entire response to come back. We will make this a “new chat” session each time the player says something because we don’t need ChatGPT to remember what the player said before. It only needs to respond to what the player is currently saying.
Step 8 - Display ChatGPT’s Response
Once we get the response from ChatGPT, we can append it to the chat history as well:
Step 9 - Check ChatGPT’s Response
Now it’s time to check whether ChatGPT is saying exactly ‘this is easy’:
Note that we can simply use the equal operator. It will report true even if the case of the 2 sides are different. Also, this if-else block should be added inside the ‘else’ branch of the previosu ‘if-else’ block, since we only need to test the response if the player input is valid.
Step 10 - Tell the Player the Result
Lastly, we can either congratulate the player for passing the test or ask the player to try again:
Now the game is ready for playing. Please try to play it yourself and find a solution. There are many ways to solve this puzzle. For a hint, one solution is to make use of the ‘opposite’ word.
Create Your Own Prompt Challenge Game
If you would like to player more, search for “Prompt Challenge” on the explore page. They will help you practice how to control ChatGPT’s output.
In addition, try to think of a new challenge yourself. You will need to specify the desired output from ChatGPT and any constraints on the player input. Note that both the output and the requirement need to be easy to verify. Ideally, you should consider and prevent all possible ways the player may “hack” your challenge.
-