Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix:dimensions not sent in embedding API requests (#1654)

MaoMengcf0a64b88e7b
diff --git a/internal/models/embedding/aliyun.go b/internal/models/embedding/aliyun.go
index 0505fd218..70c6bce6d 100644
--- a/internal/models/embedding/aliyun.go
+++ b/internal/models/embedding/aliyun.go
@@ -39,10 +39,16 @@ func (e *AliyunEmbedder) SetCustomHeaders(headers map[string]string) {
e.customHeaders = headers
}
+// AliyunEmbedParameters represents the parameters for Aliyun DashScope multimodal embedding
+type AliyunEmbedParameters struct {
+ Dimension int `json:"dimension,omitempty"`
+}
+
// AliyunEmbedRequest represents an Aliyun DashScope multimodal embedding request
type AliyunEmbedRequest struct {
- Model string `json:"model"`
- Input AliyunEmbedInput `json:"input"`
+ Model string `json:"model"`
+ Input AliyunEmbedInput `json:"input"`
+ Parameters *AliyunEmbedParameters `json:"parameters,omitempty"`
}
// AliyunEmbedInput represents the input structure for Aliyun embedding
@@ -188,6 +194,9 @@ func (e *AliyunEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]fl
Contents: contents,
},
}
+ if e.dimensions > 0 {
+ reqBody.Parameters = &AliyunEmbedParameters{Dimension: e.dimensions}
+ }
jsonData, err := json.Marshal(reqBody)
if err != nil {
diff --git a/internal/models/embedding/nvidia.go b/internal/models/embedding/nvidia.go
index f645da79d..f1566b8bf 100644
--- a/internal/models/embedding/nvidia.go
+++ b/internal/models/embedding/nvidia.go
@@ -38,6 +38,7 @@ type NvidiaEmbedRequest struct {
Model string `json:"model"`
Input []string `json:"input"`
EncodingFormat string `json:"encoding_format,omitempty"`
+ Dimensions int `json:"dimensions,omitempty"`
TruncatePromptTokens int `json:"truncate_prompt_tokens,omitempty"`
InputType string `json:"input_type,omitempty"`
}
@@ -144,6 +145,7 @@ func (e *NvidiaEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]fl
Model: e.modelName,
Input: texts,
EncodingFormat: "float",
+ Dimensions: e.dimensions,
InputType: "passage",
}
isQuery, _ := ctx.Value(types.EmbedQueryContextKey).(bool)
diff --git a/internal/models/embedding/ollama.go b/internal/models/embedding/ollama.go
index 93c98e097..e47df3e23 100644
--- a/internal/models/embedding/ollama.go
+++ b/internal/models/embedding/ollama.go
@@ -88,9 +88,10 @@ func (e *OllamaEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]fl
// Create request
req := &ollamaapi.EmbedRequest{
- Model: e.modelName,
- Input: texts,
- Options: make(map[string]interface{}),
+ Model: e.modelName,
+ Input: texts,
+ Dimensions: e.dimensions,
+ Options: make(map[string]interface{}),
}
// Set truncation parameters
diff --git a/internal/models/embedding/openai.go b/internal/models/embedding/openai.go
index 901a0a55a..71fdc3a0c 100644
--- a/internal/models/embedding/openai.go
+++ b/internal/models/embedding/openai.go
@@ -33,6 +33,7 @@ type OpenAIEmbedRequest struct {
Model string `json:"model"`
Input []string `json:"input"`
EncodingFormat string `json:"encoding_format,omitempty"`
+ Dimensions int `json:"dimensions,omitempty"`
TruncatePromptTokens int `json:"truncate_prompt_tokens,omitempty"`
}
@@ -161,6 +162,7 @@ func (e *OpenAIEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]fl
Model: e.modelName,
Input: texts,
EncodingFormat: "float",
+ Dimensions: e.dimensions,
TruncatePromptTokens: e.truncatePromptTokens,
}
diff --git a/internal/models/embedding/volcengine.go b/internal/models/embedding/volcengine.go
index c5a3e617d..b70783913 100644
--- a/internal/models/embedding/volcengine.go
+++ b/internal/models/embedding/volcengine.go
@@ -41,8 +41,9 @@ func (e *VolcengineEmbedder) SetCustomHeaders(headers map[string]string) {
// VolcengineEmbedRequest represents a Volcengine Ark multimodal embedding request
type VolcengineEmbedRequest struct {
- Model string `json:"model"`
- Input []VolcengineInputContent `json:"input"`
+ Model string `json:"model"`
+ Input []VolcengineInputContent `json:"input"`
+ Dimensions int `json:"dimensions,omitempty"`
}
// VolcengineInputContent represents a single input item for Volcengine
@@ -199,8 +200,9 @@ func (e *VolcengineEmbedder) BatchEmbed(ctx context.Context, texts []string) ([]
}
reqBody := VolcengineEmbedRequest{
- Model: e.modelName,
- Input: input,
+ Model: e.modelName,
+ Input: input,
+ Dimensions: e.dimensions,
}
jsonData, err := json.Marshal(reqBody)
diff --git a/internal/models/embedding/weknoracloud.go b/internal/models/embedding/weknoracloud.go
index b49bfcb25..8cdf72d8e 100644
--- a/internal/models/embedding/weknoracloud.go
+++ b/internal/models/embedding/weknoracloud.go
@@ -56,6 +56,7 @@ func NewWeKnoraCloudEmbedder(config Config) (*WeKnoraCloudEmbedder, error) {
type weKnoraCloudEmbedRequest struct {
Model string `json:"model"`
Input []string `json:"input"`
+ Dimensions int `json:"dimensions,omitempty"`
TruncatePromptTokens int `json:"truncate_prompt_tokens,omitempty"`
}
@@ -78,7 +79,7 @@ func (e *WeKnoraCloudEmbedder) Embed(ctx context.Context, text string) ([]float3
}
func (e *WeKnoraCloudEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]float32, error) {
- reqBody := weKnoraCloudEmbedRequest{Model: e.effectiveModelName(), Input: texts}
+ reqBody := weKnoraCloudEmbedRequest{Model: e.effectiveModelName(), Input: texts, Dimensions: e.dimensions}
bodyBytes, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("weknoracloud embedder: marshal: %w", err)
diff --git a/internal/models/embedding/zhipu.go b/internal/models/embedding/zhipu.go
index 941054c00..7bfee7b76 100644
--- a/internal/models/embedding/zhipu.go
+++ b/internal/models/embedding/zhipu.go
@@ -33,6 +33,7 @@ type ZhipuEmbedder struct {
type ZhipuEmbedRequest struct {
Model string `json:"model"`
Input []string `json:"input"`
+ Dimensions int `json:"dimensions,omitempty"`
TruncatePromptTokens int `json:"truncate_prompt_tokens,omitempty"`
}
@@ -148,6 +149,7 @@ func (e *ZhipuEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]flo
reqBody := ZhipuEmbedRequest{
Model: e.modelName,
Input: texts,
+ Dimensions: e.dimensions,
TruncatePromptTokens: e.truncatePromptTokens,
}