ollama

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package ollama implements the Embedder interface via Ollama's HTTP API. Useful when ONNX model files are unavailable (e.g. HuggingFace blocked) or when the user prefers Ollama's model management.

Prerequisites: Ollama running locally (ollama serve) with the desired model pulled (ollama pull bge-m3).

Usage:

ckv build --embedder=ollama --model-name=bge-m3 --src ./project

Index

Constants

View Source
const DefaultEndpoint = "http://localhost:11434"

DefaultEndpoint is the default Ollama API base URL. Override with CKV_OLLAMA_ENDPOINT environment variable.

View Source
const DefaultMaxInputTokens = 8192

DefaultMaxInputTokens is the fallback context limit for an Ollama model not present in the embedding registry. bge-m3's value; safe for BERT-family embedders that don't advertise a larger window.

View Source
const DefaultTimeout = 60 * time.Second

DefaultTimeout bounds every request to the Ollama daemon. Without it a wedged daemon (model loading, stuck GPU) that accepts the connection but never responds would block embed calls — and the startup probe — forever, hanging the build, the query path, and any consumer that opens the adapter at startup. A single embed of a chunk batch should be well under this.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

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

Adapter implements types.Embedder via Ollama's /api/embed endpoint.

func Open

func Open(opts Options) (*Adapter, error)

Open creates an Ollama adapter and verifies connectivity by embedding a test string to determine the output dimension.

func (*Adapter) Close

func (a *Adapter) Close() error

func (*Adapter) Dimension

func (a *Adapter) Dimension() int

func (*Adapter) Embed

func (a *Adapter) Embed(ctx context.Context, batch []string) ([][]float32, error)

Embed calls Ollama's /api/embed endpoint with batch input.

func (*Adapter) EmbedQuery

func (a *Adapter) EmbedQuery(ctx context.Context, queries []string) ([][]float32, error)

EmbedQuery embeds retrieval queries. For an asymmetric model (Qwen3, which carries a QueryInstruct in the registry) it wraps each query in the model's instruct prompt before embedding; symmetric models (bge-*) fall through to Embed unchanged. Passages always go through Embed. Implements types.QueryEmbedder.

func (*Adapter) Identity

func (a *Adapter) Identity() types.EmbeddingIdentity

Identity reports the embedding space. Ollama performs tokenization and pooling internally and does not expose those, so Pooling/Normalize are left empty; Provider+Model+Dim still distinguish an Ollama-built index from one built by another backend (e.g. ONNX) for the same model name, which is the swap query.Open must reject.

func (*Adapter) MaxInputTokens

func (a *Adapter) MaxInputTokens() int

func (*Adapter) Name

func (a *Adapter) Name() string

type Options

type Options struct {
	Endpoint  string        // Ollama API URL (default: http://localhost:11434)
	ModelName string        // model name as known to Ollama (e.g. "bge-m3")
	Timeout   time.Duration // per-request timeout (default: DefaultTimeout); <=0 uses the default
	// TargetDim, when >0 and smaller than the model's native dimension,
	// truncates every embedding to its first TargetDim components and
	// re-normalizes to unit length (Matryoshka Representation Learning). Used
	// by Qwen3-Embedding, which is MRL-trained, to trade a little precision for
	// a smaller vector (storage + search cost). 0 keeps the native dimension.
	TargetDim int
}

Options configures the Ollama adapter.

Jump to

Keyboard shortcuts

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