pgvector

package
v0.1.14-update.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package pgvector contains an implementation of the VectorStore interface using pgvector.

Index

Constants

View Source
const (
	DefaultCollectionName           = "langchain"
	DefaultPreDeleteCollection      = false
	DefaultEmbeddingStoreTableName  = "langchain_pg_embedding"
	DefaultCollectionStoreTableName = "langchain_pg_collection"
)

Variables

View Source
var (
	ErrEmbedderWrongNumberVectors = errors.New("number of vectors from embedder does not match number of documents")
	ErrInvalidScoreThreshold      = errors.New("score threshold must be between 0 and 1")
	ErrInvalidFilters             = errors.New("invalid filters")
	ErrInvalidFilterKey           = errors.New("filter key must be a bare identifier")
	ErrInvalidMetadataIndex       = errors.New("invalid metadata index")
	ErrUnsupportedOptions         = errors.New("unsupported options")
)
View Source
var ErrInvalidOptions = errors.New("invalid options")

ErrInvalidOptions is returned when the options given are invalid.

Functions

This section is empty.

Types

type CloseNoErr

type CloseNoErr interface {
	Close()
}

type HNSWIndex

type HNSWIndex struct {
	// contains filtered or unexported fields
}

type MetadataIndex

type MetadataIndex struct {
	// Name overrides the derived name. Leave it empty unless an existing index
	// has to be adopted under the name it already carries.
	Name string

	// Keys are the cmetadata keys in index order, most selective predicate
	// first. At least one is required.
	Keys []string

	// Exclude restricts the index to rows whose key does NOT hold the given
	// value. It earns its place when the interesting rows are a small minority
	// of the table: the index then spans that minority rather than every row.
	Exclude map[string]string
}

MetadataIndex declares an index over keys inside the cmetadata column. The store creates every declared index it does not already find when it starts.

Declaring an index is only half the work: a query reaches it solely when it constrains Keys from the first one onwards, so the leading key belongs to whichever filter every caller of that search path supplies.

type Option

type Option func(p *Store)

Option is a function type that can be used to modify the client.

func WithCollectionMetadata

func WithCollectionMetadata(metadata map[string]any) Option

WithCollectionMetadata is an option for specifying the collection metadata.

func WithCollectionName

func WithCollectionName(name string) Option

WithCollectionName is an option for specifying the collection name.

func WithCollectionTableName

func WithCollectionTableName(name string) Option

WithCollectionTableName is an option for specifying the collection table name.

func WithConn

func WithConn(conn PGXConn) Option

WithConn is an option for specifying the Postgres connection. From pgx doc: it is not safe for concurrent usage.Use a connection pool to manage access to multiple database connections from multiple goroutines.

func WithConnectionURL

func WithConnectionURL(connectionURL string) Option

WithConnectionURL is an option for specifying the Postgres connection URL. Either this or WithConn must be used.

func WithEmbedder

func WithEmbedder(e embeddings.Embedder) Option

WithEmbedder is an option for setting the embedder to use. Must be set.

func WithEmbeddingTableName

func WithEmbeddingTableName(name string) Option

WithEmbeddingTableName is an option for specifying the embedding table name.

func WithHNSWIndex

func WithHNSWIndex(m int, efConstruction int, distanceFunction string) Option

WithHNSWIndex is an option for specifying the HNSW index parameters. See here for more details: https://github.com/pgvector/pgvector#hnsw

m: he max number of connections per layer (16 by default) efConstruction: the size of the dynamic candidate list for constructing the graph (64 by default) distanceFunction: the distance function to use (l2 by default).

func WithMetadataIndexes

func WithMetadataIndexes(indexes ...MetadataIndex) Option

WithMetadataIndexes declares indexes over keys inside the cmetadata column. The store creates any of them it does not find when it starts.

Without one, a filtered search reads every row of the table: the metadata predicates narrow the candidate set only after a distance has already been computed for each row, and on a table of embeddings that cost is dominated by detoasting vectors that were never going to be returned.

func WithPreDeleteCollection

func WithPreDeleteCollection(preDelete bool) Option

WithPreDeleteCollection is an option for setting if the collection should be deleted before creating.

func WithVectorDimensions

func WithVectorDimensions(size int) Option

WithVectorDimensions is an option for specifying the vector size.

type PGXConn

type PGXConn interface {
	Ping(ctx context.Context) error
	Begin(ctx context.Context) (pgx.Tx, error)
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, arguments ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, arguments ...any) pgx.Row
	SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults
}

PGXConn represents both a pgx.Conn and pgxpool.Pool conn.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a wrapper around the pgvector client.

func New

func New(ctx context.Context, opts ...Option) (Store, error)

New creates a new Store with options.

func (Store) AddDocuments

func (s Store) AddDocuments(
	ctx context.Context,
	docs []schema.Document,
	options ...vectorstores.Option,
) ([]string, error)

AddDocuments adds documents to the Postgres collection associated with 'Store'. and returns the ids of the added documents.

func (Store) Close

func (s Store) Close() error

Close closes the connection.

func (Store) DropTables

func (s Store) DropTables(ctx context.Context) error

func (Store) RemoveCollection

func (s Store) RemoveCollection(ctx context.Context, tx pgx.Tx) error

func (Store) Search

func (s Store) Search(
	ctx context.Context,
	numDocuments int,
	options ...vectorstores.Option,
) ([]schema.Document, error)

func (Store) SimilaritySearch

func (s Store) SimilaritySearch(
	ctx context.Context,
	query string,
	numDocuments int,
	options ...vectorstores.Option,
) ([]schema.Document, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL