Maintain chat history in generative AI apps with Valkey
Tags: redis, database, machinelearning, go
Published: 2024-07-01 07:57:54

> Integrate Valkey with LangChain

A while back I wrote up a blog post on how to use [Redis as a chat history component with LangChain](https://community.aws/content/2aq9ju6xvYtywGVbuPoWFTk5oK4/build-a-streamlit-app-using-langchain-amazon-bedrock-and-redis). Since LangChain already had Redis chat history available as a component, it was  quite convenient to write a client application.

But, thats not the same with [langchaingo](https://github.com/tmc/langchaingo/) which is a Go port of LangChain. I am going to walk through how to do the same, but for [Valkey](https://valkey.io/) (not Redis). Valkey is an open source alternative to Redis. It's a community-driven, [Linux Foundation project](https://www.linuxfoundation.org/press/linux-foundation-launches-open-source-valkey-community) created to keep the project available for use and distribution under the open source Berkeley Software Distribution (BSD) 3-clause license after the [Redis license changes](https://redis.com/blog/redis-adopts-dual-source-available-licensing/).

> I also wrote about a [similar approach for DynamoDB](https://community.aws/content/2b4szPY94f8mCJa4eVIVH5jpfVj/lambda-ddb-app) as well as how to use [Valkey with JavaScript](https://community.aws/content/2hx81ITCvDiWqrAz06SECOvepoa)


Refer to **Before You Begin** section [in this blog post](https://community.aws/concepts/amazon-bedrock-golang-getting-started#before-you-begin) to complete the prerequisites for running the examples. This includes installing Go, configuring Amazon Bedrock access and providing necessary IAM permissions. The application uses the [Anthropic Claude 3 Sonnet model on Amazon Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html).

## Run the chat application

The chatbot is a simple CLI application. Before we run it, let's start a Valkey instance using the [Valkey Docker image](https://hub.docker.com/r/valkey/valkey/):

```bash
docker run --rm -p 6379:637 valkey/valkey
```

Also, head over to https://valkey.io/download to get OS specific distribution, or use Homebrew (on Mac) - `brew install valkey`. You should now be able to use the Valkey CLI (`valkey-cli`).

Clone the app form GitHub and run the chat application:

```shell
git clone https://github.com/abhirockzz/langchain-valkey-chat-history
cd langchain-valkey-chat-history

go run *.go
```

Start a conversation - as you do that, all the conversation history will be stored in Valkey.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vajiqeky0fucerwrcz75.png)

If you peek into Valkey, notice that the conversations are saved in a `List`:

```bash
valkey-cli keys *
valkey-cli LRANGE <enter list name> 0 -1
```

> Don't run` keys *` in production - its just for demo purposes


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feckcs9xdpsfr85jkix1.png)

## Chat history component implementation

You can refer to the [complete implementation here](https://github.com/abhirockzz/langchain-valkey-chat-history/blob/master/valkey_chat_history.go). The component implements the `schema.ChatMessageHistory` interface methods in `langchaingo` and uses `List` data structure behind the scenes. Starting the application creates a new "chat session" is associated with a `List` - each new instance will be backed by a separate `List`.

Key methods that were implemented:

- `AddMessage` - Stores a conversation message, using [LPUSH](https://valkey.io/commands/lpush/)
- `Messages` - Retrieves all the messages in a conversation using [LRANGE](https://valkey.io/commands/lrange/)
- `Clear` - Deletes all messages in a conversation using [DEL](https://valkey.io/commands/del/)

## Conclusion

It's important to note that it's possible to use any Redis-compatible client with Valkey. I used the [go-redis](https://github.com/redis/go-redis) client, but (at the time of writing) there is work underway to build Valkey specific client libraries. Check the [Valkey GitHub org](https://github.com/orgs/valkey-io/repositories) to take look at the forks of existing Redis client libraries such as [valkey-go](https://github.com/valkey-io/valkey-go) (corresponding to [rueidis](https://github.com/redis/rueidis)), [iovalkey](https://github.com/valkey-io/Jackey) (corresponding to [ioredis](https://github.com/redis/ioredis)), [Jackey](https://github.com/valkey-io/iovalkey) (corresponding to [jedis](https://github.com/redis/jedis)) etc. These are very early days (at the time of writing), and it will be interesting to see the progress here!

Happy building!