Documentation
¶
Overview ¶
Package pgvector provides a retriever backed by PostgreSQL with the pgvector extension. Callers provide a plain-text query; the retriever converts it to an embedding via EmbedFunc and runs a cosine-similarity nearest-neighbour search against the configured table.
Index ¶
- type EmbedFunc
- type Option
- func WithContentCol(col string) Option
- func WithDSN(dsn string) Option
- func WithEmbeddingCol(col string) Option
- func WithLogLevel(level string) Option
- func WithLogger(l logger.Logger) Option
- func WithMinScore(minScore float64) Option
- func WithPool(pool *pgxpool.Pool) Option
- func WithSourceCol(col string) Option
- func WithTable(table string) Option
- func WithTopK(topK int) Option
- type PgvectorRetriever
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EmbedFunc ¶
EmbedFunc converts a plain-text query into a vector embedding. Callers typically wrap an LLM embedding API (e.g. OpenAI text-embedding-3-small).
type Option ¶
type Option func(*PgvectorRetriever)
Option configures PgvectorRetriever.
func WithContentCol ¶
WithContentCol sets the column that holds document text. Defaults to types.DefaultContentField.
func WithDSN ¶
WithDSN sets the PostgreSQL connection string used to create a new pool when WithPool is omitted. The pool is configured to register pgvector types on each new connection automatically.
func WithEmbeddingCol ¶
WithEmbeddingCol sets the column that holds the pgvector embedding. Defaults to "embedding".
func WithLogLevel ¶
WithLogLevel sets the default log level when WithLogger is omitted. Defaults to "error".
func WithLogger ¶
WithLogger sets the logger. When omitted, a default logger at the configured log level is used.
func WithMinScore ¶
WithMinScore sets the minimum cosine similarity (0–1) for returned documents. Defaults to types.DefaultMinScore.
func WithPool ¶
WithPool sets an existing pgxpool.Pool. When provided, WithDSN is ignored. Callers must register pgvector types on the pool:
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
return pgxvec.RegisterTypes(ctx, conn)
}
func WithSourceCol ¶
WithSourceCol sets the column that holds the document source identifier. Defaults to types.DefaultSourceField.
func WithTopK ¶
WithTopK sets the maximum number of documents returned per search. Defaults to types.DefaultTopK.
type PgvectorRetriever ¶
type PgvectorRetriever struct {
// contains filtered or unexported fields
}
PgvectorRetriever searches a PostgreSQL table with the pgvector extension using cosine similarity. The query text is converted to an embedding via EmbedFunc before each search.
func NewRetriever ¶
func NewRetriever(name string, embed EmbedFunc, opts ...Option) (*PgvectorRetriever, error)
NewRetriever builds a PgvectorRetriever. name must be non-empty and unique across all retrievers registered with the same agent. embed is required. WithTable is required. When WithPool is omitted, WithDSN must be provided. Zero-valued topK and minScore default to types.DefaultTopK and types.DefaultMinScore.
func (*PgvectorRetriever) Name ¶
func (r *PgvectorRetriever) Name() string
Name implements interfaces.Retriever.
func (*PgvectorRetriever) Search ¶
func (r *PgvectorRetriever) Search(ctx context.Context, query string) ([]interfaces.Document, error)
Search embeds the query and runs a cosine-similarity nearest-neighbour search against the configured PostgreSQL table. Returns at most WithTopK documents with similarity ≥ WithMinScore.
Table and column names are developer-controlled build-time configuration and are not sanitised against SQL injection because they are never derived from runtime user input.