Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] Reuse the same file in localvec when indexing #286

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go/plugins/localvec/localvec.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func (r *retriever) Index(ctx context.Context, req *ai.IndexerRequest) error {
}

// Update the file every time we add documents.
// We use a temporary file to avoid losing the original
// file, in case of a crash.
tmpname := r.filename + ".tmp"
f, err := os.Create(tmpname)
if err != nil {
Expand All @@ -146,6 +148,9 @@ func (r *retriever) Index(ctx context.Context, req *ai.IndexerRequest) error {
if err := f.Close(); err != nil {
return err
}
if err := os.Rename(tmpname, r.filename); err != nil {
return err
}

return nil
}
Expand Down
87 changes: 87 additions & 0 deletions go/plugins/localvec/localvec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,93 @@ func TestLocalVec(t *testing.T) {
}
}

func TestPersistentIndexing(t *testing.T) {
ctx := context.Background()

const dim = 32
v1 := make([]float32, dim)
v2 := make([]float32, dim)
v3 := make([]float32, dim)
for i := range v1 {
v1[i] = float32(i)
v2[i] = float32(i)
v3[i] = float32(i)
}

d1 := ai.DocumentFromText("hello1", nil)
d2 := ai.DocumentFromText("hello2", nil)
d3 := ai.DocumentFromText("goodbye", nil)

embedder := fakeembedder.New()
embedder.Register(d1, v1)
embedder.Register(d2, v2)
embedder.Register(d3, v3)

tDir := t.TempDir()

r, err := newRetriever(ctx, tDir, "testLocalVec", embedder, nil)
if err != nil {
t.Fatal(err)
}

indexerReq := &ai.IndexerRequest{
Documents: []*ai.Document{d1, d2},
}
err = r.Index(ctx, indexerReq)
if err != nil {
t.Fatalf("Index operation failed: %v", err)
}

retrieverOptions := &RetrieverOptions{
K: 100, // fetch all docs
}

retrieverReq := &ai.RetrieverRequest{
Document: d1,
Options: retrieverOptions,
}
retrieverResp, err := r.Retrieve(ctx, retrieverReq)
if err != nil {
t.Fatalf("Retrieve operation failed: %v", err)
}

docs := retrieverResp.Documents
if len(docs) != 2 {
t.Errorf("got %d results, expected 2", len(docs))
}

rAnother, err := newRetriever(ctx, tDir, "testLocalVec", embedder, nil)
if err != nil {
t.Fatal(err)
}

indexerReq = &ai.IndexerRequest{
Documents: []*ai.Document{d3},
}
err = rAnother.Index(ctx, indexerReq)
if err != nil {
t.Fatalf("Index operation failed: %v", err)
}

retrieverOptions = &RetrieverOptions{
K: 100, // fetch all docs
}

retrieverReq = &ai.RetrieverRequest{
Document: d1,
Options: retrieverOptions,
}
retrieverResp, err = rAnother.Retrieve(ctx, retrieverReq)
if err != nil {
t.Fatalf("Retrieve operation failed: %v", err)
}

docs = retrieverResp.Documents
if len(docs) != 3 {
t.Errorf("got %d results, expected 3", len(docs))
}
}

func TestSimilarity(t *testing.T) {
x := []float32{5, 23, 2, 5, 9}
y := []float32{3, 21, 2, 5, 14}
Expand Down
Loading