WeKnora/internal/models/embedding/azure_openai.go

234 lines
7.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package embedding
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/api"
"github.com/Tencent/WeKnora/internal/models/catalog"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/Tencent/WeKnora/internal/types"
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 is the stored extra_config.api_version, empty for the v1
// GA data plane. It only selects requestURL; see azureEmbeddingURL.
apiVersion string
requestURL 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 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,
requestURL: azureEmbeddingURL(baseURL, modelName, apiVersion),
httpClient: newEmbeddingHTTPClient(60 * time.Second),
maxRetries: 3,
EmbedderPooler: pooler,
}, nil
}
// azureEmbeddingURL resolves the embeddings URL through the catalog's Azure
// vendor, so one stored row lands on the same data plane for embedding as it
// does for chat and VLM (which both go through catalog.Vendor.Endpoint via
// internal/models/chat): an empty api_version means the v1 GA path
// {base}/openai/v1/embeddings with the deployment name in the body's `model`
// field, and a non-empty one means the dated
// {base}/openai/deployments/{deployment}/embeddings?api-version=... path.
// See the package comment of internal/models/vendors/azure_openai for the
// documentation behind that rule.
//
// The literal fallback below only runs if the vendor package was not linked
// in; embedder.go blank-imports internal/models/vendors so it normally is.
func azureEmbeddingURL(baseURL, deployment, apiVersion string) string {
root := strings.TrimRight(strings.TrimSpace(baseURL), "/")
extra := map[string]string{}
version := strings.TrimSpace(apiVersion)
if version != "" {
extra[catalog.ExtraAPIVersion] = version
}
if vendor, ok := catalog.Get(string(provider.ProviderAzureOpenAI)); ok && vendor.Endpoint != nil {
target, query := vendor.Endpoint(catalog.EndpointRequest{
BaseURL: root,
Model: deployment,
ModelType: types.ModelTypeEmbedding,
API: api.APIOpenAICompletions,
Extra: extra,
})
if target != "" {
return api.Endpoint{URL: target, Query: query}.Resolve("")
}
}
if version != "" {
return root + "/openai/deployments/" + url.PathEscape(deployment) +
"/embeddings?api-version=" + version
}
return root + "/openai/v1/embeddings"
}
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) {
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", e.requestURL, 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 }