Skip to content

Commit 700e66a

Browse files
committed
vectorstores: add doc and test for Replacement opt
- add doc for `WithReplacement` opt - add test in pgvector_test
1 parent 8ffd9dd commit 700e66a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

vectorstores/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func WithDeduplicater(fn func(ctx context.Context, doc schema.Document) bool) Op
6161
}
6262
}
6363

64+
// WithReplacement returns an Option for setting the replacement flag that could be used
65+
// when adding documents. If set to true, the existing document with the **Metadata** will
66+
// be replaced with the new document. If set to false(default case), the new document will
67+
// be added to the store and the existing document will be kept.
6468
func WithReplacement(replacement bool) Option {
6569
return func(o *Options) {
6670
o.Replacement = replacement

vectorstores/pgvector/pgvector_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,66 @@ func TestDeduplicater(t *testing.T) {
615615
require.Equal(t, "vegetable", docs[0].Metadata["type"])
616616
}
617617

618+
func TestReplacement(t *testing.T) {
619+
t.Parallel()
620+
pgvectorURL := preCheckEnvSetting(t)
621+
ctx := context.Background()
622+
623+
llm, err := openai.New()
624+
require.NoError(t, err)
625+
e, err := embeddings.NewEmbedder(llm)
626+
require.NoError(t, err)
627+
628+
conn, err := pgx.Connect(ctx, pgvectorURL)
629+
require.NoError(t, err)
630+
631+
store, err := pgvector.New(
632+
ctx,
633+
pgvector.WithConn(conn),
634+
pgvector.WithEmbedder(e),
635+
)
636+
require.NoError(t, err)
637+
638+
defer cleanupTestArtifacts(ctx, t, store, pgvectorURL)
639+
640+
_, err = store.AddDocuments(context.Background(), []schema.Document{
641+
{PageContent: "Math", Metadata: map[string]any{
642+
"courseID": "foo",
643+
}},
644+
{PageContent: "Physics", Metadata: map[string]any{
645+
"courseID": "bar",
646+
}},
647+
})
648+
require.NoError(t, err)
649+
650+
_, err = store.AddDocuments(context.Background(), []schema.Document{
651+
{PageContent: "Discrete Math", Metadata: map[string]any{
652+
"courseID": "foo",
653+
}},
654+
}, vectorstores.WithReplacement(true))
655+
require.NoError(t, err)
656+
657+
docs, err := store.Search(ctx, 2, vectorstores.WithFilters(map[string]any{
658+
"courseID": "foo",
659+
}))
660+
require.NoError(t, err)
661+
require.Len(t, docs, 1)
662+
require.Equal(t, "Discrete Math", docs[0].PageContent)
663+
664+
_, err = store.AddDocuments(context.Background(), []schema.Document{
665+
{PageContent: "Calculus", Metadata: map[string]any{
666+
"courseID": "foo",
667+
}},
668+
}, vectorstores.WithReplacement(false))
669+
require.NoError(t, err)
670+
671+
docs, err = store.Search(ctx, 2, vectorstores.WithFilters(map[string]any{
672+
"courseID": "foo",
673+
}))
674+
require.NoError(t, err)
675+
require.Len(t, docs, 2)
676+
}
677+
618678
func TestWithAllOptions(t *testing.T) {
619679
t.Parallel()
620680
pgvectorURL := preCheckEnvSetting(t)

0 commit comments

Comments
 (0)