Class PostgresVectorStore (0.10.0)

PostgresVectorStore(
    key: object,
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    vs: langchain_google_cloud_sql_pg.async_vectorstore.AsyncPostgresVectorStore,
)

Google Cloud SQL for PostgreSQL Vector Store class

Properties

embeddings

Access the query embedding object if available.

Methods

PostgresVectorStore

PostgresVectorStore(
    key: object,
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    vs: langchain_google_cloud_sql_pg.async_vectorstore.AsyncPostgresVectorStore,
)

PostgresVectorStore constructor.

Parameters
Name Description
key object

Prevent direct constructor usage.

engine PostgresEngine

Connection pool engine for managing connections to Postgres database.

vs AsyncPostgresVectorstore

The async only VectorStore implementation

Exceptions
Type Description
Exception If called directly by user.

_select_relevance_score_fn

_select_relevance_score_fn() -> typing.Callable[[float], float]

Select a relevance function based on distance strategy.

aadd_documents

aadd_documents(
    documents: typing.List[langchain_core.documents.base.Document],
    ids: typing.Optional[typing.List] = None,
    **kwargs: typing.Any
) -> typing.List[str]

Embed documents and add to the table.

Exceptions
Type Description
InvalidTextRepresentationErro

aadd_texts

aadd_texts(
    texts: typing.Iterable[str],
    metadatas: typing.Optional[typing.List[dict]] = None,
    ids: typing.Optional[typing.List] = None,
    **kwargs: typing.Any
) -> typing.List[str]

Embed texts and add to the table.

Exceptions
Type Description
InvalidTextRepresentationErro

aapply_vector_index

aapply_vector_index(
    index: langchain_google_cloud_sql_pg.indexes.BaseIndex,
    name: typing.Optional[str] = None,
    concurrently: bool = False,
) -> None

Create an index on the vector store table.

add_documents

add_documents(
    documents: typing.List[langchain_core.documents.base.Document],
    ids: typing.Optional[typing.List] = None,
    **kwargs: typing.Any
) -> typing.List[str]

Embed documents and add to the table.

Exceptions
Type Description
InvalidTextRepresentationErro

add_texts

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

Embed texts and add to the table.

Exceptions
Type Description
InvalidTextRepresentationErro

adelete

adelete(
    ids: typing.Optional[typing.List] = None, **kwargs: typing.Any
) -> typing.Optional[bool]

Delete records from the table.

Exceptions
Type Description
InvalidTextRepresentationErro

adrop_vector_index

adrop_vector_index(index_name: typing.Optional[str] = None) -> None

Drop the vector index.

afrom_documents

afrom_documents(
    documents: typing.List[langchain_core.documents.base.Document],
    embedding: langchain_core.embeddings.embeddings.Embeddings,
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    table_name: str,
    schema_name: str = "public",
    ids: typing.Optional[typing.List] = None,
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    ignore_metadata_columns: typing.Optional[typing.List[str]] = None,
    id_column: str = "langchain_id",
    metadata_json_column: str = "langchain_metadata",
    distance_strategy: langchain_google_cloud_sql_pg.indexes.DistanceStrategy = DistanceStrategy.COSINE_DISTANCE,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    index_query_options: typing.Optional[
        langchain_google_cloud_sql_pg.indexes.QueryOptions
    ] = None,
) -> langchain_google_cloud_sql_pg.vectorstore.PostgresVectorStore

Create an PostgresVectorStore instance from documents.

Parameters
Name Description
documents List[Document]

Documents to add to the vector store.

embedding Embeddings

Text embedding model to use.

engine PostgresEngine

Connection pool engine for managing connections to Postgres database.

table_name str

Name of the existing table or the table to be created.

schema_name str, optional

Database schema name of the table. Defaults to "public".

metadatas Optional[List[dict]]

List of metadatas to add to table records.

content_column str

Column that represent a Document’s page_content. Defaults to "content".

embedding_column str

Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".

metadata_columns List[str]

Column(s) that represent a document's metadata.

ignore_metadata_columns List[str]

Column(s) to ignore in pre-existing tables for a document's metadata. Can not be used with metadata_columns. Defaults to None.

id_column str

Column that represents the Document's id. Defaults to "langchain_id".

metadata_json_column str

Column to store metadata as JSON. Defaults to "langchain_metadata".

distance_strategy DistanceStrategy

Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.

k int

Number of Documents to return from search. Defaults to 4.

fetch_k int

Number of Documents to fetch to pass to MMR algorithm.

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.

index_query_options QueryOptions

Index query option.

Exceptions
Type Description
InvalidTextRepresentationErro

afrom_texts

afrom_texts(
    texts: typing.List[str],
    embedding: langchain_core.embeddings.embeddings.Embeddings,
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    table_name: str,
    schema_name: str = "public",
    metadatas: typing.Optional[typing.List[dict]] = None,
    ids: typing.Optional[typing.List] = None,
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    ignore_metadata_columns: typing.Optional[typing.List[str]] = None,
    id_column: str = "langchain_id",
    metadata_json_column: str = "langchain_metadata",
    distance_strategy: langchain_google_cloud_sql_pg.indexes.DistanceStrategy = DistanceStrategy.COSINE_DISTANCE,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    index_query_options: typing.Optional[
        langchain_google_cloud_sql_pg.indexes.QueryOptions
    ] = None,
) -> langchain_google_cloud_sql_pg.vectorstore.PostgresVectorStore

Create an PostgresVectorStore instance from texts.

Parameters
Name Description
texts List[str]

Texts to add to the vector store.

embedding Embeddings

Text embedding model to use.

engine PostgresEngine

Connection pool engine for managing connections to Postgres database.

table_name str

Name of the existing table or the table to be created.

schema_name str, optional

Database schema name of the table. Defaults to "public".

metadatas Optional[List[dict]]

List of metadatas to add to table records.

content_column str

Column that represent a Document’s page_content. Defaults to "content".

embedding_column str

Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".

metadata_columns List[str]

Column(s) that represent a document's metadata.

ignore_metadata_columns List[str]

Column(s) to ignore in pre-existing tables for a document's metadata. Can not be used with metadata_columns. Defaults to None.

id_column str

Column that represents the Document's id. Defaults to "langchain_id".

metadata_json_column str

Column to store metadata as JSON. Defaults to "langchain_metadata".

distance_strategy DistanceStrategy

Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.

k int

Number of Documents to return from search. Defaults to 4.

fetch_k int

Number of Documents to fetch to pass to MMR algorithm.

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.

index_query_options QueryOptions

Index query option.

Exceptions
Type Description
InvalidTextRepresentationErro

ais_valid_index

ais_valid_index(index_name: typing.Optional[str] = None) -> bool

Check if index exists in the table.

amax_marginal_relevance_search(
    query: str,
    k: typing.Optional[int] = None,
    fetch_k: typing.Optional[int] = None,
    lambda_mult: typing.Optional[float] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search_by_vector

amax_marginal_relevance_search_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    fetch_k: typing.Optional[int] = None,
    lambda_mult: typing.Optional[float] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search_with_score_by_vector

amax_marginal_relevance_search_with_score_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    fetch_k: typing.Optional[int] = None,
    lambda_mult: typing.Optional[float] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[typing.Tuple[langchain_core.documents.base.Document, float]]

Return docs and distance scores selected using the maximal marginal relevance.

apply_vector_index

apply_vector_index(
    index: langchain_google_cloud_sql_pg.indexes.BaseIndex,
    name: typing.Optional[str] = None,
    concurrently: bool = False,
) -> None

Create an index on the vector store table.

areindex

areindex(index_name: typing.Optional[str] = None) -> None

Re-index the vector store table.

asimilarity_search(
    query: str,
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected by similarity search on query.

asimilarity_search_by_vector

asimilarity_search_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected by vector similarity search.

asimilarity_search_with_score

asimilarity_search_with_score(
    query: str,
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[typing.Tuple[langchain_core.documents.base.Document, float]]

Return docs and distance scores selected by similarity search on query.

asimilarity_search_with_score_by_vector

asimilarity_search_with_score_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[typing.Tuple[langchain_core.documents.base.Document, float]]

Return docs and distance scores selected by vector similarity search.

create

create(
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    embedding_service: langchain_core.embeddings.embeddings.Embeddings,
    table_name: str,
    schema_name: str = "public",
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    ignore_metadata_columns: typing.Optional[typing.List[str]] = None,
    id_column: str = "langchain_id",
    metadata_json_column: typing.Optional[str] = "langchain_metadata",
    distance_strategy: langchain_google_cloud_sql_pg.indexes.DistanceStrategy = DistanceStrategy.COSINE_DISTANCE,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    index_query_options: typing.Optional[
        langchain_google_cloud_sql_pg.indexes.QueryOptions
    ] = None,
) -> langchain_google_cloud_sql_pg.vectorstore.PostgresVectorStore

Create a new PostgresVectorStore instance.

Parameters
Name Description
engine PostgresEngine

Connection pool engine for managing connections to Cloud SQL for PostgreSQL database.

embedding_service Embeddings

Text embedding model to use.

table_name str

Name of an existing table or table to be created.

schema_name str, optional

Database schema name of the table. Defaults to "public".

content_column str

Column that represent a Document's page_content. Defaults to "content".

embedding_column str

Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".

metadata_columns List[str]

Column(s) that represent a document's metadata.

ignore_metadata_columns List[str]

Column(s) to ignore in pre-existing tables for a document's metadata. Can not be used with metadata_columns. Defaults to None.

id_column str

Column that represents the Document's id. Defaults to "langchain_id".

metadata_json_column str

Column to store metadata as JSON. Defaults to "langchain_metadata".

distance_strategy DistanceStrategy

Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.

k int

Number of Documents to return from search. Defaults to 4.

fetch_k int

Number of Documents to fetch to pass to MMR algorithm.

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.

index_query_options QueryOptions

Index query option.

create_sync

create_sync(
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    embedding_service: langchain_core.embeddings.embeddings.Embeddings,
    table_name: str,
    schema_name: str = "public",
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    ignore_metadata_columns: typing.Optional[typing.List[str]] = None,
    id_column: str = "langchain_id",
    metadata_json_column: str = "langchain_metadata",
    distance_strategy: langchain_google_cloud_sql_pg.indexes.DistanceStrategy = DistanceStrategy.COSINE_DISTANCE,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    index_query_options: typing.Optional[
        langchain_google_cloud_sql_pg.indexes.QueryOptions
    ] = None,
) -> langchain_google_cloud_sql_pg.vectorstore.PostgresVectorStore

Create a new PostgresVectorStore instance.

Parameters
Name Description
engine PostgresEngine

Connection pool engine for managing connections to Cloud SQL for PostgreSQL database.

embedding_service Embeddings

Text embedding model to use.

table_name str

Name of an existing table or table to be created.

schema_name str, optional

Database schema name of the table. Defaults to "public".

content_column str

Column that represent a Document's page_content. Defaults to "content".

embedding_column str

Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".

metadata_columns List[str]

Column(s) that represent a document's metadata.

ignore_metadata_columns List[str]

Column(s) to ignore in pre-existing tables for a document's metadata. Can not be used with metadata_columns. Defaults to None.

id_column str

Column that represents the Document's id. Defaults to "langchain_id".

metadata_json_column str

Column to store metadata as JSON. Defaults to "langchain_metadata".

distance_strategy DistanceStrategy

Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.

k int

Number of Documents to return from search. Defaults to 4.

fetch_k int

Number of Documents to fetch to pass to MMR algorithm.

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.

index_query_options QueryOptions

Index query option.

delete

delete(
    ids: typing.Optional[typing.List] = None, **kwargs: typing.Any
) -> typing.Optional[bool]

Delete records from the table.

Exceptions
Type Description
InvalidTextRepresentationErro

drop_vector_index

drop_vector_index(index_name: typing.Optional[str] = None) -> None

Drop the vector index.

from_documents

from_documents(
    documents: typing.List[langchain_core.documents.base.Document],
    embedding: langchain_core.embeddings.embeddings.Embeddings,
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    table_name: str,
    schema_name: str = "public",
    ids: typing.Optional[typing.List] = None,
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    ignore_metadata_columns: typing.Optional[typing.List[str]] = None,
    id_column: str = "langchain_id",
    metadata_json_column: str = "langchain_metadata",
    distance_strategy: langchain_google_cloud_sql_pg.indexes.DistanceStrategy = DistanceStrategy.COSINE_DISTANCE,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    index_query_options: typing.Optional[
        langchain_google_cloud_sql_pg.indexes.QueryOptions
    ] = None,
) -> langchain_google_cloud_sql_pg.vectorstore.PostgresVectorStore

Create an PostgresVectorStore instance from documents.

Parameters
Name Description
documents List[Document]

Documents to add to the vector store.

embedding Embeddings

Text embedding model to use.

engine PostgresEngine

Connection pool engine for managing connections to Postgres database.

table_name str

Name of the existing table or the table to be created.

schema_name str, optional

Database schema name of the table. Defaults to "public".

metadatas Optional[List[dict]]

List of metadatas to add to table records.

content_column str

Column that represent a Document’s page_content. Defaults to "content".

embedding_column str

Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".

metadata_columns List[str]

Column(s) that represent a document's metadata.

ignore_metadata_columns List[str]

Column(s) to ignore in pre-existing tables for a document's metadata. Can not be used with metadata_columns. Defaults to None.

id_column str

Column that represents the Document's id. Defaults to "langchain_id".

metadata_json_column str

Column to store metadata as JSON. Defaults to "langchain_metadata".

distance_strategy DistanceStrategy

Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.

k int

Number of Documents to return from search. Defaults to 4.

fetch_k int

Number of Documents to fetch to pass to MMR algorithm.

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.

index_query_options QueryOptions

Index query option.

Exceptions
Type Description
InvalidTextRepresentationErro

from_texts

from_texts(
    texts: typing.List[str],
    embedding: langchain_core.embeddings.embeddings.Embeddings,
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    table_name: str,
    schema_name: str = "public",
    metadatas: typing.Optional[typing.List[dict]] = None,
    ids: typing.Optional[typing.List] = None,
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    ignore_metadata_columns: typing.Optional[typing.List[str]] = None,
    id_column: str = "langchain_id",
    metadata_json_column: str = "langchain_metadata",
    distance_strategy: langchain_google_cloud_sql_pg.indexes.DistanceStrategy = DistanceStrategy.COSINE_DISTANCE,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    index_query_options: typing.Optional[
        langchain_google_cloud_sql_pg.indexes.QueryOptions
    ] = None,
) -> langchain_google_cloud_sql_pg.vectorstore.PostgresVectorStore

Create an PostgresVectorStore instance from texts.

Parameters
Name Description
texts List[str]

Texts to add to the vector store.

embedding Embeddings

Text embedding model to use.

engine PostgresEngine

Connection pool engine for managing connections to Postgres database.

table_name str

Name of the existing table or the table to be created.

schema_name str, optional

Database schema name of the table. Defaults to "public".

metadatas Optional[List[dict]]

List of metadatas to add to table records.

content_column str

Column that represent a Document’s page_content. Defaults to "content".

embedding_column str

Column for embedding vectors. The embedding is generated from the document value. Defaults to "embedding".

metadata_columns List[str]

Column(s) that represent a document's metadata.

ignore_metadata_columns List[str]

Column(s) to ignore in pre-existing tables for a document's metadata. Can not be used with metadata_columns. Defaults to None.

id_column str

Column that represents the Document's id. Defaults to "langchain_id".

metadata_json_column str

Column to store metadata as JSON. Defaults to "langchain_metadata".

distance_strategy DistanceStrategy

Distance strategy to use for vector similarity search. Defaults to COSINE_DISTANCE.

k int

Number of Documents to return from search. Defaults to 4.

fetch_k int

Number of Documents to fetch to pass to MMR algorithm.

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.

index_query_options QueryOptions

Index query option.

Exceptions
Type Description
InvalidTextRepresentationErro

is_valid_index

is_valid_index(index_name: typing.Optional[str] = None) -> bool

Check if index exists in the table.

max_marginal_relevance_search(
    query: str,
    k: typing.Optional[int] = None,
    fetch_k: typing.Optional[int] = None,
    lambda_mult: typing.Optional[float] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected using the maximal marginal relevance.

max_marginal_relevance_search_by_vector

max_marginal_relevance_search_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    fetch_k: typing.Optional[int] = None,
    lambda_mult: typing.Optional[float] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected using the maximal marginal relevance.

max_marginal_relevance_search_with_score_by_vector

max_marginal_relevance_search_with_score_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    fetch_k: typing.Optional[int] = None,
    lambda_mult: typing.Optional[float] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[typing.Tuple[langchain_core.documents.base.Document, float]]

Return docs and distance scores selected using the maximal marginal relevance.

reindex

reindex(index_name: typing.Optional[str] = None) -> None

Re-index the vector store table.

similarity_search(
    query: str,
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected by similarity search on query.

similarity_search_by_vector

similarity_search_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[langchain_core.documents.base.Document]

Return docs selected by vector similarity search.

similarity_search_with_score

similarity_search_with_score(
    query: str,
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[typing.Tuple[langchain_core.documents.base.Document, float]]

Return docs and distance scores selected by similarity search on query.

similarity_search_with_score_by_vector

similarity_search_with_score_by_vector(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.List[typing.Tuple[langchain_core.documents.base.Document, float]]

Return docs and distance scores selected by similarity search on vector.