# Class AlloyDBEngine (0.15.0)

0.15.0 (latest) 0.14.0 0.12.0 0.11.0 0.10.0 0.9.3 0.8.0 0.7.0 0.6.0 0.5.0 0.4.1 0.3.0 0.2.2

    AlloyDBEngine(
        key: object,
        pool: sqlalchemy.ext.asyncio.engine.AsyncEngine,
        loop: typing.Optional[asyncio.events.AbstractEventLoop],
        thread: typing.Optional[threading.Thread],
    )

A class for managing connections to a AlloyDB database.

## Methods

### AlloyDBEngine

    AlloyDBEngine(
        key: object,
        pool: sqlalchemy.ext.asyncio.engine.AsyncEngine,
        loop: typing.Optional[asyncio.events.AbstractEventLoop],
        thread: typing.Optional[threading.Thread],
    )

PGEngine constructor.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `key` | `object` Prevent direct constructor usage. |
| `pool` | `AsyncEngine` Async engine connection pool. |
| `loop` | `Optional[asyncio.AbstractEventLoop]` Async event loop used to create the engine. |
| `thread` | `Optional[Thread]` Thread used to create the engine async. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `Exception` | If the constructor is called directly by the user. |

### _ainit_chat_history_table

    _ainit_chat_history_table(table_name: str, schema_name: str = "public") -> None

Create an AlloyDB table to save chat history messages.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` The table name to store chat history. |
| `schema_name` | `str` The schema name to store the chat history table. Default: "public". |

### _ainit_checkpoint_table

    _ainit_checkpoint_table(
        table_name: str = "checkpoints", schema_name: str = "public"
    ) -> None

Create AlloyDB tables to save checkpoints.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `schema_name` | `str` The schema name to store the checkpoint tables. Default: "public". |

### _ainit_document_table

    _ainit_document_table(
        table_name: str,
        schema_name: str = "public",
        content_column: str = "page_content",
        metadata_columns: typing.Optional[list[langchain_postgres.v2.engine.Column]] = None,
        metadata_json_column: str = "langchain_metadata",
        store_metadata: bool = True,
    ) -> None

Creates a table for storing LangChain documents.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` The PgSQL database table name. |
| `schema_name` | `str` The schema name. Default: "public". |
| `content_column` | `str` Name of the column to store document content. Default: "page_content". |
| `metadata_columns` | `Optional[list[Column]]` A list of Columns to create for custom metadata. Optional. |
| `metadata_json_column` | `str` The column to store extra metadata in JSON format. Default: "langchain_metadata". Optional. |
| `store_metadata` | `bool` Whether to store extra metadata in a metadata column if not described in 'metadata' field list (Default: True). |

### _aload_table_schema

    _aload_table_schema(
        table_name: str, schema_name: str = "public"
    ) -> sqlalchemy.sql.schema.Table

Load table schema from an existing table in a PgSQL database, potentially from a specific database schema.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `(sqlalchemy.Table)` | The loaded table, including its table schema information. |

### _create

    _create(
        project_id: str,
        region: str,
        cluster: str,
        instance: str,
        database: str,
        ip_type: typing.Union[str, google.cloud.alloydbconnector.enums.IPTypes],
        user: typing.Optional[str] = None,
        password: typing.Optional[str] = None,
        loop: typing.Optional[asyncio.events.AbstractEventLoop] = None,
        thread: typing.Optional[threading.Thread] = None,
        iam_account_email: typing.Optional[str] = None,
        engine_args: typing.Mapping = {},
    ) -> langchain_google_alloydb_pg.engine.AlloyDBEngine

Create an AlloyDBEngine from an AlloyDB instance.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `project_id` | `str` GCP project ID. |
| `region` | `str` Cloud AlloyDB instance region. |
| `cluster` | `str` Cloud AlloyDB cluster name. |
| `instance` | `str` Cloud AlloyDB instance name. |
| `database` | `str` Database name. |
| `ip_type` | `Union[str, IPTypes]` IP address type. Defaults to IPTypes.PUBLIC. |
| `user` | `Optional[str]` Cloud AlloyDB user name. Defaults to None. |
| `password` | `Optional[str]` Cloud AlloyDB user password. Defaults to None. |
| `loop` | `Optional[asyncio.AbstractEventLoop]` Async event loop used to create the engine. |
| `thread` | `Optional[Thread]` Thread used to create the engine async. |
| `iam_account_email` | `Optional[str]` IAM service account email. |
| `engine_args` | `Mapping` Additional arguments that are passed directly to `sqlalchemy.ext.asyncio.mymodule.MyClass.create_async_engine`. This can be used to specify additional parameters to the underlying pool during it's creation. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `ValueError` | Raises error if only one of 'user' or 'password' is specified. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `AlloyDBEngine` | A newly created AlloyDBEngine instance. |

### afrom_instance

    afrom_instance(
        project_id: str,
        region: str,
        cluster: str,
        instance: str,
        database: str,
        user: typing.Optional[str] = None,
        password: typing.Optional[str] = None,
        ip_type: typing.Union[
            str, google.cloud.alloydbconnector.enums.IPTypes
        ] = IPTypes.PUBLIC,
        iam_account_email: typing.Optional[str] = None,
        engine_args: typing.Mapping = {},
    ) -> langchain_google_alloydb_pg.engine.AlloyDBEngine

Create an AlloyDBEngine from an AlloyDB instance.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `project_id` | `str` GCP project ID. |
| `region` | `str` Cloud AlloyDB instance region. |
| `cluster` | `str` Cloud AlloyDB cluster name. |
| `instance` | `str` Cloud AlloyDB instance name. |
| `database` | `str` Cloud AlloyDB database name. |
| `user` | `Optional[str], optional` Cloud AlloyDB user name. Defaults to None. |
| `password` | `Optional[str], optional` Cloud AlloyDB user password. Defaults to None. |
| `ip_type` | `Union[str, IPTypes], optional` IP address type. Defaults to IPTypes.PUBLIC. |
| `iam_account_email` | `Optional[str], optional` IAM service account email. Defaults to None. |
| `engine_args` | `Mapping` Additional arguments that are passed directly to `sqlalchemy.ext.asyncio.mymodule.MyClass.create_async_engine`. This can be used to specify additional parameters to the underlying pool during it's creation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `AlloyDBEngine` | A newly created AlloyDBEngine instance. |

### ainit_chat_history_table

    ainit_chat_history_table(table_name: str, schema_name: str = "public") -> None

Create an AlloyDB table to save chat history messages.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` The table name to store chat history. |
| `schema_name` | `str` The schema name to store chat history table. Default: "public". |

### ainit_checkpoint_table

    ainit_checkpoint_table(
        table_name: str = "checkpoints", schema_name: str = "public"
    ) -> None

Create an AlloyDB table to save checkpoint messages.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `schema_name` | `str` The schema name to store checkpoint tables. Default: "public". |

### ainit_document_table

    ainit_document_table(
        table_name: str,
        schema_name: str = "public",
        content_column: str = "page_content",
        metadata_columns: typing.Optional[list[langchain_postgres.v2.engine.Column]] = None,
        metadata_json_column: str = "langchain_metadata",
        store_metadata: bool = True,
    ) -> None

Creates a table for storing LangChain documents.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` The PgSQL database table name. |
| `schema_name` | `str` The schema name. Default: "public". |
| `content_column` | `str` Name of the column to store document content. Default: "page_content". |
| `metadata_columns` | `Optional[list[Column]]` A list of SQLAlchemy Columns to create for custom metadata. Optional. |
| `metadata_json_column` | `str` The column to store extra metadata in JSON format. Default: "langchain_metadata". Optional. |
| `store_metadata` | `bool` Whether to store extra metadata in a metadata column if not described in 'metadata' field list (Default: True). |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `DuplicateTableErro` |   |

### from_connection_string

    from_connection_string(
        url: str | sqlalchemy.engine.url.URL, **kwargs: typing.Any
    ) -> langchain_google_alloydb_pg.engine.AlloyDBEngine

Create an AlloyDBEngine instance from arguments

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `url` | `Optional[str]` the URL used to connect to a database. Use url or set other arguments. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `ValueError` | If not all database url arguments are specified |

### from_engine_args

    from_engine_args(
        url: str | sqlalchemy.engine.url.URL, **kwargs: typing.Any
    ) -> langchain_google_alloydb_pg.engine.AlloyDBEngine

Create an AlloyDBEngine instance from arguments

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `url` | `Optional[str]` the URL used to connect to a database. Use url or set other arguments. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `ValueError` | If not all database url arguments are specified |

### from_instance

    from_instance(
        project_id: str,
        region: str,
        cluster: str,
        instance: str,
        database: str,
        user: typing.Optional[str] = None,
        password: typing.Optional[str] = None,
        ip_type: typing.Union[
            str, google.cloud.alloydbconnector.enums.IPTypes
        ] = IPTypes.PUBLIC,
        iam_account_email: typing.Optional[str] = None,
        engine_args: typing.Mapping = {},
    ) -> langchain_google_alloydb_pg.engine.AlloyDBEngine

Create an AlloyDBEngine from an AlloyDB instance.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `project_id` | `str` GCP project ID. |
| `region` | `str` Cloud AlloyDB instance region. |
| `cluster` | `str` Cloud AlloyDB cluster name. |
| `instance` | `str` Cloud AlloyDB instance name. |
| `database` | `str` Database name. |
| `user` | `Optional[str]` Cloud AlloyDB user name. Defaults to None. |
| `password` | `Optional[str]` Cloud AlloyDB user password. Defaults to None. |
| `ip_type` | `Union[str, IPTypes], optional` IP address type. Defaults to IPTypes.PUBLIC. |
| `iam_account_email` | `Optional[str], optional` IAM service account email. Defaults to None. |
| `engine_args` | `Mapping` Additional arguments that are passed directly to `sqlalchemy.ext.asyncio.mymodule.MyClass.create_async_engine`. This can be used to specify additional parameters to the underlying pool during it's creation. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `AlloyDBEngine` | A newly created AlloyDBEngine instance. |

### init_chat_history_table

    init_chat_history_table(table_name: str, schema_name: str = "public") -> None

Create a Cloud SQL table to store chat history.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` Table name to store chat history. |
| `schema_name` | `str` The schema name to store chat history table. Default: "public". |

### init_checkpoint_table

    init_checkpoint_table(
        table_name: str = "checkpoints", schema_name: str = "public"
    ) -> None

Create Cloud SQL tables to store checkpoints.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `schema_name` | `str` The schema name to store checkpoint tables. Default: "public". |

### init_document_table

    init_document_table(
        table_name: str,
        schema_name: str = "public",
        content_column: str = "page_content",
        metadata_columns: typing.Optional[list[langchain_postgres.v2.engine.Column]] = None,
        metadata_json_column: str = "langchain_metadata",
        store_metadata: bool = True,
    ) -> None

Creates a table for storing LangChain documents.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `table_name` | `str` The PgSQL database table name. |
| `schema_name` | `str` The schema name to store the PgSQL database table. Default: "public". |
| `content_column` | `str` Name of the column to store document content. Default: "page_content". |
| `metadata_columns` | `Optional[list[Column]]` A list of SQLAlchemy Columns to create for custom metadata. Optional. |
| `metadata_json_column` | `str` The column to store extra metadata in JSON format. Default: "langchain_metadata". Optional. |
| `store_metadata` | `bool` Whether to store extra metadata in a metadata column if not described in 'metadata' field list (Default: True). |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `DuplicateTableErro` |   |