forked from philippgille/chromem-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed_openai_test.go
86 lines (78 loc) · 2.11 KB
/
embed_openai_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package chromem_test
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
"github.com/philippgille/chromem-go"
)
type openAIResponse struct {
Data []struct {
Embedding []float32 `json:"embedding"`
} `json:"data"`
}
func TestNewEmbeddingFuncOpenAICompat(t *testing.T) {
apiKey := "secret"
model := "model-small"
baseURLSuffix := "/v1"
input := "hello world"
wantBody, err := json.Marshal(map[string]string{
"input": input,
"model": model,
})
if err != nil {
t.Fatal("unexpected error:", err)
}
wantRes := []float32{-0.40824828, 0.40824828, 0.81649655} // normalized version of `{-0.1, 0.1, 0.2}`
// Mock server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check URL
if !strings.HasSuffix(r.URL.Path, baseURLSuffix+"/embeddings") {
t.Fatal("expected URL", baseURLSuffix+"/embeddings", "got", r.URL.Path)
}
// Check method
if r.Method != "POST" {
t.Fatal("expected method POST, got", r.Method)
}
// Check headers
if r.Header.Get("Authorization") != "Bearer "+apiKey {
t.Fatal("expected Authorization header", "Bearer "+apiKey, "got", r.Header.Get("Authorization"))
}
if r.Header.Get("Content-Type") != "application/json" {
t.Fatal("expected Content-Type header", "application/json", "got", r.Header.Get("Content-Type"))
}
// Check body
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal("unexpected error:", err)
}
if !bytes.Equal(body, wantBody) {
t.Fatal("expected body", wantBody, "got", body)
}
// Write response
resp := openAIResponse{
Data: []struct {
Embedding []float32 `json:"embedding"`
}{
{Embedding: wantRes},
},
}
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(resp)
}))
defer ts.Close()
baseURL := ts.URL + baseURLSuffix
f := chromem.NewEmbeddingFuncOpenAICompat(chromem.NewOpenAICompatConfig(baseURL, apiKey, model))
res, err := f(context.Background(), input)
if err != nil {
t.Fatal("expected nil, got", err)
}
if slices.Compare(wantRes, res) != 0 {
t.Fatal("expected res", wantRes, "got", res)
}
}