forked from philippgille/chromem-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
231 lines (214 loc) · 5.77 KB
/
query_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package chromem
import (
"context"
"reflect"
"slices"
"testing"
)
func TestFilterDocs(t *testing.T) {
docs := map[string]*Document{
"1": {
ID: "1",
Metadata: map[string]string{
"language": "en",
},
Embedding: []float32{0.1, 0.2, 0.3},
Content: "hello world",
},
"2": {
ID: "2",
Metadata: map[string]string{
"language": "de",
},
Embedding: []float32{0.2, 0.3, 0.4},
Content: "hallo welt",
},
"3": {
ID: "3",
Content: "bonjour and hello foo baz bom",
},
"4": {
ID: "4",
Content: "bonjour and hello foo bar baz",
},
"5": {
ID: "5",
Content: "bonjour and hello spam eggs",
},
}
tt := []struct {
name string
where map[string]string
whereDocument []WhereDocument
want []*Document
}{
{
name: "meta match",
where: map[string]string{"language": "de"},
whereDocument: nil,
want: []*Document{docs["2"]},
},
{
name: "meta no match",
where: map[string]string{"language": "fr"},
whereDocument: nil,
want: nil,
},
{
name: "content contains all",
where: nil,
whereDocument: []WhereDocument{{Operator: WhereDocumentOperatorContains, Value: "llo"}},
want: []*Document{docs["1"], docs["2"], docs["3"], docs["4"], docs["5"]},
},
{
name: "content contains one",
where: nil,
whereDocument: []WhereDocument{{Operator: WhereDocumentOperatorContains, Value: "hallo"}},
want: []*Document{docs["2"]},
},
{
name: "content contains none",
where: nil,
whereDocument: []WhereDocument{{Operator: WhereDocumentOperatorContains, Value: "salute"}},
want: nil,
},
{
name: "content not_contains all",
where: nil,
whereDocument: []WhereDocument{{Operator: WhereDocumentOperatorNotContains, Value: "bonjour"}},
want: []*Document{docs["1"], docs["2"]},
},
{
name: "content not_contains one",
where: nil,
whereDocument: []WhereDocument{{Operator: WhereDocumentOperatorNotContains, Value: "hello"}},
want: []*Document{docs["2"]},
},
{
name: "meta and content match",
where: map[string]string{"language": "de"},
whereDocument: []WhereDocument{{Operator: WhereDocumentOperatorContains, Value: "hallo"}},
want: []*Document{docs["2"]},
},
{
name: "meta + contains + not_contains",
where: map[string]string{"language": "de"},
whereDocument: []WhereDocument{{Operator: WhereDocumentOperatorContains, Value: "hallo"}, {Operator: WhereDocumentOperatorNotContains, Value: "bonjour"}},
want: []*Document{docs["2"]},
},
{
name: "contains or (contains and not_contains",
whereDocument: []WhereDocument{
{Operator: WhereDocumentOperatorOr, WhereDocuments: []WhereDocument{
{Operator: WhereDocumentOperatorContains, Value: "bar"},
{Operator: WhereDocumentOperatorAnd, WhereDocuments: []WhereDocument{
{Operator: WhereDocumentOperatorContains, Value: "bonjour"},
{Operator: WhereDocumentOperatorNotContains, Value: "foo"},
},
},
}},
},
want: []*Document{docs["4"], docs["5"]},
},
}
// To avoid issues with checking equality of concurrently produced slices, we sort by ID
sortDocs := func(d []*Document) {
slices.SortFunc(d, func(d1, d2 *Document) int {
if d1.ID < d2.ID {
return -1
}
if d1.ID > d2.ID {
return 1
}
return 0
})
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := filterDocs(docs, tc.where, tc.whereDocument)
sortDocs(got)
sortDocs(tc.want)
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("got %v; want %v", got, tc.want)
}
})
}
}
func TestNegative(t *testing.T) {
ctx := context.Background()
db := NewDB()
c, err := db.CreateCollection("test", nil, nil)
if err != nil {
panic(err)
}
if err := c.AddDocuments(ctx, []Document{
{
ID: "1",
Embedding: testEmbeddings["search_document: Village Builder Game"],
},
{
ID: "2",
Embedding: testEmbeddings["search_document: Town Craft Idle Game"],
},
{
ID: "3",
Embedding: testEmbeddings["search_document: Some Idle Game"],
},
}, 1); err != nil {
t.Fatalf("failed to add documents: %v", err)
}
t.Run("NEGATIVE_MODE_SUBTRACT", func(t *testing.T) {
res, err := c.QueryWithOptions(ctx, QueryOptions{
QueryEmbedding: testEmbeddings["search_query: town"],
NResults: c.Count(),
Negative: NegativeQueryOptions{
Embedding: testEmbeddings["search_query: idle"],
Mode: NEGATIVE_MODE_SUBTRACT,
},
})
if err != nil {
panic(err)
}
for _, r := range res {
t.Logf("%s: %v", r.ID, r.Similarity)
}
if len(res) != 3 {
t.Fatalf("expected 3 results, got %d", len(res))
}
// Village Builder Game
if res[0].ID != "1" {
t.Fatalf("expected document with ID 1, got %s", res[0].ID)
}
// Town Craft Idle Game
if res[1].ID != "2" {
t.Fatalf("expected document with ID 2, got %s", res[1].ID)
}
// Some Idle Game
if res[2].ID != "3" {
t.Fatalf("expected document with ID 3, got %s", res[2].ID)
}
})
t.Run("NEGATIVE_MODE_FILTER", func(t *testing.T) {
res, err := c.QueryWithOptions(ctx, QueryOptions{
QueryEmbedding: testEmbeddings["search_query: town"],
NResults: c.Count(),
Negative: NegativeQueryOptions{
Embedding: testEmbeddings["search_query: idle"],
Mode: NEGATIVE_MODE_FILTER,
},
})
if err != nil {
panic(err)
}
for _, r := range res {
t.Logf("%s: %v", r.ID, r.Similarity)
}
if len(res) != 1 {
t.Fatalf("expected 1 result, got %d", len(res))
}
// Village Builder Game
if res[0].ID != "1" {
t.Fatalf("expected document with ID 1, got %s", res[0].ID)
}
})
}