update Zep version

This commit is contained in:
Henry 2023-12-05 19:02:33 +00:00
parent 1867f9be02
commit bfeebcd229
3 changed files with 51 additions and 75 deletions

View File

@ -1,9 +1,8 @@
import { SystemMessage } from 'langchain/schema' import { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep'
import { getBufferString, InputValues, MemoryVariables, OutputValues } from 'langchain/memory'
import { INode, INodeData, INodeParams } from '../../../src/Interface' import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep'
import { ICommonObject } from '../../../src' import { ICommonObject } from '../../../src'
import { getBufferString } from 'langchain/memory'
class ZepMemory_Memory implements INode { class ZepMemory_Memory implements INode {
label: string label: string
@ -20,7 +19,7 @@ class ZepMemory_Memory implements INode {
constructor() { constructor() {
this.label = 'Zep Memory' this.label = 'Zep Memory'
this.name = 'ZepMemory' this.name = 'ZepMemory'
this.version = 1.0 this.version = 2.0
this.type = 'ZepMemory' this.type = 'ZepMemory'
this.icon = 'zep.png' this.icon = 'zep.png'
this.category = 'Memory' this.category = 'Memory'
@ -41,17 +40,12 @@ class ZepMemory_Memory implements INode {
type: 'string', type: 'string',
default: 'http://127.0.0.1:8000' default: 'http://127.0.0.1:8000'
}, },
{
label: 'Auto Summary',
name: 'autoSummary',
type: 'boolean',
default: true
},
{ {
label: 'Session Id', label: 'Session Id',
name: 'sessionId', name: 'sessionId',
type: 'string', type: 'string',
description: 'If not specified, the first CHAT_MESSAGE_ID will be used as sessionId', description:
'If not specified, a random id will be used. Learn <a target="_blank" href="https://docs.flowiseai.com/memory/long-term-memory#ui-and-embedded-chat">more</a>',
default: '', default: '',
additionalParams: true, additionalParams: true,
optional: true optional: true
@ -60,15 +54,10 @@ class ZepMemory_Memory implements INode {
label: 'Size', label: 'Size',
name: 'k', name: 'k',
type: 'number', type: 'number',
default: '10', placeholder: '10',
description: 'Window of size k to surface the last k back-and-forth to use as memory.' description: 'Window of size k to surface the last k back-and-forth to use as memory.',
}, additionalParams: true,
{ optional: true
label: 'Auto Summary Template',
name: 'autoSummaryTemplate',
type: 'string',
default: 'This is the summary of the following conversation:\n{summary}',
additionalParams: true
}, },
{ {
label: 'AI Prefix', label: 'AI Prefix',
@ -109,36 +98,7 @@ class ZepMemory_Memory implements INode {
} }
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const autoSummaryTemplate = nodeData.inputs?.autoSummaryTemplate as string return await initalizeZep(nodeData, options)
const autoSummary = nodeData.inputs?.autoSummary as boolean
const k = nodeData.inputs?.k as string
let zep = await initalizeZep(nodeData, options)
// hack to support summary
let tmpFunc = zep.loadMemoryVariables
zep.loadMemoryVariables = async (values) => {
let data = await tmpFunc.bind(zep, values)()
if (autoSummary && zep.returnMessages && data[zep.memoryKey] && data[zep.memoryKey].length) {
const zepClient = await zep.zepClientPromise
const memory = await zepClient.memory.getMemory(zep.sessionId, parseInt(k, 10) ?? 10)
if (memory?.summary) {
let summary = autoSummaryTemplate.replace(/{summary}/g, memory.summary.content)
// eslint-disable-next-line no-console
console.log('[ZepMemory] auto summary:', summary)
data[zep.memoryKey].unshift(new SystemMessage(summary))
}
}
// for langchain zep memory compatibility, or we will get "Missing value for input variable chat_history"
if (data instanceof Array) {
data = {
[zep.memoryKey]: data
}
}
return data
}
return zep
} }
//@ts-ignore //@ts-ignore
@ -169,40 +129,72 @@ const initalizeZep = async (nodeData: INodeData, options: ICommonObject): Promis
const humanPrefix = nodeData.inputs?.humanPrefix as string const humanPrefix = nodeData.inputs?.humanPrefix as string
const memoryKey = nodeData.inputs?.memoryKey as string const memoryKey = nodeData.inputs?.memoryKey as string
const inputKey = nodeData.inputs?.inputKey as string const inputKey = nodeData.inputs?.inputKey as string
const sessionId = nodeData.inputs?.sessionId as string const k = nodeData.inputs?.k as string
const chatId = options?.chatId as string const chatId = options?.chatId as string
let isSessionIdUsingChatMessageId = false let isSessionIdUsingChatMessageId = false
if (!sessionId && chatId) isSessionIdUsingChatMessageId = true let sessionId = ''
if (!nodeData.inputs?.sessionId && chatId) {
isSessionIdUsingChatMessageId = true
sessionId = chatId
} else {
sessionId = nodeData.inputs?.sessionId
}
const credentialData = await getCredentialData(nodeData.credential ?? '', options) const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const apiKey = getCredentialParam('apiKey', credentialData, nodeData) const apiKey = getCredentialParam('apiKey', credentialData, nodeData)
const obj: ZepMemoryInput & Partial<ZepMemoryExtendedInput> = { const obj: ZepMemoryInput & ZepMemoryExtendedInput = {
baseURL, baseURL,
sessionId: sessionId ? sessionId : chatId, sessionId: sessionId ? sessionId : chatId,
aiPrefix, aiPrefix,
humanPrefix, humanPrefix,
returnMessages: true, returnMessages: true,
memoryKey, memoryKey,
inputKey inputKey,
isSessionIdUsingChatMessageId,
k: k ? parseInt(k, 10) : undefined
} }
if (apiKey) obj.apiKey = apiKey if (apiKey) obj.apiKey = apiKey
if (isSessionIdUsingChatMessageId) obj.isSessionIdUsingChatMessageId = true
return new ZepMemoryExtended(obj) return new ZepMemoryExtended(obj)
} }
interface ZepMemoryExtendedInput { interface ZepMemoryExtendedInput {
isSessionIdUsingChatMessageId: boolean isSessionIdUsingChatMessageId: boolean
k?: number
} }
class ZepMemoryExtended extends ZepMemory { class ZepMemoryExtended extends ZepMemory {
isSessionIdUsingChatMessageId? = false isSessionIdUsingChatMessageId? = false
lastN?: number
constructor(fields: ZepMemoryInput & Partial<ZepMemoryExtendedInput>) { constructor(fields: ZepMemoryInput & ZepMemoryExtendedInput) {
super(fields) super(fields)
this.isSessionIdUsingChatMessageId = fields.isSessionIdUsingChatMessageId this.isSessionIdUsingChatMessageId = fields.isSessionIdUsingChatMessageId
this.lastN = fields.k
}
async loadMemoryVariables(values: InputValues, overrideSessionId = ''): Promise<MemoryVariables> {
if (overrideSessionId) {
super.sessionId = overrideSessionId
}
return super.loadMemoryVariables({ ...values, lastN: this.lastN })
}
async saveContext(inputValues: InputValues, outputValues: OutputValues, overrideSessionId = ''): Promise<void> {
if (overrideSessionId) {
super.sessionId = overrideSessionId
}
return super.saveContext(inputValues, outputValues)
}
async clear(overrideSessionId = ''): Promise<void> {
if (overrideSessionId) {
super.sessionId = overrideSessionId
}
return super.clear()
} }
} }

View File

@ -21,7 +21,7 @@
"@aws-sdk/client-s3": "^3.427.0", "@aws-sdk/client-s3": "^3.427.0",
"@dqbd/tiktoken": "^1.0.7", "@dqbd/tiktoken": "^1.0.7",
"@elastic/elasticsearch": "^8.9.0", "@elastic/elasticsearch": "^8.9.0",
"@getzep/zep-js": "^0.6.3", "@getzep/zep-js": "^0.9.0",
"@gomomento/sdk": "^1.51.1", "@gomomento/sdk": "^1.51.1",
"@gomomento/sdk-core": "^1.51.1", "@gomomento/sdk-core": "^1.51.1",
"@google-ai/generativelanguage": "^0.2.1", "@google-ai/generativelanguage": "^0.2.1",

View File

@ -205,7 +205,7 @@
"data": { "data": {
"id": "ZepMemory_0", "id": "ZepMemory_0",
"label": "Zep Memory", "label": "Zep Memory",
"version": 1, "version": 2,
"name": "ZepMemory", "name": "ZepMemory",
"type": "ZepMemory", "type": "ZepMemory",
"baseClasses": ["ZepMemory", "BaseChatMemory", "BaseMemory"], "baseClasses": ["ZepMemory", "BaseChatMemory", "BaseMemory"],
@ -228,13 +228,6 @@
"default": "http://127.0.0.1:8000", "default": "http://127.0.0.1:8000",
"id": "ZepMemory_0-input-baseURL-string" "id": "ZepMemory_0-input-baseURL-string"
}, },
{
"label": "Auto Summary",
"name": "autoSummary",
"type": "boolean",
"default": true,
"id": "ZepMemory_0-input-autoSummary-boolean"
},
{ {
"label": "Session Id", "label": "Session Id",
"name": "sessionId", "name": "sessionId",
@ -251,17 +244,10 @@
"type": "number", "type": "number",
"default": "10", "default": "10",
"step": 1, "step": 1,
"additionalParams": true,
"description": "Window of size k to surface the last k back-and-forths to use as memory.", "description": "Window of size k to surface the last k back-and-forths to use as memory.",
"id": "ZepMemory_0-input-k-number" "id": "ZepMemory_0-input-k-number"
}, },
{
"label": "Auto Summary Template",
"name": "autoSummaryTemplate",
"type": "string",
"default": "This is the summary of the following conversation:\n{summary}",
"additionalParams": true,
"id": "ZepMemory_0-input-autoSummaryTemplate-string"
},
{ {
"label": "AI Prefix", "label": "AI Prefix",
"name": "aiPrefix", "name": "aiPrefix",
@ -306,10 +292,8 @@
"inputAnchors": [], "inputAnchors": [],
"inputs": { "inputs": {
"baseURL": "http://127.0.0.1:8000", "baseURL": "http://127.0.0.1:8000",
"autoSummary": true,
"sessionId": "", "sessionId": "",
"k": "10", "k": "10",
"autoSummaryTemplate": "This is the summary of the following conversation:\n{summary}",
"aiPrefix": "ai", "aiPrefix": "ai",
"humanPrefix": "human", "humanPrefix": "human",
"memoryKey": "chat_history", "memoryKey": "chat_history",