Making an intelligent Robot NPC using Hugging Face ๐ค and Unity
Making Games with AI Course, Chapter 1
This tutorial is part of the Making Games with AI Course. A free upcoming course on creating AI-powered games for Unity and Unreal.
The integration of cutting-edge AI models in video games is opening a whole range of new exciting gameplays.
One of them is the ability for Non-Player Characters (NPCs) to understand and respond to the playerโs voice or text inputs. And this is what weโre going to do today.
In this tutorial, youโre going to create this smart robot that can understand your orders and perform them.
You can play with my version on your browser here ๐https://huggingface.co/spaces/ThomasSimonini/SmartRobot
To be able to do it, weโre going to use an AI model that understands any text input and finds the closer (if any) action on its list. Itโs called a Sentence Similarity model.
Whatโs interesting withย that system, contrary to classical game development, is that youย donโt need to hard-code every interaction.ย Instead,ย you use this Sentence Similarity model that selects whatโs robot possible action is the most appropriate given user input.
To make this project, weโre going to use:
Unity Game Engine (2022.3.13 and +).
The Jammo Robot asset is made byย Mix and Jam.
Hugging Face Unity API ๐ค, an easy-to-use integration for the Hugging Face Inference API, allows developers to access and use Hugging Face AI models within their Unity projects. Donโt worry if you never used it before weโll explain how to use it step by step.
The requirements:
You need to know to use Unity. If itโs not the case, you can check this excellent free course ๐ Create with code
At the end of this tutorial, youโll build your own version of this smart robot, add more actions and share it with others online for free.
Then, youโll be able to create other games using this AI. Imagine a stealth game, for example, where you guide your character to sneak around a museum and pull off daring heists?
(One of the prototype Iโm working on using Sentence Similarity)
You can find the complete Jammo the Robot Unity Project ๐ here
So letโs get started!
The power of Sentence Similarity ๐ค
Before diving into the implementation, we need to understand how the game works and what sentence similarity is.
How does the game work?
With this game, we want to give more liberty to the player. Instead of giving an order to a robot by just clicking a button,ย we want them to interact with it through text.
The robot has a list of actions and uses a sentence similarity model that selects the closest action (if any) given the playerโs order.
For instance, if I write, โHey grab me the red boxโ, the robot wasnโt programmed to know whatโs โHey grab me the red boxโ, is. But the sentence similarity model made the connection between this order and the โbring me red cubeโ action.
Therefore, thanks to this technique,ย we can build believable character AI without having the tedious process of mapping by hand every possible player input interaction to robot response.ย By letting the sentence similarity model do the job.
What is Sentence Similarity?
Sentence Similarity are language models able, given a source sentence and sentences,ย to calculate how much similar sentences are to the source sentence.
For instance, if our source sentence is โHey there!โ itโs very close to the โHello!โ sentence.
You can try for yourself here in your browser ๐ here
By using a Sentence Similarity model, weโre able to let our robot โdecideโ given input text whatโs the most appropriate action to take.ย The models are already trained so we can use it directly.
And to find a Sentence Similarity model, weโre going to use Hugging Face.
What is Hugging Face ๐ค?
Hugging Face is a platform where users can upload or download pre-trained AI models in more than 35 different tasks from text generation with Llama v2, speech-to-text with Whisper, text-to-speech (AI voices) with Coqui to 3D generation.
For most of these models, Hugging Face provides a free API called Inference API that allows you use them via simple API calls.
So how I can select a Sentence Similarity Model on Hugging Face ๐ค?
All the AI models are listed here https://huggingface.co/models. On the left of this page, you have the differents tasks.
In our case you want to select Sentence Similarity.
Now we have a list of models that are Sentence Similarity models (approximately 3,000). Weโll see just after how to test them.
Now that we understand what Sentence Similarity and Hugging Face is, letโs build our smart robot ๐ค!
Letโs build our smart robot ๐ค
Step 1: Getting Started with Hugging Face ๐ค
Hugging Face contains a lot of amazing AI models and an API (Accelerated Inference API) to directly plug them into your projects.
But first, you need toย create a free account.
When your account is created, go to Settings by clicking on your profile picture (top right-hand corner).
In Settings, select Access Tokens
Create a new Access Token, give it the name you want, and select write Role:
Now that the token is created, copy the API Token, itโs the key youโll need to be able to use the API.
โ ๏ธ For security reasons, DO NOT SHARE THIS KEY to others, itโs a private key.
Step 2: Select your sentence similarity model ๐ค
Now that we have the API key, we can try some models to choose one.
When you click on of the models, you can try the model directly on the website. Letโs make a test, go to the first 5 models and test them.
Here I tested this model: all-MiniLM-L6-v2
We see that โfetch the red blockโ is close to the robot action โbring me red cubeโ, so the model is working correctly.
Now that we choose this one, we need to get the API URL, to do that click on Deploy > Inference API.
This opens a window where you can copy the API_URL:
Youโre now ready to use this model in Unity.
Step 3: Install the Unity API Plugin
You can find the complete Jammo the Robot Unity Project ๐ here
Open the Unity Project
Go to
Window
->Package Manager
Click
+
and selectAdd Package from git URL
Enter
https://github.com/huggingface/unity-api.git
Once installed, the Unity API wizard should pop up. If not, go to
Window
->Hugging Face API Wizard
Enter your API key. Your API key can be created in your Hugging Face account settings.
Test the API key by clicking
Test API key
in the API Wizard.Paste your model API_URL on the Sentence Similarity Task
Check โ wait for model
Step 4: Build the Robot Behavior ๐ค
Now that weโve connected our Unity Project to the Hugging Face API, we need to define the behavior of our robot.
The idea is that our robot has different possible actions and the choice of the actions will depend on the API output.
We need first to define the Finite State Machine, a simple AI where each State defines a certain behavior.
Then, weโll make the utility function that will select the State hence the series of actions to perform.
The State Machine ๐ง ๐ค
In a state machine, each state represents a behavior, for instance, moving to a column, saying hello, etc. Based on the state the agent is it will perform a series of actions.
In our case, we have 7 states:
The first thing we need to do is create an enum called State that contains each of the possible States:
Because we need to constantly check the state, we define the state machine into the Update() method using a switch system where each case is a state.
For each state case, we define the behavior of our agents, for instance in our state Hello, the robot must move towards the player, face him correctly and then launch its Hello animation, then go back to an Idle State.
We have now defined the behavior for each different State. The magic here will come from the fact thatโs the language model that will define what State is the closest to the Player input. And in the utility function, we call this state.
Letโs define the Utility Function ๐
Our action list looks like this:
The sentence is what will be fed to the API
The verb is the State
Noun (if any) is the object to interact with (Pillar, Cube, etc)
This utility function will select the Verb and Noun associated with the sentence having the highest similarity score with the player input text.
But first, to get rid of a lot of strange input text, we need to have a similarity score threshold. I put for my model 0.20, but donโt hesitate to test with different thresold to see whatโs the best.
For example, if I say โLook at all the rabbitsโ, none of our possible actions are relevant. Hence instead of choosing the action with the highest score, weโll call the State Puzzled which will animate the robot with a perplexed animation.
If the score is higher, then weโll get the verb corresponding to a State and the noun (goalObject) if any.
We set the state corresponding to the verb. That will activate the behavior corresponding to it.
And thatโs it, now weโre ready to interact with our robot!
Step 5: Letโs interact with our Robot ๐ค
In this step, you just need to click on the play button in the editor. And you can prompt some orders and see the results.
You can also try online ๐ https://huggingface.co/spaces/ThomasSimonini/SmartRobot
It may have some bugs, especially the model loading bug, if you see the robot staying in idle mode, wait 45 seconds because it may be the model loading for the first time. And check the console for any error messages.
Step 6: Add new actions
How can I add more actions?
Letโs take an example:
Copy YellowPillar game object and move it
Change the name to GreenPillar
Create a new material and set it to green
Place the material on GreenPillar
Now that weโve placed the new game object, we need to add this possibility into the sentences, click on Jammo_Player.
In the list of actions click on the plus button and fill in this new action item:
Add Go to the green column
GoTo
GreenColumn
And thatโs it!
Step 7: Share your demo with the world ๐ฅ
Congratulations on completing your demo! Now, let's make it accessible to everyone to play it by publishing it online.
Hugging Face offers a convenient and free solution called Spaces.
We've prepared a quick 5-minute tutorial to guide you through the process: https://huggingface.co/blog/unity-in-spaces
Follow the steps in the tutorial to showcase your creation and let others experience it ๐
I published mine on Spaces, you can try ๐here!
Thatโs all for today, with a few steps weโve just built a robot thatโs able to perform actions based on your orders thatโs amazing!
What you can do next is dive deeper into the code in Scripts/Final and look at the final scene in Scenes/Final. I commented on every part of it so it should be relatively straightforward.
If you liked my article, please click the โค๏ธ. And donโt hesitate to follow me on substack and Twitter. I write about AI in Games.
This tutorial is part of the Making Games with AI Course. A free upcoming course on creating AI-powered games for Unity and Unreal. Donโt forget to sign up ๐ here
If you want to know more about language models and check out the amazing HuggingFace course on it (itโs free) ๐ https://huggingface.co/course/chapter1
In the next tutorial, youโll build a game demo call "Say Something ๐ฃ๏ธ," where an AI asks you to write something nice, mean, or neutral.
๐ค The AI understands you and rate what you said, and if โ
gives some points and additional time โฑ๏ธ.
See you next time,
Keep learning, stay awesome ๐ค
Thanks for writing this very detailed guide. I can see this can be expanded into a lot more complex systems. Hope you keep building and sharing :)
Hey. Thank you very much for your tutorial. Are you planning to upgrade the project and documentation to the latest Unity Sentis version (2.1.0)? Kind regards