Getting Began With Valkey Utilizing JavaScript – DZone – Uplaza

Valkey is an open-source various to Redis. It is a community-driven, Linux Basis venture created to maintain the venture obtainable to be used and distribution underneath the open-source Berkeley Software program Distribution (BSD) 3-clause license after the Redis license adjustments.

I believe the trail to Valkey was nicely summarised on this inaugural weblog put up:

I’ll stroll by the best way to use Valkey for JavaScript purposes utilizing present shoppers in Redis ecosystem in addition to iovalkey (a pleasant fork of ioredis).

Utilizing Valkey With node-redis

node-redis is a well-liked and broadly used consumer. Right here is a straightforward program that makes use of the Subscriber element of the PubSub API to subscribe to a channel.

{
attempt {
await consumer.join();
console.log(‘Related to Redis server’);

await consumer.subscribe(channelName, (message, channel) => {
console.log(`message “${message}” acquired from channel “${channel}”`)
});

console.log(‘Ready for messages…’);
} catch (err) {
console.error(‘Error:’, err);
}
})();” data-lang=”text/javascript”>

import redis from 'redis';

const consumer = redis.createClient();
const channelName="valkey-channel";

(async () => {
  attempt {
    await consumer.join();
    console.log('Related to Redis server');

    await consumer.subscribe(channelName, (message, channel) => {
      console.log(`message "${message}" acquired from channel "${channel}"`)
    });

    console.log('Ready for messages...');
  } catch (err) {
    console.error('Error:', err);
  }
})();

To do that with Valkey, let’s begin an occasion utilizing the Valkey Docker picture:

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

Additionally, head right here to get OS-specific distribution, or use Homebrew (on Mac) — brew set up valkey. You need to now be capable of use the Valkey CLI (valkey-cli).

Get the code from GitHub repo:

git clone https://github.com/abhirockzz/valkey-javascript
cd valkey-javascript

npm set up

Begin the subscriber app:

node subscriber.js

Publish a message and be certain that the subscriber is ready to obtain it:

valkey-cli PUBLISH valkey-channel 'howdy valkey'

Good! We had been in a position to write a easy software with an present Redis consumer and run it utilizing Valkey (as an alternative of Redis). Certain, that is an oversimplified instance, however there have been no code adjustments required.

Use Valkey With ioredis Consumer

ioredis is one other common consumer. To be doubly certain, let’s attempt ioredis with Valkey as nicely. Let’s write a writer software:

import Redis from 'ioredis';

const redisClient = new Redis();
const channelName="valkey-channel";

const message = course of.argv[2];

if (!message) {
  console.error('Please present a message to publish.');
  course of.exit(1);
}

async operate publishMessage() {
  attempt {
    const receivedCount = await redisClient.publish(channelName, message);
    console.log(`Message "${message}" printed to channel "${channelName}". Obtained by ${receivedCount} subscriber(s).`);
  } catch (err) {
    console.error('Error publishing message:', err);
  } lastly {
    // Shut the consumer connection
    await redisClient.stop();
  }
}

publishMessage();

Run the writer, and ensure that the subscriber app is ready to obtain it:

node writer.js 'hello1'
node writer.js 'hello2'

You need to see these logs within the subscriber software:

message "hello1" acquired from channel "valkey-channel"
message "hello2" acquired from channel "valkey-channel"

Change to iovalkey Consumer

As talked about, iovalkey is a fork of ioredis. I made the next adjustments to port the producer code to make use of iovalkey:

  1. Commented out import Redis from 'ioredis';
  2. Added import Redis from 'iovalkey';
  3. Put in iovalkeynpm set up iovalkey

Right here is the up to date model — sure, this was all I wanted to alter (at the very least for this easy software):

// import Redis from 'ioredis';
import Redis from 'iovalkey';

Run the brand new iovalkey based mostly writer, and ensure that the subscriber is ready to obtain it:

node writer.js 'howdy from iovalkey'

You need to see these logs within the subscriber software:

message "hello from iovalkey" acquired from channel "valkey-channel"

Superior, that is going nicely. We’re able to sprinkle some generative AI now!

Use Valkey With LangChainJS

Together with Python, JavaScript/TypeScript can be getting used within the generative AI ecosystem. LangChain is a well-liked framework for growing purposes powered by massive language fashions (LLMs). LangChain has JS/TS help within the type of LangchainJS.

Having labored lots with the Go port (langchaingo), in addition to Python, I needed to attempt LangchainJS.

One of many frequent use circumstances is to make use of Redis as a chat historical past element in generative AI apps. LangchainJS has this built-in, so let’s attempt it out with Valkey.

Utilizing Valkey as Chat Historical past in LangChain

To put in LangchainJS:

npm set up langchain

For the LLM, I will probably be utilizing Amazon Bedrock (its supported natively with LangchainJS), however be at liberty to make use of others.

For Amazon Bedrock, you have to to configure and arrange Amazon Bedrock, together with requesting entry to the Basis Mannequin(s).

Right here is the chat software. As you possibly can see, it makes use of the RedisChatMessageHistory element.

import { BedrockChat } from "@langchain/community/chat_models/bedrock";
import { RedisChatMessageHistory } from "@langchain/redis";
import { ConversationChain } from "langchain/chains";
import { BufferMemory } from "langchain/memory";
import immediate from "prompt";

import {
  ChatPromptTemplate,
  MessagesPlaceholder,
} from "@langchain/core/prompts";

const chatPrompt = ChatPromptTemplate.fromMessages([
  [
    "system",
    "The following is a friendly conversation between a human and an AI.",
  ],
  new MessagesPlaceholder("chat_history"),
  ["human", "{input}"],
]);

const reminiscence = new BufferMemory({
  chatHistory: new RedisChatMessageHistory({
    sessionId: new Date().toISOString(),
    sessionTTL: 300,
    host: "localhost",
    port: 6379,
  }),
  returnMessages: true,
  memoryKey: "chat_history",
});

const mannequin = "anthropic.claude-3-sonnet-20240229-v1:0"
const area = "us-east-1"

const langchainBedrockChatModel = new BedrockChat({
  mannequin: mannequin,
  area: area,
  modelKwargs: {
    anthropic_version: "bedrock-2023-05-31",
  },
});

const chain = new ConversationChain({
   llm: langchainBedrockChatModel,
   reminiscence: reminiscence,
   immediate: chatPrompt,
});


whereas (true) {
  immediate.begin({noHandleSIGINT: true});
  const {message} = await immediate.get(['message']);
  const response = await chain.invoke({
    enter: message,
  });
  console.log(response);

Run the appliance:

node chat.js

Begin a dialog:

start a conversation

If you happen to peek into Valkey, discover that the conversations are saved in a Listing:

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

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

Utilizing iovalkey Implementation for Chat Historical past

The present implementation makes use of the node-redis consumer, however I needed to check out iovalkey consumer. I’m not a JS/TS knowledgeable, however it was easy sufficient to port the present implementation. You may discuss with the code on GitHub

So far as the consumer (chat) app is worried, I solely needed to make a number of adjustments to change the implementation:

  • Remark out import { RedisChatMessageHistory } from "@langchain/redis";
  • Add import { ValkeyChatMessageHistory } from "./valkey_chat_history.js";
  • Substitute RedisChatMessageHistory with ValkeyChatMessageHistory (whereas creating the reminiscence occasion)

It labored the identical approach as above. Be happy to provide it a attempt!

Wrapping Up

It is nonetheless early days for the Valkey (on the time of writing), and there’s a lengthy technique to go. I am concerned with how the venture evolves and likewise the consumer ecosystem for Valkey.

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