Bugfix/Buffer Memory for Anthropic (#3242)

fix buffer memory
This commit is contained in:
Henry Heng 2024-09-24 15:19:08 +01:00 committed by GitHub
parent 7d88183eca
commit a6a0398074
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 8 deletions

View File

@ -118,14 +118,16 @@ class BufferWindowMemoryExtended extends FlowiseWindowMemory implements MemoryMe
sessionId: id, sessionId: id,
chatflowid: this.chatflowid chatflowid: this.chatflowid
}, },
take: this.k + 1,
order: { order: {
createdDate: 'DESC' // we get the latest top K createdDate: 'ASC'
} }
}) })
// reverse the order of human and ai messages if (this.k <= 0) {
if (chatMessage.length) chatMessage.reverse() chatMessage = []
} else {
chatMessage = chatMessage.slice(-this.k * 2)
}
if (prependMessages?.length) { if (prependMessages?.length) {
chatMessage.unshift(...prependMessages) chatMessage.unshift(...prependMessages)

View File

@ -10,9 +10,10 @@ import {
} from '../../../src/Interface' } from '../../../src/Interface'
import { getBaseClasses, mapChatMessageToBaseMessage } from '../../../src/utils' import { getBaseClasses, mapChatMessageToBaseMessage } from '../../../src/utils'
import { BaseLanguageModel } from '@langchain/core/language_models/base' 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 { ConversationSummaryBufferMemory, ConversationSummaryBufferMemoryInput } from 'langchain/memory'
import { DataSource } from 'typeorm' import { DataSource } from 'typeorm'
import { ChatAnthropic } from '../../chatmodels/ChatAnthropic/FlowiseChatAnthropic'
class ConversationSummaryBufferMemory_Memory implements INode { class ConversationSummaryBufferMemory_Memory implements INode {
label: string label: string
@ -163,8 +164,13 @@ class ConversationSummaryBufferMemoryExtended extends FlowiseSummaryBufferMemory
// ----------- Finished Pruning --------------- // ----------- Finished Pruning ---------------
if (this.movingSummaryBuffer) { if (this.movingSummaryBuffer) {
// 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] baseMessages = [new this.summaryChatMessageClass(this.movingSummaryBuffer), ...baseMessages]
} }
}
if (returnBaseMessages) { if (returnBaseMessages) {
return baseMessages return baseMessages

View File

@ -10,9 +10,10 @@ import {
} from '../../../src/Interface' } from '../../../src/Interface'
import { getBaseClasses, mapChatMessageToBaseMessage } from '../../../src/utils' import { getBaseClasses, mapChatMessageToBaseMessage } from '../../../src/utils'
import { BaseLanguageModel } from '@langchain/core/language_models/base' 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 { ConversationSummaryMemory, ConversationSummaryMemoryInput } from 'langchain/memory'
import { DataSource } from 'typeorm' import { DataSource } from 'typeorm'
import { ChatAnthropic } from '../../chatmodels/ChatAnthropic/FlowiseChatAnthropic'
class ConversationSummaryMemory_Memory implements INode { class ConversationSummaryMemory_Memory implements INode {
label: string label: string
@ -135,8 +136,13 @@ class ConversationSummaryMemoryExtended extends FlowiseSummaryMemory implements
} }
if (returnBaseMessages) { if (returnBaseMessages) {
// 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)] return [new SystemMessage(this.buffer)]
} }
}
if (this.buffer) { if (this.buffer) {
return [ return [