-
-
Notifications
You must be signed in to change notification settings - Fork 23.2k
feature/SupportZepMemoryAndAutoSummary #284
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
Merged
HenryHengZJ
merged 5 commits into
FlowiseAI:main
from
wangerzi:feature/SupportZepMemoryAndAutoSummary
Jun 12, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
packages/components/nodes/memory/ZepMemory/ZepMemory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { SystemChatMessage } from 'langchain/schema' | ||
| import { INode, INodeData, INodeParams } from '../../../src/Interface' | ||
| import { getBaseClasses } from '../../../src/utils' | ||
| import { ZepMemory, ZepMemoryInput } from 'langchain/memory/zep' | ||
| import { ICommonObject } from '../../../src' | ||
|
|
||
| class ZepMemory_Memory implements INode { | ||
| label: string | ||
| name: string | ||
| description: string | ||
| type: string | ||
| icon: string | ||
| category: string | ||
| baseClasses: string[] | ||
| inputs: INodeParams[] | ||
|
|
||
| constructor() { | ||
| this.label = 'Zep Memory' | ||
| this.name = 'ZepMemory' | ||
| this.type = 'ZepMemory' | ||
| this.icon = 'memory.svg' | ||
| this.category = 'Memory' | ||
| this.description = 'Summarizes the conversation and stores the memory in zep server' | ||
| this.baseClasses = [this.type, ...getBaseClasses(ZepMemory)] | ||
| this.inputs = [ | ||
| { | ||
| label: 'Base URL', | ||
| name: 'baseURL', | ||
| type: 'string', | ||
| default: 'http://127.0.0.1:8000' | ||
| }, | ||
| { | ||
| label: 'Auto Summary', | ||
| name: 'autoSummary', | ||
| type: 'boolean', | ||
| default: true | ||
| }, | ||
| { | ||
| label: 'Session Id', | ||
| name: 'sessionId', | ||
| type: 'string', | ||
| description: 'if empty, chatId will be used automatically', | ||
| default: '', | ||
| additionalParams: 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', | ||
| name: 'aiPrefix', | ||
| type: 'string', | ||
| default: 'ai', | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Human Prefix', | ||
| name: 'humanPrefix', | ||
| type: 'string', | ||
| default: 'human', | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Memory Key', | ||
| name: 'memoryKey', | ||
| type: 'string', | ||
| default: 'chat_history', | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Input Key', | ||
| name: 'inputKey', | ||
| type: 'string', | ||
| default: 'input', | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Output Key', | ||
| name: 'outputKey', | ||
| type: 'string', | ||
| default: 'text', | ||
| additionalParams: true | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { | ||
| const baseURL = nodeData.inputs?.baseURL as string | ||
| const aiPrefix = nodeData.inputs?.aiPrefix as string | ||
| const humanPrefix = nodeData.inputs?.humanPrefix as string | ||
| const memoryKey = nodeData.inputs?.memoryKey as string | ||
| const inputKey = nodeData.inputs?.inputKey as string | ||
| const autoSummaryTemplate = nodeData.inputs?.autoSummaryTemplate as string | ||
| const autoSummary = nodeData.inputs?.autoSummary as boolean | ||
| const sessionId = nodeData.inputs?.sessionId as string | ||
|
|
||
| const chatId = options?.chatId as string | ||
|
|
||
| const obj: ZepMemoryInput = { | ||
| baseURL, | ||
| sessionId: sessionId ? sessionId : chatId, | ||
| aiPrefix, | ||
| humanPrefix, | ||
| returnMessages: true, | ||
| memoryKey, | ||
| inputKey | ||
| } | ||
|
|
||
| let zep = new ZepMemory(obj) | ||
|
|
||
| // 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 memory = await zep.zepClient.getMemory(zep.sessionId, 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 SystemChatMessage(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 | ||
| } | ||
| } | ||
|
|
||
| module.exports = { nodeClass: ZepMemory_Memory } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.