CodeBlocks:OpenAI API
Slow walks through written code that note the good, the bad, the docs and the ugly.
At the lab, everyone's favorite person is Susan. Susan is 60 year old woman who has gained 20 pounds in the last two weeks, has all of a sudden experienced a hard time breathing and can be quite sassy if you ask her the wrong questions. She is also an avatar created by Character Creator who is meant to replace a standardized patient in a medical simulation. Instead of having to pay an actor to come in, the goal is to have VR headsets always available for students to put on and practice diagnosing a virtual patient.
Susan currently has 6 factual responses if you ask the right questions, and 9 responses that either ignore or deplore you for remarks unrelated to her situation.
I began by utilizing a Unity-OpenAI github package that helps write the WebRequests to OpenAI's server and supplies a C# version of the API. https://github.com/srcnalt/OpenAI-Unity
If you see something odd, please message me or leave a comment. But be gentle, this is my first time doing this:
This script requires a callback, as the information is sent out to OpenAI, so there are quite a few async methods in this script. I had worked with a few async variables before, but this whas the first time with so many of them. The name of the function needs to be changed as before it was calling a different class, but that was instantiating two openAI clients which was getting messy, so I reduced it down to the same class. A previous function handles the voice to text, but I felt this one might be more interesting to write about.
After a quick debug log check if it entered in the unity console and passing some text from the editor what previously defined variable, I create a new chat message. The open AI chat completion requires a list of messages to send to the server. These messages can have four different roles.
System: the high level overall role and situation of the AI chat bot.
User: input that the user would give to the chat bot
Assistant: a response that the AI chat bot would give to the user
the really interesting thing about these messages is you can create an example conversation that gives the AI chat bot much more context to answer your question in the way you see fit. Over here you'll see that I only use the system message, and this is because the other messages seem to be messing with the responses of the question. I was not sure why but system was enough to get this function working.
I never really liked the idea of prompt engineering or using AI to write code, but since this project I find it to be quite interesting and helpful. Two things I learned or that it's better to give very long detailed prompts, to denote user input with some sort of delimitation such as three hyphens in this case.
The system's role is assigned and the message is added to the list.
The chat completions request object has its messages defined by the system message above. Here we're just using the GPT 3.5 turbo. It's super cheap I've been playing with this model all week and it's only cost me about $0.56 after having made numerous calls for audio to text and tap response.