DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Deploying AI With an Event-Driven Platform
  • Redis-Based Tomcat Session Management
  • Why Use LocalPV with NVMe for Your Workload?
  • Overview of Redisson: The Redis Java Client

Trending

  • Spring AI: How To Write GenAI Applications With Java
  • Integration Testing With Keycloak, Spring Security, Spring Boot, and Spock Framework
  • Leveraging Microsoft Graph API for Unified Data Access and Insights
  • Front-End Application Performance Monitoring (APM)
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Maintain Chat History in Generative AI Apps With Valkey

Maintain Chat History in Generative AI Apps With Valkey

This article presents a walkthrough on how to use a chat history component with Valkey, an open-source alternative to Redis.

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Jul. 04, 24 · Tutorial
Like (1)
Save
Tweet
Share
2.1K Views

Join the DZone community and get the full member experience.

Join For Free

A while back I wrote up a blog post on how to use Redis as a chat history component with LangChain. Since LangChain already had Redis chat history available as a component, it was quite convenient to write a client application.

But, that's not the same with langchaingo which is a Go port of LangChain. I am going to walk through how to do the same, but for Valkey (not Redis). Valkey is an open-source alternative to Redis. It's a community-driven, Linux Foundation project created to keep the project available for use and distribution under the open-source Berkeley Software Distribution (BSD) three-clause license after the Redis license changes.

I also wrote about a similar approach for DynamoDB as well as how to use Valkey with JavaScript

Refer to the Before You Begin section in this blog post 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.

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:

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 from GitHub and run the chat application:

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.

conversation

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

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


Don't runkeys * in production — its just for demo purposes.

example

Chat History Component Implementation

You can refer to the complete implementation here. 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
  • Messages : Retrieves all the messages in a conversation using LRANGE
  • Clear : Deletes all messages in a conversation using DEL

Conclusion

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

Happy building!

AI Open source application Redis (company)

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Deploying AI With an Event-Driven Platform
  • Redis-Based Tomcat Session Management
  • Why Use LocalPV with NVMe for Your Workload?
  • Overview of Redisson: The Redis Java Client

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: