Class PostgresVectorStore (0.8.0)

PostgresVectorStore(
    key: object,
    engine: langchain_google_cloud_sql_pg.engine.PostgresEngine,
    embedding_service: langchain_core.embeddings.embeddings.Embeddings,
    table_name: str,
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    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,
)

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,
    embedding_service: langchain_core.embeddings.embeddings.Embeddings,
    table_name: str,
    content_column: str = "content",
    embedding_column: str = "embedding",
    metadata_columns: typing.List[str] = [],
    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,
)

PostgresVectorStore constructor.

Parameters
Name Description
key object

Prevent direct constructor usage.

engine PostgresEngine

Connection pool engine for managing connections to Postgres database.

embedding_service Embeddings

Text embedding model to use.

table_name str

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

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.

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
Exception If called directly by user.

__query_collection

__query_collection(
    embedding: typing.List[float],
    k: typing.Optional[int] = None,
    filter: typing.Optional[str] = None,
    **kwargs: typing.Any
) -> typing.Sequence[sqlalchemy.engine.row.RowMapping]

Perform similarity search query on the vector store table.

_aadd_embeddings

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

Add embeddings to the table.

_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[str]] = None,
    **kwargs: typing.Any
) -> typing.List[str]

Embed documents and add to the table

aadd_texts

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

Embed texts and add to the table.

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[str]] = None,
    **kwargs: typing.Any
) -> typing.List[str]

Embed documents and add to the table.

add_texts

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

Embed texts and add to the table.

adelete

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

Delete records from the table.

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,
    ids: typing.Optional[typing.List[str]] = 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",
    **kwargs: typing.Any
) -> 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.

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".

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,
    metadatas: typing.Optional[typing.List[dict]] = None,
    ids: typing.Optional[typing.List[str]] = 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",
    **kwargs: typing.Any
) -> 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.

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".

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.

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,
    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.

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,
    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.

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[str]] = None, **kwargs: typing.Any
) -> typing.Optional[bool]

Delete records from the table.

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,
    ids: typing.Optional[typing.List[str]] = 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",
    **kwargs: typing.Any
) -> 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.

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".

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,
    metadatas: typing.Optional[typing.List[dict]] = None,
    ids: typing.Optional[typing.List[str]] = 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",
    **kwargs: typing.Any
) -> 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.

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".

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.

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.