feat: add support for dimensions parameter in Azure OpenAI and OpenAI embedding requests
This commit is contained in:
@@ -77,6 +77,9 @@ func (e *AzureOpenAIEmbedder) BatchEmbed(ctx context.Context, texts []string) ([
|
||||
Input: texts,
|
||||
EncodingFormat: "float",
|
||||
}
|
||||
if e.dimensions > 0 {
|
||||
reqBody.Dimensions = e.dimensions
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
@@ -157,4 +160,4 @@ func (e *AzureOpenAIEmbedder) doRequestWithRetry(ctx context.Context, jsonData [
|
||||
|
||||
func (e *AzureOpenAIEmbedder) GetModelName() string { return e.modelName }
|
||||
func (e *AzureOpenAIEmbedder) GetDimensions() int { return e.dimensions }
|
||||
func (e *AzureOpenAIEmbedder) GetModelID() string { return e.modelID }
|
||||
func (e *AzureOpenAIEmbedder) GetModelID() string { return e.modelID }
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package embedding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAzureOpenAIEmbedderBatchEmbedSendsConfiguredDimensions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var requestBody map[string]any
|
||||
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Fatalf("expected POST request, got %s", r.Method)
|
||||
}
|
||||
|
||||
if got, want := r.URL.String(),
|
||||
"https://example-resource.openai.azure.com/openai/deployments/test-deployment/embeddings?api-version=2024-10-21"; got != want {
|
||||
t.Fatalf("unexpected request path: got %s want %s", got, want)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
||||
t.Fatalf("decode request body: %v", err)
|
||||
}
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(bytes.NewBufferString(`{"data":[{"embedding":[0.1,0.2],"index":0}]}`)),
|
||||
}, nil
|
||||
})
|
||||
|
||||
embedder, err := NewAzureOpenAIEmbedder(
|
||||
"test-key",
|
||||
"https://example-resource.openai.azure.com",
|
||||
"test-deployment",
|
||||
511,
|
||||
256,
|
||||
"model-id",
|
||||
"2024-10-21",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create embedder: %v", err)
|
||||
}
|
||||
embedder.httpClient = &http.Client{Transport: transport}
|
||||
|
||||
if _, err := embedder.BatchEmbed(context.Background(), []string{"hello"}); err != nil {
|
||||
t.Fatalf("BatchEmbed returned error: %v", err)
|
||||
}
|
||||
|
||||
got, ok := requestBody["dimensions"]
|
||||
if !ok {
|
||||
t.Fatalf("expected request body to include dimensions, got %v", requestBody)
|
||||
}
|
||||
|
||||
if got != float64(256) {
|
||||
t.Fatalf("unexpected dimensions value: got %v want 256", got)
|
||||
}
|
||||
}
|
||||
|
||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
return f(r)
|
||||
}
|
||||
@@ -31,6 +31,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"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user