Skip to content

Commit a149d66

Browse files
committed
vectorstores: add pgvector
Signed-off-by: Abirdcfly <[email protected]>
1 parent 00f364f commit a149d66

File tree

9 files changed

+845
-3
lines changed

9 files changed

+845
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ require (
6161
github.com/mitchellh/mapstructure v1.5.0 // indirect
6262
github.com/mitchellh/reflectwalk v1.0.0 // indirect
6363
github.com/oklog/ulid v1.3.1 // indirect
64+
github.com/pgvector/pgvector-go v0.1.1 // indirect
6465
github.com/pkg/errors v0.9.1 // indirect
6566
github.com/pmezard/go-difflib v1.0.0 // indirect
6667
github.com/rogpeppe/go-internal v1.11.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
420420
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
421421
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
422422
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
423+
github.com/pgvector/pgvector-go v0.1.1 h1:kqJigGctFnlWvskUiYIvJRNwUtQl/aMSUZVs0YWQe+g=
424+
github.com/pgvector/pgvector-go v0.1.1/go.mod h1:wLJgD/ODkdtd2LJK4l6evHXTuG+8PxymYAVomKHOWac=
423425
github.com/pinecone-io/go-pinecone v0.3.0 h1:+t0CiYaaA+JN6YM9QRNlvfLEr2kkGzcVEj/xNmSAON4=
424426
github.com/pinecone-io/go-pinecone v0.3.0/go.mod h1:VdSieE1r4jT3XydjFi+iL5w9qsGRz/x8LxWach2Hnv8=
425427
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=

vectorstores/chroma/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// Package chroma contains an implementation of the vectorStore interface that connects to an external Chroma database.
1+
// Package chroma contains an implementation of the VectorStore interface that connects to an external Chroma database.
22
package chroma

vectorstores/pgvector/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package pgvector contains an implementation of the VectorStore
2+
// interface using pgvector.
3+
package pgvector

vectorstores/pgvector/options.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package pgvector
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
8+
"github.com/jackc/pgx/v5"
9+
"github.com/tmc/langchaingo/embeddings"
10+
)
11+
12+
const (
13+
DefaultCollectionName = "langchain"
14+
DefaultPreDeleteCollection = false
15+
DefaultEmbeddingStoreTableName = "langchain_pg_embedding"
16+
DefaultCollectionStoreTableName = "langchain_pg_collection"
17+
)
18+
19+
// ErrInvalidOptions is returned when the options given are invalid.
20+
var ErrInvalidOptions = errors.New("invalid options")
21+
22+
// Option is a function type that can be used to modify the client.
23+
type Option func(p *Store)
24+
25+
// WithEmbedder is an option for setting the embedder to use. Must be set.
26+
func WithEmbedder(e embeddings.Embedder) Option {
27+
return func(p *Store) {
28+
p.embedder = e
29+
}
30+
}
31+
32+
// WithConnectionURL is an option for specifying the Postgres connection URL. Must be set.
33+
func WithConnectionURL(connectionURL string) Option {
34+
return func(p *Store) {
35+
p.postgresConnectionURL = connectionURL
36+
}
37+
}
38+
39+
// WithPreDeleteCollection is an option for setting if the collection should be deleted before creating.
40+
func WithPreDeleteCollection(preDelete bool) Option {
41+
return func(p *Store) {
42+
p.preDeleteCollection = preDelete
43+
}
44+
}
45+
46+
// WithCollectionName is an option for specifying the collection name.
47+
func WithCollectionName(name string) Option {
48+
return func(p *Store) {
49+
p.collectionName = name
50+
}
51+
}
52+
53+
// WithEmbeddingTableName is an option for specifying the embedding table name.
54+
func WithEmbeddingTableName(name string) Option {
55+
return func(p *Store) {
56+
p.embeddingTableName = pgx.Identifier{name}.Sanitize()
57+
}
58+
}
59+
60+
// WithCollectionTableName is an option for specifying the collection table name.
61+
func WithCollectionTableName(name string) Option {
62+
return func(p *Store) {
63+
p.collectionTableName = pgx.Identifier{name}.Sanitize()
64+
}
65+
}
66+
67+
func applyClientOptions(opts ...Option) (Store, error) {
68+
o := &Store{
69+
collectionName: DefaultCollectionName,
70+
preDeleteCollection: DefaultPreDeleteCollection,
71+
embeddingTableName: DefaultEmbeddingStoreTableName,
72+
collectionTableName: DefaultCollectionStoreTableName,
73+
}
74+
75+
for _, opt := range opts {
76+
opt(o)
77+
}
78+
79+
if o.postgresConnectionURL == "" {
80+
o.postgresConnectionURL = os.Getenv("PGVECTOR_CONNECTION_STRING")
81+
}
82+
83+
if o.postgresConnectionURL == "" {
84+
return Store{}, fmt.Errorf("%w: missing postgresConnectionURL", ErrInvalidOptions)
85+
}
86+
87+
if o.embedder == nil {
88+
return Store{}, fmt.Errorf("%w: missing embedder", ErrInvalidOptions)
89+
}
90+
91+
return *o, nil
92+
}

0 commit comments

Comments
 (0)