Merge pull request #1379 from kzhang-dsg/feature/redis-memory-window-size

Add windowSize input to avoid "token exceeded error"
This commit is contained in:
Henry Heng 2023-12-15 12:19:09 +00:00 committed by GitHub
commit 1b0b1f13fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 1 deletions

View File

@ -57,6 +57,14 @@ class RedisBackedChatMemory_Memory implements INode {
type: 'string', type: 'string',
default: 'chat_history', default: 'chat_history',
additionalParams: true 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
} }
] ]
} }
@ -89,6 +97,7 @@ const initalizeRedis = async (nodeData: INodeData, options: ICommonObject): Prom
const sessionId = nodeData.inputs?.sessionId as string const sessionId = nodeData.inputs?.sessionId as string
const sessionTTL = nodeData.inputs?.sessionTTL as number const sessionTTL = nodeData.inputs?.sessionTTL as number
const memoryKey = nodeData.inputs?.memoryKey as string const memoryKey = nodeData.inputs?.memoryKey as string
const windowSize = nodeData.inputs?.windowSize as number
const chatId = options?.chatId as string const chatId = options?.chatId as string
let isSessionIdUsingChatMessageId = false let isSessionIdUsingChatMessageId = false
@ -133,7 +142,7 @@ const initalizeRedis = async (nodeData: INodeData, options: ICommonObject): Prom
const redisChatMessageHistory = new RedisChatMessageHistory(obj) const redisChatMessageHistory = new RedisChatMessageHistory(obj)
redisChatMessageHistory.getMessages = async (): Promise<BaseMessage[]> => { 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)) const orderedMessages = rawStoredMessages.reverse().map((message) => JSON.parse(message))
return orderedMessages.map(mapStoredMessageToChatMessage) return orderedMessages.map(mapStoredMessageToChatMessage)
} }