autogen.agentchat.contrib.vectordb.pgvectordb.Collection
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-transformers
metadata (Optional[dict[str, Any]]): The metadata of the collection.
get_or_create (Optional): The flag indicating whether to get or create the collection.
Initialize the Collection object.
Name | Description |
---|---|
client | Type: Any | None Default: None |
collection_name | Type: str Default: ‘ag2-docs’ |
embedding_function | Type: Callable[…, Any] | None Default: None |
metadata | Type: Any | None Default: None |
get_or_create | Type: Any | None Default: None |
Instance Methods
add
Add documents to the collection.
Name | Description |
---|---|
ids | A list of document IDs. Type: list[str | int] |
documents | A list of documents. Type: list[Document] | None |
embeddings | A list of document embeddings. Optional Type: list[typing.Any] | None Default: None |
metadatas | A list of document metadatas. Optional Type: list[typing.Any] | None Default: None |
Type | Description |
---|---|
None | None |
convert_string_to_array
Convert a string representation of an array to a list of floats.
Parameters:
- 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.
Name | Description |
---|---|
array_string | Type: str |
Type | Description |
---|---|
list[float] | - list: A list of floats parsed from the input string. If the input is not a string, it returns the input itself. |
cosine_distance
Calculate the cosine distance between two vectors.
Parameters:
- arr1 (List[float]): The first vector.
- arr2 (List[float]): The second vector.
Returns:
- float: The cosine distance between arr1 and arr2.
Name | Description |
---|---|
arr1 | Type: list[float] |
arr2 | Type: list[float] |
Type | Description |
---|---|
float | - float: The cosine distance between arr1 and arr2. |
count
Get the total number of documents in the collection.
Returns:
int: The total number of documents.
Type | Description |
---|---|
int | int: The total number of documents. |
create_collection
Create a new collection.
Name | Description |
---|---|
collection_name | The name of the new collection. Type: str | None Default: None |
dimension | The dimension size of the sentence embedding model Type: int | str | None Default: None |
Type | Description |
---|---|
None | None |
delete
Delete documents from the collection.
Name | Description |
---|---|
ids | A list of document IDs to delete. Type: list[str | int] |
collection_name | The name of the collection to delete. Type: str | None Default: None |
Type | Description |
---|---|
None | None |
delete_collection
Delete the entire collection.
Name | Description |
---|---|
collection_name | The name of the collection to delete. Type: str | None Default: None |
Type | Description |
---|---|
None | None |
euclidean_distance
Calculate the Euclidean distance between two vectors.
Parameters:
- arr1 (List[float]): The first vector.
- arr2 (List[float]): The second vector.
Returns:
- float: The Euclidean distance between arr1 and arr2.
Name | Description |
---|---|
arr1 | Type: list[float] |
arr2 | Type: list[float] |
Type | Description |
---|---|
float | - float: The Euclidean distance between arr1 and arr2. |
get
Retrieve documents from the collection.
Name | Description |
---|---|
ids | A list of document IDs. Type: str | None Default: None |
include | The fields to include. Type: str | None Default: None |
where | Additional filtering criteria. Type: str | None Default: None |
limit | The maximum number of documents to retrieve. Type: int | str | None Default: None |
offset | The offset for pagination. Type: int | str | None Default: None |
Type | Description |
---|---|
list[Document] | List: The retrieved documents. |
inner_product_distance
Calculate the Euclidean distance between two vectors.
Parameters:
- arr1 (List[float]): The first vector.
- arr2 (List[float]): The second vector.
Returns:
- float: The Euclidean distance between arr1 and arr2.
Name | Description |
---|---|
arr1 | Type: list[float] |
arr2 | Type: list[float] |
Type | Description |
---|---|
float | - float: The Euclidean distance between arr1 and arr2. |
modify
Modify metadata for the collection.
Name | Description |
---|---|
metadata | The new metadata. |
collection_name | The name of the collection. Type: str | None Default: None |
Type | Description |
---|---|
None | None |
query
Query documents in the collection.
Name | Description |
---|---|
query_texts | A list of query texts. Type: list[str] |
collection_name | The name of the collection. Type: str | None Default: None |
n_results | The maximum number of results to return. Type: int | None Default: 10 |
distance_type | Distance search type - euclidean or cosine Type: str | None Default: ‘euclidean’ |
distance_threshold | Distance threshold to limit searches Type: float | None Default: -1 |
include_embedding | Include embedding values in QueryResults Type: bool | None Default: False |
Type | Description |
---|---|
list[list[tuple[Document, float]]] | QueryResults: The query results. |
set_collection_name
Name | Description |
---|---|
collection_name |
table_exists
Check if a table exists in the PostgreSQL database.
Name | Description |
---|---|
table_name | The name of the table to check. Type: str |
Type | Description |
---|---|
bool | bool: True if the table exists, False otherwise. |
update
Update documents in the collection.
Name | Description |
---|---|
ids | A list of document IDs. Type: list[str] |
embeddings | A list of document embeddings. Type: list[typing.Any] |
metadatas | A list of document metadatas. Type: list[typing.Any] |
documents | A list of documents. Type: list[Document] |
Type | Description |
---|---|
None | None |
upsert
Upsert documents into the collection.
Name | Description |
---|---|
ids | A list of document IDs. Type: list[str | int] |
documents | A list of documents. Type: list[Document] |
embeddings | A list of document embeddings. Type: list[typing.Any] | None Default: None |
metadatas | A list of document metadatas. Type: list[typing.Any] | None Default: None |
Type | Description |
---|---|
None | None |