Documentation
¶
Index ¶
- Constants
- Variables
- func ComputeSimilarity(a, b []float32) (float32, error)
- func DestroyONNXRuntime() error
- func FormatSize(bytes int64) string
- func InitializeONNXRuntime() error
- func ListAvailableModels() []string
- func NormalizeEmbedding(embedding []float32) []float32
- func PrepareTextForEmbedding(organism, libraryStrategy, title, abstract string) string
- func VerifyFile(filePath string, expectedSHA256 string) error
- type BertTokenizer
- type DownloadProgress
- type Downloader
- type Embedder
- func (e *Embedder) Close() error
- func (e *Embedder) DownloadModel(modelID string, variantName string, progress chan<- DownloadProgress) error
- func (e *Embedder) Embed(text string) ([]float32, error)
- func (e *Embedder) EmbedBatch(texts []string) ([][]float32, error)
- func (e *Embedder) EmbedText(text string) ([]float32, error)
- func (e *Embedder) EmbedTexts(texts []string) ([][]float32, error)
- func (e *Embedder) GetLoadedModel() string
- func (e *Embedder) GetManager() *Manager
- func (e *Embedder) IsEnabled() bool
- func (e *Embedder) IsModelLoaded() bool
- func (e *Embedder) LoadDefaultModel() error
- func (e *Embedder) LoadModel(modelID string) error
- func (e *Embedder) TestModel(modelID string, text string) ([]float32, error)
- type EmbedderConfig
- type Encoding
- type Manager
- func (m *Manager) GetActiveVariantPath(modelID string) (string, error)
- func (m *Manager) GetModel(modelID string) (*ModelInfo, error)
- func (m *Manager) GetModelPath(modelID string) string
- func (m *Manager) GetModelsDir() string
- func (m *Manager) ListModels() ([]*ModelInfo, error)
- func (m *Manager) RefreshModels() error
- func (m *Manager) RegisterModel(modelID string) error
- func (m *Manager) RemoveModel(modelID string) error
- func (m *Manager) SetActiveVariant(modelID, variantName string) error
- type Model
- type ModelConfig
- type ModelInfo
- type ModelVariant
- type ONNXEmbedder
- type SearchEmbedder
- type Tokenizer
- type TokenizerConfig
- type VariantInfo
Constants ¶
const ( MB = 1024 * 1024 GB = 1024 * MB )
Variables ¶
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 ¶
ComputeSimilarity computes cosine similarity between two embeddings
func DestroyONNXRuntime ¶
func DestroyONNXRuntime() error
DestroyONNXRuntime cleans up the ONNX Runtime library (stub)
func FormatSize ¶
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 ¶
NormalizeEmbedding normalizes an embedding to unit length
func PrepareTextForEmbedding ¶
PrepareTextForEmbedding prepares SRA metadata for embedding
func VerifyFile ¶
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
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) DownloadModel ¶
func (e *Embedder) DownloadModel(modelID string, variantName string, progress chan<- DownloadProgress) error
DownloadModel downloads a model using the embedded downloader
func (*Embedder) Embed ¶
Embed generates an embedding for a text string (implements EmbedderInterface)
func (*Embedder) EmbedBatch ¶
EmbedBatch generates embeddings for multiple texts (implements EmbedderInterface)
func (*Embedder) EmbedTexts ¶
EmbedTexts generates embeddings for multiple texts
func (*Embedder) GetLoadedModel ¶
GetLoadedModel returns the ID of the currently loaded model
func (*Embedder) GetManager ¶
GetManager returns the model manager
func (*Embedder) IsModelLoaded ¶
IsModelLoaded checks if a model is currently loaded
func (*Embedder) LoadDefaultModel ¶
LoadDefaultModel loads the default model
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 ¶
NewManager creates a new model manager
func (*Manager) GetActiveVariantPath ¶
GetActiveVariantPath returns the path to the active variant's ONNX file
func (*Manager) GetModelPath ¶
GetModelPath returns the path to a model's directory
func (*Manager) GetModelsDir ¶
GetModelsDir returns the models directory path
func (*Manager) ListModels ¶
ListModels returns all installed models
func (*Manager) RefreshModels ¶
RefreshModels rescans the models directory
func (*Manager) RegisterModel ¶
RegisterModel registers a newly downloaded model
func (*Manager) RemoveModel ¶
RemoveModel removes a model from the manager (but doesn't delete files)
func (*Manager) SetActiveVariant ¶
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) EmbedSingle ¶
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 ¶
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) 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) 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 ¶
LoadEmbeddedTokenizer loads the embedded tokenizer
func LoadTokenizer ¶
LoadTokenizer loads a tokenizer from model directory
func (*Tokenizer) GetSpecialTokens ¶
GetSpecialTokens returns the special tokens
func (*Tokenizer) GetVocabSize ¶
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