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
@@ -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<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',
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<any> {
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
}
}
@@ -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<any> {
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)
}