Skip to content

Haoming02/ollama-unity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

for

A series of C# functions wrapping the ollama APIs, mainly for the UnityEngine

Prerequisite

Both the developer and the user's systems need to have a working ollama setup already:

  1. Download and Install ollama
  2. Pull a model of choice from the Library
    • Recommend gemma3:4b for general conversation
      ollama pull gemma3:4b
    • Recommend gemma3:1b for device with limited memory
      ollama pull gemma3:1b
    • Recommend deepseek-r1:7b if "reasoning" is needed
      ollama pull deepseek-r1:7b
    • Recommend nomic-embed-text for generating embeddings (vectors)
      ollama pull nomic-embed-text

In Unity, you need the Newtonsoft.Json package:

  1. Unity Editor
  2. Window
  3. Package Manager
  4. Add package by name
  5. Name:
    com.unity.nuget.newtonsoft-json
    
  6. Add

Then, simply download and install the .unitypackage from Releases


Features

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

Tip

You can use the families attribute to determine if a model is multimodal (see #2608)

Generate

  • 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

Important

You need to manually tell the model to use a JSON format in the prompt

Chat

  • 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() or LoadChatHistory() to be called first
    • Example:
                "Tell me a joke" <<
      >> ...
              "Explain the joke" <<
      >> ...
      
  • 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

RAG

Retrieval Augmented Generation

  • Ask()
    • Ask a question based on given context
    • Requires both InitRAG() and AppendData() 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()

Important

Not all models can generate embeddings

  • AppendData()
    • Add a context (eg. a document) to retrieve from

Demos

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() with Texture2D

About

Ollama API bindings for Unity

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Languages