-
Notifications
You must be signed in to change notification settings - Fork 3
/
text_embeddings_test.go
189 lines (184 loc) · 3.82 KB
/
text_embeddings_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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package vertexai
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
type httpMockPredictTextEmbedding struct{}
func (h *httpMockPredictTextEmbedding) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader([]byte(`{
"predictions": [
{
"embeddings": {
"statistics": {
"truncated": true,
"token_count": 6
},
"values": [
0.014090980403125286,
-0.016960423439741135,
-0.033782251179218292,
0.0079174842685461044
]
}
}
]
}`))),
}, nil
}
func Test_client_TextEmbedding(t *testing.T) {
type fields struct {
clientConfig clientConfig
requestBuilder requestBuilder
httpClient httpClient
}
type args struct {
ctx context.Context
req TextEmbeddingRequest
}
tests := []struct {
name string
fields fields
args args
want *TextEmbeddingResponse
wantErr bool
}{
{
name: "error both content and inputs passed",
fields: fields{
clientConfig: clientConfig{
authToken: "test_token",
},
},
args: args{
ctx: context.Background(),
req: TextEmbeddingRequest{
Content: "sample content",
Inputs: []string{
"sample content",
"sample content",
},
},
},
wantErr: true,
},
{
name: "error building request",
fields: fields{
clientConfig: clientConfig{
authToken: "test_token",
},
requestBuilder: &requestBuilderImpl1{},
},
args: args{
ctx: context.Background(),
req: TextEmbeddingRequest{},
},
wantErr: true,
},
{
name: "error making http call",
fields: fields{
clientConfig: clientConfig{
authToken: "test_token",
},
requestBuilder: &requestBuilderImpl2{},
httpClient: &httpMock2{},
},
args: args{
ctx: context.Background(),
req: TextEmbeddingRequest{},
},
wantErr: true,
},
{
name: "success - Content",
fields: fields{
clientConfig: clientConfig{
authToken: "test_token",
},
requestBuilder: &requestBuilderImpl2{},
httpClient: &httpMockPredictTextEmbedding{},
},
args: args{
ctx: context.Background(),
req: TextEmbeddingRequest{
Content: "sample content",
},
},
want: &TextEmbeddingResponse{
Predictions: []Predictions{
Predictions{
Embedding: Embedding{
Values: []float64{
0.014090980403125286,
-0.016960423439741135,
-0.033782251179218292,
0.0079174842685461044,
},
Statistics: EmbeddingStats{
TokenCount: 6,
Truncated: true,
},
},
},
},
},
},
{
name: "success - Inputs",
fields: fields{
clientConfig: clientConfig{
authToken: "test_token",
},
requestBuilder: &requestBuilderImpl2{},
httpClient: &httpMockPredictTextEmbedding{},
},
args: args{
ctx: context.Background(),
req: TextEmbeddingRequest{
Inputs: []string{
"sample content",
"sample content",
},
},
},
want: &TextEmbeddingResponse{
Predictions: []Predictions{
Predictions{
Embedding: Embedding{
Values: []float64{
0.014090980403125286,
-0.016960423439741135,
-0.033782251179218292,
0.0079174842685461044,
},
Statistics: EmbeddingStats{
TokenCount: 6,
Truncated: true,
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &client{
clientConfig: tt.fields.clientConfig,
requestBuilder: tt.fields.requestBuilder,
httpClient: tt.fields.httpClient,
tokenizer: &tokenMockSuccess{},
}
got, err := c.TextEmbedding(tt.args.ctx, tt.args.req)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.wantErr, err != nil)
})
}
}