From 29be2d2608de2e7c21b762041409aab8b827cb7d Mon Sep 17 00:00:00 2001 From: vinodkiran Date: Sat, 30 Sep 2023 18:51:14 +0530 Subject: [PATCH] Addition of Redis LLM Cache --- .../credentials/RedisCacheApi.credential.ts | 43 +++++++++++ .../llmcache/LLMRedisCache/LLMRedisCache.ts | 75 +++++++++++++++++++ .../nodes/llmcache/LLMRedisCache/redis.svg | 1 + packages/components/package.json | 1 + 4 files changed, 120 insertions(+) create mode 100644 packages/components/credentials/RedisCacheApi.credential.ts create mode 100644 packages/components/nodes/llmcache/LLMRedisCache/LLMRedisCache.ts create mode 100644 packages/components/nodes/llmcache/LLMRedisCache/redis.svg diff --git a/packages/components/credentials/RedisCacheApi.credential.ts b/packages/components/credentials/RedisCacheApi.credential.ts new file mode 100644 index 00000000..e09a94e7 --- /dev/null +++ b/packages/components/credentials/RedisCacheApi.credential.ts @@ -0,0 +1,43 @@ +import { INodeParams, INodeCredential } from '../src/Interface' + +class RedisCacheApi implements INodeCredential { + label: string + name: string + version: number + description: string + inputs: INodeParams[] + + constructor() { + this.label = 'Redis Cache API' + this.name = 'redisCacheApi' + this.version = 1.0 + this.inputs = [ + { + label: 'Redis Host', + name: 'redisCacheHost', + type: 'string', + default: '127.0.0.1' + }, + { + label: 'Port', + name: 'redisCachePort', + type: 'number', + default: '6789' + }, + { + label: 'User', + name: 'redisCacheUser', + type: 'string', + placeholder: '' + }, + { + label: 'Password', + name: 'redisCachePwd', + type: 'password', + placeholder: '' + } + ] + } +} + +module.exports = { credClass: RedisCacheApi } diff --git a/packages/components/nodes/llmcache/LLMRedisCache/LLMRedisCache.ts b/packages/components/nodes/llmcache/LLMRedisCache/LLMRedisCache.ts new file mode 100644 index 00000000..4a098e82 --- /dev/null +++ b/packages/components/nodes/llmcache/LLMRedisCache/LLMRedisCache.ts @@ -0,0 +1,75 @@ +import { + getBaseClasses, + getCredentialData, + getCredentialParam, + ICommonObject, + INode, + INodeData, + INodeOutputsValue, + INodeParams +} from '../../../src' +import { RedisCache } from 'langchain/cache/ioredis' +import { Redis } from 'ioredis' + +class LLMRedisCache implements INode { + label: string + name: string + version: number + description: string + type: string + icon: string + category: string + baseClasses: string[] + inputs: INodeParams[] + outputs: INodeOutputsValue[] + credential: INodeParams + + constructor() { + this.label = 'Redis LLM Cache' + this.name = 'redisCache' + this.version = 1.0 + this.type = 'LLMCache' + this.icon = 'redis.svg' + this.category = 'LLM Cache' + this.baseClasses = [this.type, 'LLMCacheBase'] + this.credential = { + label: 'Connect Credential', + name: 'credential', + type: 'credential', + optional: true, + credentialNames: ['redisCacheApi'] + } + this.inputs = [] + this.outputs = [ + { + label: 'LLM Cache', + name: 'cache', + baseClasses: [this.type, ...getBaseClasses(RedisCache)] + } + ] + } + + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { + const credentialData = await getCredentialData(nodeData.credential ?? '', options) + const username = getCredentialParam('redisCacheUser', credentialData, nodeData) + const password = getCredentialParam('redisCachePwd', credentialData, nodeData) + const portStr = getCredentialParam('redisCachePort', credentialData, nodeData) + const host = getCredentialParam('redisCacheHost', credentialData, nodeData) + let port = 6379 + try { + port = portStr ? parseInt(portStr) : 6379 + } catch (e) { + port = 6379 + } + + const client = new Redis({ + port: port, // Redis port + host: host, + username: username, + password: password + }) + return new RedisCache(client) + } +} + +module.exports = { nodeClass: LLMRedisCache } diff --git a/packages/components/nodes/llmcache/LLMRedisCache/redis.svg b/packages/components/nodes/llmcache/LLMRedisCache/redis.svg new file mode 100644 index 00000000..90359069 --- /dev/null +++ b/packages/components/nodes/llmcache/LLMRedisCache/redis.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/components/package.json b/packages/components/package.json index 18bf8e6a..5e993f86 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -43,6 +43,7 @@ "google-auth-library": "^9.0.0", "graphql": "^16.6.0", "html-to-text": "^9.0.5", + "ioredis": "^5.3.2", "langchain": "^0.0.154", "langfuse-langchain": "^1.0.14-alpha.0", "langsmith": "^0.0.32",