Let’s Have ChatGPT Play The Role of an NPC in an RPG Game
Introduction
Although there have been various language generation models before ChatGPT became popular a few years ago, most of them were limited to specific knowledge backgrounds or generated sentences that were not natural enough. ChatGPT’s strength lies in its massive training data, which can generate sentences in various fields that conform to the knowledge background of that field. Some people have even tested giving ChatGPT some instructions to role-play.
In an RPG game, if you keep talking to a certain NPC, he will only give you the pre-written answers. If we can make the NPC give some random dialogues based on the background knowledge we set, it may make the characters more vivid and realistic. Under this assumption, ChatGPT is very suitable as a tool to generate dialogues for NPCs.
In early March, OpenAI released the ChatGPT API and announced its pricing model. I decided to follow suit and try an experiment to verify the effectiveness of applying ChatGPT to NPCs.
ChatGPT Experimentation
After studying the ChatGPT API, basically, it is a matter of POSTing a sequence of dialogues between two people to get the next dialogue. Here’s an example:
POST
{
"user" : "aaa",
"assistant" : "bbb",
"user" : "ccc",
"assistant" : "ddd",
...
"user" : "[keyword]",
}
↓↓↓ response{
"assistant" : "[response of keyword]",
}
In the ChatGPT playground, “user” corresponds to the user input, and “assistant” corresponds to ChatGPT’s response. Therefore, we can first try out in the ChatGPT playground what POST content can make the keywords have the dialogue response we want.
Taking the example of creating a vendor NPC, I hope he can have the following responses:
- When I first talk to him, he will promote some items to me.
- When I try to buy a sword from him, he will tell me that it’s sold out.
- After I buy an apple from him, he will thank me.
Beforehand, I prompted ChatGPT with the keywords “Communicate,” “A sword,” and “An apple” for the three conditions respectively. Then, I inputted the resulting keywords into ChatGPT and got the following results:
From the above results, it seems that ChatGPT is able to respond with relevant dialogue for each of the given prompts without any redundant sentences. Looks great right! Once we find a set of viable responses, we can apply them to the content then send to the ChatGPT API as follows:
POST
{
"user" : "You are acting as a vendor in an RPG game. When I talk to
you with the word \"Communicate\", you reply me a random sentence
to ask me to buy something. When I talk to you with the words \"A
sword\" , you reply me a random sentence to tell me that those are
out of store. When I talk to you with the words \"An apple\" , you
reply me a random sentence to thank to my purchase.",
"assistant" : "Communicate: Greetings adventurer! What brings you to
my humble shop? Are you in need of some powerful items to aid you
on your quest? A sword: I apologize, but it seems we are
currently out of stock on swords. Perhaps you could consider a
sturdy shield or some enchanted armor instead? An apple: Ah, an
excellent choice! Thank you for your patronage. May this delicious
fruit give you the energy you need to conquer your foes and claim
victory!",
"user" : "Communicate", // or "A sword" or "An apple"
}
Regarding the ChatGPT’s responses, during testing, they were originally surrounded by double quotes at the beginning and ending. Therefore, I made some minor adjustments to remove the quotes from the POSTed assistant content, resulting in response contents with no quotes at all.
NPC Implementation
Based on the experiments conducted, it was found that having ChatGPT play the role of an NPC is feasible. The implementation in the game resulted in the demo video shown below, where there are different responses each time the player interacts with the NPC. It’s very interesting! If you’re interested, the demo game is also available on GitHub where you can create your own NPC and try to communicate with him.
GitHub repo: https://github.com/chiaohao/RPG-ChatGPT-Demo
Conclusion
In the demo game, I implemented five different NPCs — a vendor, a missionary, a beggar, a swordsman, and a sorcerer— each with their own set of keywords and knowledge for responses. ChatGPT was able to play the role of an NPC very well, and the responses were of good enough quality for commercial use, I think. This approach is especially useful during the prototyping phase of game development, as it allows for rapid creation of various NPCs with different personalities for testing purposes.
However, there were limitations in my experiments when it came to keywords outside of the given scope. For example, in the case of the vendor, if given the keywords “Your coat” or “A pen”, ChatGPT would give a response that breaks the fourth wall.
Initially, I had thought of creating a game where users could input text and have a real conversation with NPCs. But based on the experiments, it seems that fully trusting users to input the right keywords is not yet feasible at this stage. Perhaps by adding more constraints to the dialogue prompts, the range of responses could be narrowed down, but that would require more time and experimentation.