> ## 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.Collection

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

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

```python theme={null}
Collection(
    client: Any | None = None,
    collection_name: str = 'ag2-docs',
    embedding_function: Callable[..., Any] | None = None,
    metadata: Any | None = None,
    get_or_create: Any | None = None
)
```

A Collection object for PGVector.<br />Attributes: <br />    client: The PGVector client.<br />    collection\_name (str): The name of the collection. Default is "documents".<br />    embedding\_function (Callable): The embedding function used to generate the vector representation.<br />        Default is None. SentenceTransformer("all-MiniLM-L6-v2").encode will be used when None.<br />        Models can be chosen from: <br />        [https://huggingface.co/models?library=sentence-transformers](https://huggingface.co/models?library=sentence-transformers)
metadata (Optional\[dict\[str, Any]]): The metadata of the collection.<br />    get\_or\_create (Optional): The flag indicating whether to get or create the collection.<br />Initialize the Collection object.<br />

<b>Parameters:</b>

| Name                 | Description                                                        |
| -------------------- | ------------------------------------------------------------------ |
| `client`             | **Type:** Any \| None<br /><br />**Default:** None                 |
| `collection_name`    | **Type:** str<br /><br />**Default:** 'ag2-docs'                   |
| `embedding_function` | **Type:** Callable\[..., Any] \| None<br /><br />**Default:** None |
| `metadata`           | **Type:** Any \| None<br /><br />**Default:** None                 |
| `get_or_create`      | **Type:** Any \| None<br /><br />**Default:** None                 |

### Instance Methods

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

#### add

```python theme={null}
add(
    self,
    ids: list[str | int],
    documents: list[Document] | None,
    embeddings: list[Any] | None = None,
    metadatas: list[Any] | None = None
) -> None
```

Add documents to the collection.<br />

<b>Parameters:</b>

| Name         | Description                                                                                                                               |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| `ids`        | A list of document IDs.<br /><br />**Type:** list\[str \| int]                                                                            |
| `documents`  | A list of documents.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] \| None |
| `embeddings` | A list of document embeddings.<br /><br />Optional<br /><br />**Type:** list\[typing.Any] \| None<br /><br />**Default:** None            |
| `metadatas`  | A list of document metadatas.<br /><br />Optional<br /><br />**Type:** list\[typing.Any] \| None<br /><br />**Default:** None             |

<b>Returns:</b>

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

<br />

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

#### convert\_string\_to\_array

```python theme={null}
convert_string_to_array(array_string: str) -> list[float]
```

Convert a string representation of an array to a list of floats.<br />Parameters: <br />- array\_string (str): The string representation of the array.<br />Returns: <br />- list: A list of floats parsed from the input string. If the input is
not a string, it returns the input itself.

<b>Parameters:</b>

| Name           | Description   |
| -------------- | ------------- |
| `array_string` | **Type:** str |

<b>Returns:</b>

| 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. |

<br />

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

#### cosine\_distance

```python theme={null}
cosine_distance(arr1: list[float], arr2: list[float]) -> float
```

Calculate the cosine distance between two vectors.<br />Parameters: <br />- arr1 (List\[float]): The first vector.<br />- arr2 (List\[float]): The second vector.<br />Returns: <br />- float: The cosine distance between arr1 and arr2.

<b>Parameters:</b>

| Name   | Description            |
| ------ | ---------------------- |
| `arr1` | **Type:** list\[float] |
| `arr2` | **Type:** list\[float] |

<b>Returns:</b>

| Type  | Description                                         |
| ----- | --------------------------------------------------- |
| float | - float: The cosine distance between arr1 and arr2. |

<br />

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

#### count

```python theme={null}
count(self) -> int
```

Get the total number of documents in the collection.<br />Returns: <br />    int: The total number of documents.

<b>Returns:</b>

| Type | Description                         |
| ---- | ----------------------------------- |
| int  | int: The total number of documents. |

<br />

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

#### create\_collection

```python theme={null}
create_collection(
    self,
    collection_name: str | None = None,
    dimension: int | str | None = None
) -> None
```

Create a new collection.<br />

<b>Parameters:</b>

| Name              | Description                                                                                                             |
| ----------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `collection_name` | The name of the new collection.<br /><br />**Type:** str \| None<br /><br />**Default:** None                           |
| `dimension`       | The dimension size of the sentence embedding model<br /><br />**Type:** int \| str \| None<br /><br />**Default:** None |

<b>Returns:</b>

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

<br />

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

#### delete

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

Delete documents from the collection.<br />

<b>Parameters:</b>

| Name              | Description                                                                                         |
| ----------------- | --------------------------------------------------------------------------------------------------- |
| `ids`             | A list of document IDs to delete.<br /><br />**Type:** list\[str \| int]                            |
| `collection_name` | The name of the collection to delete.<br /><br />**Type:** str \| None<br /><br />**Default:** None |

<b>Returns:</b>

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

<br />

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

#### delete\_collection

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

Delete the entire collection.<br />

<b>Parameters:</b>

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

<b>Returns:</b>

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

<br />

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

#### euclidean\_distance

```python theme={null}
euclidean_distance(arr1: list[float], arr2: list[float]) -> float
```

Calculate the Euclidean distance between two vectors.<br />Parameters: <br />- arr1 (List\[float]): The first vector.<br />- arr2 (List\[float]): The second vector.<br />Returns: <br />- float: The Euclidean distance between arr1 and arr2.

<b>Parameters:</b>

| Name   | Description            |
| ------ | ---------------------- |
| `arr1` | **Type:** list\[float] |
| `arr2` | **Type:** list\[float] |

<b>Returns:</b>

| Type  | Description                                            |
| ----- | ------------------------------------------------------ |
| float | - float: The Euclidean distance between arr1 and arr2. |

<br />

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

#### get

```python theme={null}
get(
    self,
    ids: str | None = None,
    include: str | None = None,
    where: str | None = None,
    limit: int | str | None = None,
    offset: int | str | None = None
) -> list[Document]
```

Retrieve documents from the collection.<br />

<b>Parameters:</b>

| Name      | Description                                                                                                       |
| --------- | ----------------------------------------------------------------------------------------------------------------- |
| `ids`     | A list of document IDs.<br /><br />**Type:** str \| None<br /><br />**Default:** None                             |
| `include` | The fields to include.<br /><br />**Type:** str \| None<br /><br />**Default:** None                              |
| `where`   | Additional filtering criteria.<br /><br />**Type:** str \| None<br /><br />**Default:** None                      |
| `limit`   | The maximum number of documents to retrieve.<br /><br />**Type:** int \| str \| None<br /><br />**Default:** None |
| `offset`  | The offset for pagination.<br /><br />**Type:** int \| str \| None<br /><br />**Default:** None                   |

<b>Returns:</b>

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

<br />

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

#### inner\_product\_distance

```python theme={null}
inner_product_distance(arr1: list[float], arr2: list[float]) -> float
```

Calculate the Euclidean distance between two vectors.<br />Parameters: <br />- arr1 (List\[float]): The first vector.<br />- arr2 (List\[float]): The second vector.<br />Returns: <br />- float: The Euclidean distance between arr1 and arr2.

<b>Parameters:</b>

| Name   | Description            |
| ------ | ---------------------- |
| `arr1` | **Type:** list\[float] |
| `arr2` | **Type:** list\[float] |

<b>Returns:</b>

| Type  | Description                                            |
| ----- | ------------------------------------------------------ |
| float | - float: The Euclidean distance between arr1 and arr2. |

<br />

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

#### modify

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

Modify metadata for the collection.<br />

<b>Parameters:</b>

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

<b>Returns:</b>

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

<br />

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

#### query

```python theme={null}
query(
    self,
    query_texts: list[str],
    collection_name: str | None = None,
    n_results: int | None = 10,
    distance_type: str | None = 'euclidean',
    distance_threshold: float | None = -1,
    include_embedding: bool | None = False
) -> list[list[tuple[Document, float]]]
```

Query documents in the collection.<br />

<b>Parameters:</b>

| Name                 | Description                                                                                                     |
| -------------------- | --------------------------------------------------------------------------------------------------------------- |
| `query_texts`        | A list of query texts.<br /><br />**Type:** list\[str]                                                          |
| `collection_name`    | The name of the collection.<br /><br />**Type:** str \| None<br /><br />**Default:** None                       |
| `n_results`          | The maximum number of results to return.<br /><br />**Type:** int \| None<br /><br />**Default:** 10            |
| `distance_type`      | Distance search type - euclidean or cosine<br /><br />**Type:** str \| None<br /><br />**Default:** 'euclidean' |
| `distance_threshold` | Distance threshold to limit searches<br /><br />**Type:** float \| None<br /><br />**Default:** -1              |
| `include_embedding`  | Include embedding values in QueryResults<br /><br />**Type:** bool \| None<br /><br />**Default:** False        |

<b>Returns:</b>

| Type                                                                                                          | Description                      |
| ------------------------------------------------------------------------------------------------------------- | -------------------------------- |
| list\[list\[tuple\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document), float]]] | QueryResults: The query results. |

<br />

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

#### set\_collection\_name

```python theme={null}
set_collection_name(self, collection_name) -> str
```

<b>Parameters:</b>

| Name              | Description |
| ----------------- | ----------- |
| `collection_name` |             |

<br />

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

#### table\_exists

```python theme={null}
table_exists(self, table_name: str) -> bool
```

Check if a table exists in the PostgreSQL database.<br />

<b>Parameters:</b>

| Name         | Description                                              |
| ------------ | -------------------------------------------------------- |
| `table_name` | The name of the table to check.<br /><br />**Type:** str |

<b>Returns:</b>

| Type | Description                                      |
| ---- | ------------------------------------------------ |
| bool | bool: True if the table exists, False otherwise. |

<br />

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

#### update

```python theme={null}
update(
    self,
    ids: list[str],
    embeddings: list[Any],
    metadatas: list[Any],
    documents: list[Document]
) -> None
```

Update documents in the collection.<br />

<b>Parameters:</b>

| Name         | Description                                                                                                                       |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------- |
| `ids`        | A list of document IDs.<br /><br />**Type:** list\[str]                                                                           |
| `embeddings` | A list of document embeddings.<br /><br />**Type:** list\[typing.Any]                                                             |
| `metadatas`  | A list of document metadatas.<br /><br />**Type:** list\[typing.Any]                                                              |
| `documents`  | A list of documents.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] |

<b>Returns:</b>

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

<br />

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

#### upsert

```python theme={null}
upsert(
    self,
    ids: list[str | int],
    documents: list[Document],
    embeddings: list[Any] | None = None,
    metadatas: list[Any] | None = None
) -> None
```

Upsert documents into the collection.<br />

<b>Parameters:</b>

| Name         | Description                                                                                                                       |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------- |
| `ids`        | A list of document IDs.<br /><br />**Type:** list\[str \| int]                                                                    |
| `documents`  | A list of documents.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] |
| `embeddings` | A list of document embeddings.<br /><br />**Type:** list\[typing.Any] \| None<br /><br />**Default:** None                        |
| `metadatas`  | A list of document metadatas.<br /><br />**Type:** list\[typing.Any] \| None<br /><br />**Default:** None                         |

<b>Returns:</b>

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

<br />
