autogen.agentchat.contrib.vectordb.pgvectordb.PGVectorDB
PGVectorDB
A vector database that uses PGVector as the backend.
Initialize the vector database.
Note: connection_string or host + port + dbname must be specified
Name | Description |
---|---|
conn | Type: ForwardRef(‘psycopg.Connection’) | None Default: None |
connection_string | Type: str | None Default: None |
host | Type: str | None Default: None |
port | Type: int | str | None Default: None |
dbname | Type: str | None Default: None |
username | Type: str | None Default: None |
password | Type: str | None Default: None |
connect_timeout | Type: int | None Default: 10 |
embedding_function | Type: Callable Default: None |
metadata | Type: dict[str, typing.Any] | None Default: None |
Class Attributes
active_collection
embedding_function
type
Instance Methods
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.
Name | Description | |
---|---|---|
collection_name | str | The name of the collection. Type: str |
overwrite | bool | Whether to overwrite the collection if it exists. Default is False. Type: bool Default: False |
get_or_create | bool | Whether to get the collection if it exists. Default is True. Type: bool Default: True |
Type | Description |
---|---|
Collection | Collection | The collection object. |
delete_collection
Delete the collection from the vector database.
Name | Description | |
---|---|---|
collection_name | str | The name of the collection. Type: str |
Type | Description |
---|---|
None | None |
delete_docs
Delete documents from the collection of the vector database.
Name | Description | |
---|---|---|
ids | List[ItemID] | A list of document ids. Each id is a typed ItemID .Type: list[str | int] |
collection_name | str | The name of the collection. Default is None. Type: str Default: None |
Type | Description |
---|---|
None | None |
establish_connection
Establishes a connection to a PostgreSQL database using psycopg.
Name | Description |
---|---|
conn | An existing psycopg connection object. If provided, this connection will be used. Type: ForwardRef(‘psycopg.Connection’) | None Default: None |
connection_string | A string containing the connection information. If provided, a new connection will be established using this string. Type: str | None Default: None |
host | The hostname of the PostgreSQL server. Used if connection_string is not provided. Type: str | None Default: None |
port | The port number to connect to at the server host. Used if connection_string is not provided. Type: int | str | None Default: None |
dbname | The database name. Used if connection_string is not provided. Type: str | None Default: None |
username | The username to connect as. Used if connection_string is not provided. Type: str | None Default: None |
password | The user’s password. Used if connection_string is not provided. Type: str | None Default: None |
connect_timeout | Maximum wait for connection, in seconds. The default is 10 seconds. Type: int | None Default: 10 |
Type | Description |
---|---|
psycopg.Connection | A psycopg.Connection object representing the established connection. |
get_collection
Get the collection from the vector database.
Name | Description | |
---|---|---|
collection_name | str | The name of the collection. Default is None. If None, return the current active collection. Type: str Default: None |
Type | Description |
---|---|
Collection | Collection | The collection object. |
get_docs_by_ids
Retrieve documents from the collection of the vector database based on the ids.
Name | Description | |
---|---|---|
ids | List[ItemID] | A list of document ids. If None, will return all the documents. Default is None. Type: list[str | int] Default: None |
collection_name | str | The name of the collection. Default is None. Type: str Default: None |
include=None | ||
**kwargs |
Type | Description |
---|---|
list[Document] | List[Document] | The results. |
insert_docs
Insert documents into the collection of the vector database.
Name | Description | |
---|---|---|
docs | List[Document] | A list of documents. Each document is a TypedDict Document .Type: list[Document] |
collection_name | str | The name of the collection. Default is None. Type: str Default: None |
upsert | bool | Whether to update the document if it exists. Default is False. Type: bool Default: False |
Type | Description |
---|---|
None | None |
retrieve_docs
Retrieve documents from the collection of the vector database based on the queries.
Name | Description | |
---|---|---|
queries | List[str] | A list of queries. Each query is a string. Type: list[str] |
collection_name | str | The name of the collection. Default is None. Type: str Default: None |
n_results | int | The number of relevant documents to return. Default is 10. Type: int Default: 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. Type: float Default: -1 |
Type | Description |
---|---|
list[list[tuple[Document, float]]] | QueryResults | The query results. Each query result is a list of list of tuples containing the document and the distance. |
update_docs
Update documents in the collection of the vector database.
Name | Description | |
---|---|---|
docs | List[Document] | A list of documents. Type: list[Document] |
collection_name | str | The name of the collection. Default is None. Type: str Default: None |
Type | Description |
---|---|
None | None |