> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ag2.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# autogen.agentchat.contrib.vectordb.pgvectordb.PGVectorDB

<h2 id="autogen.agentchat.contrib.vectordb.pgvectordb.PGVectorDB" class="doc doc-heading">
  <code class="doc-symbol doc-symbol-heading doc-symbol-class" />

  <span class="doc doc-object-name doc-class-name">PGVectorDB</span>
</h2>

```python theme={null}
PGVectorDB(
    *,
    conn: ForwardRef('Connection') | None = None,
    connection_string: str | None = None,
    host: str | None = None,
    port: int | str | None = None,
    dbname: str | None = None,
    username: str | None = None,
    password: str | None = None,
    connect_timeout: int | None = 10,
    embedding_function: Callable = None,
    metadata: dict[str, Any] | None = None
)
```

A vector database that uses PGVector as the backend.<br />Initialize the vector database.<br />Note: connection\_string or host + port + dbname must be specified

<b>Parameters:</b>

| Name                 | Description                                                                     |
| -------------------- | ------------------------------------------------------------------------------- |
| `conn`               | **Type:** ForwardRef('psycopg.Connection') \| None<br /><br />**Default:** None |
| `connection_string`  | **Type:** str \| None<br /><br />**Default:** None                              |
| `host`               | **Type:** str \| None<br /><br />**Default:** None                              |
| `port`               | **Type:** int \| str \| None<br /><br />**Default:** None                       |
| `dbname`             | **Type:** str \| None<br /><br />**Default:** None                              |
| `username`           | **Type:** str \| None<br /><br />**Default:** None                              |
| `password`           | **Type:** str \| None<br /><br />**Default:** None                              |
| `connect_timeout`    | **Type:** int \| None<br /><br />**Default:** 10                                |
| `embedding_function` | **Type:** Callable<br /><br />**Default:** None                                 |
| `metadata`           | **Type:** dict\[str, typing.Any] \| None<br /><br />**Default:** None           |

### Class Attributes

<code class="doc-symbol doc-symbol-heading doc-symbol-attribute" />

#### active\_collection

<br />

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-attribute" />

#### embedding\_function

<br />

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-attribute" />

#### type

<br />

<br />

### Instance Methods

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### create\_collection

```python theme={null}
create_collection(
    self,
    collection_name: str,
    overwrite: bool = False,
    get_or_create: bool = True
) -> Collection
```

Create a collection in the vector database.<br />Case 1. if the collection does not exist, create the collection.<br />Case 2. the collection exists, if overwrite is True, it will overwrite the collection.<br />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.<br />

<b>Parameters:</b>

| Name              | Description |                                                                                                                                        |
| ----------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `collection_name` | str         | The name of the collection.<br /><br />**Type:** str                                                                                   |
| `overwrite`       | bool        | Whether to overwrite the collection if it exists.<br /><br />Default is False.<br /><br />**Type:** bool<br /><br />**Default:** False |
| `get_or_create`   | bool        | Whether to get the collection if it exists.<br /><br />Default is True.<br /><br />**Type:** bool<br /><br />**Default:** True         |

<b>Returns:</b>

| Type                                                                                       | Description                          |
| ------------------------------------------------------------------------------------------ | ------------------------------------ |
| [Collection](/docs/api-reference/autogen/agentchat/contrib/vectordb/pgvectordb/Collection) | Collection \| The collection object. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### delete\_collection

```python theme={null}
delete_collection(self, collection_name: str) -> None
```

Delete the collection from the vector database.<br />

<b>Parameters:</b>

| Name              | Description |                                                      |
| ----------------- | ----------- | ---------------------------------------------------- |
| `collection_name` | str         | The name of the collection.<br /><br />**Type:** str |

<b>Returns:</b>

| Type | Description |
| ---- | ----------- |
| None | None        |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### delete\_docs

```python theme={null}
delete_docs(
    self,
    ids: list[str | int],
    collection_name: str = None
) -> None
```

Delete documents from the collection of the vector database.<br />

<b>Parameters:</b>

| Name              | Description   |                                                                                                               |
| ----------------- | ------------- | ------------------------------------------------------------------------------------------------------------- |
| `ids`             | List\[ItemID] | A list of document ids.<br /><br />Each id is a typed `ItemID`.<br /><br />**Type:** list\[str \| int]        |
| `collection_name` | str           | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None |

<b>Returns:</b>

| Type | Description |
| ---- | ----------- |
| None | None        |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### establish\_connection

```python theme={null}
establish_connection(
    self,
    conn: ForwardRef('Connection') | None = None,
    connection_string: str | None = None,
    host: str | None = None,
    port: int | str | None = None,
    dbname: str | None = None,
    username: str | None = None,
    password: str | None = None,
    connect_timeout: int | None = 10
) -> Connection
```

Establishes a connection to a PostgreSQL database using psycopg.<br />

<b>Parameters:</b>

| Name                | Description                                                                                                                                                                                   |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `conn`              | An existing psycopg connection object.<br /><br />If provided, this connection will be used.<br /><br />**Type:** ForwardRef('psycopg.Connection') \| None<br /><br />**Default:** None       |
| `connection_string` | A string containing the connection information.<br /><br />If provided, a new connection will be established using this string.<br /><br />**Type:** str \| None<br /><br />**Default:** None |
| `host`              | The hostname of the PostgreSQL server.<br /><br />Used if connection\_string is not provided.<br /><br />**Type:** str \| None<br /><br />**Default:** None                                   |
| `port`              | The port number to connect to at the server host.<br /><br />Used if connection\_string is not provided.<br /><br />**Type:** int \| str \| None<br /><br />**Default:** None                 |
| `dbname`            | The database name.<br /><br />Used if connection\_string is not provided.<br /><br />**Type:** str \| None<br /><br />**Default:** None                                                       |
| `username`          | The username to connect as.<br /><br />Used if connection\_string is not provided.<br /><br />**Type:** str \| None<br /><br />**Default:** None                                              |
| `password`          | The user's password.<br /><br />Used if connection\_string is not provided.<br /><br />**Type:** str \| None<br /><br />**Default:** None                                                     |
| `connect_timeout`   | Maximum wait for connection, in seconds.<br /><br />The default is 10 seconds.<br /><br />**Type:** int \| None<br /><br />**Default:** 10                                                    |

<b>Returns:</b>

| Type               | Description                                                          |
| ------------------ | -------------------------------------------------------------------- |
| psycopg.Connection | A psycopg.Connection object representing the established connection. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### get\_collection

```python theme={null}
get_collection(self, collection_name: str = None) -> Collection
```

Get the collection from the vector database.<br />

<b>Parameters:</b>

| Name              | Description |                                                                                                                                                                         |
| ----------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `collection_name` | str         | The name of the collection.<br /><br />Default is None.<br /><br />If None, return the current active collection.<br /><br />**Type:** str<br /><br />**Default:** None |

<b>Returns:</b>

| Type                                                                                       | Description                          |
| ------------------------------------------------------------------------------------------ | ------------------------------------ |
| [Collection](/docs/api-reference/autogen/agentchat/contrib/vectordb/pgvectordb/Collection) | Collection \| The collection object. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### get\_docs\_by\_ids

```python theme={null}
get_docs_by_ids(
    self,
    ids: list[str | int] = None,
    collection_name: str = None,
    include=None,
    **kwargs
) -> list[Document]
```

Retrieve documents from the collection of the vector database based on the ids.<br />

<b>Parameters:</b>

| Name              | Description   |                                                                                                                                                                            |
| ----------------- | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ids`             | List\[ItemID] | A list of document ids.<br /><br />If None, will return all the documents.<br /><br />Default is None.<br /><br />**Type:** list\[str \| int]<br /><br />**Default:** None |
| `collection_name` | str           | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                                                              |
| `include=None`    |               |                                                                                                                                                                            |
| `**kwargs`        |               |                                                                                                                                                                            |

<b>Returns:</b>

| Type                                                                                    | Description                     |
| --------------------------------------------------------------------------------------- | ------------------------------- |
| list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] | List\[Document] \| The results. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### insert\_docs

```python theme={null}
insert_docs(
    self,
    docs: list[Document],
    collection_name: str = None,
    upsert: bool = False
) -> None
```

Insert documents into the collection of the vector database.<br />

<b>Parameters:</b>

| Name              | Description     |                                                                                                                                                                                       |
| ----------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `docs`            | List\[Document] | A list of documents.<br /><br />Each document is a TypedDict `Document`.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] |
| `collection_name` | str             | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                                                                         |
| `upsert`          | bool            | Whether to update the document if it exists.<br /><br />Default is False.<br /><br />**Type:** bool<br /><br />**Default:** False                                                     |

<b>Returns:</b>

| Type | Description |
| ---- | ----------- |
| None | None        |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### retrieve\_docs

```python theme={null}
retrieve_docs(
    self,
    queries: list[str],
    collection_name: str = None,
    n_results: int = 10,
    distance_threshold: float = -1
) -> list[list[tuple[Document, float]]]
```

Retrieve documents from the collection of the vector database based on the queries.<br />

<b>Parameters:</b>

| Name                 | Description |                                                                                                                                                                                                                |
| -------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `queries`            | List\[str]  | A list of queries.<br /><br />Each query is a string.<br /><br />**Type:** list\[str]                                                                                                                          |
| `collection_name`    | str         | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                                                                                                  |
| `n_results`          | int         | The number of relevant documents to return.<br /><br />Default is 10.<br /><br />**Type:** int<br /><br />**Default:** 10                                                                                      |
| `distance_threshold` | float       | The threshold for the distance score, only distance smaller than it will be returned.<br /><br />Don't filter with it if ` 0`.<br /><br />Default is -1.<br /><br />**Type:** float<br /><br />**Default:** -1 |

<b>Returns:</b>

| Type                                                                                                          | Description                                                                                                                |
| ------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| list\[list\[tuple\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document), float]]] | QueryResults \| The query results. Each query result is a list of list of tuples containing the document and the distance. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### update\_docs

```python theme={null}
update_docs(
    self,
    docs: list[Document],
    collection_name: str = None
) -> None
```

Update documents in the collection of the vector database.<br />

<b>Parameters:</b>

| Name              | Description     |                                                                                                                                   |
| ----------------- | --------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `docs`            | List\[Document] | A list of documents.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] |
| `collection_name` | str             | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                     |

<b>Returns:</b>

| Type | Description |
| ---- | ----------- |
| None | None        |

<br />
