Addition of Cache Option for Chat Models

This commit is contained in:
vinodkiran
2023-10-08 11:44:43 +05:30
parent dbd655580d
commit 679eac1d0b
10 changed files with 115 additions and 16 deletions
@@ -2,6 +2,8 @@ import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Inter
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ChatBedrock } from 'langchain/chat_models/bedrock'
import { BaseBedrockInput } from 'langchain/dist/util/bedrock'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
/**
* I had to run the following to build the component
@@ -25,7 +27,7 @@ class AWSChatBedrock_ChatModels implements INode {
constructor() {
this.label = 'AWS Bedrock'
this.name = 'awsChatBedrock'
this.version = 1.1
this.version = 2.0
this.type = 'AWSChatBedrock'
this.icon = 'awsBedrock.png'
this.category = 'Chat Models'
@@ -39,6 +41,12 @@ class AWSChatBedrock_ChatModels implements INode {
optional: true
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Region',
name: 'region',
@@ -130,8 +138,9 @@ class AWSChatBedrock_ChatModels implements INode {
const iModel = nodeData.inputs?.model as string
const iTemperature = nodeData.inputs?.temperature as string
const iMax_tokens_to_sample = nodeData.inputs?.max_tokens_to_sample as string
const cache = nodeData.inputs?.llmCache as BaseCache
const obj: BaseBedrockInput = {
const obj: BaseBedrockInput & BaseLLMParams = {
region: iRegion,
model: iModel,
maxTokens: parseInt(iMax_tokens_to_sample, 10),
@@ -157,6 +166,7 @@ class AWSChatBedrock_ChatModels implements INode {
sessionToken: credentialApiSession
}
}
if (cache) obj.cache = cache
const amazonBedrock = new ChatBedrock(obj)
return amazonBedrock
@@ -2,6 +2,8 @@ import { OpenAIBaseInput } from 'langchain/dist/types/openai-types'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { AzureOpenAIInput, ChatOpenAI } from 'langchain/chat_models/openai'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class AzureChatOpenAI_ChatModels implements INode {
label: string
@@ -18,7 +20,7 @@ class AzureChatOpenAI_ChatModels implements INode {
constructor() {
this.label = 'Azure ChatOpenAI'
this.name = 'azureChatOpenAI'
this.version = 1.0
this.version = 2.0
this.type = 'AzureChatOpenAI'
this.icon = 'Azure.svg'
this.category = 'Chat Models'
@@ -31,6 +33,12 @@ class AzureChatOpenAI_ChatModels implements INode {
credentialNames: ['azureOpenAIApi']
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
@@ -107,6 +115,7 @@ class AzureChatOpenAI_ChatModels implements INode {
const presencePenalty = nodeData.inputs?.presencePenalty as string
const timeout = nodeData.inputs?.timeout as string
const streaming = nodeData.inputs?.streaming as boolean
const cache = nodeData.inputs?.llmCache as BaseCache
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const azureOpenAIApiKey = getCredentialParam('azureOpenAIApiKey', credentialData, nodeData)
@@ -114,7 +123,7 @@ class AzureChatOpenAI_ChatModels implements INode {
const azureOpenAIApiDeploymentName = getCredentialParam('azureOpenAIApiDeploymentName', credentialData, nodeData)
const azureOpenAIApiVersion = getCredentialParam('azureOpenAIApiVersion', credentialData, nodeData)
const obj: Partial<AzureOpenAIInput> & Partial<OpenAIBaseInput> = {
const obj: Partial<AzureOpenAIInput> & BaseLLMParams & Partial<OpenAIBaseInput> = {
temperature: parseFloat(temperature),
modelName,
azureOpenAIApiKey,
@@ -128,6 +137,7 @@ class AzureChatOpenAI_ChatModels implements INode {
if (frequencyPenalty) obj.frequencyPenalty = parseFloat(frequencyPenalty)
if (presencePenalty) obj.presencePenalty = parseFloat(presencePenalty)
if (timeout) obj.timeout = parseInt(timeout, 10)
if (cache) obj.cache = cache
const model = new ChatOpenAI(obj)
return model
@@ -1,6 +1,7 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { NIBittensorChatModel, BittensorInput } from 'langchain/experimental/chat_models/bittensor'
import { BaseCache } from 'langchain/schema'
class Bittensor_ChatModels implements INode {
label: string
@@ -16,13 +17,19 @@ class Bittensor_ChatModels implements INode {
constructor() {
this.label = 'NIBittensorChat'
this.name = 'NIBittensorChatModel'
this.version = 1.0
this.version = 2.0
this.type = 'BittensorChat'
this.icon = 'logo.png'
this.category = 'Chat Models'
this.description = 'Wrapper around Bittensor subnet 1 large language models'
this.baseClasses = [this.type, ...getBaseClasses(NIBittensorChatModel)]
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'System prompt',
name: 'system_prompt',
@@ -35,9 +42,13 @@ class Bittensor_ChatModels implements INode {
async init(nodeData: INodeData, _: string): Promise<any> {
const system_prompt = nodeData.inputs?.system_prompt as string
const cache = nodeData.inputs?.llmCache as BaseCache
const obj: Partial<BittensorInput> = {
systemPrompt: system_prompt
}
if (cache) obj.cache = cache
const model = new NIBittensorChatModel(obj)
return model
}
@@ -1,6 +1,8 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { AnthropicInput, ChatAnthropic } from 'langchain/chat_models/anthropic'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class ChatAnthropic_ChatModels implements INode {
label: string
@@ -17,7 +19,7 @@ class ChatAnthropic_ChatModels implements INode {
constructor() {
this.label = 'ChatAnthropic'
this.name = 'chatAnthropic'
this.version = 1.0
this.version = 2.0
this.type = 'ChatAnthropic'
this.icon = 'chatAnthropic.png'
this.category = 'Chat Models'
@@ -30,6 +32,12 @@ class ChatAnthropic_ChatModels implements INode {
credentialNames: ['anthropicApi']
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
@@ -135,11 +143,12 @@ class ChatAnthropic_ChatModels implements INode {
const topP = nodeData.inputs?.topP as string
const topK = nodeData.inputs?.topK as string
const streaming = nodeData.inputs?.streaming as boolean
const cache = nodeData.inputs?.llmCache as BaseCache
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const anthropicApiKey = getCredentialParam('anthropicApiKey', credentialData, nodeData)
const obj: Partial<AnthropicInput> & { anthropicApiKey?: string } = {
const obj: Partial<AnthropicInput> & BaseLLMParams & { anthropicApiKey?: string } = {
temperature: parseFloat(temperature),
modelName,
anthropicApiKey,
@@ -149,6 +158,7 @@ class ChatAnthropic_ChatModels implements INode {
if (maxTokensToSample) obj.maxTokensToSample = parseInt(maxTokensToSample, 10)
if (topP) obj.topP = parseFloat(topP)
if (topK) obj.topK = parseFloat(topK)
if (cache) obj.cache = cache
const model = new ChatAnthropic(obj)
return model
@@ -1,6 +1,7 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ChatGooglePaLM, GooglePaLMChatInput } from 'langchain/chat_models/googlepalm'
import { BaseCache } from 'langchain/schema'
class ChatGooglePaLM_ChatModels implements INode {
label: string
@@ -17,7 +18,7 @@ class ChatGooglePaLM_ChatModels implements INode {
constructor() {
this.label = 'ChatGooglePaLM'
this.name = 'chatGooglePaLM'
this.version = 1.0
this.version = 2.0
this.type = 'ChatGooglePaLM'
this.icon = 'Google_PaLM_Logo.svg'
this.category = 'Chat Models'
@@ -30,6 +31,12 @@ class ChatGooglePaLM_ChatModels implements INode {
credentialNames: ['googleMakerSuite']
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
@@ -96,6 +103,7 @@ class ChatGooglePaLM_ChatModels implements INode {
const temperature = nodeData.inputs?.temperature as string
const topP = nodeData.inputs?.topP as string
const topK = nodeData.inputs?.topK as string
const cache = nodeData.inputs?.llmCache as BaseCache
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const googleMakerSuiteKey = getCredentialParam('googleMakerSuiteKey', credentialData, nodeData)
@@ -108,6 +116,7 @@ class ChatGooglePaLM_ChatModels implements INode {
if (topP) obj.topP = parseFloat(topP)
if (topK) obj.topK = parseFloat(topK)
if (cache) obj.cache = cache
const model = new ChatGooglePaLM(obj)
return model
@@ -2,6 +2,7 @@ import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Inter
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ChatGoogleVertexAI, GoogleVertexAIChatInput } from 'langchain/chat_models/googlevertexai'
import { GoogleAuthOptions } from 'google-auth-library'
import { BaseCache } from 'langchain/schema'
class GoogleVertexAI_ChatModels implements INode {
label: string
@@ -18,7 +19,7 @@ class GoogleVertexAI_ChatModels implements INode {
constructor() {
this.label = 'ChatGoogleVertexAI'
this.name = 'chatGoogleVertexAI'
this.version = 1.0
this.version = 2.0
this.type = 'ChatGoogleVertexAI'
this.icon = 'vertexai.svg'
this.category = 'Chat Models'
@@ -34,6 +35,12 @@ class GoogleVertexAI_ChatModels implements INode {
'Google Vertex AI credential. If you are using a GCP service like Cloud Run, or if you have installed default credentials on your local machine, you do not need to set this credential.'
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
@@ -113,6 +120,7 @@ class GoogleVertexAI_ChatModels implements INode {
const modelName = nodeData.inputs?.modelName as string
const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string
const topP = nodeData.inputs?.topP as string
const cache = nodeData.inputs?.llmCache as BaseCache
const obj: GoogleVertexAIChatInput<GoogleAuthOptions> = {
temperature: parseFloat(temperature),
@@ -122,6 +130,7 @@ class GoogleVertexAI_ChatModels implements INode {
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP)
if (cache) obj.cache = cache
const model = new ChatGoogleVertexAI(obj)
return model
@@ -1,6 +1,7 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { HFInput, HuggingFaceInference } from './core'
import { BaseCache } from 'langchain/schema'
class ChatHuggingFace_ChatModels implements INode {
label: string
@@ -17,7 +18,7 @@ class ChatHuggingFace_ChatModels implements INode {
constructor() {
this.label = 'ChatHuggingFace'
this.name = 'chatHuggingFace'
this.version = 1.0
this.version = 2.0
this.type = 'ChatHuggingFace'
this.icon = 'huggingface.png'
this.category = 'Chat Models'
@@ -30,6 +31,12 @@ class ChatHuggingFace_ChatModels implements INode {
credentialNames: ['huggingFaceApi']
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Model',
name: 'model',
@@ -102,6 +109,7 @@ class ChatHuggingFace_ChatModels implements INode {
const hfTopK = nodeData.inputs?.hfTopK as string
const frequencyPenalty = nodeData.inputs?.frequencyPenalty as string
const endpoint = nodeData.inputs?.endpoint as string
const cache = nodeData.inputs?.llmCache as BaseCache
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const huggingFaceApiKey = getCredentialParam('huggingFaceApiKey', credentialData, nodeData)
@@ -119,6 +127,7 @@ class ChatHuggingFace_ChatModels implements INode {
if (endpoint) obj.endpoint = endpoint
const huggingFace = new HuggingFaceInference(obj)
if (cache) huggingFace.cache = cache
return huggingFace
}
}
@@ -2,6 +2,8 @@ import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { OpenAIChat } from 'langchain/llms/openai'
import { OpenAIChatInput } from 'langchain/chat_models/openai'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class ChatLocalAI_ChatModels implements INode {
label: string
@@ -17,13 +19,19 @@ class ChatLocalAI_ChatModels implements INode {
constructor() {
this.label = 'ChatLocalAI'
this.name = 'chatLocalAI'
this.version = 1.0
this.version = 2.0
this.type = 'ChatLocalAI'
this.icon = 'localai.png'
this.category = 'Chat Models'
this.description = 'Use local LLMs like llama.cpp, gpt4all using LocalAI'
this.baseClasses = [this.type, 'BaseChatModel', ...getBaseClasses(OpenAIChat)]
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Base Path',
name: 'basePath',
@@ -78,8 +86,9 @@ class ChatLocalAI_ChatModels implements INode {
const topP = nodeData.inputs?.topP as string
const timeout = nodeData.inputs?.timeout as string
const basePath = nodeData.inputs?.basePath as string
const cache = nodeData.inputs?.llmCache as BaseCache
const obj: Partial<OpenAIChatInput> & { openAIApiKey?: string } = {
const obj: Partial<OpenAIChatInput> & BaseLLMParams & { openAIApiKey?: string } = {
temperature: parseFloat(temperature),
modelName,
openAIApiKey: 'sk-'
@@ -88,6 +97,7 @@ class ChatLocalAI_ChatModels implements INode {
if (maxTokens) obj.maxTokens = parseInt(maxTokens, 10)
if (topP) obj.topP = parseFloat(topP)
if (timeout) obj.timeout = parseInt(timeout, 10)
if (cache) obj.cache = cache
const model = new OpenAIChat(obj, { basePath })
@@ -1,6 +1,8 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ChatOpenAI, OpenAIChatInput } from 'langchain/chat_models/openai'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class ChatOpenAI_ChatModels implements INode {
label: string
@@ -17,7 +19,7 @@ class ChatOpenAI_ChatModels implements INode {
constructor() {
this.label = 'ChatOpenAI'
this.name = 'chatOpenAI'
this.version = 1.0
this.version = 2.0
this.type = 'ChatOpenAI'
this.icon = 'openai.png'
this.category = 'Chat Models'
@@ -30,6 +32,12 @@ class ChatOpenAI_ChatModels implements INode {
credentialNames: ['openAIApi']
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
@@ -151,7 +159,9 @@ class ChatOpenAI_ChatModels implements INode {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const openAIApiKey = getCredentialParam('openAIApiKey', credentialData, nodeData)
const obj: Partial<OpenAIChatInput> & { openAIApiKey?: string } = {
const cache = nodeData.inputs?.llmCache as BaseCache
const obj: Partial<OpenAIChatInput> & BaseLLMParams & { openAIApiKey?: string } = {
temperature: parseFloat(temperature),
modelName,
openAIApiKey,
@@ -163,6 +173,7 @@ class ChatOpenAI_ChatModels implements INode {
if (frequencyPenalty) obj.frequencyPenalty = parseFloat(frequencyPenalty)
if (presencePenalty) obj.presencePenalty = parseFloat(presencePenalty)
if (timeout) obj.timeout = parseInt(timeout, 10)
if (cache) obj.cache = cache
let parsedBaseOptions: any | undefined = undefined
@@ -1,6 +1,8 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { ChatOpenAI, OpenAIChatInput } from 'langchain/chat_models/openai'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class ChatOpenAICustom_ChatModels implements INode {
label: string
@@ -17,7 +19,7 @@ class ChatOpenAICustom_ChatModels implements INode {
constructor() {
this.label = 'ChatOpenAI Custom'
this.name = 'chatOpenAICustom'
this.version = 1.0
this.version = 2.0
this.type = 'ChatOpenAI-Custom'
this.icon = 'openai.png'
this.category = 'Chat Models'
@@ -31,6 +33,12 @@ class ChatOpenAICustom_ChatModels implements INode {
optional: true
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'LLMCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
@@ -113,11 +121,12 @@ class ChatOpenAICustom_ChatModels implements INode {
const streaming = nodeData.inputs?.streaming as boolean
const basePath = nodeData.inputs?.basepath as string
const baseOptions = nodeData.inputs?.baseOptions
const cache = nodeData.inputs?.llmCache as BaseCache
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const openAIApiKey = getCredentialParam('openAIApiKey', credentialData, nodeData)
const obj: Partial<OpenAIChatInput> & { openAIApiKey?: string } = {
const obj: Partial<OpenAIChatInput> & BaseLLMParams & { openAIApiKey?: string } = {
temperature: parseFloat(temperature),
modelName,
openAIApiKey,
@@ -129,6 +138,7 @@ class ChatOpenAICustom_ChatModels implements INode {
if (frequencyPenalty) obj.frequencyPenalty = parseFloat(frequencyPenalty)
if (presencePenalty) obj.presencePenalty = parseFloat(presencePenalty)
if (timeout) obj.timeout = parseInt(timeout, 10)
if (cache) obj.cache = cache
let parsedBaseOptions: any | undefined = undefined