Repository navigation
Expand file tree
/
Copy pathindex_codec_test.go
More file actions
116 lines (106 loc) · 3.17 KB
/
Copy pathindex_codec_test.go
File metadata and controls
116 lines (106 loc) · 3.17 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
package search_test
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/kelindar/search"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCodecFailures(t *testing.T) {
for _, binary := range []bool{false, true} {
t.Run(fmt.Sprint(binary), func(t *testing.T) {
var encoded bytes.Buffer
strings := search.NewIndex[string]()
strings.Add([]float32{1, 0}, "cat")
blobs := search.NewIndex[[]byte]()
blobs.Add([]float32{1, 0}, []byte("cat"))
write := strings.WriteTo
read := strings.ReadFrom
if binary {
write, read = blobs.WriteTo, blobs.ReadFrom
}
n, err := write(&encoded)
require.NoError(t, err)
assert.Equal(t, int64(encoded.Len()), n)
for cutoff := 0; cutoff < encoded.Len(); cutoff++ {
_, err := read(bytes.NewReader(encoded.Bytes()[:cutoff]))
assert.Error(t, err, "truncated at byte %d", cutoff)
}
_, err = read(bytes.NewReader(encoded.Bytes()))
require.NoError(t, err)
failure := errors.New("write failed")
for cutoff := 0; cutoff < encoded.Len(); cutoff++ {
_, err := write(&limitedWriter{remaining: cutoff, err: failure})
assert.ErrorIs(t, err, failure, "writer failed at byte %d", cutoff)
}
})
}
index := search.NewIndex[string]()
_, err := index.ReadFrom(bytes.NewReader([]byte{2}))
assert.EqualError(t, err, "unsupported version: 2")
path := filepath.Join(t.TempDir(), "missing", "index.bin")
assert.Error(t, index.ReadFile(path))
assert.Error(t, index.WriteFile(path))
path = filepath.Join(t.TempDir(), "corrupt.bin")
require.NoError(t, os.WriteFile(path, []byte("invalid compressed index"), 0600))
assert.Error(t, index.ReadFile(path))
}
type limitedWriter struct {
remaining int
err error
}
func (w *limitedWriter) Write(p []byte) (int, error) {
if len(p) > w.remaining {
n := w.remaining
w.remaining = 0
return n, w.err
}
w.remaining -= len(p)
return len(p), nil
}
func TestCodecVectors(t *testing.T) {
input := search.NewIndex[int]()
input.Add([]float32{1, 0}, 42)
var encoded bytes.Buffer
_, err := input.WriteTo(&encoded)
require.NoError(t, err)
output := search.NewIndex[int]()
_, err = output.ReadFrom(&encoded)
require.NoError(t, err)
// Only string and byte slice values are persisted; other types retain vectors.
assert.Equal(t, []search.Result[int]{{Value: 0, Relevance: 1}}, output.Search([]float32{1, 0}, 1))
}
// A custom provider needs no imports from llama or openai.
type customProvider struct{}
func (customProvider) EmbedText(ctx context.Context, text string) ([]float32, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
// Replace this demonstration mapping with your embedding service.
if text == "cat" {
return []float32{1, 0}, nil
}
return []float32{0, 1}, nil
}
func ExampleIndex() {
provider := customProvider{}
index := search.NewIndex[string]()
for _, text := range []string{"cat", "dog"} {
vector, err := provider.EmbedText(context.Background(), text)
if err != nil {
panic(err)
}
index.Add(vector, text)
}
query, err := provider.EmbedText(context.Background(), "cat")
if err != nil {
panic(err)
}
fmt.Println(index.Search(query, 1)[0].Value)
// Output: cat
}