pgvector

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmbedFunc

type EmbedFunc func(ctx context.Context, text string) ([]float32, error)

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

func WithContentCol(col string) Option

WithContentCol sets the column that holds document text. Defaults to types.DefaultContentField.

func WithDSN

func WithDSN(dsn string) Option

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

func WithEmbeddingCol(col string) Option

WithEmbeddingCol sets the column that holds the pgvector embedding. Defaults to "embedding".

func WithLogLevel

func WithLogLevel(level string) Option

WithLogLevel sets the default log level when WithLogger is omitted. Defaults to "error".

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger sets the logger. When omitted, a default logger at the configured log level is used.

func WithMinScore

func WithMinScore(minScore float64) Option

WithMinScore sets the minimum cosine similarity (0–1) for returned documents. Defaults to types.DefaultMinScore.

func WithPool

func WithPool(pool *pgxpool.Pool) Option

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

func WithSourceCol(col string) Option

WithSourceCol sets the column that holds the document source identifier. Defaults to types.DefaultSourceField.

func WithTable

func WithTable(table string) Option

WithTable sets the PostgreSQL table (or view) to search. Required.

func WithTopK

func WithTopK(topK int) Option

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.

Jump to

Keyboard shortcuts

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