langchain.vectorstores.pgvector.PGVector¶
class langchain.vectorstores.pgvector.PGVector(connection_string: str, embedding_function: Embeddings, 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)[source]¶
Postgres/PGVector vector store.
To use, you should have the pgvector python package installed.
Parameters
connection_string – Postgres connection string.
embedding_function – Any embedding function implementing
langchain.embeddings.base.Embeddings interface.
collection_name – 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 – The distance strategy to use. (default: COSINE)
pre_delete_collection – If True, will delete the collection if it exists.
(default: False). Useful for testing.
Example
from langchain.vectorstores import PGVector
from langchain.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)