Skip to content

Commit 76ac97f

Browse files
wip
1 parent 9cb901c commit 76ac97f

File tree

5 files changed

+445
-0
lines changed

5 files changed

+445
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import { IMessage, INode, INodeData, INodeParams, MemoryMethods, MessageType } from '../../../src/Interface'
2+
import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
3+
import { ZepMemory, ZepMemoryInput } from '@getzep/zep-cloud/langchain'
4+
5+
import { ICommonObject } from '../../../src'
6+
import { InputValues, MemoryVariables, OutputValues } from 'langchain/memory'
7+
import { BaseMessage } from 'langchain/schema'
8+
9+
class ZepMemoryCloud_Memory implements INode {
10+
label: string
11+
name: string
12+
version: number
13+
description: string
14+
type: string
15+
icon: string
16+
category: string
17+
baseClasses: string[]
18+
credential: INodeParams
19+
inputs: INodeParams[]
20+
21+
constructor() {
22+
this.label = 'Zep Memory - Cloud'
23+
this.name = 'ZepMemory (Cloud)'
24+
this.version = 2.0
25+
this.type = 'ZepMemory'
26+
this.icon = 'zep.svg'
27+
this.category = 'Memory'
28+
this.description = 'Summarizes the conversation and stores the memory in zep server'
29+
this.baseClasses = [this.type, ...getBaseClasses(ZepMemory)]
30+
this.credential = {
31+
label: 'Connect Credential',
32+
name: 'credential',
33+
type: 'credential',
34+
optional: true,
35+
description: 'Configure JWT authentication on your Zep instance (Optional)',
36+
credentialNames: ['zepMemoryApi']
37+
}
38+
this.inputs = [
39+
{
40+
label: 'Session Id',
41+
name: 'sessionId',
42+
type: 'string',
43+
description:
44+
'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>',
45+
default: '',
46+
additionalParams: true,
47+
optional: true
48+
},
49+
{
50+
label: 'Memory Type',
51+
name: 'memoryType',
52+
type: 'string',
53+
default: 'perpetual',
54+
description: 'Zep Memory Type, can be perpetual or message_window',
55+
additionalParams: true
56+
},
57+
{
58+
label: 'AI Prefix',
59+
name: 'aiPrefix',
60+
type: 'string',
61+
default: 'ai',
62+
additionalParams: true
63+
},
64+
{
65+
label: 'Human Prefix',
66+
name: 'humanPrefix',
67+
type: 'string',
68+
default: 'human',
69+
additionalParams: true
70+
},
71+
{
72+
label: 'Memory Key',
73+
name: 'memoryKey',
74+
type: 'string',
75+
default: 'chat_history',
76+
additionalParams: true
77+
},
78+
{
79+
label: 'Output Key',
80+
name: 'outputKey',
81+
type: 'string',
82+
default: 'text',
83+
additionalParams: true
84+
}
85+
]
86+
}
87+
88+
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
89+
return await initalizeZep(nodeData, options)
90+
}
91+
}
92+
93+
const initalizeZep = async (nodeData: INodeData, options: ICommonObject): Promise<ZepMemory> => {
94+
const aiPrefix = nodeData.inputs?.aiPrefix as string
95+
const humanPrefix = nodeData.inputs?.humanPrefix as string
96+
const memoryKey = nodeData.inputs?.memoryKey as string
97+
const memoryType = nodeData.inputs?.memoryType as 'perpetual' | 'message_window'
98+
const sessionId = nodeData.inputs?.sessionId as string
99+
100+
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
101+
const apiKey = getCredentialParam('apiKey', credentialData, nodeData)
102+
103+
const obj: ZepMemoryInput & ZepMemoryExtendedInput = {
104+
apiKey,
105+
baseURL: 'https://api.development.getzep.com',
106+
aiPrefix,
107+
humanPrefix,
108+
memoryKey,
109+
sessionId,
110+
memoryType: memoryType
111+
}
112+
113+
return new ZepMemoryExtended(obj)
114+
}
115+
116+
interface ZepMemoryExtendedInput {
117+
memoryType?: 'perpetual' | 'message_window'
118+
}
119+
120+
class ZepMemoryExtended extends ZepMemory implements MemoryMethods {
121+
memoryType: 'perpetual' | 'message_window'
122+
123+
constructor(fields: ZepMemoryInput & ZepMemoryExtendedInput) {
124+
super(fields)
125+
this.memoryType = fields.memoryType ?? 'perpetual'
126+
}
127+
128+
async loadMemoryVariables(values: InputValues, overrideSessionId = ''): Promise<MemoryVariables> {
129+
if (overrideSessionId) {
130+
this.sessionId = overrideSessionId
131+
}
132+
return super.loadMemoryVariables({ ...values, memoryType: this.memoryType })
133+
}
134+
135+
async saveContext(inputValues: InputValues, outputValues: OutputValues, overrideSessionId = ''): Promise<void> {
136+
if (overrideSessionId) {
137+
this.sessionId = overrideSessionId
138+
}
139+
return super.saveContext(inputValues, outputValues)
140+
}
141+
142+
async clear(overrideSessionId = ''): Promise<void> {
143+
if (overrideSessionId) {
144+
this.sessionId = overrideSessionId
145+
}
146+
return super.clear()
147+
}
148+
149+
async getChatMessages(overrideSessionId = '', returnBaseMessages = false): Promise<IMessage[] | BaseMessage[]> {
150+
const id = overrideSessionId ? overrideSessionId : this.sessionId
151+
const memoryVariables = await this.loadMemoryVariables({}, id)
152+
const baseMessages = memoryVariables[this.memoryKey]
153+
return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages)
154+
}
155+
156+
async addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId = ''): Promise<void> {
157+
const id = overrideSessionId ? overrideSessionId : this.sessionId
158+
const input = msgArray.find((msg) => msg.type === 'userMessage')
159+
const output = msgArray.find((msg) => msg.type === 'apiMessage')
160+
const inputValues = { ['input']: input?.text }
161+
const outputValues = { output: output?.text }
162+
163+
await this.saveContext(inputValues, outputValues, id)
164+
}
165+
166+
async clearChatMessages(overrideSessionId = ''): Promise<void> {
167+
const id = overrideSessionId ? overrideSessionId : this.sessionId
168+
await this.clear(id)
169+
}
170+
}
171+
172+
module.exports = { nodeClass: ZepMemoryCloud_Memory }
Lines changed: 19 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)