Suppose you have a character and a gamer simulation. You want to use ChatBots to guide that character’s speech. How can you do that? In this video, I’ll show you exactly how to set up your Unity project to use ChatBots or any other language model (LLM) that you can access via an API.
To make this work, you’ll need three main pieces:
- An API key stored securely.
- An HTTP client to handle communication with the API.
- ChatBots agents or characters that will be using the client to generate dialogue.
To obtain your API key, you’ll need to create an account at OpenAI or whichever site you’re using. Once you have an account, navigate to the documentation or your user profile to find where to generate an API key. This API key is like a password for using the API, so keep it secret and safe.
To keep your API key out of your project source code, you can store it in an environment file. Create a file called .env in the top or root folder of your project and put your OpenAI API key (or any other information you want to keep secret) on separate lines in this file. This is a common practice to protect sensitive information when sharing your project on platforms like GitHub.
In
Next, in any scene where you plan to use generative AI dialogue, you can place an object called a ChatClient. This object will manage API calls. Alternatively, you can handle this logic separately with each chat agent. These pieces can be rearranged as needed.
The ChatClient will need to retrieve the API key from the .env file and also the URL of the API you’re making requests to. You can also set other properties here, such as the model you’re using and the temperature or randomness of the replies you get.
You’ll also need to define a few models to translate the responses you get from the API from JSON to C#. First, define a ChatMessage, which represents an individual message in a conversation. Specify the role of the message (user, system, assistant, or function) and the content of the message.
Next, set up a ChatRequestBody, which defines the request body sent to the API. This includes the model, temperature, maximum number of tokens, and the list of messages.
To handle the response from the API, define a ChatResponseBody. This contains the ID, object created, model choices, and usage. The main property you want is choices, which contains the response(s) from the model.
Now that you have the necessary models set up, you can build a request and send it. Create the ChatRequestBody, convert it to JSON, and use an HTTP client to send the request. Convert the JSON response back to a ChatResponseBody in order to use it in your code.
You can then decide what to do with the response. You can create a delegate that takes the response text as a parameter, allowing the agent using the client to decide how to handle it.
To set up a chat agent, create an NPC or character that will use the generated text. You can store an entire message history in this class to keep a conversation going. You can also vary the responses by adjusting the temperature property.
To trigger the chat agent, you can use a collider on a child component. When a player enters the trigger, it can send a message to the API to get a response. Once the response is received, it can be displayed in a speech bubble or used in any other way you desire.
By following these steps, you can use ChatBots to generate dynamic dialogue for your characters in Unity. Have fun experimenting with different models and settings to create engaging and interactive experiences!