Merge pull request #1794 from FlowiseAI/feature/Upstash
Feature/Upstash Singleton
This commit is contained in:
commit
ada19b68a2
|
|
@ -1,4 +1,5 @@
|
||||||
import { Redis } from '@upstash/redis'
|
import { Redis, RedisConfigNodejs } from '@upstash/redis'
|
||||||
|
import { isEqual } from 'lodash'
|
||||||
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
|
import { BufferMemory, BufferMemoryInput } from 'langchain/memory'
|
||||||
import { UpstashRedisChatMessageHistory } from '@langchain/community/stores/message/upstash_redis'
|
import { UpstashRedisChatMessageHistory } from '@langchain/community/stores/message/upstash_redis'
|
||||||
import { mapStoredMessageToChatMessage, AIMessage, HumanMessage, StoredMessage, BaseMessage } from '@langchain/core/messages'
|
import { mapStoredMessageToChatMessage, AIMessage, HumanMessage, StoredMessage, BaseMessage } from '@langchain/core/messages'
|
||||||
|
|
@ -6,6 +7,24 @@ import { FlowiseMemory, IMessage, INode, INodeData, INodeParams, MemoryMethods,
|
||||||
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||||
import { ICommonObject } from '../../../src/Interface'
|
import { ICommonObject } from '../../../src/Interface'
|
||||||
|
|
||||||
|
let redisClientSingleton: Redis
|
||||||
|
let redisClientOption: RedisConfigNodejs
|
||||||
|
|
||||||
|
const getRedisClientbyOption = (option: RedisConfigNodejs) => {
|
||||||
|
if (!redisClientSingleton) {
|
||||||
|
// if client doesn't exists
|
||||||
|
redisClientSingleton = new Redis(option)
|
||||||
|
redisClientOption = option
|
||||||
|
return redisClientSingleton
|
||||||
|
} else if (redisClientSingleton && !isEqual(option, redisClientOption)) {
|
||||||
|
// if client exists but option changed
|
||||||
|
redisClientSingleton = new Redis(option)
|
||||||
|
redisClientOption = option
|
||||||
|
return redisClientSingleton
|
||||||
|
}
|
||||||
|
return redisClientSingleton
|
||||||
|
}
|
||||||
|
|
||||||
class UpstashRedisBackedChatMemory_Memory implements INode {
|
class UpstashRedisBackedChatMemory_Memory implements INode {
|
||||||
label: string
|
label: string
|
||||||
name: string
|
name: string
|
||||||
|
|
@ -75,7 +94,7 @@ const initalizeUpstashRedis = async (nodeData: INodeData, options: ICommonObject
|
||||||
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
||||||
const upstashRestToken = getCredentialParam('upstashRestToken', credentialData, nodeData)
|
const upstashRestToken = getCredentialParam('upstashRestToken', credentialData, nodeData)
|
||||||
|
|
||||||
const client = new Redis({
|
const client = getRedisClientbyOption({
|
||||||
url: baseURL,
|
url: baseURL,
|
||||||
token: upstashRestToken
|
token: upstashRestToken
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue