Feature/Buffer Memory SessionId (#2111)

* add sessionId to buffer memory, add conversation summary buffer memory

* add fix for conv retrieval qa chain
This commit is contained in:
Henry Heng
2024-04-11 11:18:39 +01:00
committed by GitHub
parent 57b716c7d7
commit c33642cdf9
39 changed files with 784 additions and 574 deletions
+11 -13
View File
@@ -7,7 +7,6 @@ import { z } from 'zod'
import { DataSource } from 'typeorm'
import { ICommonObject, IDatabaseEntity, IMessage, INodeData, IVariable } from './Interface'
import { AES, enc } from 'crypto-js'
import { ChatMessageHistory } from 'langchain/memory'
import { AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages'
export const numberOrExpressionRegex = '^(\\d+\\.?\\d*|{{.*}})$' //return true if string consists only numbers OR expression {{}}
@@ -576,22 +575,21 @@ export const getUserHome = (): string => {
}
/**
* Map incoming chat history to ChatMessageHistory
* @param {ICommonObject} options
* @returns {ChatMessageHistory}
* Map ChatMessage to BaseMessage
* @param {IChatMessage[]} chatmessages
* @returns {BaseMessage[]}
*/
export const mapChatHistory = (options: ICommonObject): ChatMessageHistory => {
export const mapChatMessageToBaseMessage = (chatmessages: any[] = []): BaseMessage[] => {
const chatHistory = []
const histories: IMessage[] = options.chatHistory ?? []
for (const message of histories) {
if (message.type === 'apiMessage') {
chatHistory.push(new AIMessage(message.message))
} else if (message.type === 'userMessage') {
chatHistory.push(new HumanMessage(message.message))
for (const message of chatmessages) {
if (message.role === 'apiMessage') {
chatHistory.push(new AIMessage(message.content))
} else if (message.role === 'userMessage') {
chatHistory.push(new HumanMessage(message.content))
}
}
return new ChatMessageHistory(chatHistory)
return chatHistory
}
/**
@@ -615,7 +613,7 @@ export const convertChatHistoryToText = (chatHistory: IMessage[] = []): string =
/**
* Serialize array chat history to string
* @param {IMessage[]} chatHistory
* @param {string | Array<string>} chatHistory
* @returns {string}
*/
export const serializeChatHistory = (chatHistory: string | Array<string>) => {