Create characters in Unity with LLMs!
The LLM backend, LlamaLib, is built on top of the awesome llama.cpp library and provided as a standalone C++/C# library.
At a glance • How to help • Games / Projects using LLM for Unity • Setup • Quick start • Advanced usage • RAG • LLM model management • Examples • License Tested on Unity: 2021 LTS, 2022 LTS, 2023, Unity 6
Upcoming Releases
For business inquiries you can reach out at [email protected].
Contact [email protected] to add your project!
Method 1: Install using the asset store
Add to My AssetsWindow > Package ManagerPackages: My Assets option from the drop-downLLM for Unity package, click Download and then ImportMethod 2: Install using the GitHub repo:
Window > Package Manager+ button and select Add package from git URLhttps://github.com/undreamai/LLMUnity.git and click AddFirst you will setup the LLM for your game:
Add Component and select the LLM script.Download Model button (~GBs).Load model button (see LLM model management).Then you can setup each of your characters as follows:
Add Component and select the LLMAgent script.System Prompt.LLM field if you have more than one LLM GameObjects.In your script you can then use it as follows:
using LLMUnity;
public class MyScript {
public LLMAgent llmAgent;
void HandleReply(string replySoFar){
// do something with the reply from the model as it is being produced
Debug.Log(replySoFar);
}
void Game(){
// handle the response as it is being produced
...
_ = llmAgent.Chat("Hello bot!", HandleReply);
...
}
async void GameAsync(){
// or handle the entire response in one go
...
string reply = await llmAgent.Chat("Hello bot!");
Debug.Log(reply);
...
}
}You can also specify a function to call when the model reply has been completed:
void ReplyCompleted(){
// do something when the reply from the model is complete
Debug.Log("The AI has finished replying");
}
void Game(){
// your game function
...
_ = llmAgent.Chat("Hello bot!", HandleReply, ReplyCompleted);
...
}To stop the chat without waiting for its completion you can use:
llmAgent.CancelRequests();That's it! Your AI character is ready to chat! ✨
For mobile apps you can use models with up to 1-2 billion parameters ("Tiny models" in the LLM model manager).
Larger models will typically not work due to the limited mobile hardware.
iOS iOS can be built with the default player settings.
Android
On Android you need to specify the IL2CPP scripting backend and the ARM64 as the target architecture in the player settings.
These settings can be accessed from the Edit > Project Settings menu within the Player > Other Settings section.
Since mobile app sizes are typically small, you can download the LLM model the first time the app launches.
This functionality is enabled with the Download on Build option.
In your project you can wait until the model download is complete with:
await LLM.WaitUntilModelSetup();You can also receive calls the download progress during the model download:
await LLM.WaitUntilModelSetup(SetProgress);
void SetProgress(float progress){
string progressPercent = ((int)(progress * 100)).ToString() + "%";
Debug.Log($"Download progress: {progressPercent}");
}This is useful to present e.g. a progress bar. The MobileDemo demonstrates an example application for Android / iOS.
Restrict the output of the LLM / Function calling / GrammarTo restrict the output of the LLM you can use a grammar, read more here.
The grammar can edited directly in the Grammar field of the LLMAgent or saved in a gbnf / json schema file and loaded with the Load Grammar button (Advanced options).
For instance to receive replies in json format you can use the json.gbnf grammar.
Alternatively you can set the grammar directly in your script:
llmAgent.grammar = "your grammar here";For function calling you can define similarly a grammar that allows only the function names as output, and then call the respective function.
You can look into the FunctionCalling sample for an example implementation.
To add new messages you can do:
_ = llmAgent.AddUserMessage("your user message");
_ = llmAgent.AddAssistantMessage("your assistant reply");To automatically save / load your chat history, you can specify the Save parameter of the LLMAgent to the filename (or relative path) of your choice.
The chat history is saved in the persistentDataPath folder of Unity as a json object.
To manually save your chat history, you can use:
_ = llmAgent.SaveHistory();and to load the history:
_ = llmAgent.Loadistory(); void WarmupCompleted(){
// do something when the warmup is complete
Debug.Log("The AI is nice and ready");
}
void Game(){
// your game function
...
_ = llmAgent.Warmup(WarmupCompleted);
...
} The last argument of the Chat function is a boolean that specifies whether to add the message to the history (default: true):
void Game(){
// your game function
...
string message = "Hello bot!";
_ = llmAgent.Chat(message, HandleReply, ReplyCompleted, false);
...
} void Game(){
// your game function
...
string message = "The cat is away";
_ = llmAgent.Completion(message, HandleReply, ReplyCompleted);
...
}…You can use a remote server to carry out the processing and implement characters that interact with it.
**C
No open issues yet, or sync has not completed.