fix: gate Azure OpenAI dimensions support

This commit is contained in:
liweixin
2026-04-08 11:43:01 +08:00
committed by lyingbug
parent bf514ff68a
commit 5233394d01
4 changed files with 166 additions and 6 deletions
+34 -2
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/logger"
@@ -26,6 +27,13 @@ type AzureOpenAIEmbedder struct {
EmbedderPooler
}
type azureOpenAIEmbedRequest struct {
Model string `json:"model"`
Input []string `json:"input"`
EncodingFormat string `json:"encoding_format,omitempty"`
Dimensions int `json:"dimensions,omitempty"`
}
// NewAzureOpenAIEmbedder creates a new Azure OpenAI embedder
func NewAzureOpenAIEmbedder(apiKey, baseURL, modelName string,
truncatePromptTokens int, dimensions int, modelID string,
@@ -72,12 +80,12 @@ func (e *AzureOpenAIEmbedder) Embed(ctx context.Context, text string) ([]float32
}
func (e *AzureOpenAIEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]float32, error) {
reqBody := OpenAIEmbedRequest{
reqBody := azureOpenAIEmbedRequest{
Model: e.modelName,
Input: texts,
EncodingFormat: "float",
}
if e.dimensions > 0 {
if e.supportsDimensionsParam() {
reqBody.Dimensions = e.dimensions
}
@@ -158,6 +166,30 @@ func (e *AzureOpenAIEmbedder) doRequestWithRetry(ctx context.Context, jsonData [
return nil, err
}
func (e *AzureOpenAIEmbedder) supportsDimensionsParam() bool {
if e.dimensions <= 0 {
return false
}
// Azure only supports the dimensions parameter on newer embeddings APIs.
if strings.TrimSpace(e.apiVersion) < "2024-10-21" {
return false
}
modelRef := strings.ToLower(strings.TrimSpace(e.modelID))
if modelRef == "" {
modelRef = strings.ToLower(strings.TrimSpace(e.modelName))
}
// Fixed-dimension legacy models reject the field entirely.
if strings.Contains(modelRef, "ada-002") {
return false
}
return strings.Contains(modelRef, "text-embedding-3-small") ||
strings.Contains(modelRef, "text-embedding-3-large")
}
func (e *AzureOpenAIEmbedder) GetModelName() string { return e.modelName }
func (e *AzureOpenAIEmbedder) GetDimensions() int { return e.dimensions }
func (e *AzureOpenAIEmbedder) GetModelID() string { return e.modelID }