-
-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1036 from FlowiseAI/feature/InMemoryCache
Feature/In-Mem LLMcache
- Loading branch information
Showing
11 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
65 changes: 65 additions & 0 deletions
65
packages/components/nodes/cache/InMemoryCache/InMemoryCache.ts
This file contains 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,65 @@ | ||
import { getBaseClasses, ICommonObject, INode, INodeData, INodeParams } from '../../../src' | ||
import { BaseCache } from 'langchain/schema' | ||
import hash from 'object-hash' | ||
|
||
class InMemoryCache implements INode { | ||
label: string | ||
name: string | ||
version: number | ||
description: string | ||
type: string | ||
icon: string | ||
category: string | ||
baseClasses: string[] | ||
inputs: INodeParams[] | ||
credential: INodeParams | ||
|
||
constructor() { | ||
this.label = 'InMemory Cache' | ||
this.name = 'inMemoryCache' | ||
this.version = 1.0 | ||
this.type = 'InMemoryCache' | ||
this.description = 'Cache LLM response in memory, will be cleared once app restarted' | ||
this.icon = 'inmemorycache.png' | ||
this.category = 'Cache' | ||
this.baseClasses = [this.type, ...getBaseClasses(InMemoryCacheExtended)] | ||
this.inputs = [] | ||
} | ||
|
||
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { | ||
const memoryMap = options.cachePool.getLLMCache(options.chatflowid) ?? new Map() | ||
const inMemCache = new InMemoryCacheExtended(memoryMap) | ||
|
||
inMemCache.lookup = async (prompt: string, llmKey: string): Promise<any | null> => { | ||
const memory = options.cachePool.getLLMCache(options.chatflowid) ?? inMemCache.cache | ||
return Promise.resolve(memory.get(getCacheKey(prompt, llmKey)) ?? null) | ||
} | ||
|
||
inMemCache.update = async (prompt: string, llmKey: string, value: any): Promise<void> => { | ||
inMemCache.cache.set(getCacheKey(prompt, llmKey), value) | ||
options.cachePool.addLLMCache(options.chatflowid, inMemCache.cache) | ||
} | ||
return inMemCache | ||
} | ||
} | ||
|
||
const getCacheKey = (...strings: string[]): string => hash(strings.join('_')) | ||
|
||
class InMemoryCacheExtended extends BaseCache { | ||
cache: Map<string, any> | ||
|
||
constructor(map: Map<string, any>) { | ||
super() | ||
this.cache = map | ||
} | ||
|
||
lookup(prompt: string, llmKey: string): Promise<any | null> { | ||
return Promise.resolve(this.cache.get(getCacheKey(prompt, llmKey)) ?? null) | ||
} | ||
|
||
async update(prompt: string, llmKey: string, value: any): Promise<void> { | ||
this.cache.set(getCacheKey(prompt, llmKey), value) | ||
} | ||
} | ||
|
||
module.exports = { nodeClass: InMemoryCache } |
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 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 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 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 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 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,53 @@ | ||
import { IActiveCache } from './Interface' | ||
|
||
/** | ||
* This pool is to keep track of in-memory cache used for LLM and Embeddings | ||
*/ | ||
export class CachePool { | ||
activeLLMCache: IActiveCache = {} | ||
activeEmbeddingCache: IActiveCache = {} | ||
|
||
/** | ||
* Add to the llm cache pool | ||
* @param {string} chatflowid | ||
* @param {Map<any, any>} value | ||
*/ | ||
addLLMCache(chatflowid: string, value: Map<any, any>) { | ||
this.activeLLMCache[chatflowid] = value | ||
} | ||
|
||
/** | ||
* Add to the embedding cache pool | ||
* @param {string} chatflowid | ||
* @param {Map<any, any>} value | ||
*/ | ||
addEmbeddingCache(chatflowid: string, value: Map<any, any>) { | ||
this.activeEmbeddingCache[chatflowid] = value | ||
} | ||
|
||
/** | ||
* Get item from llm cache pool | ||
* @param {string} chatflowid | ||
*/ | ||
getLLMCache(chatflowid: string): Map<any, any> | undefined { | ||
return this.activeLLMCache[chatflowid] | ||
} | ||
|
||
/** | ||
* Get item from embedding cache pool | ||
* @param {string} chatflowid | ||
*/ | ||
getEmbeddingCache(chatflowid: string): Map<any, any> | undefined { | ||
return this.activeEmbeddingCache[chatflowid] | ||
} | ||
} | ||
|
||
let cachePoolInstance: CachePool | undefined | ||
|
||
export function getInstance(): CachePool { | ||
if (cachePoolInstance === undefined) { | ||
cachePoolInstance = new CachePool() | ||
} | ||
|
||
return cachePoolInstance | ||
} |
This file contains 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 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 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