Skip to content

Generate

The Generate endpoint allows you to produce text completions from a given prompt using a specified model. It can be used for tasks like answering questions, drafting content, or continuing a piece of text.

Editor Window

The Ollama Integration includes an Editor Window that allows you to generate text directly from the Unity Editor without entering Play mode.

You can open the window from the menu: Tools/Dajno/Ollama/Generate.

In the window, you can set your Base URL, select a model, and enter a prompt. Clicking the Generate button will produce a response which is displayed directly in the window, allowing you to quickly test prompts and prototype content.

C# Usage

To generate text from C# you simply call the GenerateAsync method:

public async void Start()
{
    // Replace with your actual running Ollama API URL
    string baseUrl = "http://localhost:11434";
    OllamaClient api = new OllamaClient(baseUrl);

    // Specify your model
    string model = "gpt-oss:latest";

    // Create a prompt
    string prompt = "Hello, how are you?";
    GenerateRequest request = new GenerateRequest
    {
        Model = model,
        Prompt = prompt
    };

    GenerateResponse response = await api.GenerateAsync(request);

    // Print the response to the Unity console
    Debug.Log(response.Response); 
}

Streaming

You can also stream the response:

public class OllamaStreamExample : MonoBehaviour
{
    private async void Start()
    {
        string baseUrl = "http://localhost:11434";
        string model = "gpt-oss:latest";
        string prompt = "Write a short, inspiring message about coding.";

        var api = new OllamaClient(baseUrl);

        await foreach (var chunk in api.GenerateStreamAsync(new GenerateRequest
        {
            Model = model,
            Prompt = prompt
        }))
        {
            Debug.Log(chunk.Response); // Print each streamed chunk as it arrives
        }
    }
}

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 OllamaGenerateExample : MonoBehaviour
{
    private void Start()
    {
        string baseUrl = "http://localhost:11434";
        string model = "gpt-oss:latest";

        var api = new OllamaClient(baseUrl);

        var request = new GenerateRequest
        {
            Model = model,
            Prompt = "Hello, how are you?"
        };

        api.Generate(
            request,
            OnGenerateSuccess,
            OnGenerateError
        );
    }

    void OnGenerateSuccess(GenerateResponse response)
    {
        Debug.Log(response.Response);
    }

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