langchain_astradb.chat_message_histories.AstraDBChatMessageHistory¶

class langchain_astradb.chat_message_histories.AstraDBChatMessageHistory(*, session_id: str, collection_name: str = 'langchain_message_store', token: str | TokenProvider | None = None, api_endpoint: str | None = None, environment: str | None = None, astra_db_client: AstraDB | None = None, async_astra_db_client: AsyncAstraDB | None = None, namespace: str | None = None, setup_mode: SetupMode = SetupMode.SYNC, pre_delete_collection: bool = False)[source]¶

Chat message history that stores history in Astra DB.

Parameters
  • session_id (str) – arbitrary key that is used to store the messages of a single chat session.

  • collection_name (str) – name of the Astra DB collection to create/use.

  • token (str | TokenProvider | None) – API token for Astra DB usage, either in the form of a string or a subclass of astrapy.authentication.TokenProvider. If not provided, the environment variable ASTRA_DB_APPLICATION_TOKEN is inspected.

  • api_endpoint (str | None) – full URL to the API endpoint, such as https://<DB-ID>-us-east1.apps.astra.datastax.com. If not provided, the environment variable ASTRA_DB_API_ENDPOINT is inspected.

  • environment (str | None) – a string specifying the environment of the target Data API. If omitted, defaults to “prod” (Astra DB production). Other values are in astrapy.constants.Environment enum class.

  • astra_db_client (AstraDB | None) – DEPRECATED starting from version 0.3.5. Please use ‘token’, ‘api_endpoint’ and optionally ‘environment’. you can pass an already-created ‘astrapy.db.AstraDB’ instance (alternatively to ‘token’, ‘api_endpoint’ and ‘environment’).

  • async_astra_db_client (AsyncAstraDB | None) – DEPRECATED starting from version 0.3.5. Please use ‘token’, ‘api_endpoint’ and optionally ‘environment’. you can pass an already-created ‘astrapy.db.AsyncAstraDB’ instance (alternatively to ‘token’, ‘api_endpoint’ and ‘environment’).

  • namespace (str | None) – namespace (aka keyspace) where the collection is created. If not provided, the environment variable ASTRA_DB_KEYSPACE is inspected. Defaults to the database’s “default namespace”.

  • setup_mode (SetupMode) – mode used to create the Astra DB collection (SYNC, ASYNC or OFF).

  • pre_delete_collection (bool) – whether to delete the collection.

Attributes

messages

Retrieve all session messages from DB.

Methods

__init__(*, session_id[, collection_name, ...])

Chat message history that stores history in Astra DB.

aadd_messages(messages)

Async add a list of messages.

aclear()

Async remove all messages from the store

add_ai_message(message)

Convenience method for adding an AI message string to the store.

add_message(message)

Add a Message object to the store.

add_messages(messages)

Add a list of messages.

add_user_message(message)

Convenience method for adding a human message string to the store.

aget_messages()

Async version of getting messages.

clear()

Remove all messages from the store

__init__(*, session_id: str, collection_name: str = 'langchain_message_store', token: str | TokenProvider | None = None, api_endpoint: str | None = None, environment: str | None = None, astra_db_client: AstraDB | None = None, async_astra_db_client: AsyncAstraDB | None = None, namespace: str | None = None, setup_mode: SetupMode = SetupMode.SYNC, pre_delete_collection: bool = False) None[source]¶

Chat message history that stores history in Astra DB.

Parameters
  • session_id (str) – arbitrary key that is used to store the messages of a single chat session.

  • collection_name (str) – name of the Astra DB collection to create/use.

  • token (str | TokenProvider | None) – API token for Astra DB usage, either in the form of a string or a subclass of astrapy.authentication.TokenProvider. If not provided, the environment variable ASTRA_DB_APPLICATION_TOKEN is inspected.

  • api_endpoint (str | None) – full URL to the API endpoint, such as https://<DB-ID>-us-east1.apps.astra.datastax.com. If not provided, the environment variable ASTRA_DB_API_ENDPOINT is inspected.

  • environment (str | None) – a string specifying the environment of the target Data API. If omitted, defaults to “prod” (Astra DB production). Other values are in astrapy.constants.Environment enum class.

  • astra_db_client (AstraDB | None) – DEPRECATED starting from version 0.3.5. Please use ‘token’, ‘api_endpoint’ and optionally ‘environment’. you can pass an already-created ‘astrapy.db.AstraDB’ instance (alternatively to ‘token’, ‘api_endpoint’ and ‘environment’).

  • async_astra_db_client (AsyncAstraDB | None) – DEPRECATED starting from version 0.3.5. Please use ‘token’, ‘api_endpoint’ and optionally ‘environment’. you can pass an already-created ‘astrapy.db.AsyncAstraDB’ instance (alternatively to ‘token’, ‘api_endpoint’ and ‘environment’).

  • namespace (str | None) – namespace (aka keyspace) where the collection is created. If not provided, the environment variable ASTRA_DB_KEYSPACE is inspected. Defaults to the database’s “default namespace”.

  • setup_mode (SetupMode) – mode used to create the Astra DB collection (SYNC, ASYNC or OFF).

  • pre_delete_collection (bool) – whether to delete the collection.

Return type

None

async aadd_messages(messages: Sequence[BaseMessage]) None[source]¶

Async add a list of messages.

Parameters

messages (Sequence[BaseMessage]) – A sequence of BaseMessage objects to store.

Return type

None

async aclear() None[source]¶

Async remove all messages from the store

Return type

None

add_ai_message(message: Union[AIMessage, str]) None¶

Convenience method for adding an AI message string to the store.

Please note that this is a convenience method. Code should favor the bulk add_messages interface instead to save on round-trips to the underlying persistence layer.

This method may be deprecated in a future release.

Parameters

message (Union[AIMessage, str]) – The AI message to add.

Return type

None

add_message(message: BaseMessage) None¶

Add a Message object to the store.

Parameters

message (BaseMessage) – A BaseMessage object to store.

Raises

NotImplementedError – If the sub-class has not implemented an efficient add_messages method.

Return type

None

add_messages(messages: Sequence[BaseMessage]) None[source]¶

Add a list of messages.

Implementations should over-ride this method to handle bulk addition of messages in an efficient manner to avoid unnecessary round-trips to the underlying store.

Parameters

messages (Sequence[BaseMessage]) – A sequence of BaseMessage objects to store.

Return type

None

add_user_message(message: Union[HumanMessage, str]) None¶

Convenience method for adding a human message string to the store.

Please note that this is a convenience method. Code should favor the bulk add_messages interface instead to save on round-trips to the underlying persistence layer.

This method may be deprecated in a future release.

Parameters

message (Union[HumanMessage, str]) – The human message to add to the store.

Return type

None

async aget_messages() list[langchain_core.messages.base.BaseMessage][source]¶

Async version of getting messages.

Can over-ride this method to provide an efficient async implementation.

In general, fetching messages may involve IO to the underlying persistence layer.

Return type

list[langchain_core.messages.base.BaseMessage]

clear() None[source]¶

Remove all messages from the store

Return type

None

Examples using AstraDBChatMessageHistory¶