Extending support for Caching to all LLM's.

This commit is contained in:
vinodkiran
2023-09-29 08:19:44 +05:30
parent d588ac0480
commit d81869fd59
8 changed files with 83 additions and 15 deletions
@@ -1,7 +1,8 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { AzureOpenAIInput, OpenAI, OpenAIInput } from 'langchain/llms/openai' import { AzureOpenAIInput, OpenAI, OpenAIInput } from 'langchain/llms/openai'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class AzureOpenAI_LLMs implements INode { class AzureOpenAI_LLMs implements INode {
label: string label: string
name: string name: string
@@ -17,7 +18,7 @@ class AzureOpenAI_LLMs implements INode {
constructor() { constructor() {
this.label = 'Azure OpenAI' this.label = 'Azure OpenAI'
this.name = 'azureOpenAI' this.name = 'azureOpenAI'
this.version = 1.0 this.version = 2.0
this.type = 'AzureOpenAI' this.type = 'AzureOpenAI'
this.icon = 'Azure.svg' this.icon = 'Azure.svg'
this.category = 'LLMs' this.category = 'LLMs'
@@ -30,6 +31,12 @@ class AzureOpenAI_LLMs implements INode {
credentialNames: ['azureOpenAIApi'] credentialNames: ['azureOpenAIApi']
} }
this.inputs = [ this.inputs = [
{
label: 'Cache',
name: 'llmCache',
type: 'LLMCache',
optional: true
},
{ {
label: 'Model Name', label: 'Model Name',
name: 'modelName', name: 'modelName',
@@ -163,7 +170,9 @@ class AzureOpenAI_LLMs implements INode {
const azureOpenAIApiDeploymentName = getCredentialParam('azureOpenAIApiDeploymentName', credentialData, nodeData) const azureOpenAIApiDeploymentName = getCredentialParam('azureOpenAIApiDeploymentName', credentialData, nodeData)
const azureOpenAIApiVersion = getCredentialParam('azureOpenAIApiVersion', credentialData, nodeData) const azureOpenAIApiVersion = getCredentialParam('azureOpenAIApiVersion', credentialData, nodeData)
const obj: Partial<AzureOpenAIInput> & Partial<OpenAIInput> = { const llmCache = nodeData.inputs?.llmCache as BaseCache
const obj: Partial<AzureOpenAIInput> & BaseLLMParams & Partial<OpenAIInput> = {
temperature: parseFloat(temperature), temperature: parseFloat(temperature),
modelName, modelName,
azureOpenAIApiKey, azureOpenAIApiKey,
@@ -179,6 +188,7 @@ class AzureOpenAI_LLMs implements INode {
if (presencePenalty) obj.presencePenalty = parseFloat(presencePenalty) if (presencePenalty) obj.presencePenalty = parseFloat(presencePenalty)
if (timeout) obj.timeout = parseInt(timeout, 10) if (timeout) obj.timeout = parseInt(timeout, 10)
if (bestOf) obj.bestOf = parseInt(bestOf, 10) if (bestOf) obj.bestOf = parseInt(bestOf, 10)
if (llmCache) obj.cache = llmCache
const model = new OpenAI(obj) const model = new OpenAI(obj)
return model return model
@@ -1,6 +1,8 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface' import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils' import { getBaseClasses } from '../../../src/utils'
import { NIBittensorLLM, BittensorInput } from 'langchain/experimental/llms/bittensor' import { NIBittensorLLM, BittensorInput } from 'langchain/experimental/llms/bittensor'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class Bittensor_LLMs implements INode { class Bittensor_LLMs implements INode {
label: string label: string
@@ -16,13 +18,19 @@ class Bittensor_LLMs implements INode {
constructor() { constructor() {
this.label = 'NIBittensorLLM' this.label = 'NIBittensorLLM'
this.name = 'NIBittensorLLM' this.name = 'NIBittensorLLM'
this.version = 1.0 this.version = 2.0
this.type = 'Bittensor' this.type = 'Bittensor'
this.icon = 'logo.png' this.icon = 'logo.png'
this.category = 'LLMs' this.category = 'LLMs'
this.description = 'Wrapper around Bittensor subnet 1 large language models' this.description = 'Wrapper around Bittensor subnet 1 large language models'
this.baseClasses = [this.type, ...getBaseClasses(NIBittensorLLM)] this.baseClasses = [this.type, ...getBaseClasses(NIBittensorLLM)]
this.inputs = [ this.inputs = [
{
label: 'Cache',
name: 'llmCache',
type: 'LLMCache',
optional: true
},
{ {
label: 'System prompt', label: 'System prompt',
name: 'system_prompt', name: 'system_prompt',
@@ -44,10 +52,13 @@ class Bittensor_LLMs implements INode {
async init(nodeData: INodeData, _: string): Promise<any> { async init(nodeData: INodeData, _: string): Promise<any> {
const system_prompt = nodeData.inputs?.system_prompt as string const system_prompt = nodeData.inputs?.system_prompt as string
const topResponses = Number(nodeData.inputs?.topResponses as number) const topResponses = Number(nodeData.inputs?.topResponses as number)
const obj: Partial<BittensorInput> = { const llmCache = nodeData.inputs?.llmCache as BaseCache
const obj: Partial<BittensorInput> & BaseLLMParams = {
systemPrompt: system_prompt, systemPrompt: system_prompt,
topResponses: topResponses topResponses: topResponses
} }
if (llmCache) obj.cache = llmCache
const model = new NIBittensorLLM(obj) const model = new NIBittensorLLM(obj)
return model return model
@@ -1,6 +1,7 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { Cohere, CohereInput } from './core' import { Cohere, CohereInput } from './core'
import { BaseCache } from 'langchain/schema'
class Cohere_LLMs implements INode { class Cohere_LLMs implements INode {
label: string label: string
@@ -17,7 +18,7 @@ class Cohere_LLMs implements INode {
constructor() { constructor() {
this.label = 'Cohere' this.label = 'Cohere'
this.name = 'cohere' this.name = 'cohere'
this.version = 1.0 this.version = 2.0
this.type = 'Cohere' this.type = 'Cohere'
this.icon = 'cohere.png' this.icon = 'cohere.png'
this.category = 'LLMs' this.category = 'LLMs'
@@ -30,6 +31,12 @@ class Cohere_LLMs implements INode {
credentialNames: ['cohereApi'] credentialNames: ['cohereApi']
} }
this.inputs = [ this.inputs = [
{
label: 'Cache',
name: 'llmCache',
type: 'LLMCache',
optional: true
},
{ {
label: 'Model Name', label: 'Model Name',
name: 'modelName', name: 'modelName',
@@ -85,7 +92,7 @@ class Cohere_LLMs implements INode {
const temperature = nodeData.inputs?.temperature as string const temperature = nodeData.inputs?.temperature as string
const modelName = nodeData.inputs?.modelName as string const modelName = nodeData.inputs?.modelName as string
const maxTokens = nodeData.inputs?.maxTokens as string const maxTokens = nodeData.inputs?.maxTokens as string
const llmCache = nodeData.inputs?.llmCache as BaseCache
const credentialData = await getCredentialData(nodeData.credential ?? '', options) const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const cohereApiKey = getCredentialParam('cohereApiKey', credentialData, nodeData) const cohereApiKey = getCredentialParam('cohereApiKey', credentialData, nodeData)
@@ -96,7 +103,7 @@ class Cohere_LLMs implements INode {
if (maxTokens) obj.maxTokens = parseInt(maxTokens, 10) if (maxTokens) obj.maxTokens = parseInt(maxTokens, 10)
if (modelName) obj.model = modelName if (modelName) obj.model = modelName
if (temperature) obj.temperature = parseFloat(temperature) if (temperature) obj.temperature = parseFloat(temperature)
if (llmCache) obj.cache = llmCache
const model = new Cohere(obj) const model = new Cohere(obj)
return model return model
} }
@@ -1,7 +1,7 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { GooglePaLM, GooglePaLMTextInput } from 'langchain/llms/googlepalm' import { GooglePaLM, GooglePaLMTextInput } from 'langchain/llms/googlepalm'
import { BaseCache } from 'langchain/schema'
class GooglePaLM_LLMs implements INode { class GooglePaLM_LLMs implements INode {
label: string label: string
name: string name: string
@@ -17,7 +17,7 @@ class GooglePaLM_LLMs implements INode {
constructor() { constructor() {
this.label = 'GooglePaLM' this.label = 'GooglePaLM'
this.name = 'GooglePaLM' this.name = 'GooglePaLM'
this.version = 1.0 this.version = 2.0
this.type = 'GooglePaLM' this.type = 'GooglePaLM'
this.icon = 'Google_PaLM_Logo.svg' this.icon = 'Google_PaLM_Logo.svg'
this.category = 'LLMs' this.category = 'LLMs'
@@ -30,6 +30,12 @@ class GooglePaLM_LLMs implements INode {
credentialNames: ['googleMakerSuite'] credentialNames: ['googleMakerSuite']
} }
this.inputs = [ this.inputs = [
{
label: 'Cache',
name: 'llmCache',
type: 'LLMCache',
optional: true
},
{ {
label: 'Model Name', label: 'Model Name',
name: 'modelName', name: 'modelName',
@@ -126,6 +132,7 @@ class GooglePaLM_LLMs implements INode {
const topP = nodeData.inputs?.topP as string const topP = nodeData.inputs?.topP as string
const topK = nodeData.inputs?.topK as string const topK = nodeData.inputs?.topK as string
const stopSequencesObj = nodeData.inputs?.stopSequencesObj const stopSequencesObj = nodeData.inputs?.stopSequencesObj
const llmCache = nodeData.inputs?.llmCache as BaseCache
const credentialData = await getCredentialData(nodeData.credential ?? '', options) const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const googleMakerSuiteKey = getCredentialParam('googleMakerSuiteKey', credentialData, nodeData) const googleMakerSuiteKey = getCredentialParam('googleMakerSuiteKey', credentialData, nodeData)
@@ -139,6 +146,7 @@ class GooglePaLM_LLMs implements INode {
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10) if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP) if (topP) obj.topP = parseFloat(topP)
if (topK) obj.topK = parseFloat(topK) if (topK) obj.topK = parseFloat(topK)
if (llmCache) obj.cache = llmCache
let parsedStopSequences: any | undefined = undefined let parsedStopSequences: any | undefined = undefined
if (stopSequencesObj) { if (stopSequencesObj) {
@@ -2,6 +2,7 @@ import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Inter
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { GoogleVertexAI, GoogleVertexAITextInput } from 'langchain/llms/googlevertexai' import { GoogleVertexAI, GoogleVertexAITextInput } from 'langchain/llms/googlevertexai'
import { GoogleAuthOptions } from 'google-auth-library' import { GoogleAuthOptions } from 'google-auth-library'
import { BaseCache } from 'langchain/schema'
class GoogleVertexAI_LLMs implements INode { class GoogleVertexAI_LLMs implements INode {
label: string label: string
@@ -18,7 +19,7 @@ class GoogleVertexAI_LLMs implements INode {
constructor() { constructor() {
this.label = 'GoogleVertexAI' this.label = 'GoogleVertexAI'
this.name = 'googlevertexai' this.name = 'googlevertexai'
this.version = 1.0 this.version = 2.0
this.type = 'GoogleVertexAI' this.type = 'GoogleVertexAI'
this.icon = 'vertexai.svg' this.icon = 'vertexai.svg'
this.category = 'LLMs' this.category = 'LLMs'
@@ -34,6 +35,12 @@ class GoogleVertexAI_LLMs 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.' '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 = [ this.inputs = [
{
label: 'Cache',
name: 'llmCache',
type: 'LLMCache',
optional: true
},
{ {
label: 'Model Name', label: 'Model Name',
name: 'modelName', name: 'modelName',
@@ -120,6 +127,7 @@ class GoogleVertexAI_LLMs implements INode {
const modelName = nodeData.inputs?.modelName as string const modelName = nodeData.inputs?.modelName as string
const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string
const topP = nodeData.inputs?.topP as string const topP = nodeData.inputs?.topP as string
const llmCache = nodeData.inputs?.llmCache as BaseCache
const obj: Partial<GoogleVertexAITextInput> = { const obj: Partial<GoogleVertexAITextInput> = {
temperature: parseFloat(temperature), temperature: parseFloat(temperature),
@@ -129,6 +137,7 @@ class GoogleVertexAI_LLMs implements INode {
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10) if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP) if (topP) obj.topP = parseFloat(topP)
if (llmCache) obj.cache = llmCache
const model = new GoogleVertexAI(obj) const model = new GoogleVertexAI(obj)
return model return model
@@ -1,6 +1,7 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { HFInput, HuggingFaceInference } from './core' import { HFInput, HuggingFaceInference } from './core'
import { BaseCache } from 'langchain/schema'
class HuggingFaceInference_LLMs implements INode { class HuggingFaceInference_LLMs implements INode {
label: string label: string
@@ -17,7 +18,7 @@ class HuggingFaceInference_LLMs implements INode {
constructor() { constructor() {
this.label = 'HuggingFace Inference' this.label = 'HuggingFace Inference'
this.name = 'huggingFaceInference_LLMs' this.name = 'huggingFaceInference_LLMs'
this.version = 1.0 this.version = 2.0
this.type = 'HuggingFaceInference' this.type = 'HuggingFaceInference'
this.icon = 'huggingface.png' this.icon = 'huggingface.png'
this.category = 'LLMs' this.category = 'LLMs'
@@ -30,6 +31,12 @@ class HuggingFaceInference_LLMs implements INode {
credentialNames: ['huggingFaceApi'] credentialNames: ['huggingFaceApi']
} }
this.inputs = [ this.inputs = [
{
label: 'Cache',
name: 'llmCache',
type: 'LLMCache',
optional: true
},
{ {
label: 'Model', label: 'Model',
name: 'model', name: 'model',
@@ -106,6 +113,8 @@ class HuggingFaceInference_LLMs implements INode {
const credentialData = await getCredentialData(nodeData.credential ?? '', options) const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const huggingFaceApiKey = getCredentialParam('huggingFaceApiKey', credentialData, nodeData) const huggingFaceApiKey = getCredentialParam('huggingFaceApiKey', credentialData, nodeData)
const llmCache = nodeData.inputs?.llmCache as BaseCache
const obj: Partial<HFInput> = { const obj: Partial<HFInput> = {
model, model,
apiKey: huggingFaceApiKey apiKey: huggingFaceApiKey
@@ -119,6 +128,8 @@ class HuggingFaceInference_LLMs implements INode {
if (endpoint) obj.endpoint = endpoint if (endpoint) obj.endpoint = endpoint
const huggingFace = new HuggingFaceInference(obj) const huggingFace = new HuggingFaceInference(obj)
if (llmCache) huggingFace.cache = llmCache
return huggingFace return huggingFace
} }
} }
@@ -1,7 +1,7 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { OpenAI, OpenAIInput } from 'langchain/llms/openai' import { OpenAI, OpenAIInput } from 'langchain/llms/openai'
import { BaseLLMParams } from 'langchain/dist/llms/base' import { BaseLLMParams } from 'langchain/llms/base'
import { BaseCache } from 'langchain/schema' import { BaseCache } from 'langchain/schema'
class OpenAI_LLMs implements INode { class OpenAI_LLMs implements INode {
@@ -1,6 +1,8 @@
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { Replicate, ReplicateInput } from 'langchain/llms/replicate' import { Replicate, ReplicateInput } from 'langchain/llms/replicate'
import { BaseCache } from 'langchain/schema'
import { BaseLLMParams } from 'langchain/llms/base'
class Replicate_LLMs implements INode { class Replicate_LLMs implements INode {
label: string label: string
@@ -17,7 +19,7 @@ class Replicate_LLMs implements INode {
constructor() { constructor() {
this.label = 'Replicate' this.label = 'Replicate'
this.name = 'replicate' this.name = 'replicate'
this.version = 1.0 this.version = 2.0
this.type = 'Replicate' this.type = 'Replicate'
this.icon = 'replicate.svg' this.icon = 'replicate.svg'
this.category = 'LLMs' this.category = 'LLMs'
@@ -30,6 +32,12 @@ class Replicate_LLMs implements INode {
credentialNames: ['replicateApi'] credentialNames: ['replicateApi']
} }
this.inputs = [ this.inputs = [
{
label: 'Cache',
name: 'llmCache',
type: 'LLMCache',
optional: true
},
{ {
label: 'Model', label: 'Model',
name: 'model', name: 'model',
@@ -103,7 +111,9 @@ class Replicate_LLMs implements INode {
const name = modelName.split(':')[0].split('/').pop() const name = modelName.split(':')[0].split('/').pop()
const org = modelName.split(':')[0].split('/')[0] const org = modelName.split(':')[0].split('/')[0]
const obj: ReplicateInput = { const llmCache = nodeData.inputs?.llmCache as BaseCache
const obj: ReplicateInput & BaseLLMParams = {
model: `${org}/${name}:${version}`, model: `${org}/${name}:${version}`,
apiKey apiKey
} }
@@ -120,6 +130,8 @@ class Replicate_LLMs implements INode {
} }
if (Object.keys(inputs).length) obj.input = inputs if (Object.keys(inputs).length) obj.input = inputs
if (llmCache) obj.cache = llmCache
const model = new Replicate(obj) const model = new Replicate(obj)
return model return model
} }