There are currently 15 players online.

Connect now using the IP mc.autcraft.com

Internet Explorer

Internet Explorer is not supported. Please upgrade to a more modern browser.

OpenAI on the Autcraft Minecraft Server

I was introduced to AI the same way many others were, via ChatGPT.
I asked it many questions and found it amusing.

Then I discovered that OpenAI, the makers of ChatGPT, included tools to integrate the AI into your own projects.

Getting Started

You will need to create an account at OpenIA's Beta Platform. And from there, get an API Key which you will use to identify yourself in your code.

Next, you will need some API code to do the work. I found Theo Kanning's OpenAI Java API and it works perfectly. You will need to use this in your code.

Some Things to Know Before the Code

Each conversation is self contained, meaning that the entire text of the ongoing conversation is sent each time. This gives the AI context and past information to reference within that conversation. So in your code, you will need to keep appending the player's and the AI's messages into one long message.

The API recognizes when to stop generating more text based on some keywords that you can pass into it, in the form of a List of strings. In my case, I reference the player as "Player:" and the AI as "Autcraft:" By doing this, I can tell the API to stop at both of those terms. You could use a player's actual name but for the sake of anonymity and player's safety, I have the code refer to each player as simply "Player" in each conversation.

The conversation itself begins with an instruction.

Once a conversation is initiated, the first message sent through the API should be an instruction as to how the AI should respond. In my case, I instructed the server to behave as if it actually is the Autcraft server itself. This is an example of what I would send: "The following is a conversation with an AI that represents the Minecraft server named Autcraft. The AI should do it's best to respond as if it actually is the Minecraft server itself, responding to a player on the server."

So let's say that you wanted the NPCs on your server to have AI, you could send an instruction such as "This conversation is between a Minecraft player and a Minecraft villager with the farmer profession. The AI is to respond as if it is the farmer villager talking to a Minecraft player."

For the sake of the examples below, I'll refer to the player as Player and the AI as Farmer.

The Code

To begin, setup a hashmap with the player's UUID as the key, to keep each player's conversations separate.

When the conversation is initiated, set the first element to be the instruction statement as stated before. Something like this:

By including the player's greeting at the end of the preliminary instruction, the AI will respond, which will look to the player like the AI is greeting them.

From here, you can just append messages onto the existing conversation with something like this:

It's important to also include the part about the Farmer at the end as it serves as a cut off point for the AI to know which section of the conversation belongs to whom.

The next bit of code will be to list those points of separation for the AI to know who is saying which parts. You must pass in a list of strings to know which part is which.

This can look like this:

And now for the most important part... making the call to the OpenAI API.

The call to the AI must be done asynchronously as it must wait for a response from OpenAI and then send it to the player once received. For that, something like this will suffice:

And then in the getResponse method, you'd have something like this:

Important!

OpenAI is wrong a lot of the time. It doesn't know Minecraft inside and out and it most definitely does not know your server all that well. The instruction that you send through in that first message is your best bet at providing it with some key information to get started. My instruction line includes a list of ranks, server IP addresses, a bit of basic history, and an additional instruction to keep all responses suitable for children and people of all ages.

There is, however, only so much of a complete set of text that one can send to the OpenAI and as each message is appended to that initial instruction, it's best to keep it short and concise.

With a little additional code, you could also limit the number of interactions per player with the AI to keep usage/cost down as it does cost money to use. We have our AI limited to 20 interactions per player per day.

We've also added a disclaimer to the beginning of each interaction for the player to see as a reminder that the AI is not always right and is to be used for entertainment purposes and does not dictate rules or decisions on the server.

Remember, this is more of a toy than a tool at the moment, but I'm sure you can make it work even better than I ever could! I can't wait to see what you come up with!!