From a6a039807483dd99ae973b02f438222cd46c0060 Mon Sep 17 00:00:00 2001 From: Henry Heng Date: Tue, 24 Sep 2024 15:19:08 +0100 Subject: [PATCH] Bugfix/Buffer Memory for Anthropic (#3242) fix buffer memory --- .../memory/BufferWindowMemory/BufferWindowMemory.ts | 10 ++++++---- .../ConversationSummaryBufferMemory.ts | 10 ++++++++-- .../ConversationSummaryMemory.ts | 10 ++++++++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/components/nodes/memory/BufferWindowMemory/BufferWindowMemory.ts b/packages/components/nodes/memory/BufferWindowMemory/BufferWindowMemory.ts index 5cf717a2199..82cfd92916f 100644 --- a/packages/components/nodes/memory/BufferWindowMemory/BufferWindowMemory.ts +++ b/packages/components/nodes/memory/BufferWindowMemory/BufferWindowMemory.ts @@ -118,14 +118,16 @@ class BufferWindowMemoryExtended extends FlowiseWindowMemory implements MemoryMe sessionId: id, chatflowid: this.chatflowid }, - take: this.k + 1, order: { - createdDate: 'DESC' // we get the latest top K + createdDate: 'ASC' } }) - // reverse the order of human and ai messages - if (chatMessage.length) chatMessage.reverse() + if (this.k <= 0) { + chatMessage = [] + } else { + chatMessage = chatMessage.slice(-this.k * 2) + } if (prependMessages?.length) { chatMessage.unshift(...prependMessages) diff --git a/packages/components/nodes/memory/ConversationSummaryBufferMemory/ConversationSummaryBufferMemory.ts b/packages/components/nodes/memory/ConversationSummaryBufferMemory/ConversationSummaryBufferMemory.ts index f9b0c14f0b8..c45f93484be 100644 --- a/packages/components/nodes/memory/ConversationSummaryBufferMemory/ConversationSummaryBufferMemory.ts +++ b/packages/components/nodes/memory/ConversationSummaryBufferMemory/ConversationSummaryBufferMemory.ts @@ -10,9 +10,10 @@ import { } from '../../../src/Interface' import { getBaseClasses, mapChatMessageToBaseMessage } from '../../../src/utils' import { BaseLanguageModel } from '@langchain/core/language_models/base' -import { BaseMessage, getBufferString } from '@langchain/core/messages' +import { BaseMessage, getBufferString, HumanMessage } from '@langchain/core/messages' import { ConversationSummaryBufferMemory, ConversationSummaryBufferMemoryInput } from 'langchain/memory' import { DataSource } from 'typeorm' +import { ChatAnthropic } from '../../chatmodels/ChatAnthropic/FlowiseChatAnthropic' class ConversationSummaryBufferMemory_Memory implements INode { label: string @@ -163,7 +164,12 @@ class ConversationSummaryBufferMemoryExtended extends FlowiseSummaryBufferMemory // ----------- Finished Pruning --------------- if (this.movingSummaryBuffer) { - baseMessages = [new this.summaryChatMessageClass(this.movingSummaryBuffer), ...baseMessages] + // Anthropic doesn't support multiple system messages + if (this.llm instanceof ChatAnthropic) { + baseMessages = [new HumanMessage(`Below is the summarized conversation:\n\n${this.movingSummaryBuffer}`), ...baseMessages] + } else { + baseMessages = [new this.summaryChatMessageClass(this.movingSummaryBuffer), ...baseMessages] + } } if (returnBaseMessages) { diff --git a/packages/components/nodes/memory/ConversationSummaryMemory/ConversationSummaryMemory.ts b/packages/components/nodes/memory/ConversationSummaryMemory/ConversationSummaryMemory.ts index a1219c1fff9..6c4d078baf7 100644 --- a/packages/components/nodes/memory/ConversationSummaryMemory/ConversationSummaryMemory.ts +++ b/packages/components/nodes/memory/ConversationSummaryMemory/ConversationSummaryMemory.ts @@ -10,9 +10,10 @@ import { } from '../../../src/Interface' import { getBaseClasses, mapChatMessageToBaseMessage } from '../../../src/utils' import { BaseLanguageModel } from '@langchain/core/language_models/base' -import { BaseMessage, SystemMessage } from '@langchain/core/messages' +import { BaseMessage, HumanMessage, SystemMessage } from '@langchain/core/messages' import { ConversationSummaryMemory, ConversationSummaryMemoryInput } from 'langchain/memory' import { DataSource } from 'typeorm' +import { ChatAnthropic } from '../../chatmodels/ChatAnthropic/FlowiseChatAnthropic' class ConversationSummaryMemory_Memory implements INode { label: string @@ -135,7 +136,12 @@ class ConversationSummaryMemoryExtended extends FlowiseSummaryMemory implements } if (returnBaseMessages) { - return [new SystemMessage(this.buffer)] + // Anthropic doesn't support multiple system messages + if (this.llm instanceof ChatAnthropic) { + return [new HumanMessage(`Below is the summarized conversation:\n\n${this.buffer}`)] + } else { + return [new SystemMessage(this.buffer)] + } } if (this.buffer) {