embeddings

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MB = 1024 * 1024
	GB = 1024 * MB
)

Variables

View Source
var ModelRegistry = map[string]ModelConfig{
	"Xenova/SapBERT-from-PubMedBERT-fulltext": {
		ID:           "Xenova/SapBERT-from-PubMedBERT-fulltext",
		Organization: "Xenova",
		Name:         "SapBERT-from-PubMedBERT-fulltext",
		Description:  "Biomedical entity embedding model trained on PubMed",
		ModelType:    "bert",
		HiddenSize:   768,
		MaxLength:    512,
		BaseURL:      "https://huggingface.co/Xenova/SapBERT-from-PubMedBERT-fulltext/resolve/main",
		Variants: []ModelVariant{
			{
				Name:        "quantized",
				Filename:    "onnx/model_quantized.onnx",
				Size:        111 * MB,
				URL:         "https://huggingface.co/Xenova/SapBERT-from-PubMedBERT-fulltext/resolve/main/onnx/model_quantized.onnx",
				Default:     true,
				Description: "Quantized model (INT8) - fastest, smallest, good accuracy",
			},
			{
				Name:        "fp16",
				Filename:    "onnx/model_fp16.onnx",
				Size:        219 * MB,
				URL:         "https://huggingface.co/Xenova/SapBERT-from-PubMedBERT-fulltext/resolve/main/onnx/model_fp16.onnx",
				Description: "Half precision (FP16) - balanced speed and accuracy",
			},
			{
				Name:        "full",
				Filename:    "onnx/model.onnx",
				Size:        438 * MB,
				URL:         "https://huggingface.co/Xenova/SapBERT-from-PubMedBERT-fulltext/resolve/main/onnx/model.onnx",
				Description: "Full precision (FP32) - highest accuracy, slowest",
			},
		},
		TokenizerFiles: []string{
			"tokenizer.json",
			"tokenizer_config.json",
			"special_tokens_map.json",
			"vocab.txt",
		},
		ConfigFiles: []string{
			"config.json",
		},
	},
	"sentence-transformers/all-MiniLM-L6-v2": {
		ID:           "sentence-transformers/all-MiniLM-L6-v2",
		Organization: "sentence-transformers",
		Name:         "all-MiniLM-L6-v2",
		Description:  "General-purpose sentence embedding model",
		ModelType:    "bert",
		HiddenSize:   384,
		MaxLength:    256,
		BaseURL:      "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main",
		Variants: []ModelVariant{
			{
				Name:        "quantized",
				Filename:    "onnx/model_quantized.onnx",
				Size:        23 * MB,
				URL:         "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model_quantized.onnx",
				Default:     true,
				Description: "Quantized model - very fast and small",
			},
			{
				Name:        "full",
				Filename:    "onnx/model.onnx",
				Size:        90 * MB,
				URL:         "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx",
				Description: "Full precision model",
			},
		},
		TokenizerFiles: []string{
			"tokenizer.json",
			"tokenizer_config.json",
			"special_tokens_map.json",
			"vocab.txt",
		},
		ConfigFiles: []string{
			"config.json",
			"sentence_bert_config.json",
		},
	},
}

ModelRegistry contains information about available models and their variants

Functions

func ComputeSimilarity

func ComputeSimilarity(a, b []float32) (float32, error)

ComputeSimilarity computes cosine similarity between two embeddings

func DestroyONNXRuntime

func DestroyONNXRuntime() error

DestroyONNXRuntime cleans up the ONNX Runtime library (stub)

func FormatSize

func FormatSize(bytes int64) string

FormatSize formats a size in bytes to a human-readable string

func InitializeONNXRuntime

func InitializeONNXRuntime() error

InitializeONNXRuntime initializes the ONNX Runtime library (stub)

func ListAvailableModels

func ListAvailableModels() []string

ListAvailableModels returns a list of all available models in the registry

func NormalizeEmbedding

func NormalizeEmbedding(embedding []float32) []float32

NormalizeEmbedding normalizes an embedding to unit length

func PrepareTextForEmbedding

func PrepareTextForEmbedding(organism, libraryStrategy, title, abstract string) string

PrepareTextForEmbedding prepares SRA metadata for embedding

func VerifyFile

func VerifyFile(filePath string, expectedSHA256 string) error

VerifyFile verifies a downloaded file against its expected hash

Types

type BertTokenizer

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

BertTokenizer handles BERT-style tokenization

func NewBertTokenizer

func NewBertTokenizer(vocabPath string) (*BertTokenizer, error)

NewBertTokenizer creates a new BERT tokenizer

func (*BertTokenizer) Encode

func (t *BertTokenizer) Encode(text string, maxLength int) []int

Encode converts text to token IDs

type DownloadProgress

type DownloadProgress struct {
	File            string
	BytesDownloaded int64
	TotalBytes      int64
	Percentage      float64
	Speed           float64 // MB/s
	ETA             time.Duration
}

DownloadProgress represents download progress information

type Downloader

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

Downloader handles model downloading from HuggingFace

func NewDownloader

func NewDownloader(manager *Manager, progress chan<- DownloadProgress) *Downloader

NewDownloader creates a new model downloader

func (*Downloader) CleanupTempFiles

func (d *Downloader) CleanupTempFiles() error

CleanupTempFiles removes any .tmp files in the models directory

func (*Downloader) DownloadModel

func (d *Downloader) DownloadModel(modelID string, variantName string) error

DownloadModel downloads a complete model with specified variant

func (*Downloader) DownloadVariant

func (d *Downloader) DownloadVariant(modelID string, variantName string) error

DownloadVariant downloads only a specific variant of an already installed model

type Embedder

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

Embedder is the main service for generating embeddings

func NewEmbedder

func NewEmbedder(config *EmbedderConfig) (*Embedder, error)

NewEmbedder creates a new embedder service

func (*Embedder) Close

func (e *Embedder) Close() error

Close releases all resources

func (*Embedder) DownloadModel

func (e *Embedder) DownloadModel(modelID string, variantName string, progress chan<- DownloadProgress) error

DownloadModel downloads a model using the embedded downloader

func (*Embedder) Embed

func (e *Embedder) Embed(text string) ([]float32, error)

Embed generates an embedding for a text string (implements EmbedderInterface)

func (*Embedder) EmbedBatch

func (e *Embedder) EmbedBatch(texts []string) ([][]float32, error)

EmbedBatch generates embeddings for multiple texts (implements EmbedderInterface)

func (*Embedder) EmbedText

func (e *Embedder) EmbedText(text string) ([]float32, error)

EmbedText generates an embedding for a text string

func (*Embedder) EmbedTexts

func (e *Embedder) EmbedTexts(texts []string) ([][]float32, error)

EmbedTexts generates embeddings for multiple texts

func (*Embedder) GetLoadedModel

func (e *Embedder) GetLoadedModel() string

GetLoadedModel returns the ID of the currently loaded model

func (*Embedder) GetManager

func (e *Embedder) GetManager() *Manager

GetManager returns the model manager

func (*Embedder) IsEnabled

func (e *Embedder) IsEnabled() bool

IsEnabled checks if the embedder is enabled and has a model loaded

func (*Embedder) IsModelLoaded

func (e *Embedder) IsModelLoaded() bool

IsModelLoaded checks if a model is currently loaded

func (*Embedder) LoadDefaultModel

func (e *Embedder) LoadDefaultModel() error

LoadDefaultModel loads the default model

func (*Embedder) LoadModel

func (e *Embedder) LoadModel(modelID string) error

LoadModel loads a specific model for embedding

func (*Embedder) TestModel

func (e *Embedder) TestModel(modelID string, text string) ([]float32, error)

TestModel tests a model by generating an embedding for sample text

type EmbedderConfig

type EmbedderConfig struct {
	ModelsDir    string `yaml:"models_dir"`
	DefaultModel string `yaml:"default_model"`
	BatchSize    int    `yaml:"batch_size"`
	MaxLength    int    `yaml:"max_length"`
	NumThreads   int    `yaml:"num_threads"`
	CacheEnabled bool   `yaml:"cache_enabled"`
}

EmbedderConfig contains configuration for the embedder

func DefaultEmbedderConfig

func DefaultEmbedderConfig() *EmbedderConfig

DefaultEmbedderConfig returns the default configuration

type Encoding

type Encoding struct {
	InputIDs      []int64  `json:"input_ids"`
	AttentionMask []int64  `json:"attention_mask"`
	TokenTypeIDs  []int64  `json:"token_type_ids"`
	Tokens        []string `json:"tokens"`
}

Encoding represents tokenized text

type Manager

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

Manager handles model storage and management following HuggingFace conventions

func NewManager

func NewManager(modelsDir string) (*Manager, error)

NewManager creates a new model manager

func (*Manager) GetActiveVariantPath

func (m *Manager) GetActiveVariantPath(modelID string) (string, error)

GetActiveVariantPath returns the path to the active variant's ONNX file

func (*Manager) GetModel

func (m *Manager) GetModel(modelID string) (*ModelInfo, error)

GetModel returns information about a specific model

func (*Manager) GetModelPath

func (m *Manager) GetModelPath(modelID string) string

GetModelPath returns the path to a model's directory

func (*Manager) GetModelsDir

func (m *Manager) GetModelsDir() string

GetModelsDir returns the models directory path

func (*Manager) ListModels

func (m *Manager) ListModels() ([]*ModelInfo, error)

ListModels returns all installed models

func (*Manager) RefreshModels

func (m *Manager) RefreshModels() error

RefreshModels rescans the models directory

func (*Manager) RegisterModel

func (m *Manager) RegisterModel(modelID string) error

RegisterModel registers a newly downloaded model

func (*Manager) RemoveModel

func (m *Manager) RemoveModel(modelID string) error

RemoveModel removes a model from the manager (but doesn't delete files)

func (*Manager) SetActiveVariant

func (m *Manager) SetActiveVariant(modelID, variantName string) error

SetActiveVariant sets the active variant for a model

type Model

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

Model represents an ONNX model for generating embeddings (stub implementation)

func LoadModel

func LoadModel(modelPath string, config *ModelConfig, variantName string) (*Model, error)

LoadModel loads an ONNX model from disk (stub implementation)

func (*Model) Close

func (m *Model) Close() error

Close releases model resources

func (*Model) Embed

func (m *Model) Embed(inputIDs [][]int64, attentionMask [][]int64) ([][]float32, error)

Embed generates embeddings for a batch of token IDs (stub implementation)

func (*Model) EmbedSingle

func (m *Model) EmbedSingle(inputIDs []int64, attentionMask []int64) ([]float32, error)

EmbedSingle generates an embedding for a single sequence of token IDs

func (*Model) GetConfig

func (m *Model) GetConfig() *ModelConfig

GetConfig returns the model configuration

func (*Model) GetVariantName

func (m *Model) GetVariantName() string

GetVariantName returns the name of the loaded variant

type ModelConfig

type ModelConfig struct {
	ID             string         `json:"id"`
	Organization   string         `json:"organization"`
	Name           string         `json:"name"`
	Description    string         `json:"description"`
	ModelType      string         `json:"model_type"`      // "bert", "roberta", etc.
	HiddenSize     int            `json:"hidden_size"`     // Embedding dimension
	MaxLength      int            `json:"max_length"`      // Maximum sequence length
	BaseURL        string         `json:"base_url"`        // Base URL for downloading files
	Variants       []ModelVariant `json:"variants"`        // Available model variants
	TokenizerFiles []string       `json:"tokenizer_files"` // Required tokenizer files
	ConfigFiles    []string       `json:"config_files"`    // Required config files
}

ModelConfig contains complete configuration for a model

func GetModelConfig

func GetModelConfig(modelID string) (*ModelConfig, error)

GetModelConfig returns the configuration for a specific model

type ModelInfo

type ModelInfo struct {
	ID            string                 `json:"id"`             // e.g., "Xenova/SapBERT-from-PubMedBERT-fulltext"
	Organization  string                 `json:"organization"`   // e.g., "Xenova"
	Name          string                 `json:"name"`           // e.g., "SapBERT-from-PubMedBERT-fulltext"
	Path          string                 `json:"path"`           // Full path to model directory
	Variants      []VariantInfo          `json:"variants"`       // Available variants
	ActiveVariant string                 `json:"active_variant"` // Currently active variant
	Config        map[string]interface{} `json:"config"`         // Model configuration from config.json
	InstalledAt   time.Time              `json:"installed_at"`
	LastUsed      time.Time              `json:"last_used"`
}

ModelInfo represents metadata about an installed model

type ModelVariant

type ModelVariant struct {
	Name        string `json:"name"`        // Variant name (e.g., "quantized")
	Filename    string `json:"filename"`    // Relative path to ONNX file
	Size        int64  `json:"size"`        // Expected file size in bytes
	URL         string `json:"url"`         // Download URL
	SHA256      string `json:"sha256"`      // Expected SHA256 hash
	Default     bool   `json:"default"`     // Is this the default variant?
	Description string `json:"description"` // Human-readable description
}

ModelVariant defines a specific variant of a model

func GetDefaultVariant

func GetDefaultVariant(modelID string) (*ModelVariant, error)

GetDefaultVariant returns the default variant for a model

func GetVariant

func GetVariant(modelID, variantName string) (*ModelVariant, error)

GetVariant returns a specific variant for a model

type ONNXEmbedder

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

ONNXEmbedder generates embeddings using ONNX Runtime

func NewONNXEmbedder

func NewONNXEmbedder(modelPath string, cacheDir string) (*ONNXEmbedder, error)

NewONNXEmbedder creates a new ONNX embedder

func (*ONNXEmbedder) Close

func (e *ONNXEmbedder) Close() error

Close cleans up resources

func (*ONNXEmbedder) Embed

func (e *ONNXEmbedder) Embed(text string) ([]float32, error)

Embed generates an embedding for a single text

func (*ONNXEmbedder) EmbedBatch

func (e *ONNXEmbedder) EmbedBatch(texts []string) ([][]float32, error)

EmbedBatch generates embeddings for multiple texts

func (*ONNXEmbedder) IsEnabled

func (e *ONNXEmbedder) IsEnabled() bool

IsEnabled returns whether the embedder is enabled

type SearchEmbedder

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

SearchEmbedder implements the search.EmbedderInterface

func NewSearchEmbedder

func NewSearchEmbedder(cfg *config.Config) (*SearchEmbedder, error)

NewSearchEmbedder creates an embedder for search integration

func (*SearchEmbedder) Close

func (s *SearchEmbedder) Close() error

Close cleans up resources

func (*SearchEmbedder) Embed

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

Embed generates an embedding for a single text

func (*SearchEmbedder) EmbedBatch

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

EmbedBatch generates embeddings for multiple texts

func (*SearchEmbedder) IsEnabled

func (s *SearchEmbedder) IsEnabled() bool

IsEnabled returns whether the embedder is enabled

type Tokenizer

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

Tokenizer handles text tokenization for BERT models

func LoadEmbeddedTokenizer

func LoadEmbeddedTokenizer() (*Tokenizer, error)

LoadEmbeddedTokenizer loads the embedded tokenizer

func LoadTokenizer

func LoadTokenizer(modelPath string) (*Tokenizer, error)

LoadTokenizer loads a tokenizer from model directory

func (*Tokenizer) Decode

func (t *Tokenizer) Decode(inputIDs []int64) string

Decode converts token IDs back to text

func (*Tokenizer) Encode

func (t *Tokenizer) Encode(text string, maxLength int) (*Encoding, error)

Encode tokenizes text and returns token IDs

func (*Tokenizer) GetSpecialTokens

func (t *Tokenizer) GetSpecialTokens() map[string]int64

GetSpecialTokens returns the special tokens

func (*Tokenizer) GetVocabSize

func (t *Tokenizer) GetVocabSize() int

GetVocabSize returns the size of the vocabulary

type TokenizerConfig

type TokenizerConfig struct {
	DoLowerCase   bool             `json:"do_lower_case"`
	MaxLength     int              `json:"model_max_length"`
	PadToken      string           `json:"pad_token"`
	SepToken      string           `json:"sep_token"`
	ClsToken      string           `json:"cls_token"`
	UnkToken      string           `json:"unk_token"`
	MaskToken     string           `json:"mask_token"`
	SpecialTokens map[string]int64 `json:"special_tokens_map"`
}

TokenizerConfig represents tokenizer configuration

type VariantInfo

type VariantInfo struct {
	Name       string    `json:"name"`       // e.g., "quantized", "fp16", "full"
	Filename   string    `json:"filename"`   // e.g., "onnx/model_quantized.onnx"
	Size       int64     `json:"size"`       // File size in bytes
	Downloaded bool      `json:"downloaded"` // Whether this variant is downloaded
	Path       string    `json:"path"`       // Full path to the ONNX file
	CreatedAt  time.Time `json:"created_at"`
}

VariantInfo describes a specific model variant

Jump to

Keyboard shortcuts

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