# AsyncElasticsearchChatMessageHistory

> **Class** in `langchain_elasticsearch`

📖 [View in docs](https://reference.langchain.com/python/langchain-elasticsearch/_async/chat_history/AsyncElasticsearchChatMessageHistory)

`Elasticsearch` chat message history.

Stores chat message history in Elasticsearch for persistence across sessions.

## Signature

```python
AsyncElasticsearchChatMessageHistory(
    self,
    index: str,
    session_id: str,
    *,
    ensure_ascii: Optional[bool] = True,
    client: Optional[AsyncElasticsearch] = None,
    es_url: Optional[str] = None,
    es_cloud_id: Optional[str] = None,
    es_user: Optional[str] = None,
    es_api_key: Optional[str] = None,
    es_password: Optional[str] = None,
)
```

## Description

**Setup:**

Install `langchain_elasticsearch` and start Elasticsearch locally using
the start-local script.

```bash
pip install -qU langchain_elasticsearch
curl -fsSL https://elastic.co/start-local | sh
```

This will create an `elastic-start-local` folder. To start Elasticsearch
and Kibana:
```bash
cd elastic-start-local
./start.sh
```

Elasticsearch will be available at `http://localhost:9200`. The password
for the `elastic` user and API key are stored in the `.env` file in the
`elastic-start-local` folder.

Initialize the ElasticsearchChatMessageHistory instance.

**Instantiate:**

```python
from langchain_elasticsearch import ElasticsearchChatMessageHistory

history = ElasticsearchChatMessageHistory(
    index="chat-history",
    session_id="user-123",
    es_url="http://localhost:9200"
)
```

**Instantiate with API key (URL):**
    ```python
    from langchain_elasticsearch import ElasticsearchChatMessageHistory

    history = ElasticsearchChatMessageHistory(
        index="chat-history",
        session_id="user-123",
        es_url="http://localhost:9200",
        es_api_key="your-api-key"
    )
    ```

**Instantiate with username/password (URL):**
    ```python
    from langchain_elasticsearch import ElasticsearchChatMessageHistory

    history = ElasticsearchChatMessageHistory(
        index="chat-history",
        session_id="user-123",
        es_url="http://localhost:9200",
        es_user="elastic",
        es_password="password"
    )
```

If you want to use a cloud hosted Elasticsearch instance, you can pass in the
es_cloud_id argument instead of the es_url argument.

**Instantiate from cloud (with API key):**
    ```python
    from langchain_elasticsearch import ElasticsearchChatMessageHistory

    history = ElasticsearchChatMessageHistory(
        index="chat-history",
        session_id="user-123",
        es_cloud_id="<cloud_id>",
        es_api_key="your-api-key"
    )
    ```

You can also connect to an existing Elasticsearch instance by passing in a
pre-existing Elasticsearch connection via the client argument.

**Instantiate from existing connection:**
    ```python
    from langchain_elasticsearch import ElasticsearchChatMessageHistory
    from elasticsearch import Elasticsearch

    client = Elasticsearch("http://localhost:9200")
    history = ElasticsearchChatMessageHistory(
        index="chat-history",
        session_id="user-123",
        client=client
    )
    ```

**Add messages:**

```python
from langchain_core.messages import HumanMessage, AIMessage

history.add_message(HumanMessage(content="Hello!"))
history.add_message(AIMessage(content="Hi there! How can I help?"))
```

**Get messages:**

```python
messages = history.messages
for msg in messages:
    print(f"{msg.type}: {msg.content}")
```

**Clear history:**

```python
history.clear()
```

For synchronous applications, use the `ElasticsearchChatMessageHistory` class.
For asynchronous applications, use the `AsyncElasticsearchChatMessageHistory`
class.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `index` | `str` | Yes | Name of the Elasticsearch index to use for storing messages. |
| `session_id` | `str` | Yes | Arbitrary key that is used to store the messages of a single chat session. |
| `ensure_ascii` | `bool` | No | Used to escape ASCII symbols in json.dumps. Defaults to True. (default: `True`) |
| `client` | `AsyncElasticsearch` | No | Pre-existing Elasticsearch connection. Either provide this OR credentials. (default: `None`) |
| `es_url` | `str` | No | URL of the Elasticsearch instance to connect to. (default: `None`) |
| `es_cloud_id` | `str` | No | Cloud ID of the Elasticsearch instance. (default: `None`) |
| `es_user` | `str` | No | Username to use when connecting to Elasticsearch. (default: `None`) |
| `es_api_key` | `str` | No | API key to use when connecting to Elasticsearch. (default: `None`) |
| `es_password` | `str` | No | Password to use when connecting to Elasticsearch. (default: `None`) |

## Extends

- `BaseChatMessageHistory`

## Constructors

```python
__init__(
    self,
    index: str,
    session_id: str,
    *,
    ensure_ascii: Optional[bool] = True,
    client: Optional[AsyncElasticsearch] = None,
    es_url: Optional[str] = None,
    es_cloud_id: Optional[str] = None,
    es_user: Optional[str] = None,
    es_api_key: Optional[str] = None,
    es_password: Optional[str] = None,
)
```

| Name | Type |
|------|------|
| `index` | `str` |
| `session_id` | `str` |
| `ensure_ascii` | `Optional[bool]` |
| `client` | `Optional[AsyncElasticsearch]` |
| `es_url` | `Optional[str]` |
| `es_cloud_id` | `Optional[str]` |
| `es_user` | `Optional[str]` |
| `es_api_key` | `Optional[str]` |
| `es_password` | `Optional[str]` |


## Properties

- `index`
- `session_id`
- `ensure_ascii`
- `client`
- `created`

## Methods

- [`create_if_missing()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/chat_history/AsyncElasticsearchChatMessageHistory/create_if_missing)
- [`aget_messages()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/chat_history/AsyncElasticsearchChatMessageHistory/aget_messages)
- [`aadd_message()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/chat_history/AsyncElasticsearchChatMessageHistory/aadd_message)
- [`aadd_messages()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/chat_history/AsyncElasticsearchChatMessageHistory/aadd_messages)
- [`aclear()`](https://reference.langchain.com/python/langchain-elasticsearch/_async/chat_history/AsyncElasticsearchChatMessageHistory/aclear)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-elastic/blob/e78a327f38e09abd10128ed28c8774e9a8f9fec0/libs/elasticsearch/langchain_elasticsearch/_async/chat_history.py#L18)