Preserve Chat Historical past in Generative AI Apps With Valkey – DZone – Uplaza

Some time again I wrote up a weblog publish on tips on how to use Redis as a chat historical past part with LangChain. Since LangChain already had Redis chat historical past obtainable as a part, it was fairly handy to write down a consumer utility.

However, that is not the identical with langchaingo which is a Go port of LangChain. I’m going to stroll by way of tips on how to do the identical, however for Valkey (not Redis). Valkey is an open-source various to Redis. It is a community-driven, Linux Basis challenge created to maintain the challenge obtainable to be used and distribution beneath the open-source Berkeley Software program Distribution (BSD) three-clause license after the Redis license modifications.

I additionally wrote a few related strategy for DynamoDB in addition to tips on how to use Valkey with JavaScript

Discuss with the Earlier than You Start part on this weblog publish to finish the conditions for operating the examples. This consists of putting in Go, configuring Amazon Bedrock entry, and offering crucial IAM permissions. The appliance makes use of the Anthropic Claude 3 Sonnet mannequin on Amazon Bedrock.

Run the Chat Software

The chatbot is an easy CLI utility. Earlier than we run it, let’s begin a Valkey occasion utilizing the Valkey Docker picture:

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

Additionally, head over to https://valkey.io/obtain to get OS-specific distribution, or use Homebrew (on Mac) — brew set up valkey. It is best to now be capable to use the Valkey CLI (valkey-cli).

Clone the app from GitHub and run the chat utility:

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

go run *.go

Begin a dialog — as you do this, all of the dialog historical past shall be saved in Valkey.

In case you peek into Valkey, discover that the conversations are saved in a Record:

valkey-cli keys *
valkey-cli LRANGE  0 -1

Do not runkeys * in manufacturing — its only for demo functions.

Chat Historical past Element Implementation

You may check with the whole implementation right here. The part implements the schema.ChatMessageHistory interface strategies in langchaingo and makes use of Record knowledge construction behind the scenes. Beginning the applying creates a brand new “chat session” is related to a Record — every new occasion shall be backed by a separate Record.

Key strategies that have been applied:

  • AddMessage : Shops a dialog message, utilizing LPUSH
  • Messages : Retrieves all of the messages in a dialog utilizing LRANGE
  • Clear : Deletes all messages in a dialog utilizing DEL

Conclusion

It is necessary to notice that it is attainable to make use of any Redis-compatible consumer with Valkey. I used the go-redis consumer, however (on the time of writing) there may be work underway to construct Valkey-specific consumer libraries. Verify the Valkey GitHub org to try the forks of present Redis consumer libraries akin to valkey-go (akin to rueidis), iovalkey (akin to ioredis), Jackey (akin to jedis), and so forth. These are very early days (on the time of writing), and it is going to be attention-grabbing to see the progress right here!

Joyful constructing!

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version