mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
Adding support for LLM Caching.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
|
||||
import { InMemoryCache } from 'langchain/cache'
|
||||
import { getBaseClasses } from '../../../src'
|
||||
|
||||
class LocalMemoryCache implements INode {
|
||||
label: string
|
||||
name: string
|
||||
version: number
|
||||
description: string
|
||||
type: string
|
||||
icon: string
|
||||
category: string
|
||||
baseClasses: string[]
|
||||
inputs: INodeParams[]
|
||||
outputs: INodeOutputsValue[]
|
||||
inMemoryCache: any
|
||||
|
||||
constructor() {
|
||||
this.label = 'Local (Builtin) Cache'
|
||||
this.name = 'localCache'
|
||||
this.version = 1.0
|
||||
this.type = 'LLMCache'
|
||||
this.icon = 'memorycache.png'
|
||||
this.category = 'LLM Cache'
|
||||
this.baseClasses = [this.type, 'LLMCacheBase']
|
||||
this.inputs = []
|
||||
this.outputs = [
|
||||
{
|
||||
label: 'LLM Cache',
|
||||
name: 'cache',
|
||||
baseClasses: [this.type, ...getBaseClasses(InMemoryCache)]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData): Promise<any> {
|
||||
if (!this.inMemoryCache) {
|
||||
this.inMemoryCache = InMemoryCache.global()
|
||||
}
|
||||
return this.inMemoryCache
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: LocalMemoryCache }
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.7 KiB |
@@ -1,6 +1,8 @@
|
||||
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
|
||||
import { OpenAI, OpenAIInput } from 'langchain/llms/openai'
|
||||
import { BaseLLMParams } from 'langchain/dist/llms/base'
|
||||
import { BaseCache } from 'langchain/schema'
|
||||
|
||||
class OpenAI_LLMs implements INode {
|
||||
label: string
|
||||
@@ -17,7 +19,7 @@ class OpenAI_LLMs implements INode {
|
||||
constructor() {
|
||||
this.label = 'OpenAI'
|
||||
this.name = 'openAI'
|
||||
this.version = 2.0
|
||||
this.version = 3.0
|
||||
this.type = 'OpenAI'
|
||||
this.icon = 'openai.png'
|
||||
this.category = 'LLMs'
|
||||
@@ -30,6 +32,12 @@ class OpenAI_LLMs implements INode {
|
||||
credentialNames: ['openAIApi']
|
||||
}
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Cache',
|
||||
name: 'llmCache',
|
||||
type: 'LLMCache',
|
||||
optional: true
|
||||
},
|
||||
{
|
||||
label: 'Model Name',
|
||||
name: 'modelName',
|
||||
@@ -149,7 +157,9 @@ class OpenAI_LLMs implements INode {
|
||||
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
|
||||
const openAIApiKey = getCredentialParam('openAIApiKey', credentialData, nodeData)
|
||||
|
||||
const obj: Partial<OpenAIInput> & { openAIApiKey?: string } = {
|
||||
const llmCache = nodeData.inputs?.llmCache as BaseCache
|
||||
|
||||
const obj: Partial<OpenAIInput> & BaseLLMParams & { openAIApiKey?: string } = {
|
||||
temperature: parseFloat(temperature),
|
||||
modelName,
|
||||
openAIApiKey,
|
||||
@@ -164,8 +174,9 @@ class OpenAI_LLMs implements INode {
|
||||
if (batchSize) obj.batchSize = parseInt(batchSize, 10)
|
||||
if (bestOf) obj.bestOf = parseInt(bestOf, 10)
|
||||
|
||||
let parsedBaseOptions: any | undefined = undefined
|
||||
if (llmCache) obj.cache = llmCache
|
||||
|
||||
let parsedBaseOptions: any | undefined = undefined
|
||||
if (baseOptions) {
|
||||
try {
|
||||
parsedBaseOptions = typeof baseOptions === 'object' ? baseOptions : JSON.parse(baseOptions)
|
||||
|
||||
Reference in New Issue
Block a user