|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "sync/atomic" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +type embeddingRequest struct { |
| 16 | + Input string `json:"input"` |
| 17 | + Model string `json:"model"` |
| 18 | + Dimensions int `json:"dimensions"` |
| 19 | +} |
| 20 | + |
| 21 | +func newEmbeddingServer(t *testing.T, embedding []float64) (*httptest.Server, *atomic.Int32) { |
| 22 | + t.Helper() |
| 23 | + |
| 24 | + var hitCount atomic.Int32 |
| 25 | + |
| 26 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 27 | + hitCount.Add(1) |
| 28 | + assert.Equal(t, "/v1/embeddings", r.URL.Path) |
| 29 | + assert.Equal(t, "Bearer sk-test", r.Header.Get("Authorization")) |
| 30 | + |
| 31 | + var req embeddingRequest |
| 32 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 33 | + t.Errorf("decode request body: %v", err) |
| 34 | + http.Error(w, "invalid request", http.StatusBadRequest) |
| 35 | + |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + assert.Equal(t, "hello world", req.Input) |
| 40 | + assert.Equal(t, "test-model", req.Model) |
| 41 | + assert.Equal(t, 2, req.Dimensions) |
| 42 | + |
| 43 | + w.Header().Set("Content-Type", "application/json") |
| 44 | + |
| 45 | + if err := json.NewEncoder(w).Encode(map[string]any{ |
| 46 | + "object": "list", |
| 47 | + "model": req.Model, |
| 48 | + "data": []map[string]any{ |
| 49 | + { |
| 50 | + "object": "embedding", |
| 51 | + "index": 0, |
| 52 | + "embedding": embedding, |
| 53 | + }, |
| 54 | + }, |
| 55 | + "usage": map[string]any{ |
| 56 | + "prompt_tokens": 1, |
| 57 | + "total_tokens": 1, |
| 58 | + }, |
| 59 | + }); err != nil { |
| 60 | + t.Errorf("encode response body: %v", err) |
| 61 | + } |
| 62 | + })) |
| 63 | + |
| 64 | + t.Cleanup(server.Close) |
| 65 | + |
| 66 | + return server, &hitCount |
| 67 | +} |
| 68 | + |
| 69 | +func TestCreateEmbedding_UsesExplicitBaseURLOverEnvironment(t *testing.T) { |
| 70 | + envServer, envHits := newEmbeddingServer(t, []float64{9, 9}) |
| 71 | + explicitServer, explicitHits := newEmbeddingServer(t, []float64{1, 2}) |
| 72 | + |
| 73 | + t.Setenv("OPENAI_BASE_URL", envServer.URL+"/v1") |
| 74 | + |
| 75 | + client := NewClient("sk-test", |
| 76 | + WithBaseURL(explicitServer.URL+"/v1"), |
| 77 | + WithDimensions(2), |
| 78 | + WithModel("test-model"), |
| 79 | + ) |
| 80 | + |
| 81 | + embedding, err := client.CreateEmbedding(context.Background(), "hello world") |
| 82 | + require.NoError(t, err) |
| 83 | + assert.Equal(t, []float32{1, 2}, embedding) |
| 84 | + assert.Equal(t, int32(0), envHits.Load()) |
| 85 | + assert.Equal(t, int32(1), explicitHits.Load()) |
| 86 | +} |
| 87 | + |
| 88 | +func TestCreateEmbedding_UsesEnvironmentBaseURLWhenExplicitBaseURLIsUnset(t *testing.T) { |
| 89 | + envServer, envHits := newEmbeddingServer(t, []float64{3, 4}) |
| 90 | + |
| 91 | + t.Setenv("OPENAI_BASE_URL", envServer.URL+"/v1") |
| 92 | + |
| 93 | + client := NewClient("sk-test", |
| 94 | + WithDimensions(2), |
| 95 | + WithModel("test-model"), |
| 96 | + ) |
| 97 | + |
| 98 | + embedding, err := client.CreateEmbedding(context.Background(), "hello world") |
| 99 | + require.NoError(t, err) |
| 100 | + assert.Equal(t, []float32{3, 4}, embedding) |
| 101 | + assert.Equal(t, int32(1), envHits.Load()) |
| 102 | +} |
0 commit comments