|
| 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