Skip to content

Commit fe102bc

Browse files
committed
vectorstores: add pgvector
Signed-off-by: Abirdcfly <[email protected]>
1 parent 2bb1ca0 commit fe102bc

File tree

6 files changed

+837
-0
lines changed

6 files changed

+837
-0
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
@@ -422,6 +422,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
422422
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
423423
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
424424
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
425+
github.com/pgvector/pgvector-go v0.1.1 h1:kqJigGctFnlWvskUiYIvJRNwUtQl/aMSUZVs0YWQe+g=
426+
github.com/pgvector/pgvector-go v0.1.1/go.mod h1:wLJgD/ODkdtd2LJK4l6evHXTuG+8PxymYAVomKHOWac=
425427
github.com/pinecone-io/go-pinecone v0.3.0 h1:+t0CiYaaA+JN6YM9QRNlvfLEr2kkGzcVEj/xNmSAON4=
426428
github.com/pinecone-io/go-pinecone v0.3.0/go.mod h1:VdSieE1r4jT3XydjFi+iL5w9qsGRz/x8LxWach2Hnv8=
427429
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=

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

0 commit comments

Comments
 (0)