## TiDB

> [TiDB Cloud](https://tidbcloud.com/), is a comprehensive Database-as-a-Service (DBaaS) solution, that provides dedicated and serverless options. TiDB Serverless is now integrating a built-in vector search into the MySQL landscape. With this enhancement, you can seamlessly develop AI applications using TiDB Serverless without the need for a new database or additional technical stacks. Be among the first to experience it by joining the waitlist for the private beta at [https://tidb.cloud/ai](https://tidb.cloud/ai).

This notebook introduces how to use TiDB to store chat message history.

## Setup[​](#setup "Direct link to Setup")

Firstly, we will install the following dependencies:

```
%pip install --upgrade --quiet langchain langchain_openai
```

Configuring your OpenAI Key

```
import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass("Input your OpenAI API key:")
```

Finally, we will configure the connection to a TiDB. In this notebook, we will follow the standard connection method provided by TiDB Cloud to establish a secure and efficient database connection.

```
# copy from tidb cloud consoletidb_connection_string_template = "mysql+pymysql://<USER>:<PASSWORD>@<HOST>:4000/<DB>?ssl_ca=/etc/ssl/cert.pem&ssl_verify_cert=true&ssl_verify_identity=true"tidb_password = getpass.getpass("Input your TiDB password:")tidb_connection_string = tidb_connection_string_template.replace(    "<PASSWORD>", tidb_password)
```

## Generating historical data[​](#generating-historical-data "Direct link to Generating historical data")

Creating a set of historical data, which will serve as the foundation for our upcoming demonstrations.

```
from datetime import datetimefrom langchain_community.chat_message_histories import TiDBChatMessageHistoryhistory = TiDBChatMessageHistory(    connection_string=tidb_connection_string,    session_id="code_gen",    earliest_time=datetime.utcnow(),  # Optional to set earliest_time to load messages after this time point.)history.add_user_message("How's our feature going?")history.add_ai_message(    "It's going well. We are working on testing now. It will be released in Feb.")
```

```
[HumanMessage(content="How's our feature going?"), AIMessage(content="It's going well. We are working on testing now. It will be released in Feb.")]
```

## Chatting with historical data[​](#chatting-with-historical-data "Direct link to Chatting with historical data")

Let’s build upon the historical data generated earlier to create a dynamic chat interaction.

Firstly, Creating a Chat Chain with LangChain:

```
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain_openai import ChatOpenAIprompt = ChatPromptTemplate.from_messages(    [        (            "system",            "You're an assistant who's good at coding. You're helping a startup build",        ),        MessagesPlaceholder(variable_name="history"),        ("human", "{question}"),    ])chain = prompt | ChatOpenAI()
```

Building a Runnable on History:

```
from langchain_core.runnables.history import RunnableWithMessageHistorychain_with_history = RunnableWithMessageHistory(    chain,    lambda session_id: TiDBChatMessageHistory(        session_id=session_id, connection_string=tidb_connection_string    ),    input_messages_key="question",    history_messages_key="history",)
```

Initiating the Chat:

```
response = chain_with_history.invoke(    {"question": "Today is Jan 1st. How many days until our feature is released?"},    config={"configurable": {"session_id": "code_gen"}},)response
```

```
AIMessage(content='There are 31 days in January, so there are 30 days until our feature is released in February.')
```

## Checking the history data[​](#checking-the-history-data "Direct link to Checking the history data")

```
history.reload_cache()history.messages
```

```
[HumanMessage(content="How's our feature going?"), AIMessage(content="It's going well. We are working on testing now. It will be released in Feb."), HumanMessage(content='Today is Jan 1st. How many days until our feature is released?'), AIMessage(content='There are 31 days in January, so there are 30 days until our feature is released in February.')]
```

* * *

#### Help us out by providing feedback on this documentation page: