Skip to content

Commit

Permalink
Merge pull request FlowiseAI#1379 from kzhang-dsg/feature/redis-memor…
Browse files Browse the repository at this point in the history
…y-window-size

Add windowSize input to avoid "token exceeded error"
  • Loading branch information
HenryHengZJ authored Dec 15, 2023
2 parents 79a9fea + 4010a8c commit a437c87
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class RedisBackedChatMemory_Memory implements INode {
type: 'string',
default: 'chat_history',
additionalParams: true
},
{
label: 'Window Size',
name: 'windowSize',
type: 'number',
description: 'Window of size k to surface the last k back-and-forth to use as memory.',
additionalParams: true,
optional: true
}
]
}
Expand Down Expand Up @@ -89,6 +97,7 @@ const initalizeRedis = async (nodeData: INodeData, options: ICommonObject): Prom
const sessionId = nodeData.inputs?.sessionId as string
const sessionTTL = nodeData.inputs?.sessionTTL as number
const memoryKey = nodeData.inputs?.memoryKey as string
const windowSize = nodeData.inputs?.windowSize as number
const chatId = options?.chatId as string

let isSessionIdUsingChatMessageId = false
Expand Down Expand Up @@ -133,7 +142,7 @@ const initalizeRedis = async (nodeData: INodeData, options: ICommonObject): Prom
const redisChatMessageHistory = new RedisChatMessageHistory(obj)

redisChatMessageHistory.getMessages = async (): Promise<BaseMessage[]> => {
const rawStoredMessages = await client.lrange((redisChatMessageHistory as any).sessionId, 0, -1)
const rawStoredMessages = await client.lrange((redisChatMessageHistory as any).sessionId, windowSize ? -windowSize : 0, -1)
const orderedMessages = rawStoredMessages.reverse().map((message) => JSON.parse(message))
return orderedMessages.map(mapStoredMessageToChatMessage)
}
Expand Down

0 comments on commit a437c87

Please sign in to comment.