A series of C# functions wrapping the ollama APIs, mainly for the UnityEngine
Both the developer and the user's systems need to have a working ollama setup already:
- Download and Install ollama
- Pull a model of choice from the Library
- Recommend
gemma3:4b
for general conversationollama pull gemma3:4b
- Recommend
gemma3:1b
for device with limited memoryollama pull gemma3:1b
- Recommend
deepseek-r1:7b
if "reasoning" is neededollama pull deepseek-r1:7b
- Recommend
nomic-embed-text
for generating embeddings (vectors)ollama pull nomic-embed-text
- Recommend
In Unity, you need the Newtonsoft.Json
package:
- Unity Editor
- Window
- Package Manager
- Add package by name
- Name:
com.unity.nuget.newtonsoft-json
- Add
Then, simply download and install the .unitypackage
from Releases
The following functions are available under the Ollama static
class; all functions are asynchronous
- List()
- Return an array of
Model
, representing all locally available models - The
Model
class follows the official specs
- Return an array of
Tip
You can use the families
attribute to determine if a model is multimodal (see #2608)
- Generate()
- The most basic function that returns a response when given a model and a prompt
- You can also pass a
Texture2D
when using a model with vision capability
- GenerateStream()
- The streaming variant that returns each word as soon as it's ready
- Requires a
callback
to handle the chunks
- GenerateJson()
- Return the response in the specified
class
/struct
format
- Return the response in the specified
Important
You need to manually tell the model to use a JSON format in the prompt
- Chat()
- Same as
Generate()
, but now with the memory of prior chat history, thus allowing you to further ask about previous conversations - Requires either
InitChat()
orLoadChatHistory()
to be called first - Example:
"Tell me a joke" << >> ... "Explain the joke" << >> ...
- Same as
- ChatStream()
- Same as above
- InitChat()
- Initialize / Reset the chat history
historyLimit
: The number of messages to keep in memory
- SaveChatHistory()
- Save the current chat history to the specified path
- LoadChatHistory()
- Load the chat history from the specified path
- Calls
InitChat()
automatically instead if the file does not exist
Retrieval Augmented Generation
- Ask()
- Ask a question based on given context
- Requires both
InitRAG()
andAppendData()
to be called first
- InitRAG()
- Initialize the database
- Requires a model to generate embeddings
- Can use a different model from the one used in
Ask()
- Can use a different model from the one used in
Important
Not all models can generate embeddings
- AppendData()
- Add a context (eg. a document) to retrieve from
4 demo scenes showcasing various features are included:
-
Generate Demo
List()
Generate()
GenerateJson()
-
Chat Demo
InitChat()
ChatStream()
-
RAG Demo
InitRAG()
AppendData()
Ask()
-
Image Demo
Generate()
withTexture2D