agentchat.contrib.vectordb.pgvectordb
Collection
A Collection object for PGVector.
Attributes:
client
- The PGVector client.collection_name
str - The name of the collection. Default is “documents”.embedding_function
Callable - The embedding function used to generate the vector representation. Default is None. SentenceTransformer(“all-MiniLM-L6-v2”).encode will be used when None. Models can be chosen from: https://huggingface.co/models?library=sentence-transformersmetadata
Optional[dict] - The metadata of the collection.get_or_create
Optional - The flag indicating whether to get or create the collection.
__init__
Initialize the Collection object.
Arguments:
client
- The PostgreSQL client.collection_name
- The name of the collection. Default is “documents”.embedding_function
- The embedding function used to generate the vector representation.metadata
- The metadata of the collection.get_or_create
- The flag indicating whether to get or create the collection.
Returns:
None
add
Add documents to the collection.
Arguments:
ids
List[ItemID] - A list of document IDs.embeddings
List - A list of document embeddings. Optionalmetadatas
List - A list of document metadatas. Optionaldocuments
List - A list of documents.
Returns:
None
upsert
Upsert documents into the collection.
Arguments:
ids
List[ItemID] - A list of document IDs.documents
List - A list of documents.embeddings
List - A list of document embeddings.metadatas
List - A list of document metadatas.
Returns:
None
count
Get the total number of documents in the collection.
Returns:
int
- The total number of documents.
table_exists
Check if a table exists in the PostgreSQL database.
Arguments:
table_name
str - The name of the table to check.
Returns:
bool
- True if the table exists, False otherwise.
get
Retrieve documents from the collection.
Arguments:
ids
Optional[List] - A list of document IDs.include
Optional - The fields to include.where
Optional - Additional filtering criteria.limit
Optional - The maximum number of documents to retrieve.offset
Optional - The offset for pagination.
Returns:
List
- The retrieved documents.
update
Update documents in the collection.
Arguments:
ids
List - A list of document IDs.embeddings
List - A list of document embeddings.metadatas
List - A list of document metadatas.documents
List - A list of documents.
Returns:
None
euclidean_distance
Calculate the Euclidean distance between two vectors.
Arguments:
- arr1 (List[float]): The first vector.
- arr2 (List[float]): The second vector.
Returns:
- float: The Euclidean distance between arr1 and arr2.
cosine_distance
Calculate the cosine distance between two vectors.
Arguments:
- arr1 (List[float]): The first vector.
- arr2 (List[float]): The second vector.
Returns:
- float: The cosine distance between arr1 and arr2.
inner_product_distance
Calculate the Euclidean distance between two vectors.
Arguments:
- arr1 (List[float]): The first vector.
- arr2 (List[float]): The second vector.
Returns:
- float: The Euclidean distance between arr1 and arr2.
query
Query documents in the collection.
Arguments:
query_texts
List[str] - A list of query texts.collection_name
Optional[str] - The name of the collection.n_results
int - The maximum number of results to return.distance_type
Optional[str] - Distance search type - euclidean or cosinedistance_threshold
Optional[float] - Distance threshold to limit searchesinclude_embedding
Optional[bool] - Include embedding values in QueryResults
Returns:
QueryResults
- The query results.
convert_string_to_array
Convert a string representation of an array to a list of floats.
Arguments:
- array_string (str): The string representation of the array.
Returns:
- list: A list of floats parsed from the input string. If the input is not a string, it returns the input itself.
modify
Modify metadata for the collection.
Arguments:
collection_name
- The name of the collection.metadata
- The new metadata.
Returns:
None
delete
Delete documents from the collection.
Arguments:
ids
List[ItemID] - A list of document IDs to delete.collection_name
str - The name of the collection to delete.
Returns:
None
delete_collection
Delete the entire collection.
Arguments:
collection_name
Optional[str] - The name of the collection to delete.
Returns:
None
create_collection
Create a new collection.
Arguments:
collection_name
Optional[str] - The name of the new collection.dimension
Optional[Union[str, int]] - The dimension size of the sentence embedding model
Returns:
None
PGVectorDB
A vector database that uses PGVector as the backend.
__init__
Initialize the vector database.
Note: connection_string or host + port + dbname must be specified
Arguments:
conn
- psycopg.Connection | A customer connection object to connect to the database. A connection object may include additional key/values: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRINGconnection_string
- “postgresql://username:password@hostname:port/database” | The PGVector connection string. Default is None.host
- str | The host to connect to. Default is None.port
- int | The port to connect to. Default is None.dbname
- str | The database name to connect to. Default is None.username
- str | The database username to use. Default is None.password
- str | The database user password to use. Default is None.connect_timeout
- int | The timeout to set for the connection. Default is 10.embedding_function
- Callable | The embedding function used to generate the vector representation. Default is None. SentenceTransformer(“all-MiniLM-L6-v2”).encode will be used when None. Models can be chosen from: https://huggingface.co/models?library=sentence-transformersmetadata
- dict | The metadata of the vector database. Default is None. If None, it will use thissetting
- {“hnsw:space”: “ip”, “hnsw:construction_ef”: 30, “hnsw:M”: 16}. Creates Index on table using hnsw (embedding vector_l2_ops) WITH (m = hnsw:M) ef_construction = “hnsw:construction_ef”. For more info: https://github.com/pgvector/pgvector?tab=readme-ov-file#hnsw
Returns:
None
establish_connection
Establishes a connection to a PostgreSQL database using psycopg.
Arguments:
conn
- An existing psycopg connection object. If provided, this connection will be used.connection_string
- A string containing the connection information. If provided, a new connection will be established using this string.host
- The hostname of the PostgreSQL server. Used if connection_string is not provided.port
- The port number to connect to at the server host. Used if connection_string is not provided.dbname
- The database name. Used if connection_string is not provided.username
- The username to connect as. Used if connection_string is not provided.password
- The user’s password. Used if connection_string is not provided.connect_timeout
- Maximum wait for connection, in seconds. The default is 10 seconds.
Returns:
A psycopg.Connection object representing the established connection.
Raises:
PermissionError if no credentials are supplied
psycopg.Error
- If an error occurs while trying to connect to the database.
create_collection
Create a collection in the vector database. Case 1. if the collection does not exist, create the collection. Case 2. the collection exists, if overwrite is True, it will overwrite the collection. Case 3. the collection exists and overwrite is False, if get_or_create is True, it will get the collection, otherwise it raise a ValueError.
Arguments:
collection_name
- str | The name of the collection.overwrite
- bool | Whether to overwrite the collection if it exists. Default is False.get_or_create
- bool | Whether to get the collection if it exists. Default is True.
Returns:
Collection | The collection object.
get_collection
Get the collection from the vector database.
Arguments:
collection_name
- str | The name of the collection. Default is None. If None, return the current active collection.
Returns:
Collection | The collection object.
delete_collection
Delete the collection from the vector database.
Arguments:
collection_name
- str | The name of the collection.
Returns:
None
insert_docs
Insert documents into the collection of the vector database.
Arguments:
docs
- List[Document] | A list of documents. Each document is a TypedDictDocument
.collection_name
- str | The name of the collection. Default is None.upsert
- bool | Whether to update the document if it exists. Default is False.kwargs
- Dict | Additional keyword arguments.
Returns:
None
update_docs
Update documents in the collection of the vector database.
Arguments:
docs
- List[Document] | A list of documents.collection_name
- str | The name of the collection. Default is None.
Returns:
None
delete_docs
Delete documents from the collection of the vector database.
Arguments:
ids
- List[ItemID] | A list of document ids. Each id is a typedItemID
.collection_name
- str | The name of the collection. Default is None.kwargs
- Dict | Additional keyword arguments.
Returns:
None
retrieve_docs
Retrieve documents from the collection of the vector database based on the queries.
Arguments:
queries
- List[str] | A list of queries. Each query is a string.collection_name
- str | The name of the collection. Default is None.n_results
- int | The number of relevant documents to return. Default is 10.distance_threshold
- float | The threshold for the distance score, only distance smaller than it will be returned. Don’t filter with it if < 0. Default is -1.kwargs
- Dict | Additional keyword arguments.
Returns:
QueryResults | The query results. Each query result is a list of list of tuples containing the document and the distance.
get_docs_by_ids
Retrieve documents from the collection of the vector database based on the ids.
Arguments:
ids
- List[ItemID] | A list of document ids. If None, will return all the documents. Default is None.collection_name
- str | The name of the collection. Default is None.include
- List[str] | The fields to include. Default is None. If None, will include [“metadatas”, “documents”], ids will always be included.kwargs
- dict | Additional keyword arguments.
Returns:
List[Document] | The results.