Some time again I wrote up a weblog submit on how you can use Redis as a chat historical past element with LangChain. Since LangChain already had Redis chat historical past obtainable as a element, it was fairly handy to jot down a consumer software.
However, that is not the identical with langchaingo which is a Go port of LangChain. I’m going to stroll by means of how you can do the identical, however for Valkey (not Redis). Valkey is an open-source different to Redis. It is a community-driven, Linux Basis undertaking created to maintain the undertaking obtainable to be used and distribution beneath the open-source Berkeley Software program Distribution (BSD) three-clause license after the Redis license adjustments.
I additionally wrote a few related method for DynamoDB in addition to how you can use Valkey with JavaScript
Seek advice from the Earlier than You Start part on this weblog submit to finish the stipulations for working the examples. This consists of putting in Go, configuring Amazon Bedrock entry, and offering vital IAM permissions. The applying makes use of the Anthropic Claude 3 Sonnet mannequin on Amazon Bedrock.
Run the Chat Software
The chatbot is an easy CLI software. 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
. You need to now have the ability to use the Valkey CLI (valkey-cli
).
Clone the app from GitHub and run the chat software:
git clone https://github.com/abhirockzz/langchain-valkey-chat-history
cd langchain-valkey-chat-history
go run *.go
Begin a dialog — as you try this, all of the dialog historical past can be saved in Valkey.
For those who peek into Valkey, discover that the conversations are saved in a Record
:
valkey-cli keys *
valkey-cli LRANGE 0 -1
Do not run
keys *
in manufacturing — its only for demo functions.
Chat Historical past Element Implementation
You’ll be able to seek advice from the entire implementation right here. The element implements the schema.ChatMessageHistory
interface strategies in langchaingo
and makes use of Record
knowledge construction behind the scenes. Beginning the appliance creates a brand new “chat session” is related to a Record
— every new occasion can be backed by a separate Record
.
Key strategies that have been applied:
AddMessage
: Shops a dialog message, utilizing LPUSHMessages
: Retrieves all of the messages in a dialog utilizing LRANGEClear
: Deletes all messages in a dialog utilizing DEL
Conclusion
It is essential to notice that it is potential to make use of any Redis-compatible consumer with Valkey. I used the go-redis consumer, however (on the time of writing) there’s work underway to construct Valkey-specific consumer libraries. Examine the Valkey GitHub org to check out the forks of present Redis consumer libraries similar to valkey-go (akin to rueidis), iovalkey (akin to ioredis), Jackey (akin to jedis), and so on. These are very early days (on the time of writing), and it will likely be attention-grabbing to see the progress right here!
Comfortable constructing!