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 ¶
const ( OpenAIModelVersion = "openai" OpenAIDefaultBaseURL = "https://api.openai.com/v1" OpenAIDefaultModel = "text-embedding-3-small" OpenAIDefaultDimension = 1536 )
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
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.
const MaxSequenceLength = 512
MaxSequenceLength is the maximum token sequence length for the model.
Variables ¶
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 ¶
NewService creates a new embedding service using the default model.
func NewServiceFromConfig ¶
NewServiceFromConfig creates an embedding service based on EMBEDDING_PROVIDER config. Uses "openai" provider when EMBEDDING_PROVIDER=openai, builtin ONNX otherwise.
func NewServiceWithModel ¶
NewServiceWithModel creates a new embedding service using the specified model.
func (*Service) Dimensions ¶
Dimensions returns the embedding vector size.
func (*Service) EmbedBatch ¶
EmbedBatch generates embeddings for multiple texts.