GPT-4o in Unity

date
May 15, 2024
type
Post
year
slug
gpt4o-unity
status
Published
tags
AI
Unity
OpenAI
GPT
Research
summary
How to use OpenAI’s GPT-4o model in Unity
Yesterday OpenAI released their new model GPT-4o and it seems to blow all previous models out of the water. Here’s a guide for how to use it in Unity!

API Key

First we need an OpenAI API key.
First we need an OpenAI API key.
  • Copy the key, we’re going to need it in a moment.
 
notion imagenotion image

System Environment Variable

Whoever is in posession of our secret key can call the API and thus spend money on our OpenAI account, so we want to keep it safe on our system. To make sure we don’t accidentally include it in our source code, we’ll store it in a System Environment Variable. This also allows us to reuse it across multiple projects with ease.
Whoever is in posession of our secret key can call the API and thus spend money on our OpenAI account, so we want to keep it safe on our system. To make sure we don’t accidentally include it in our source code, we’ll store it in a System Environment Variable. This also allows us to reuse it across multiple projects with ease.

🪟 Windows

  • Start menu > search for system variables and click “Edit the system environment variabels”
  • Click on the Environment Variables button
    • notion imagenotion image
  • Click on the upper New to create a new user variable
  • Enter “OPENAI_API_KEY” as the name and your secret key as the value.
    • notion imagenotion image
  • Click OK, OK, OK to dismiss all related windows - this should save our new varible.

🍎 Mac OS

  • Go to your home directory and open .bashrc, .bash_profile, or .zshrc
  • Add a new line and add export OPENAI_API_KEY=<your secret key>
And this is how we can now access our system environment variable in Unity:

Unity

page icon
Now we’re going to create a script that talks to the OpenAI API and
  • Open a Unity Project or create a new one.
  • Install the NewtonSoft JSON package
    • Window > Package Manager
    • Click the + and choose Install package by name...
      • notion imagenotion image
    • Enter com.unity.nuget.newtonsoft-json and watch it ⬇️ install.
  • Create a new MonoBehaviour script called OpenAIConnection.cs and paste in the following code:
    • Add it to a GameObject and you’re ready to go!
      To test it without any further work, you can uncomment the Send line in the Start method and hit ▶️ Play.

      How to use it

      If you look at the OpenAIConnection component you can see that it comes preconfigured like this:
      notion imagenotion image
      Model - set the model you want to use (pick one from here: 📋https://platform.openai.com/docs/models/gpt-4o)
      Messages - configure what starting instructions the model receives. While in play mode you can see the message history build up in here.
      The entire message history is sent along with every new request!
      The entire message history is sent along with every new request!
      💬 Sending Requests - call Send( message, callback ) on OpenAIConnection to add a message to the list of messages and send it to the API. The second required argument is a callback function that will be invoked once the response has arrived.
      🧽 Clear - call Clear() to restore the message history to its initial state.

      Done!

      And there you have it! GPT-4o answers your requests in Unity!
      And there you have it! GPT-4o answers your requests in Unity!

      Bonus: Chat UI

      If you want to build a quick chat UI, grab this script and add some UGUI elements: An InputField, a Button and a Text, then link it all up and start chatting.
      I made mine look like this:
      notion imagenotion image

Leave a comment