Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/Buffer Memory for Anthropic #3242

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Loading