AutoFixOutputParser: Addition of Autofix option for all output parsers

This commit is contained in:
vinodkiran
2023-10-28 13:59:32 +05:30
parent 3696c4517a
commit aa2075d60b
4 changed files with 75 additions and 9 deletions
@@ -6,6 +6,7 @@ import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from
import { BaseOutputParser } from 'langchain/schema/output_parser' import { BaseOutputParser } from 'langchain/schema/output_parser'
import { injectOutputParser } from '../../outputparsers/OutputParserHelpers' import { injectOutputParser } from '../../outputparsers/OutputParserHelpers'
import { BaseLLMOutputParser } from 'langchain/schema/output_parser' import { BaseLLMOutputParser } from 'langchain/schema/output_parser'
import { OutputFixingParser } from 'langchain/output_parsers'
class LLMChain_Chains implements INode { class LLMChain_Chains implements INode {
label: string label: string
@@ -18,6 +19,7 @@ class LLMChain_Chains implements INode {
description: string description: string
inputs: INodeParams[] inputs: INodeParams[]
outputs: INodeOutputsValue[] outputs: INodeOutputsValue[]
outputParser: BaseOutputParser
constructor() { constructor() {
this.label = 'LLM Chain' this.label = 'LLM Chain'
@@ -72,15 +74,26 @@ class LLMChain_Chains implements INode {
const prompt = nodeData.inputs?.prompt const prompt = nodeData.inputs?.prompt
const output = nodeData.outputs?.output as string const output = nodeData.outputs?.output as string
const promptValues = prompt.promptValues as ICommonObject const promptValues = prompt.promptValues as ICommonObject
const llmOutputParser = nodeData.inputs?.outputParser as BaseLLMOutputParser<string | object> 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) { 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<string | object>,
prompt,
verbose: process.env.DEBUG === 'true'
})
return chain return chain
} else if (output === 'outputPrediction') { } else if (output === 'outputPrediction') {
const chain = new LLMChain({ const chain = new LLMChain({
llm: model, llm: model,
outputParser: llmOutputParser, outputParser: this.outputParser as BaseLLMOutputParser<string | object>,
prompt, prompt,
verbose: process.env.DEBUG === 'true' verbose: process.env.DEBUG === 'true'
}) })
@@ -104,7 +117,10 @@ class LLMChain_Chains implements INode {
const chain = nodeData.instance as LLMChain const chain = nodeData.instance as LLMChain
let promptValues: ICommonObject | undefined = nodeData.inputs?.prompt.promptValues as ICommonObject let promptValues: ICommonObject | undefined = nodeData.inputs?.prompt.promptValues as ICommonObject
const outputParser = nodeData.inputs?.outputParser as BaseOutputParser 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) const res = await runPrediction(inputVariables, chain, input, promptValues, options, nodeData)
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('\x1b[93m\x1b[1m\n*****FINAL RESULT*****\n\x1b[0m\x1b[0m') console.log('\x1b[93m\x1b[1m\n*****FINAL RESULT*****\n\x1b[0m\x1b[0m')
@@ -24,12 +24,29 @@ class CSVListOutputParser implements INode {
this.icon = 'csv.png' this.icon = 'csv.png'
this.category = CATEGORY this.category = CATEGORY
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)] 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 // eslint-disable-next-line unused-imports/no-unused-vars
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
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
} }
} }
@@ -39,6 +39,13 @@ class CustomListOutputParser implements INode {
type: 'string', type: 'string',
description: 'Separator between values', description: 'Separator between values',
default: ',' 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<any> { async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const separator = nodeData.inputs?.separator as string const separator = nodeData.inputs?.separator as string
const lengthStr = nodeData.inputs?.length as string const lengthStr = nodeData.inputs?.length as string
const autoFix = nodeData.inputs?.autofixParser as boolean
let length = 5 let length = 5
if (lengthStr) length = parseInt(lengthStr, 10) 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
} }
} }
@@ -48,6 +48,13 @@ class StructuredOutputParser implements INode {
' answer: "answer to the question",\n' + ' answer: "answer to the question",\n' +
' source: "source used to answer the question, should be a website.",\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<any> { async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const structureType = nodeData.inputs?.structureType as string const structureType = nodeData.inputs?.structureType as string
const structure = nodeData.inputs?.structure as string const structure = nodeData.inputs?.structure as string
const autoFix = nodeData.inputs?.autofixParser as boolean
let parsedStructure: any | undefined = undefined let parsedStructure: any | undefined = undefined
if (structure && structureType === 'fromNamesAndDescriptions') { if (structure && structureType === 'fromNamesAndDescriptions') {
try { try {
parsedStructure = JSON.parse(structure) 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) { } catch (exception) {
throw new Error('Invalid JSON in StructuredOutputParser: ' + exception) throw new Error('Invalid JSON in StructuredOutputParser: ' + exception)
} }