Files
Cyrene_v2/internal/runtime/embedding/manager.go
T

44 lines
1.4 KiB
Go

package embedding
import (
"context"
"strings"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
)
// Manager selects the runtime embedder based on configuration and exposes it
// through a single Embedder handle.
//
// When config.EmbeddingConfig.APIURL is set, the semantic APIEmbedder is
// chosen; otherwise the built-in NGramEmbedder is used. In both cases the
// caller receives a consistent Embedder that is safe for concurrent use.
//
// It is the integration point described in DevDocs/design/04-运行时.md §7: the
// API path is a real semantic-vector enhancement, while the n-gram path keeps
// the runtime zero-dependency and offline.
type Manager struct {
embedder Embedder
}
// New builds a Manager for cfg and selects the embedder. When cfg.APIURL is set
// (ignoring surrounding whitespace), an APIEmbedder is used; otherwise an
// NGramEmbedder with the default dimension.
func New(cfg config.EmbeddingConfig) *Manager {
var embedder Embedder
if strings.TrimSpace(cfg.APIURL) != "" {
embedder = NewAPI(cfg)
} else {
embedder = NewNGram(defaultNGramDim)
}
return &Manager{embedder: embedder}
}
// Embedder returns the selected embedder.
func (m *Manager) Embedder() Embedder { return m.embedder }
// Embed is a convenience pass-through to the selected embedder.
func (m *Manager) Embed(ctx context.Context, texts []string) ([]Vector, error) {
return m.embedder.Embed(ctx, texts)
}