embedding

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

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
)

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 Service

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

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

func NewServiceFromConfig

func NewServiceFromConfig() (*Service, error)

NewServiceFromConfig creates an embedding service based on EMBEDDING_PROVIDER config. Always uses the openai provider (API-based embedding).

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