From aa2075d60b5f0c6370666afca33938f10e0bc8bb Mon Sep 17 00:00:00 2001 From: vinodkiran Date: Sat, 28 Oct 2023 13:59:32 +0530 Subject: [PATCH] AutoFixOutputParser: Addition of Autofix option for all output parsers --- .../nodes/chains/LLMChain/LLMChain.ts | 26 +++++++++++++++---- .../csvlist/CSVListOutputParser.ts | 21 +++++++++++++-- .../customlist/CustomListOutputParser.ts | 17 +++++++++++- .../structured/StructuredOutputParser.ts | 20 +++++++++++++- 4 files changed, 75 insertions(+), 9 deletions(-) diff --git a/packages/components/nodes/chains/LLMChain/LLMChain.ts b/packages/components/nodes/chains/LLMChain/LLMChain.ts index d1860f1b..7ec777be 100644 --- a/packages/components/nodes/chains/LLMChain/LLMChain.ts +++ b/packages/components/nodes/chains/LLMChain/LLMChain.ts @@ -6,6 +6,7 @@ import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from import { BaseOutputParser } from 'langchain/schema/output_parser' import { injectOutputParser } from '../../outputparsers/OutputParserHelpers' import { BaseLLMOutputParser } from 'langchain/schema/output_parser' +import { OutputFixingParser } from 'langchain/output_parsers' class LLMChain_Chains implements INode { label: string @@ -18,6 +19,7 @@ class LLMChain_Chains implements INode { description: string inputs: INodeParams[] outputs: INodeOutputsValue[] + outputParser: BaseOutputParser constructor() { this.label = 'LLM Chain' @@ -72,15 +74,26 @@ class LLMChain_Chains implements INode { const prompt = nodeData.inputs?.prompt const output = nodeData.outputs?.output as string const promptValues = prompt.promptValues as ICommonObject - const llmOutputParser = nodeData.inputs?.outputParser as BaseLLMOutputParser - + const llmOutputParser = nodeData.inputs?.outputParser as BaseOutputParser + this.outputParser = llmOutputParser + if (llmOutputParser) { + let autoFix = (llmOutputParser as any).autoFix + if (autoFix === true) { + this.outputParser = OutputFixingParser.fromLLM(model, llmOutputParser) + } + } if (output === this.name) { - const chain = new LLMChain({ llm: model, outputParser: llmOutputParser, prompt, verbose: process.env.DEBUG === 'true' }) + const chain = new LLMChain({ + llm: model, + outputParser: this.outputParser as BaseLLMOutputParser, + prompt, + verbose: process.env.DEBUG === 'true' + }) return chain } else if (output === 'outputPrediction') { const chain = new LLMChain({ llm: model, - outputParser: llmOutputParser, + outputParser: this.outputParser as BaseLLMOutputParser, prompt, verbose: process.env.DEBUG === 'true' }) @@ -104,7 +117,10 @@ class LLMChain_Chains implements INode { const chain = nodeData.instance as LLMChain let promptValues: ICommonObject | undefined = nodeData.inputs?.prompt.promptValues as ICommonObject const outputParser = nodeData.inputs?.outputParser as BaseOutputParser - promptValues = injectOutputParser(outputParser, chain, promptValues) + if (!this.outputParser && outputParser) { + this.outputParser = outputParser + } + promptValues = injectOutputParser(this.outputParser, chain, promptValues) const res = await runPrediction(inputVariables, chain, input, promptValues, options, nodeData) // eslint-disable-next-line no-console console.log('\x1b[93m\x1b[1m\n*****FINAL RESULT*****\n\x1b[0m\x1b[0m') diff --git a/packages/components/nodes/outputparsers/csvlist/CSVListOutputParser.ts b/packages/components/nodes/outputparsers/csvlist/CSVListOutputParser.ts index 4bc87851..31ada42e 100644 --- a/packages/components/nodes/outputparsers/csvlist/CSVListOutputParser.ts +++ b/packages/components/nodes/outputparsers/csvlist/CSVListOutputParser.ts @@ -24,12 +24,29 @@ class CSVListOutputParser implements INode { this.icon = 'csv.png' this.category = CATEGORY this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)] - this.inputs = [] + this.inputs = [ + { + label: 'Autofix', + name: 'autofixParser', + type: 'boolean', + rows: 4, + description: 'In the event that the first call fails, will make another call to the model to fix any errors.' + } + ] } // eslint-disable-next-line unused-imports/no-unused-vars async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { - return new CommaSeparatedListOutputParser() + const autoFix = nodeData.inputs?.autofixParser as boolean + + const commaSeparatedListOutputParser = new CommaSeparatedListOutputParser() + Object.defineProperty(commaSeparatedListOutputParser, 'autoFix', { + enumerable: true, + configurable: true, + writable: true, + value: autoFix + }) + return commaSeparatedListOutputParser } } diff --git a/packages/components/nodes/outputparsers/customlist/CustomListOutputParser.ts b/packages/components/nodes/outputparsers/customlist/CustomListOutputParser.ts index 1e9617c3..f9104f08 100644 --- a/packages/components/nodes/outputparsers/customlist/CustomListOutputParser.ts +++ b/packages/components/nodes/outputparsers/customlist/CustomListOutputParser.ts @@ -39,6 +39,13 @@ class CustomListOutputParser implements INode { type: 'string', description: 'Separator between values', default: ',' + }, + { + label: 'Autofix', + name: 'autofixParser', + type: 'boolean', + rows: 4, + description: 'In the event that the first call fails, will make another call to the model to fix any errors.' } ] } @@ -47,9 +54,17 @@ class CustomListOutputParser implements INode { async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { const separator = nodeData.inputs?.separator as string const lengthStr = nodeData.inputs?.length as string + const autoFix = nodeData.inputs?.autofixParser as boolean let length = 5 if (lengthStr) length = parseInt(lengthStr, 10) - return new LangchainCustomListOutputParser({ length: length, separator: separator }) + const parser = new LangchainCustomListOutputParser({ length: length, separator: separator }) + Object.defineProperty(parser, 'autoFix', { + enumerable: true, + configurable: true, + writable: true, + value: autoFix + }) + return parser } } diff --git a/packages/components/nodes/outputparsers/structured/StructuredOutputParser.ts b/packages/components/nodes/outputparsers/structured/StructuredOutputParser.ts index ef04de7d..30bf0bf5 100644 --- a/packages/components/nodes/outputparsers/structured/StructuredOutputParser.ts +++ b/packages/components/nodes/outputparsers/structured/StructuredOutputParser.ts @@ -48,6 +48,13 @@ class StructuredOutputParser implements INode { ' answer: "answer to the question",\n' + ' source: "source used to answer the question, should be a website.",\n' + '}' + }, + { + label: 'Autofix', + name: 'autofixParser', + type: 'boolean', + rows: 4, + description: 'In the event that the first call fails, will make another call to the model to fix any errors.' } ] } @@ -56,11 +63,22 @@ class StructuredOutputParser implements INode { async init(nodeData: INodeData, _: string, options: ICommonObject): Promise { const structureType = nodeData.inputs?.structureType as string const structure = nodeData.inputs?.structure as string + const autoFix = nodeData.inputs?.autofixParser as boolean + let parsedStructure: any | undefined = undefined if (structure && structureType === 'fromNamesAndDescriptions') { try { parsedStructure = JSON.parse(structure) - return LangchainStructuredOutputParser.fromNamesAndDescriptions(parsedStructure) + + // NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser + let structuredOutputParser = LangchainStructuredOutputParser.fromNamesAndDescriptions(parsedStructure) + Object.defineProperty(structuredOutputParser, 'autoFix', { + enumerable: true, + configurable: true, + writable: true, + value: autoFix + }) + return structuredOutputParser } catch (exception) { throw new Error('Invalid JSON in StructuredOutputParser: ' + exception) }