embedding

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package embedding provides text embedding generation using all-MiniLM-L6-v2.

Package embedding provides text embedding generation with swappable models.

Package embedding provides text embedding generation with swappable models.

Index

Constants

View Source
const (
	OpenAIModelVersion     = "openai"
	OpenAIDefaultBaseURL   = "https://api.openai.com/v1"
	OpenAIDefaultModel     = "text-embedding-3-small"
	OpenAIDefaultDimension = 1536
)
View Source
const (
	// BGEModelVersion is the version string for bge-small-en-v1.5
	BGEModelVersion = "bge-v1.5"
	// BGEModelName is the human-readable name for bge-small-en-v1.5
	BGEModelName = "bge-small-en-v1.5"
	// DefaultModelVersion is the default model to use
	DefaultModelVersion = BGEModelVersion
)

Model version constants

View Source
const EmbeddingDim = 384

EmbeddingDim is the dimension of embeddings produced by the current model. Both all-MiniLM-L6-v2 and bge-small-en-v1.5 produce 384-dimensional embeddings.

View Source
const MaxSequenceLength = 512

MaxSequenceLength is the maximum token sequence length for the model.

Variables

View Source
var DefaultRegistry = NewModelRegistry()

DefaultRegistry is the global model registry with all available models.

Functions

func GetDefaultModel

func GetDefaultModel() string

GetDefaultModel returns the default model version from the default registry.

func RegisterModel

func RegisterModel(meta ModelMetadata, factory ModelFactory)

RegisterModel adds a model to the default registry.

Types

type EmbeddingModel

type EmbeddingModel interface {
	// Name returns the human-readable model name (e.g., "bge-small-en-v1.5").
	Name() string

	// Version returns a short version string for storage (e.g., "bge-v1.5").
	Version() string

	// Dimensions returns the embedding vector size.
	Dimensions() int

	// Embed generates an embedding for a single text.
	Embed(text string) ([]float32, error)

	// EmbedBatch generates embeddings for multiple texts.
	EmbedBatch(texts []string) ([][]float32, error)

	// Close releases model resources.
	Close() error
}

EmbeddingModel represents a text embedding model.

func GetModel

func GetModel(version string) (EmbeddingModel, error)

GetModel creates a model instance from the default registry.

type ModelFactory

type ModelFactory func() (EmbeddingModel, error)

ModelFactory creates a new instance of an embedding model.

type ModelMetadata

type ModelMetadata struct {
	Name        string `json:"name"`
	Version     string `json:"version"`
	Description string `json:"description"`
	Dimensions  int    `json:"dimensions"`
	Default     bool   `json:"default"`
}

ModelMetadata describes an embedding model for UI/config.

func ListModels

func ListModels() []ModelMetadata

ListModels returns metadata for all models in the default registry.

type ModelRegistry

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

ModelRegistry provides model lookup by version.

func NewModelRegistry

func NewModelRegistry() *ModelRegistry

NewModelRegistry creates a new model registry.

func (*ModelRegistry) Default

func (r *ModelRegistry) Default() string

Default returns the default model version.

func (*ModelRegistry) Get

func (r *ModelRegistry) Get(version string) (EmbeddingModel, error)

Get creates a new instance of the model with the given version.

func (*ModelRegistry) List

func (r *ModelRegistry) List() []ModelMetadata

List returns metadata for all registered models.

func (*ModelRegistry) Register

func (r *ModelRegistry) Register(meta ModelMetadata, factory ModelFactory)

Register adds a model factory to the registry.

type ONNXConfig

type ONNXConfig struct {
	Pooling     PoolingStrategy
	InputNames  []string
	OutputNames []string
	HiddenSize  int
}

ONNXConfig describes ONNX-specific model configuration. This allows different models to specify their tensor names and pooling needs.

type ONNXConfigurer

type ONNXConfigurer interface {
	// ONNXConfig returns the model's ONNX configuration.
	ONNXConfig() ONNXConfig
}

ONNXConfigurer is an optional interface that models can implement to expose their ONNX configuration for introspection.

type PoolingStrategy

type PoolingStrategy string

PoolingStrategy defines how to pool token embeddings into sentence embeddings.

const (
	// PoolingNone means the model already outputs sentence embeddings directly.
	PoolingNone PoolingStrategy = "none"
	// PoolingMean averages all token embeddings (weighted by attention mask).
	PoolingMean PoolingStrategy = "mean"
	// PoolingCLS uses only the [CLS] token embedding.
	PoolingCLS PoolingStrategy = "cls"
)

type Service

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

Service provides thread-safe text embedding generation with model abstraction.

func NewService

func NewService() (*Service, error)

NewService creates a new embedding service using the default model.

func NewServiceFromConfig

func NewServiceFromConfig() (*Service, error)

NewServiceFromConfig creates an embedding service based on EMBEDDING_PROVIDER config. Uses "openai" provider when EMBEDDING_PROVIDER=openai, builtin ONNX otherwise.

func NewServiceWithModel

func NewServiceWithModel(version string) (*Service, error)

NewServiceWithModel creates a new embedding service using the specified model.

func (*Service) Close

func (s *Service) Close() error

Close releases model resources.

func (*Service) Dimensions

func (s *Service) Dimensions() int

Dimensions returns the embedding vector size.

func (*Service) Embed

func (s *Service) Embed(text string) ([]float32, error)

Embed generates an embedding for a single text.

func (*Service) EmbedBatch

func (s *Service) EmbedBatch(texts []string) ([][]float32, error)

EmbedBatch generates embeddings for multiple texts.

func (*Service) Name

func (s *Service) Name() string

Name returns the human-readable model name.

func (*Service) Version

func (s *Service) Version() string

Version returns the short version string for storage.

Jump to

Keyboard shortcuts

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