Documentation
¶
Overview ¶
Package embeddings provides embedding generation via multiple providers.
Package embeddings provides embedding generation via multiple providers.
Package embeddings provides embedding generation via TEI.
Index ¶
- Constants
- Variables
- func DownloadONNXRuntime(ctx context.Context, version string) error
- func EnsureONNXRuntime(ctx context.Context) (string, error)
- func GetONNXLibraryPath() string
- func ONNXRuntimeExists() bool
- type Config
- type FastEmbedConfig
- type FastEmbedProvider
- func (p *FastEmbedProvider) Close() error
- func (p *FastEmbedProvider) Dimension() int
- func (p *FastEmbedProvider) EmbedDocuments(ctx context.Context, texts []string) ([][]float32, error)
- func (p *FastEmbedProvider) EmbedQuery(ctx context.Context, text string) ([]float32, error)
- func (p *FastEmbedProvider) Embedder() vectorstore.Embedder
- type Provider
- type ProviderConfig
- type Service
- func (s *Service) Embed(ctx context.Context, texts []string) ([][]float32, error)
- func (s *Service) EmbedDocuments(ctx context.Context, texts []string) ([][]float32, error)
- func (s *Service) EmbedQuery(ctx context.Context, text string) ([]float32, error)
- func (s *Service) Embedder() vectorstore.Embedder
Constants ¶
const DefaultONNXRuntimeVersion = "1.23.0"
DefaultONNXRuntimeVersion is the ONNX runtime version matching onnxruntime_go. Update this when bumping the onnxruntime_go dependency in go.mod.
Variables ¶
var ( // ErrEmptyInput indicates empty or nil input texts ErrEmptyInput = errors.New("empty or nil input texts") // ErrInvalidConfig indicates invalid configuration ErrInvalidConfig = errors.New("invalid configuration") // ErrEmbeddingFailed indicates embedding generation failure ErrEmbeddingFailed = errors.New("embedding generation failed") )
var ErrUnsupportedPlatform = fmt.Errorf("unsupported platform")
ErrUnsupportedPlatform indicates the current OS/arch is not supported.
Functions ¶
func DownloadONNXRuntime ¶ added in v0.3.0
DownloadONNXRuntime downloads ONNX runtime for the current platform. If version is empty, uses DefaultONNXRuntimeVersion.
func EnsureONNXRuntime ¶ added in v0.3.0
EnsureONNXRuntime ensures ONNX runtime is available, downloading if needed. Returns the path to the library file.
func GetONNXLibraryPath ¶ added in v0.3.0
func GetONNXLibraryPath() string
GetONNXLibraryPath returns the path to the ONNX runtime library. Checks in order: 1. ONNX_PATH environment variable 2. Managed install at ~/.config/contextd/lib/ Returns empty string if not found.
func ONNXRuntimeExists ¶ added in v0.3.0
func ONNXRuntimeExists() bool
ONNXRuntimeExists checks if ONNX runtime is available.
Types ¶
type Config ¶
type Config struct {
// BaseURL is the base URL for the embedding API
BaseURL string
// Model is the embedding model to use
Model string
// APIKey is the API key (optional for TEI)
APIKey string
}
Config holds configuration for the embedding service.
func ConfigFromEnv ¶
func ConfigFromEnv() Config
ConfigFromEnv creates a Config from environment variables.
type FastEmbedConfig ¶
type FastEmbedConfig struct {
// Model is the embedding model to use.
// Supported: BAAI/bge-small-en-v1.5 (default), BAAI/bge-base-en-v1.5,
// sentence-transformers/all-MiniLM-L6-v2, etc.
Model string
// CacheDir is the directory to cache model files.
// Defaults to ~/.config/contextd/models
CacheDir string
// MaxLength is the maximum input sequence length.
// Defaults to 512.
MaxLength int
}
FastEmbedConfig holds configuration for the FastEmbed provider.
type FastEmbedProvider ¶
type FastEmbedProvider struct {
// contains filtered or unexported fields
}
FastEmbedProvider provides embedding generation using local ONNX models.
func NewFastEmbedProvider ¶
func NewFastEmbedProvider(cfg FastEmbedConfig) (*FastEmbedProvider, error)
NewFastEmbedProvider creates a new FastEmbed embedding provider.
func (*FastEmbedProvider) Close ¶
func (p *FastEmbedProvider) Close() error
Close releases resources held by the FastEmbed provider.
func (*FastEmbedProvider) Dimension ¶
func (p *FastEmbedProvider) Dimension() int
Dimension returns the embedding dimension for the current model.
func (*FastEmbedProvider) EmbedDocuments ¶
func (p *FastEmbedProvider) EmbedDocuments(ctx context.Context, texts []string) ([][]float32, error)
EmbedDocuments generates embeddings for multiple texts. Uses "passage: " prefix for document embeddings as recommended by BGE models.
func (*FastEmbedProvider) EmbedQuery ¶
EmbedQuery generates an embedding for a single query. Uses "query: " prefix for query embeddings as recommended by BGE models.
func (*FastEmbedProvider) Embedder ¶
func (p *FastEmbedProvider) Embedder() vectorstore.Embedder
Embedder returns an Embedder interface implementation.
type Provider ¶
type Provider interface {
vectorstore.Embedder
// Dimension returns the embedding dimension for the current model.
Dimension() int
// Close releases resources held by the provider.
Close() error
}
Provider is the interface for embedding providers.
func NewProvider ¶
func NewProvider(cfg ProviderConfig) (Provider, error)
NewProvider creates an embedding provider based on the configuration.
type ProviderConfig ¶
type ProviderConfig struct {
// Provider is the provider type: "fastembed" or "tei"
Provider string
// Model is the embedding model name
Model string
// BaseURL is the TEI URL (only used for TEI provider)
BaseURL string
// CacheDir is the model cache directory (only used for FastEmbed)
CacheDir string
}
ProviderConfig holds configuration for creating an embedding provider.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service provides embedding generation functionality.
func NewService ¶
NewService creates a new embedding service with the given configuration.
func (*Service) EmbedDocuments ¶
EmbedDocuments generates embeddings for multiple texts.
func (*Service) EmbedQuery ¶
EmbedQuery generates an embedding for a single query.
func (*Service) Embedder ¶
func (s *Service) Embedder() vectorstore.Embedder
Embedder returns an Embedder interface implementation.