Skip to content

Embeddings

The Embeddings endpoint generates vector representations of text using a specified model. These embeddings can be used for semantic search, clustering, or similarity analysis.

public async void Start()
{
    var api = new OllamaClient("http://localhost:11434");

    var request = new EmbedRequest
    {
        Model = "nomic-embed-text",
        Input = "Hello world"
    };

    EmbedResponse response = await api.EmbedAsync(request);
    Debug.Log(response.Embeddings[0].Length);
}

Unity Extension Methods (Non-Async)

For Unity projects, you can use the provided extension methods to avoid async/await entirely. These methods run on a background thread and invoke callbacks on the Unity main thread.

public class OllamaEmbedExtensionExample : MonoBehaviour
{
    private void Start()
    {
        var api = new OllamaClient("http://localhost:11434");

        var request = new EmbedRequest
        {
            Model = "nomic-embed-text",
            Input = "Hello world"
        };

        api.Embed(
            request,
            OnEmbedSuccess,
            OnEmbedError
        );
    }

    void OnEmbedSuccess(EmbedResponse response)
    {
        Debug.Log(response.Embeddings[0].Length);
    }

    void OnEmbedError(System.Exception ex)
    {
        Debug.LogError(ex);
    }
}