Twitter LogoFacebook Logo
ChatGPT
Integrating ChatGPT to your App
By: King

This plugin allows you to interact with OpenAI's ChatGPT.


Visit the official OpenAI documentation for concepts and best practices to use GPT.

Setup

1. Go to OpenAI official site and sign up for an account (developer).

2. Obtain your API Key - you should see it here.

3. In Unreal Engine, use the Set API Key (OpenAI) node to register your API Key for the plugin. Preferably you should do it in the Game Instance since it just need to be called once.

Image from Codeible.com

Generate Response

1) Start by calling the Generate Response node

Image from Codeible.com

2) Get familiar with the Best Practices for using Chat GPT documentation.

3) Start prepping for the user's input by specifying what you want the AI to become using the Messages pin and the Create Message (Chat GPT) node.

Image from Codeible.com

Notice the Role is set to System. This is how you set the behavior of the AI.

4) Then use another Create Message node to capture the user's input. 


In the example, it is hard coding the value in, but you should get the text of an input field and insert it.

Image from Codeible.com

Notice the Role is also set to User which means that the message is from a user.

5) Add the callbacks.

Image from Codeible.com

Click and hold on the callback pins and drag on an empty spot. 


Select "Add Event" then "Add Custom Event."

To get the Message from the response, right click on the "Response" pin in the callback and select "Split Struct Pin" to get the Role and Message. 

Image from Codeible.com

Alternatively, you can extract the response by using the GetResponse node.

Image from Codeible.com

Output

Image from Codeible.com

Continuous Chat While Keeping the Context 

So far, in the previous section, you learned how to use the plugin to generate a response once. Sometimes it may be good to have a continuous chat going back and forth with the AI.


However, there are limitations on how much text the API can take. In the documentation, a Token refers to a word.

If the words in all the messages combined exceed the maximum amount, it'll give an error so you will need to create a method to summarize and shorten the chat's context.

To create a chain with GPT,


1) Use the Add Message node to add the response that was received.

Image from Codeible.com

Note: You should do this with an interaction like clicking on a button.

2) Add a new Message to the new Array by using the Add Message node again.

Image from Codeible.com

3) Call the Generate Response again using the new Array.

Image from Codeible.com

By adding the previous messages to the original array, it'll save the context and the AI will save the progress. But if there are too many back and forth between the user and GPT, you may get an error that says the Maximum Token was exceeded. 


To solve this issue, from the official documentation, you should have a request to summarize the responses from time to time to avoid getting an error. 


Sign In