embedding

package
v0.18.19 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package embedding provides text embedding via external APIs (OpenAI, Ollama).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterFunctions

func RegisterFunctions()

RegisterFunctions registers embed() and embed_dim() in the expression registry.

func SetProvider

func SetProvider(p Provider)

SetProvider configures the global embedding provider used by embed().

Types

type Cache

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

Cache is an LRU cache for embeddings keyed on (model, text_hash).

func NewCache

func NewCache(maxItems int) *Cache

NewCache creates an embedding cache with the given max entries.

func (*Cache) Get

func (c *Cache) Get(model, text string) []float32

Get returns a cached embedding, or nil if not found.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of cached entries.

func (*Cache) Put

func (c *Cache) Put(model, text string, vec []float32)

Put stores an embedding in the cache, evicting oldest if full.

type Ollama

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

Ollama implements Provider using a local Ollama server's /api/embed endpoint.

func NewOllama

func NewOllama(cfg OllamaConfig, cache *Cache) *Ollama

NewOllama creates an Ollama embedding provider.

func (*Ollama) Dimension

func (o *Ollama) Dimension() int

func (*Ollama) Embed

func (o *Ollama) Embed(texts []string) ([][]float32, error)

Embed returns embeddings for the given texts, using cache where possible. Uncached texts are batched into a single /api/embed call.

func (*Ollama) Model

func (o *Ollama) Model() string

type OllamaConfig

type OllamaConfig struct {
	Model      string // default: nomic-embed-text
	Dimensions int    // 0 = inferred from the model name (falls back to first response)
	BaseURL    string // default: http://localhost:11434
}

OllamaConfig configures the Ollama embedding provider for local, self-hosted embedding models (no API key, runs on the user's machine).

type OpenAI

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

OpenAI implements Provider using the OpenAI embeddings API.

func NewOpenAI

func NewOpenAI(cfg OpenAIConfig, cache *Cache) *OpenAI

NewOpenAI creates an OpenAI embedding provider.

func (*OpenAI) Dimension

func (o *OpenAI) Dimension() int

func (*OpenAI) Embed

func (o *OpenAI) Embed(texts []string) ([][]float32, error)

Embed returns embeddings for the given texts, using cache where possible. Uncached texts are batched into a single API call.

func (*OpenAI) Model

func (o *OpenAI) Model() string

type OpenAIConfig

type OpenAIConfig struct {
	APIKey     string // required
	Model      string // default: text-embedding-3-small
	Dimensions int    // 0 = model default (1536 for small, 3072 for large)
	BaseURL    string // default: https://api.openai.com/v1
}

OpenAIConfig configures the OpenAI embedding provider.

type Provider

type Provider interface {
	// Embed returns embedding vectors for one or more texts.
	// The returned slice has the same length as texts.
	Embed(texts []string) ([][]float32, error)

	// Dimension returns the output vector dimension.
	Dimension() int

	// Model returns the model identifier.
	Model() string
}

Provider generates vector embeddings from text.

func GetProvider

func GetProvider() Provider

GetProvider returns the current global provider, or nil.

type Voyage

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

Voyage implements Provider using the Voyage AI embeddings API.

func NewVoyage

func NewVoyage(cfg VoyageConfig, cache *Cache) *Voyage

NewVoyage creates a Voyage AI embedding provider.

func (*Voyage) Dimension

func (v *Voyage) Dimension() int

func (*Voyage) Embed

func (v *Voyage) Embed(texts []string) ([][]float32, error)

Embed returns embeddings for the given texts, using cache where possible. Uncached texts are batched into a single API call.

func (*Voyage) Model

func (v *Voyage) Model() string

type VoyageConfig

type VoyageConfig struct {
	APIKey     string // required
	Model      string // default: voyage-3.5
	Dimensions int    // 0 = model default (1024 for voyage-3.5 / voyage-3-large)
	InputType  string // optional: "document" or "query" — improves retrieval quality
	BaseURL    string // default: https://api.voyageai.com/v1
}

VoyageConfig configures the Voyage AI embedding provider. Voyage AI is Anthropic's officially recommended embeddings provider — Anthropic does not serve a native embeddings endpoint, so "Anthropic embeddings" means Voyage. See https://docs.claude.com/en/docs/build-with-claude/embeddings.

Jump to

Keyboard shortcuts

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