6b60d292f8
Align embedding outbound requests with chat module defenses by validating base URLs at construction time and using SSRFSafeDialContext with redirect checks.
192 lines
5.2 KiB
Go
192 lines
5.2 KiB
Go
package embedding
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
secutils "github.com/Tencent/WeKnora/internal/utils"
|
|
)
|
|
|
|
// AzureOpenAIEmbedder implements text vectorization using Azure OpenAI API
|
|
type AzureOpenAIEmbedder struct {
|
|
apiKey string
|
|
baseURL string
|
|
modelName string
|
|
truncatePromptTokens int
|
|
dimensions int
|
|
modelID string
|
|
apiVersion string
|
|
httpClient *http.Client
|
|
maxRetries int
|
|
customHeaders map[string]string
|
|
supportsDimensionOverride bool
|
|
EmbedderPooler
|
|
}
|
|
|
|
// SetCustomHeaders 设置用户自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)。
|
|
func (e *AzureOpenAIEmbedder) SetCustomHeaders(headers map[string]string) {
|
|
e.customHeaders = headers
|
|
}
|
|
|
|
func (e *AzureOpenAIEmbedder) SetSupportsDimensionOverride(supported bool) {
|
|
e.supportsDimensionOverride = supported
|
|
}
|
|
|
|
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,
|
|
apiVersion string, pooler EmbedderPooler,
|
|
) (*AzureOpenAIEmbedder, error) {
|
|
if baseURL == "" {
|
|
return nil, fmt.Errorf("Azure resource endpoint (base URL) is required")
|
|
}
|
|
if modelName == "" {
|
|
return nil, fmt.Errorf("deployment name (model name) is required")
|
|
}
|
|
if apiVersion == "" {
|
|
apiVersion = "2024-10-21"
|
|
}
|
|
if truncatePromptTokens == 0 {
|
|
truncatePromptTokens = 511
|
|
}
|
|
|
|
if err := validateEmbeddingBaseURL(baseURL); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &AzureOpenAIEmbedder{
|
|
apiKey: apiKey,
|
|
baseURL: baseURL,
|
|
modelName: modelName,
|
|
truncatePromptTokens: truncatePromptTokens,
|
|
dimensions: dimensions,
|
|
modelID: modelID,
|
|
apiVersion: apiVersion,
|
|
httpClient: newEmbeddingHTTPClient(60 * time.Second),
|
|
maxRetries: 3,
|
|
EmbedderPooler: pooler,
|
|
}, nil
|
|
}
|
|
|
|
func (e *AzureOpenAIEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {
|
|
for range 3 {
|
|
embeddings, err := e.BatchEmbed(ctx, []string{text})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(embeddings) > 0 {
|
|
return embeddings[0], nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("no embedding returned")
|
|
}
|
|
|
|
func (e *AzureOpenAIEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]float32, error) {
|
|
reqBody := azureOpenAIEmbedRequest{
|
|
Model: e.modelName,
|
|
Input: texts,
|
|
EncodingFormat: "float",
|
|
}
|
|
if e.supportsDimensionsParam() {
|
|
reqBody.Dimensions = e.dimensions
|
|
}
|
|
|
|
jsonData, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal request: %w", err)
|
|
}
|
|
|
|
logger.GetLogger(ctx).Debugf("AzureOpenAIEmbedder BatchEmbed: model=%s, input_count=%d",
|
|
e.modelName, len(texts))
|
|
|
|
resp, err := e.doRequestWithRetry(ctx, jsonData)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("send request: %w", err)
|
|
}
|
|
if resp.Body != nil {
|
|
defer resp.Body.Close()
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read response: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
bodyStr := string(body)
|
|
if len(bodyStr) > 1000 {
|
|
bodyStr = bodyStr[:1000] + "... (truncated)"
|
|
}
|
|
return nil, fmt.Errorf("Azure Embedding API error: Http Status %s, Response: %s", resp.Status, bodyStr)
|
|
}
|
|
|
|
var response OpenAIEmbedResponse
|
|
if err := json.Unmarshal(body, &response); err != nil {
|
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
|
}
|
|
|
|
embeddings := make([][]float32, 0, len(response.Data))
|
|
for _, data := range response.Data {
|
|
embeddings = append(embeddings, data.Embedding)
|
|
}
|
|
return embeddings, nil
|
|
}
|
|
|
|
func (e *AzureOpenAIEmbedder) doRequestWithRetry(ctx context.Context, jsonData []byte) (*http.Response, error) {
|
|
url := fmt.Sprintf("%s/openai/deployments/%s/embeddings?api-version=%s",
|
|
e.baseURL, e.modelName, e.apiVersion)
|
|
|
|
var resp *http.Response
|
|
var err error
|
|
|
|
for i := 0; i <= e.maxRetries; i++ {
|
|
if i > 0 {
|
|
backoffTime := time.Duration(1<<uint(i-1)) * time.Second
|
|
if backoffTime > 10*time.Second {
|
|
backoffTime = 10 * time.Second
|
|
}
|
|
select {
|
|
case <-time.After(backoffTime):
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
}
|
|
|
|
req, reqErr := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
|
if reqErr != nil {
|
|
err = reqErr
|
|
continue
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("api-key", e.apiKey)
|
|
secutils.ApplyCustomHeaders(req, e.customHeaders)
|
|
|
|
resp, err = e.httpClient.Do(req)
|
|
if err == nil {
|
|
return resp, nil
|
|
}
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func (e *AzureOpenAIEmbedder) supportsDimensionsParam() bool {
|
|
return e.supportsDimensionOverride && e.dimensions > 0
|
|
}
|
|
|
|
func (e *AzureOpenAIEmbedder) GetModelName() string { return e.modelName }
|
|
func (e *AzureOpenAIEmbedder) GetDimensions() int { return e.dimensions }
|
|
func (e *AzureOpenAIEmbedder) GetModelID() string { return e.modelID }
|