Add feature to be able to chain prompt values

This commit is contained in:
Henry
2023-04-16 23:17:08 +01:00
parent 0681a34408
commit 4b9c39cf54
25 changed files with 1496 additions and 246 deletions
@@ -1,4 +1,4 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src/utils'
import { LLMChain } from 'langchain/chains'
import { BaseLanguageModel } from 'langchain/base_language'
@@ -13,6 +13,7 @@ class LLMChain_Chains implements INode {
baseClasses: string[]
description: string
inputs: INodeParams[]
outputs: INodeOutputsValue[]
constructor() {
this.label = 'LLM Chain'
@@ -33,6 +34,13 @@ class LLMChain_Chains implements INode {
name: 'prompt',
type: 'BasePromptTemplate'
},
{
label: 'Chain Name',
name: 'chainName',
type: 'string',
placeholder: 'Task Creation Chain',
optional: true
},
{
label: 'Format Prompt Values',
name: 'promptValues',
@@ -42,57 +50,99 @@ class LLMChain_Chains implements INode {
"input_language": "English",
"output_language": "French"
}`,
optional: true
optional: true,
acceptVariable: true,
list: true
}
]
this.outputs = [
{
label: this.label,
name: this.name,
type: this.type
},
{
label: 'Output Prediction',
name: 'outputPrediction',
type: 'string'
}
]
}
async init(nodeData: INodeData): Promise<any> {
async init(nodeData: INodeData, input: string): Promise<any> {
const model = nodeData.inputs?.model as BaseLanguageModel
const prompt = nodeData.inputs?.prompt as BasePromptTemplate
const output = nodeData.outputs?.output as string
const promptValuesStr = nodeData.inputs?.promptValues as string
const chain = new LLMChain({ llm: model, prompt })
return chain
if (output === this.name) {
const chain = new LLMChain({ llm: model, prompt })
return chain
} else if (output === 'outputPrediction') {
const chain = new LLMChain({ llm: model, prompt })
const inputVariables = chain.prompt.inputVariables as string[] // ["product"]
const res = await runPrediction(inputVariables, chain, input, promptValuesStr)
// eslint-disable-next-line no-console
console.log('\x1b[92m\x1b[1m\n*****OUTPUT PREDICTION*****\n\x1b[0m\x1b[0m')
// eslint-disable-next-line no-console
console.log(res)
return res
}
}
async run(nodeData: INodeData, input: string): Promise<string> {
const inputVariables = nodeData.instance.prompt.inputVariables as string[] // ["product"]
const chain = nodeData.instance as LLMChain
const promptValuesStr = nodeData.inputs?.promptValues as string
const res = await runPrediction(inputVariables, chain, input, promptValuesStr)
// eslint-disable-next-line no-console
console.log('\x1b[93m\x1b[1m\n*****FINAL RESULT*****\n\x1b[0m\x1b[0m')
// eslint-disable-next-line no-console
console.log(res)
return res
}
}
if (inputVariables.length === 1) {
const res = await chain.run(input)
return res
} else if (inputVariables.length > 1) {
const promptValuesStr = nodeData.inputs?.promptValues as string
if (!promptValuesStr) throw new Error('Please provide Prompt Values')
const runPrediction = async (inputVariables: string[], chain: LLMChain, input: string, promptValuesStr: string) => {
if (inputVariables.length === 1) {
const res = await chain.run(input)
return res
} else if (inputVariables.length > 1) {
if (!promptValuesStr) throw new Error('Please provide Prompt Values')
const promptValues = JSON.parse(promptValuesStr.replace(/\s/g, ''))
const promptValues = JSON.parse(promptValuesStr.replace(/\s/g, ''))
let seen: string[] = []
let seen: string[] = []
for (const variable of inputVariables) {
seen.push(variable)
if (promptValues[variable]) {
seen.pop()
}
for (const variable of inputVariables) {
seen.push(variable)
if (promptValues[variable]) {
seen.pop()
}
if (seen.length === 1) {
const lastValue = seen.pop()
if (!lastValue) throw new Error('Please provide Prompt Values')
const options = {
...promptValues,
[lastValue]: input
}
const res = await chain.call(options)
return res?.text
} else {
throw new Error('Please provide Prompt Values')
}
} else {
const res = await chain.run(input)
return res
}
if (seen.length === 0) {
// All inputVariables have fixed values specified
const options = {
...promptValues
}
const res = await chain.call(options)
return res?.text
} else if (seen.length === 1) {
// If one inputVariable is not specify, use input (user's question) as value
const lastValue = seen.pop()
if (!lastValue) throw new Error('Please provide Prompt Values')
const options = {
...promptValues,
[lastValue]: input
}
const res = await chain.call(options)
return res?.text
} else {
throw new Error(`Please provide Prompt Values for: ${seen.join(', ')}`)
}
} else {
const res = await chain.run(input)
return res
}
}
+13 -13
View File
@@ -2,18 +2,7 @@
* Types
*/
export type NodeParamsType =
| 'asyncOptions'
| 'options'
| 'string'
| 'number'
| 'boolean'
| 'password'
| 'json'
| 'code'
| 'date'
| 'file'
| 'folder'
export type NodeParamsType = 'options' | 'string' | 'number' | 'boolean' | 'password' | 'json' | 'code' | 'date' | 'file' | 'folder'
export type CommonType = string | number | boolean | undefined | null
@@ -40,6 +29,13 @@ export interface INodeOptionsValue {
description?: string
}
export interface INodeOutputsValue {
label: string
name: string
type: string
description?: string
}
export interface INodeParams {
label: string
name: string
@@ -50,6 +46,7 @@ export interface INodeParams {
optional?: boolean | INodeDisplay
rows?: number
list?: boolean
acceptVariable?: boolean
placeholder?: string
fileType?: string
}
@@ -75,12 +72,15 @@ export interface INodeProperties {
export interface INode extends INodeProperties {
inputs?: INodeParams[]
getInstance?(nodeData: INodeData): Promise<string>
output?: INodeOutputsValue[]
init?(nodeData: INodeData, input: string, options?: ICommonObject): Promise<any>
run?(nodeData: INodeData, input: string, options?: ICommonObject): Promise<string>
}
export interface INodeData extends INodeProperties {
id: string
inputs?: ICommonObject
outputs?: ICommonObject
instance?: any
}