Add embedding dimension override support

This commit is contained in:
wizardchen
2026-06-15 19:16:31 +08:00
committed by lyingbug
parent 90f8e8b390
commit 53e0333a53
27 changed files with 476 additions and 189 deletions
+16 -9
View File
@@ -12,11 +12,12 @@ import (
// OllamaEmbedder implements text vectorization functionality using Ollama
type OllamaEmbedder struct {
modelName string
truncatePromptTokens int
ollamaService *ollama.OllamaService
dimensions int
modelID string
modelName string
truncatePromptTokens int
ollamaService *ollama.OllamaService
dimensions int
modelID string
supportsDimensionOverride bool
EmbedderPooler
}
@@ -65,6 +66,10 @@ func (e *OllamaEmbedder) ensureModelAvailable(ctx context.Context) error {
return e.ollamaService.EnsureModelAvailable(ctx, e.modelName)
}
func (e *OllamaEmbedder) SetSupportsDimensionOverride(supported bool) {
e.supportsDimensionOverride = supported
}
// Embed converts text to vector
func (e *OllamaEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {
embedding, err := e.BatchEmbed(ctx, []string{text})
@@ -88,10 +93,12 @@ func (e *OllamaEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]fl
// Create request
req := &ollamaapi.EmbedRequest{
Model: e.modelName,
Input: texts,
Dimensions: e.dimensions,
Options: make(map[string]interface{}),
Model: e.modelName,
Input: texts,
Options: make(map[string]interface{}),
}
if e.supportsDimensionOverride && e.dimensions > 0 {
req.Dimensions = e.dimensions
}
// Set truncation parameters