Skip to content

Ollama

Installation

pip3 install genkit-plugin-ollama

You will need to download and install Ollama separately: https://ollama.com/download

User ollama CLI to pull the models you would like to use. For example:

ollama pull gemma3
ollama pull nomic-embed-text

Configuration

from genkit.plugins.ollama import Ollama, ModelDefinition, EmbeddingModelDefinition

ai = Genkit(
    plugins=[
        Ollama(
           models=[
               ModelDefinition(name='gemma3'),
               ModelDefinition(name='mistral-nemo'),
           ],
           embedders=[
               EmbeddingModelDefinition(
                   name='nomic-embed-text',
                   dimensions=512,
               )
           ],
        )
    ],
)

Then use Ollama models and embedders by specifying ollama/ prefix:

genereate_response = await ai.generate(
    prompt='...',
    model='ollama/gemma3',
)

embedding_response = await ai.embed(
    embedder='ollama/nomic-embed-text',
    documents=[Document.from_text('...')],
)

API Reference

Ollama Plugin for Genkit.

Ollama

Bases: genkit.ai.Plugin

Ollama plugin for Genkit.

Source code in plugins/ollama/src/genkit/plugins/ollama/plugin_api.py
class Ollama(Plugin):
    """Ollama plugin for Genkit."""

    name = 'ollama'

    def __init__(
        self,
        models: list[ModelDefinition] | None = None,
        embedders: list[EmbeddingModelDefinition] | None = None,
        server_address: str | None = None,
        request_headers: dict[str, str] | None = None,
    ):
        """Initialize the Ollama plugin."""
        self.models = models or []
        self.embedders = embedders or []
        self.server_address = server_address or DEFAULT_OLLAMA_SERVER_URL
        self.request_headers = request_headers or {}

        self.client = ollama_api.AsyncClient(host=self.server_address)

    def initialize(self, ai: GenkitRegistry) -> None:
        """Initialize the Ollama plugin.

        Args:
            ai: The AI registry to initialize the plugin with.
        """
        self._initialize_models(ai=ai)
        self._initialize_embedders(ai=ai)

    def _initialize_models(self, ai: GenkitRegistry):
        for model_definition in self.models:
            model = OllamaModel(
                client=self.client,
                model_definition=model_definition,
            )
            ai.define_model(
                name=ollama_name(model_definition.name),
                fn=model.generate,
                metadata={
                    'multiturn': model_definition.api_type == OllamaAPITypes.CHAT,
                    'system_role': True,
                },
            )

    def _initialize_embedders(self, ai: GenkitRegistry):
        for embedding_definition in self.embedders:
            embedder = OllamaEmbedder(
                client=self.client,
                embedding_definition=embedding_definition,
            )
            ai.define_embedder(
                name=ollama_name(embedding_definition.name),
                fn=embedder.embed,
                metadata={
                    'label': f'Ollama Embedding - {embedding_definition.name}',
                    'dimensions': embedding_definition.dimensions,
                    'supports': {
                        'input': ['text'],
                    },
                },
            )

__init__(models=None, embedders=None, server_address=None, request_headers=None)

Initialize the Ollama plugin.

Source code in plugins/ollama/src/genkit/plugins/ollama/plugin_api.py
def __init__(
    self,
    models: list[ModelDefinition] | None = None,
    embedders: list[EmbeddingModelDefinition] | None = None,
    server_address: str | None = None,
    request_headers: dict[str, str] | None = None,
):
    """Initialize the Ollama plugin."""
    self.models = models or []
    self.embedders = embedders or []
    self.server_address = server_address or DEFAULT_OLLAMA_SERVER_URL
    self.request_headers = request_headers or {}

    self.client = ollama_api.AsyncClient(host=self.server_address)

initialize(ai)

Initialize the Ollama plugin.

Parameters:

Name Type Description Default
ai genkit.ai.GenkitRegistry

The AI registry to initialize the plugin with.

required
Source code in plugins/ollama/src/genkit/plugins/ollama/plugin_api.py
def initialize(self, ai: GenkitRegistry) -> None:
    """Initialize the Ollama plugin.

    Args:
        ai: The AI registry to initialize the plugin with.
    """
    self._initialize_models(ai=ai)
    self._initialize_embedders(ai=ai)

ollama_name(name)

Get the name of the Ollama model.

Parameters:

Name Type Description Default
name str

The name of the Ollama model.

required

Returns:

Type Description
str

The name of the Ollama model.

Source code in plugins/ollama/src/genkit/plugins/ollama/plugin_api.py
def ollama_name(name: str) -> str:
    """Get the name of the Ollama model.

    Args:
        name: The name of the Ollama model.

    Returns:
        The name of the Ollama model.
    """
    return f'ollama/{name}'