langchain_community.vectorstores.pgvector.PGVector

class langchain_community.vectorstores.pgvector.PGVector(connection_string: str, embedding_function: Embeddings, embedding_length: Optional[int] = None, collection_name: str = 'langchain', collection_metadata: Optional[dict] = None, distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, pre_delete_collection: bool = False, logger: Optional[Logger] = None, relevance_score_fn: Optional[Callable[[float], float]] = None, *, connection: Optional[Connection] = None, engine_args: Optional[dict[str, Any]] = None)[source]

Postgres/PGVector vector store.

To use, you should have the pgvector python package installed.

Parameters
  • connection_string (str) – Postgres connection string.

  • embedding_function (Embeddings) – Any embedding function implementing langchain.embeddings.base.Embeddings interface.

  • embedding_length (Optional[int]) – The length of the embedding vector. (default: None) NOTE: This is not mandatory. Defining it will prevent vectors of any other size to be added to the embeddings table but, without it, the embeddings can’t be indexed.

  • collection_name (str) – The name of the collection to use. (default: langchain) NOTE: This is not the name of the table, but the name of the collection. The tables will be created when initializing the store (if not exists) So, make sure the user has the right permissions to create tables.

  • distance_strategy (DistanceStrategy) – The distance strategy to use. (default: COSINE)

  • pre_delete_collection (bool) – If True, will delete the collection if it exists. (default: False). Useful for testing.

  • engine_args (Optional[dict[str, Any]]) – SQLAlchemy’s create engine arguments.

  • collection_metadata (Optional[dict]) –

  • logger (Optional[logging.Logger]) –

  • relevance_score_fn (Optional[Callable[[float], float]]) –

  • connection (Optional[sqlalchemy.engine.Connection]) –

Example

from langchain_community.vectorstores import PGVector
from langchain_community.embeddings.openai import OpenAIEmbeddings

CONNECTION_STRING = "postgresql+psycopg2://hwc@localhost:5432/test3"
COLLECTION_NAME = "state_of_the_union_test"
embeddings = OpenAIEmbeddings()
vectorestore = PGVector.from_documents(
    embedding=embeddings,
    documents=docs,
    collection_name=COLLECTION_NAME,
    connection_string=CONNECTION_STRING,
)

Attributes

distance_strategy

embeddings

Access the query embedding object if available.

Methods

__init__(connection_string, embedding_function)

aadd_documents(documents, **kwargs)

Run more documents through the embeddings and add to the vectorstore.

aadd_texts(texts[, metadatas])

Run more texts through the embeddings and add to the vectorstore.

add_documents(documents, **kwargs)

Run more documents through the embeddings and add to the vectorstore.

add_embeddings(texts, embeddings[, ...])

Add embeddings to the vectorstore.

add_texts(texts[, metadatas, ids])

Run more texts through the embeddings and add to the vectorstore.

adelete([ids])

Delete by vector ID or other criteria.

afrom_documents(documents, embedding, **kwargs)

Return VectorStore initialized from documents and embeddings.

afrom_texts(texts, embedding[, metadatas])

Return VectorStore initialized from texts and embeddings.

amax_marginal_relevance_search(query[, k, ...])

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search_by_vector(...)

Return docs selected using the maximal marginal relevance.

as_retriever(**kwargs)

Return VectorStoreRetriever initialized from this VectorStore.

asearch(query, search_type, **kwargs)

Return docs most similar to query using specified search type.

asimilarity_search(query[, k])

Return docs most similar to query.

asimilarity_search_by_vector(embedding[, k])

Return docs most similar to embedding vector.

asimilarity_search_with_relevance_scores(query)

Return docs and relevance scores in the range [0, 1], asynchronously.

asimilarity_search_with_score(*args, **kwargs)

Run similarity search with distance asynchronously.

connection_string_from_db_params(driver, ...)

Return connection string from database parameters.

create_collection()

create_tables_if_not_exists()

create_vector_extension()

delete([ids, collection_only])

Delete vectors by ids or uuids.

delete_collection()

drop_tables()

from_documents(documents, embedding[, ...])

Return VectorStore initialized from documents and embeddings.

from_embeddings(text_embeddings, embedding)

Construct PGVector wrapper from raw documents and pre- generated embeddings.

from_existing_index(embedding[, ...])

Get instance of an existing PGVector store.This method will return the instance of the store without inserting any new embeddings

from_texts(texts, embedding[, metadatas, ...])

Return VectorStore initialized from texts and embeddings.

get_collection(session)

get_connection_string(kwargs)

max_marginal_relevance_search(query[, k, ...])

Return docs selected using the maximal marginal relevance.

max_marginal_relevance_search_by_vector(...)

Return docs selected using the maximal marginal relevance

max_marginal_relevance_search_with_score(query)

Return docs selected using the maximal marginal relevance with score.

max_marginal_relevance_search_with_score_by_vector(...)

Return docs selected using the maximal marginal relevance with score

search(query, search_type, **kwargs)

Return docs most similar to query using specified search type.

similarity_search(query[, k, filter])

Run similarity search with PGVector with distance.

similarity_search_by_vector(embedding[, k, ...])

Return docs most similar to embedding vector.

similarity_search_with_relevance_scores(query)

Return docs and relevance scores in the range [0, 1].

similarity_search_with_score(query[, k, filter])

Return docs most similar to query.

similarity_search_with_score_by_vector(embedding)

__init__(connection_string: str, embedding_function: Embeddings, embedding_length: Optional[int] = None, collection_name: str = 'langchain', collection_metadata: Optional[dict] = None, distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, pre_delete_collection: bool = False, logger: Optional[Logger] = None, relevance_score_fn: Optional[Callable[[float], float]] = None, *, connection: Optional[Connection] = None, engine_args: Optional[dict[str, Any]] = None) None[source]
Parameters
  • connection_string (str) –

  • embedding_function (Embeddings) –

  • embedding_length (Optional[int]) –

  • collection_name (str) –

  • collection_metadata (Optional[dict]) –

  • distance_strategy (DistanceStrategy) –

  • pre_delete_collection (bool) –

  • logger (Optional[Logger]) –

  • relevance_score_fn (Optional[Callable[[float], float]]) –

  • connection (Optional[Connection]) –

  • engine_args (Optional[dict[str, Any]]) –

Return type

None

async aadd_documents(documents: List[Document], **kwargs: Any) List[str]

Run more documents through the embeddings and add to the vectorstore.

Parameters
  • (List[Document] (documents) – Documents to add to the vectorstore.

  • documents (List[Document]) –

  • kwargs (Any) –

Returns

List of IDs of the added texts.

Return type

List[str]

async aadd_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any) List[str]

Run more texts through the embeddings and add to the vectorstore.

Parameters
  • texts (Iterable[str]) –

  • metadatas (Optional[List[dict]]) –

  • kwargs (Any) –

Return type

List[str]

add_documents(documents: List[Document], **kwargs: Any) List[str]

Run more documents through the embeddings and add to the vectorstore.

Parameters
  • (List[Document] (documents) – Documents to add to the vectorstore.

  • documents (List[Document]) –

  • kwargs (Any) –

Returns

List of IDs of the added texts.

Return type

List[str]

add_embeddings(texts: Iterable[str], embeddings: List[List[float]], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, **kwargs: Any) List[str][source]

Add embeddings to the vectorstore.

Parameters
  • texts (Iterable[str]) – Iterable of strings to add to the vectorstore.

  • embeddings (List[List[float]]) – List of list of embedding vectors.

  • metadatas (Optional[List[dict]]) – List of metadatas associated with the texts.

  • kwargs (Any) – vectorstore specific parameters

  • ids (Optional[List[str]]) –

Return type

List[str]

add_texts(texts: Iterable[str], metadatas: Optional[List[dict]] = None, ids: Optional[List[str]] = None, **kwargs: Any) List[str][source]

Run more texts through the embeddings and add to the vectorstore.

Parameters
  • texts (Iterable[str]) – Iterable of strings to add to the vectorstore.

  • metadatas (Optional[List[dict]]) – Optional list of metadatas associated with the texts.

  • kwargs (Any) – vectorstore specific parameters

  • ids (Optional[List[str]]) –

Returns

List of ids from adding the texts into the vectorstore.

Return type

List[str]

async adelete(ids: Optional[List[str]] = None, **kwargs: Any) Optional[bool]

Delete by vector ID or other criteria.

Parameters
  • ids (Optional[List[str]]) – List of ids to delete.

  • **kwargs (Any) – Other keyword arguments that subclasses might use.

Returns

True if deletion is successful, False otherwise, None if not implemented.

Return type

Optional[bool]

async classmethod afrom_documents(documents: List[Document], embedding: Embeddings, **kwargs: Any) VST

Return VectorStore initialized from documents and embeddings.

Parameters
Return type

VST

async classmethod afrom_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, **kwargs: Any) VST

Return VectorStore initialized from texts and embeddings.

Parameters
  • texts (List[str]) –

  • embedding (Embeddings) –

  • metadatas (Optional[List[dict]]) –

  • kwargs (Any) –

Return type

VST

Return docs selected using the maximal marginal relevance.

Parameters
  • query (str) –

  • k (int) –

  • fetch_k (int) –

  • lambda_mult (float) –

  • kwargs (Any) –

Return type

List[Document]

async amax_marginal_relevance_search_by_vector(embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[Dict[str, str]] = None, **kwargs: Any) List[Document][source]

Return docs selected using the maximal marginal relevance.

Parameters
  • embedding (List[float]) –

  • k (int) –

  • fetch_k (int) –

  • lambda_mult (float) –

  • filter (Optional[Dict[str, str]]) –

  • kwargs (Any) –

Return type

List[Document]

as_retriever(**kwargs: Any) VectorStoreRetriever

Return VectorStoreRetriever initialized from this VectorStore.

Parameters
  • search_type (Optional[str]) – Defines the type of search that the Retriever should perform. Can be “similarity” (default), “mmr”, or “similarity_score_threshold”.

  • search_kwargs (Optional[Dict]) –

    Keyword arguments to pass to the search function. Can include things like:

    k: Amount of documents to return (Default: 4) score_threshold: Minimum relevance threshold

    for similarity_score_threshold

    fetch_k: Amount of documents to pass to MMR algorithm (Default: 20) lambda_mult: Diversity of results returned by MMR;

    1 for minimum diversity and 0 for maximum. (Default: 0.5)

    filter: Filter by document metadata

  • kwargs (Any) –

Returns

Retriever class for VectorStore.

Return type

VectorStoreRetriever

Examples:

# Retrieve more documents with higher diversity
# Useful if your dataset has many similar documents
docsearch.as_retriever(
    search_type="mmr",
    search_kwargs={'k': 6, 'lambda_mult': 0.25}
)

# Fetch more documents for the MMR algorithm to consider
# But only return the top 5
docsearch.as_retriever(
    search_type="mmr",
    search_kwargs={'k': 5, 'fetch_k': 50}
)

# Only retrieve documents that have a relevance score
# Above a certain threshold
docsearch.as_retriever(
    search_type="similarity_score_threshold",
    search_kwargs={'score_threshold': 0.8}
)

# Only get the single most similar document from the dataset
docsearch.as_retriever(search_kwargs={'k': 1})

# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
    search_kwargs={'filter': {'paper_title':'GPT-4 Technical Report'}}
)
async asearch(query: str, search_type: str, **kwargs: Any) List[Document]

Return docs most similar to query using specified search type.

Parameters
  • query (str) –

  • search_type (str) –

  • kwargs (Any) –

Return type

List[Document]

Return docs most similar to query.

Parameters
  • query (str) –

  • k (int) –

  • kwargs (Any) –

Return type

List[Document]

async asimilarity_search_by_vector(embedding: List[float], k: int = 4, **kwargs: Any) List[Document]

Return docs most similar to embedding vector.

Parameters
  • embedding (List[float]) –

  • k (int) –

  • kwargs (Any) –

Return type

List[Document]

async asimilarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]]

Return docs and relevance scores in the range [0, 1], asynchronously.

0 is dissimilar, 1 is most similar.

Parameters
  • query (str) – input text

  • k (int) – Number of Documents to return. Defaults to 4.

  • **kwargs (Any) –

    kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to

    filter the resulting set of retrieved docs

Returns

List of Tuples of (doc, similarity_score)

Return type

List[Tuple[Document, float]]

async asimilarity_search_with_score(*args: Any, **kwargs: Any) List[Tuple[Document, float]]

Run similarity search with distance asynchronously.

Parameters
  • args (Any) –

  • kwargs (Any) –

Return type

List[Tuple[Document, float]]

classmethod connection_string_from_db_params(driver: str, host: str, port: int, database: str, user: str, password: str) str[source]

Return connection string from database parameters.

Parameters
  • driver (str) –

  • host (str) –

  • port (int) –

  • database (str) –

  • user (str) –

  • password (str) –

Return type

str

create_collection() None[source]
Return type

None

create_tables_if_not_exists() None[source]
Return type

None

create_vector_extension() None[source]
Return type

None

delete(ids: Optional[List[str]] = None, collection_only: bool = False, **kwargs: Any) None[source]

Delete vectors by ids or uuids.

Parameters
  • ids (Optional[List[str]]) – List of ids to delete.

  • collection_only (bool) – Only delete ids in the collection.

  • kwargs (Any) –

Return type

None

delete_collection() None[source]
Return type

None

drop_tables() None[source]
Return type

None

classmethod from_documents(documents: List[Document], embedding: Embeddings, collection_name: str = 'langchain', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any) PGVector[source]

Return VectorStore initialized from documents and embeddings. Postgres connection string is required “Either pass it as a parameter or set the PGVECTOR_CONNECTION_STRING environment variable.

Parameters
  • documents (List[Document]) –

  • embedding (Embeddings) –

  • collection_name (str) –

  • distance_strategy (DistanceStrategy) –

  • ids (Optional[List[str]]) –

  • pre_delete_collection (bool) –

  • kwargs (Any) –

Return type

PGVector

classmethod from_embeddings(text_embeddings: List[Tuple[str, List[float]]], embedding: Embeddings, metadatas: Optional[List[dict]] = None, collection_name: str = 'langchain', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any) PGVector[source]

Construct PGVector wrapper from raw documents and pre- generated embeddings.

Return VectorStore initialized from documents and embeddings. Postgres connection string is required “Either pass it as a parameter or set the PGVECTOR_CONNECTION_STRING environment variable.

Example

from langchain_community.vectorstores import PGVector
from langchain_community.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
text_embeddings = embeddings.embed_documents(texts)
text_embedding_pairs = list(zip(texts, text_embeddings))
faiss = PGVector.from_embeddings(text_embedding_pairs, embeddings)
Parameters
  • text_embeddings (List[Tuple[str, List[float]]]) –

  • embedding (Embeddings) –

  • metadatas (Optional[List[dict]]) –

  • collection_name (str) –

  • distance_strategy (DistanceStrategy) –

  • ids (Optional[List[str]]) –

  • pre_delete_collection (bool) –

  • kwargs (Any) –

Return type

PGVector

classmethod from_existing_index(embedding: Embeddings, collection_name: str = 'langchain', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, pre_delete_collection: bool = False, **kwargs: Any) PGVector[source]

Get instance of an existing PGVector store.This method will return the instance of the store without inserting any new embeddings

Parameters
  • embedding (Embeddings) –

  • collection_name (str) –

  • distance_strategy (DistanceStrategy) –

  • pre_delete_collection (bool) –

  • kwargs (Any) –

Return type

PGVector

classmethod from_texts(texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]] = None, collection_name: str = 'langchain', distance_strategy: DistanceStrategy = DistanceStrategy.COSINE, ids: Optional[List[str]] = None, pre_delete_collection: bool = False, **kwargs: Any) PGVector[source]

Return VectorStore initialized from texts and embeddings. Postgres connection string is required “Either pass it as a parameter or set the PGVECTOR_CONNECTION_STRING environment variable.

Parameters
  • texts (List[str]) –

  • embedding (Embeddings) –

  • metadatas (Optional[List[dict]]) –

  • collection_name (str) –

  • distance_strategy (DistanceStrategy) –

  • ids (Optional[List[str]]) –

  • pre_delete_collection (bool) –

  • kwargs (Any) –

Return type

PGVector

get_collection(session: Session) Any[source]
Parameters

session (Session) –

Return type

Any

classmethod get_connection_string(kwargs: Dict[str, Any]) str[source]
Parameters

kwargs (Dict[str, Any]) –

Return type

str

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity

among selected documents.

Parameters
  • query (str) – Text to look up documents similar to.

  • k (int) – Number of Documents to return. Defaults to 4.

  • fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm. Defaults to 20.

  • lambda_mult (float) – Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

  • filter (Optional[Dict[str, str]]) – Filter by metadata. Defaults to None.

  • kwargs (Any) –

Returns

List of Documents selected by maximal marginal relevance.

Return type

List[Document]

max_marginal_relevance_search_by_vector(embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[Dict[str, str]] = None, **kwargs: Any) List[Document][source]
Return docs selected using the maximal marginal relevance

to embedding vector.

Maximal marginal relevance optimizes for similarity to query AND diversity

among selected documents.

Parameters
  • embedding (str) – Text to look up documents similar to.

  • k (int) – Number of Documents to return. Defaults to 4.

  • fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm. Defaults to 20.

  • lambda_mult (float) – Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

  • filter (Optional[Dict[str, str]]) – Filter by metadata. Defaults to None.

  • kwargs (Any) –

Returns

List of Documents selected by maximal marginal relevance.

Return type

List[Document]

max_marginal_relevance_search_with_score(query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[dict] = None, **kwargs: Any) List[Tuple[Document, float]][source]

Return docs selected using the maximal marginal relevance with score.

Maximal marginal relevance optimizes for similarity to query AND diversity

among selected documents.

Parameters
  • query (str) – Text to look up documents similar to.

  • k (int) – Number of Documents to return. Defaults to 4.

  • fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm. Defaults to 20.

  • lambda_mult (float) – Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

  • filter (Optional[Dict[str, str]]) – Filter by metadata. Defaults to None.

  • kwargs (Any) –

Returns

List of Documents selected by maximal marginal

relevance to the query and score for each.

Return type

List[Tuple[Document, float]]

max_marginal_relevance_search_with_score_by_vector(embedding: List[float], k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, filter: Optional[Dict[str, str]] = None, **kwargs: Any) List[Tuple[Document, float]][source]
Return docs selected using the maximal marginal relevance with score

to embedding vector.

Maximal marginal relevance optimizes for similarity to query AND diversity

among selected documents.

Parameters
  • embedding (List[float]) – Embedding to look up documents similar to.

  • k (int) – Number of Documents to return. Defaults to 4.

  • fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm. Defaults to 20.

  • lambda_mult (float) – Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

  • filter (Optional[Dict[str, str]]) – Filter by metadata. Defaults to None.

  • kwargs (Any) –

Returns

List of Documents selected by maximal marginal

relevance to the query and score for each.

Return type

List[Tuple[Document, float]]

search(query: str, search_type: str, **kwargs: Any) List[Document]

Return docs most similar to query using specified search type.

Parameters
  • query (str) –

  • search_type (str) –

  • kwargs (Any) –

Return type

List[Document]

Run similarity search with PGVector with distance.

Parameters
  • query (str) – Query text to search for.

  • k (int) – Number of results to return. Defaults to 4.

  • filter (Optional[Dict[str, str]]) – Filter by metadata. Defaults to None.

  • kwargs (Any) –

Returns

List of Documents most similar to the query.

Return type

List[Document]

similarity_search_by_vector(embedding: List[float], k: int = 4, filter: Optional[dict] = None, **kwargs: Any) List[Document][source]

Return docs most similar to embedding vector.

Parameters
  • embedding (List[float]) – Embedding to look up documents similar to.

  • k (int) – Number of Documents to return. Defaults to 4.

  • filter (Optional[Dict[str, str]]) – Filter by metadata. Defaults to None.

  • kwargs (Any) –

Returns

List of Documents most similar to the query vector.

Return type

List[Document]

similarity_search_with_relevance_scores(query: str, k: int = 4, **kwargs: Any) List[Tuple[Document, float]]

Return docs and relevance scores in the range [0, 1].

0 is dissimilar, 1 is most similar.

Parameters
  • query (str) – input text

  • k (int) – Number of Documents to return. Defaults to 4.

  • **kwargs (Any) –

    kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to

    filter the resulting set of retrieved docs

Returns

List of Tuples of (doc, similarity_score)

Return type

List[Tuple[Document, float]]

similarity_search_with_score(query: str, k: int = 4, filter: Optional[dict] = None) List[Tuple[Document, float]][source]

Return docs most similar to query.

Parameters
  • query (str) – Text to look up documents similar to.

  • k (int) – Number of Documents to return. Defaults to 4.

  • filter (Optional[Dict[str, str]]) – Filter by metadata. Defaults to None.

Returns

List of Documents most similar to the query and score for each.

Return type

List[Tuple[Document, float]]

similarity_search_with_score_by_vector(embedding: List[float], k: int = 4, filter: Optional[dict] = None) List[Tuple[Document, float]][source]
Parameters
  • embedding (List[float]) –

  • k (int) –

  • filter (Optional[dict]) –

Return type

List[Tuple[Document, float]]

Examples using PGVector