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
+22 -11
View File
@@ -15,16 +15,17 @@ import (
// OpenAIEmbedder implements text vectorization functionality using OpenAI API
type OpenAIEmbedder struct {
apiKey string
baseURL string
modelName string
truncatePromptTokens int
dimensions int
modelID string
httpClient *http.Client
timeout time.Duration
maxRetries int
customHeaders map[string]string
apiKey string
baseURL string
modelName string
truncatePromptTokens int
dimensions int
modelID string
httpClient *http.Client
timeout time.Duration
maxRetries int
customHeaders map[string]string
supportsDimensionOverride bool
EmbedderPooler
}
@@ -88,6 +89,10 @@ func (e *OpenAIEmbedder) SetCustomHeaders(headers map[string]string) {
e.customHeaders = headers
}
func (e *OpenAIEmbedder) SetSupportsDimensionOverride(supported bool) {
e.supportsDimensionOverride = supported
}
// Embed converts text to vector
func (e *OpenAIEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {
for range 3 {
@@ -162,9 +167,11 @@ func (e *OpenAIEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]fl
Model: e.modelName,
Input: texts,
EncodingFormat: "float",
Dimensions: e.dimensions,
TruncatePromptTokens: e.truncatePromptTokens,
}
if e.supportsDimensionsParam() {
reqBody.Dimensions = e.dimensions
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
@@ -248,6 +255,10 @@ func (e *OpenAIEmbedder) GetModelName() string {
return e.modelName
}
func (e *OpenAIEmbedder) supportsDimensionsParam() bool {
return e.supportsDimensionOverride && e.dimensions > 0
}
// GetDimensions returns the vector dimensions
func (e *OpenAIEmbedder) GetDimensions() int {
return e.dimensions