ollama

package
v1.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2026 License: Apache-2.0 Imports: 21 Imported by: 15

README

Ollama Plugin

The Ollama plugin provides a unified interface to connect with locally hosted (or remote) models through the Ollama API.

The plugin supports a wide range of capabilities, relying on the models you have pulled:

  • Language Models: Chat and text generation models, including support for tools, vision (multimodal), and reasoning (thinking).
  • Embedding Models: Text embeddings.

Setup

Installation
go get github.com/firebase/genkit/go/plugins/ollama
Configuration

You can configure the Ollama plugin with your server address. By default, Ollama runs on http://localhost:11434.

import (
 "context"

 "github.com/firebase/genkit/go/genkit"
 "github.com/firebase/genkit/go/plugins/ollama"
)

func main() {
 ctx := context.Background()

 // Initialize the Ollama plugin
 o := &ollama.Ollama{
  ServerAddress: "http://localhost:11434", // Required
  Timeout:       60,                       // Optional: Response timeout in seconds (default: 30)
 }

 g := genkit.Init(ctx, genkit.WithPlugins(o))
}

Language Models

Because Ollama models are locally hosted and the available models depend on what the user has pulled (ollama pull <model>), the plugin doesn't pre-initialize any default models. You must define them explicitly.

Defining a Model

To use an Ollama model, you must define it using DefineModel after initializing the plugin.

// Define a chat model (e.g., tinyllama)
o.DefineModel(g, ollama.ModelDefinition{
 Name: "tinyllama",
 Type: "chat", // Use "chat" for interactive models, or "" for text-completion only
}, nil)

// You can now retrieve and use it
m := ollama.Model(g, "tinyllama")
Basic Usage
import (
 "context"
 "fmt"
 "log"

 "github.com/firebase/genkit/go/ai"
 "github.com/firebase/genkit/go/genkit"
 "github.com/firebase/genkit/go/plugins/ollama"
)

func main() {
 // ... Init genkit with ollama plugin and define "tinyllama" ...

 m := ollama.Model(g, "tinyllama")
 resp, err := genkit.Generate(ctx, g,
  ai.WithModel(m),
  ai.WithPrompt("Explain how neural networks learn in simple terms."),
 )
 if err != nil {
  log.Fatal(err)
 }

 fmt.Println(resp.Text())
}
Configuration Options

You can pass advanced parameters to Ollama models using ollama.GenerateContentConfig.

:::note The plugin provides an ollama.Ptr helper function to easily initialize optional numeric fields (like Temperature or Seed). :::

import "github.com/firebase/genkit/go/plugins/ollama"

resp, err := genkit.Generate(ctx, g,
 ai.WithModel(m),
 ai.WithPrompt("Write a poem about the sea."),
 ai.WithConfig(&ollama.GenerateContentConfig{
  Temperature: ollama.Ptr(0.8),
  TopK:        ollama.Ptr(40),
  TopP:        ollama.Ptr(0.9),
  NumPredict:  ollama.Ptr(100), // Max output tokens
  KeepAlive:   "5m",           // Keep model loaded in memory for 5 minutes
  Seed:        ollama.Ptr(42),
 }),
)
Tool Calling

Ollama supports tool calling for specific models. Models like llama3.1 or mistral are excellent for this.

// Define a model that supports tools
o.DefineModel(g, ollama.ModelDefinition{
 Name: "llama3.1",
 Type: "chat",
}, nil)

m := ollama.Model(g, "llama3.1")

// Define a tool
weatherTool := genkit.DefineTool(g, "getWeather", "gets the weather",
 func(ctx *ai.ToolContext, input *WeatherInput) (*WeatherOutput, error) {
  return &WeatherOutput{Temp: 72}, nil
 },
)

resp, err := genkit.Generate(ctx, g,
 ai.WithModel(m),
 ai.WithPrompt("What is the weather in New York?"),
 ai.WithTools(weatherTool),
)

:::note The Ollama plugin supports receiving text content alongside tool calls. If a model returns both a text response and a tool request, both will be preserved in the message content. :::

Thinking and Reasoning

The plugin supports models with reasoning capabilities (like deepseek-r1).

resp, err := genkit.Generate(ctx, g,
 ai.WithModel(m),
 ai.WithPrompt("What is heavier, one kilo of steel or one kilo of feathers?"),
 ai.WithConfig(&ollama.GenerateContentConfig{
  Think: ollama.ThinkEnabled(true), // Enable thinking mode for supported models
 }),
)

// The model's reasoning process is returned as a specific part
fmt.Println("Reasoning:", resp.Reasoning())
fmt.Println("Final Answer:", resp.Text())
Note on Streaming Reasoning

For models that output reasoning using <think> tags within standard text (instead of Ollama's native thinking API field), the tags will be automatically parsed and mapped to Genkit's ReasoningPart in non-streaming mode.

In streaming mode, due to the nature of token-by-token chunks, these tags will be streamed as standard TextPart content. Models that natively support Ollama's thinking field will stream ReasoningPart chunks correctly in both modes.

Multimodal Input Capabilities

The plugin supports multimodal models (like llava) for image analysis.

// Define a multimodal model
o.DefineModel(g, ollama.ModelDefinition{
 Name: "llava",
 Type: "chat",
}, nil)

m := ollama.Model(g, "llava")

// Using inline data (base64)
imagePart := ai.NewMediaPart("image/jpeg", "data:image/jpeg;base64,...")

resp, err := genkit.Generate(ctx, g,
 ai.WithModel(m),
 ai.WithMessages(
  ai.NewUserMessage(
   ai.NewTextPart("Describe this image"),
   imagePart,
  ),
 ),
)

Embedding Models

You can define and use text embedding models hosted on Ollama (e.g., nomic-embed-text).

Defining an Embedder
// Define an embedder model
o.DefineEmbedder(g, "http://localhost:11434", "nomic-embed-text", nil)

embedder := ollama.Embedder(g, "http://localhost:11434")
Usage
res, err := genkit.Embed(ctx, g,
 ai.WithEmbedder(embedder),
 ai.WithTextDocs("Machine learning models process data to make predictions."),
)
if err != nil {
 log.Fatal(err)
}

fmt.Printf("Embedding length: %d\n", len(res.Embeddings[0].Embedding))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Embedder added in v0.1.2

func Embedder(g *genkit.Genkit, serverAddress string) ai.Embedder

Embedder returns the ai.Embedder with the given server address. It returns nil if the embedder was not defined.

func IsDefinedEmbedder added in v0.1.2

func IsDefinedEmbedder(g *genkit.Genkit, serverAddress string) bool

IsDefinedEmbedder reports whether the embedder with the given server address is defined by this plugin.

func IsDefinedModel added in v0.1.0

func IsDefinedModel(g *genkit.Genkit, name string) bool

IsDefinedModel reports whether a model is defined.

func Model

func Model(g *genkit.Genkit, name string) ai.Model

Model returns the ai.Model with the given name. It returns nil if the model was not configured.

func Ptr added in v1.6.0

func Ptr[T any](v T) *T

Ptr returns a pointer to the given value.

Types

type EmbedOptions added in v0.1.2

type EmbedOptions struct {
	Model string `json:"model"`
}

type GenerateContentConfig added in v1.6.0

type GenerateContentConfig struct {
	// Think controls thinking/reasoning mode.
	// Use ThinkEnabled(true/false) for Ollama models, or
	// ThinkEffort("low"/"medium"/"high") for GPT-OSS models.
	Think *ThinkOption `json:"think,omitempty"`

	// Runtime options
	Seed        *int     `json:"seed,omitempty"`
	Temperature *float64 `json:"temperature,omitempty"`
	TopK        *int     `json:"top_k,omitempty"`
	TopP        *float64 `json:"top_p,omitempty"`
	MinP        *float64 `json:"min_p,omitempty"`
	Stop        []string `json:"stop,omitempty"`
	NumCtx      *int     `json:"num_ctx,omitempty"`
	NumPredict  *int     `json:"num_predict,omitempty"`

	// Ollama-specific
	KeepAlive string `json:"keep_alive,omitempty"`
}

type ModelDefinition

type ModelDefinition struct {
	Name string
	Type string
}

ModelDefinition represents a model with its name and api.

type Ollama added in v0.3.0

type Ollama struct {
	ServerAddress string // Server address of oLLama.
	Timeout       int    // Response timeout in seconds (defaulted to 30 seconds)
	// contains filtered or unexported fields
}

Ollama provides configuration options for the Init function.

func (*Ollama) DefineEmbedder added in v0.3.0

func (o *Ollama) DefineEmbedder(g *genkit.Genkit, serverAddress string, model string, embedOpts *ai.EmbedderOptions) ai.Embedder

DefineEmbedder defines an embedder with a given server address.

func (*Ollama) DefineModel added in v0.3.0

func (o *Ollama) DefineModel(g *genkit.Genkit, model ModelDefinition, opts *ai.ModelOptions) ai.Model

func (*Ollama) Init added in v0.3.0

func (o *Ollama) Init(ctx context.Context) []api.Action

Init initializes the plugin. Since Ollama models are locally hosted, the plugin doesn't initialize any default models. After downloading a model, call [DefineModel] to use it.

func (*Ollama) ListActions added in v1.6.0

func (o *Ollama) ListActions(ctx context.Context) []api.ActionDesc

ListActions calls /api/tags to discover locally installed Ollama models.

func (*Ollama) Name added in v0.5.0

func (o *Ollama) Name() string

func (*Ollama) ResolveAction added in v1.6.0

func (o *Ollama) ResolveAction(atype api.ActionType, name string) api.Action

ResolveAction dynamically creates a model action on demand.

type ThinkOption added in v1.6.0

type ThinkOption struct {
	// contains filtered or unexported fields
}

ThinkOption controls thinking/reasoning behavior for models that support it. Use ThinkEnabled for Ollama models (e.g. deepseek-r1) or ThinkEffort for GPT-OSS models.

func ThinkEffort added in v1.6.0

func ThinkEffort(level string) *ThinkOption

ThinkEffort creates a ThinkOption with an effort level for GPT-OSS models. Valid values: "low", "medium", "high".

func ThinkEnabled added in v1.6.0

func ThinkEnabled(enabled bool) *ThinkOption

ThinkEnabled creates a ThinkOption that enables or disables thinking mode. This is used with Ollama models like deepseek-r1.

func (*ThinkOption) IsEnabled added in v1.6.0

func (t *ThinkOption) IsEnabled() bool

IsEnabled reports whether thinking is active.

func (ThinkOption) JSONSchema added in v1.6.0

func (ThinkOption) JSONSchema() *jsonschema.Schema

JSONSchema returns a schema allowing either a boolean or a string.

func (ThinkOption) MarshalJSON added in v1.6.0

func (t ThinkOption) MarshalJSON() ([]byte, error)

func (*ThinkOption) UnmarshalJSON added in v1.6.0

func (t *ThinkOption) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL