-
Notifications
You must be signed in to change notification settings - Fork 1
/
embedding_test.go
93 lines (84 loc) · 2.54 KB
/
embedding_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
package vector
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestEmbedding(t *testing.T) {
for _, ns := range namespaces {
t.Run("namespace_"+ns, func(t *testing.T) {
client, err := newEmbeddingTestClient()
require.NoError(t, err)
namespace := client.Namespace(ns)
id0 := "tr"
id1 := "jp"
id2 := "uk"
id3 := "fr"
err = namespace.UpsertDataMany([]UpsertData{
{
Id: id0,
Data: "Capital of Türkiye is Ankara.",
Metadata: map[string]any{"country": id0, "capital": "Ankara"},
},
{
Id: id1,
Data: "Capital of Japan is Tokyo.",
Metadata: map[string]any{"country": id1, "capital": "Tokyo"},
},
{
Id: id2,
Data: "Capital of England is London.",
Metadata: map[string]any{"country": id2, "capital": "London"},
},
{
Id: id3,
Data: "Capital of France is Paris.",
Metadata: map[string]any{"country": id3, "capital": "Paris"},
},
})
require.NoError(t, err)
require.Eventually(t, func() bool {
info, err := client.Info()
require.NoError(t, err)
return info.PendingVectorCount == 0
}, 10*time.Second, 1*time.Second)
t.Run("score", func(t *testing.T) {
scores, err := namespace.QueryData(QueryData{
Data: "where is the capital of Japan?",
TopK: 1,
})
require.NoError(t, err)
require.Equal(t, 1, len(scores))
require.Equal(t, id1, scores[0].Id)
})
t.Run("with metadata", func(t *testing.T) {
scores, err := namespace.QueryData(QueryData{
Data: "Which country's capital is Ankara?",
TopK: 1,
IncludeMetadata: true,
IncludeData: true,
})
require.NoError(t, err)
require.Equal(t, 1, len(scores))
require.Equal(t, id0, scores[0].Id)
require.Equal(t, map[string]any{"country": "tr", "capital": "Ankara"}, scores[0].Metadata)
require.Equal(t, "Capital of Türkiye is Ankara.", scores[0].Data)
})
t.Run("with metadata filtering", func(t *testing.T) {
query := QueryData{
Data: "Where is the capital of France?",
TopK: 1,
IncludeMetadata: true,
IncludeData: true,
Filter: `country = 'fr'`,
}
scores, err := namespace.QueryData(query)
require.NoError(t, err)
require.Equal(t, 1, len(scores))
require.Equal(t, id3, scores[0].Id)
require.Equal(t, map[string]any{"country": "fr", "capital": "Paris"}, scores[0].Metadata)
require.Equal(t, "Capital of France is Paris.", scores[0].Data)
})
})
}
}