-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembeddings_test.go
More file actions
157 lines (139 loc) · 4.23 KB
/
Copy pathembeddings_test.go
File metadata and controls
157 lines (139 loc) · 4.23 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package blmstudio_test
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
blmstudio "github.com/bearaujus/blmstudio"
)
func TestEmbed_Happy(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, blmstudio.EmbeddingResponse{
Data: []blmstudio.EmbeddingObject{{
Embedding: []float64{0.1, 0.2, 0.3},
Index: 0,
}},
Model: "embed-model",
})
}))
defer srv.Close()
c := newTestClient(t, srv)
resp, err := c.Embed(context.Background(), &blmstudio.EmbeddingRequest{
Model: "embed-model",
Input: blmstudio.NewEmbeddingStringsInput([]string{"hello world"}),
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) == 0 {
t.Fatal("expected at least one embedding")
}
if len(resp.Data[0].Embedding) != 3 {
t.Errorf("got %d dims, want 3", len(resp.Data[0].Embedding))
}
}
func TestEmbed_Single(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, blmstudio.EmbeddingResponse{
Data: []blmstudio.EmbeddingObject{{Embedding: []float64{1.0}, Index: 0}},
})
}))
defer srv.Close()
c := newTestClient(t, srv)
resp, err := c.Embed(context.Background(), &blmstudio.EmbeddingRequest{
Model: "embed-model",
Input: blmstudio.NewEmbeddingStringInput("single"),
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) == 0 {
t.Fatal("expected embedding data")
}
}
func TestEmbed_Empty(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, blmstudio.EmbeddingResponse{})
}))
defer srv.Close()
c := newTestClient(t, srv)
// Zero-value EmbeddingInput has nil raw → triggers ErrEmptyText
_, err := c.Embed(context.Background(), &blmstudio.EmbeddingRequest{
Model: "embed-model",
})
if !errors.Is(err, blmstudio.ErrEmptyText) {
t.Errorf("got %v, want ErrEmptyText", err)
}
}
func TestEmbed_4xx(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 422, map[string]any{"error": "invalid input", "message": "bad text"})
}))
defer srv.Close()
c := newTestClient(t, srv)
_, err := c.Embed(context.Background(), &blmstudio.EmbeddingRequest{
Model: "embed-model",
Input: blmstudio.NewEmbeddingStringInput("text"),
})
if _, ok := blmstudio.IsAPIError(err); !ok {
t.Errorf("expected APIError, got %v", err)
}
}
func TestEmbed_MalformedJSON(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte("not-json"))
}))
defer srv.Close()
c := newTestClient(t, srv)
_, err := c.Embed(context.Background(), &blmstudio.EmbeddingRequest{
Model: "embed-model",
Input: blmstudio.NewEmbeddingStringInput("text"),
})
if err == nil {
t.Fatal("expected error for malformed JSON, got nil")
}
}
func TestEmbed_Timeout(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
writeJSON(w, 200, blmstudio.EmbeddingResponse{})
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
c := newTestClient(t, srv)
_, err := c.Embed(ctx, &blmstudio.EmbeddingRequest{
Model: "embed-model",
Input: blmstudio.NewEmbeddingStringInput("text"),
})
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("got %v, want DeadlineExceeded", err)
}
}
func BenchmarkEmbed(b *testing.B) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, blmstudio.EmbeddingResponse{
Data: []blmstudio.EmbeddingObject{{Embedding: make([]float64, 384), Index: 0}},
})
}))
defer srv.Close()
c := blmstudio.New(blmstudio.WithBaseURL(srv.URL))
input := &blmstudio.EmbeddingRequest{
Model: "bench-model",
Input: blmstudio.NewEmbeddingStringInput("benchmark text for embedding"),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = c.Embed(context.Background(), input)
}
}