mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 17:01:00 +03:00
Adding support for LLM Caching.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Types
|
||||
*/
|
||||
import { BaseCache } from 'langchain/schema'
|
||||
|
||||
export type NodeParamsType =
|
||||
| 'asyncOptions'
|
||||
@@ -176,3 +177,9 @@ export class VectorStoreRetriever {
|
||||
this.vectorStore = fields.vectorStore
|
||||
}
|
||||
}
|
||||
|
||||
export interface LLMCacheBase {
|
||||
name: string
|
||||
description: string
|
||||
baseCache: BaseCache
|
||||
}
|
||||
|
||||
@@ -151,6 +151,7 @@ export class CustomChainHandler extends BaseCallbackHandler {
|
||||
socketIOClientId = ''
|
||||
skipK = 0 // Skip streaming for first K numbers of handleLLMStart
|
||||
returnSourceDocuments = false
|
||||
cachedResponse = true
|
||||
|
||||
constructor(socketIO: Server, socketIOClientId: string, skipK?: number, returnSourceDocuments?: boolean) {
|
||||
super()
|
||||
@@ -161,6 +162,7 @@ export class CustomChainHandler extends BaseCallbackHandler {
|
||||
}
|
||||
|
||||
handleLLMStart() {
|
||||
this.cachedResponse = false
|
||||
if (this.skipK > 0) this.skipK -= 1
|
||||
}
|
||||
|
||||
@@ -175,13 +177,31 @@ export class CustomChainHandler extends BaseCallbackHandler {
|
||||
}
|
||||
|
||||
handleLLMEnd() {
|
||||
this.socketIO.to(this.socketIOClientId).emit('end')
|
||||
/* send the end event from handleChainEnd */
|
||||
// this.socketIO.to(this.socketIOClientId).emit('end')
|
||||
}
|
||||
|
||||
handleChainEnd(outputs: ChainValues): void | Promise<void> {
|
||||
if (this.returnSourceDocuments) {
|
||||
this.socketIO.to(this.socketIOClientId).emit('sourceDocuments', outputs?.sourceDocuments)
|
||||
}
|
||||
/*
|
||||
Langchain does not call handleLLMStart, handleLLMEnd, handleLLMNewToken when the chain is cached.
|
||||
Callback Order is "Chain Start -> LLM Start --> LLM Token --> LLM End -> Chain End" for normal responses.
|
||||
Callback Order is "Chain Start -> Chain End" for cached responses.
|
||||
*/
|
||||
if (this.cachedResponse) {
|
||||
const cachedValue = outputs.text as string
|
||||
//split at whitespace, and keep the whitespace. This is to preserve the original formatting.
|
||||
const result = cachedValue.split(/(\s+)/)
|
||||
result.forEach((token: string, index: number) => {
|
||||
if (index === 0) {
|
||||
this.socketIO.to(this.socketIOClientId).emit('start', token)
|
||||
}
|
||||
this.socketIO.to(this.socketIOClientId).emit('token', token)
|
||||
})
|
||||
}
|
||||
this.socketIO.to(this.socketIOClientId).emit('end')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user