New Feature - Output Parsers

This commit is contained in:
vinodkiran
2023-10-23 13:16:12 +05:30
parent 26898a2230
commit 4654f69951
7 changed files with 227 additions and 10 deletions
@@ -0,0 +1,35 @@
import { getBaseClasses, ICommonObject, INode, INodeData, INodeParams } from '../../../src'
import { BaseOutputParser } from 'langchain/schema/output_parser'
import { CommaSeparatedListOutputParser } from 'langchain/output_parsers'
class CSVListOutputParser implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'CSV Output Parser'
this.name = 'csvOutputParser'
this.version = 1.0
this.type = 'CSVListOutputParser'
this.description = 'Parse the output of an LLM call as a comma-separated list of values'
this.icon = 'csv.png'
this.category = 'Output Parser'
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)]
this.inputs = []
}
// eslint-disable-next-line unused-imports/no-unused-vars
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
return new CommaSeparatedListOutputParser()
}
}
module.exports = { nodeClass: CSVListOutputParser }
Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

@@ -0,0 +1,55 @@
import { getBaseClasses, ICommonObject, INode, INodeData, INodeParams } from '../../../src'
import { BaseOutputParser } from 'langchain/schema/output_parser'
import { CustomListOutputParser as LangchainCustomListOutputParser } from 'langchain/output_parsers'
class CustomListOutputParser implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'Custom List Output Parser'
this.name = 'customListOutputParser'
this.version = 1.0
this.type = 'CustomListOutputParser'
this.description = 'Parse the output of an LLM call as a list of values.'
this.icon = 'list.png'
this.category = 'Output Parser'
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)]
this.inputs = [
{
label: 'Length',
name: 'length',
type: 'number',
default: 5,
step: 1,
description: 'Number of values to return'
},
{
label: 'Separator',
name: 'separator',
type: 'string',
description: 'Separator between values',
default: ','
}
]
}
// eslint-disable-next-line unused-imports/no-unused-vars
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const separator = nodeData.inputs?.separator as string
const lengthStr = nodeData.inputs?.length as string
let length = 5
if (lengthStr) length = parseInt(lengthStr, 10)
return new LangchainCustomListOutputParser({ length: length, separator: separator })
}
}
module.exports = { nodeClass: CustomListOutputParser }
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

@@ -0,0 +1,77 @@
import { getBaseClasses, ICommonObject, INode, INodeData, INodeParams } from '../../../src'
import { BaseOutputParser } from 'langchain/schema/output_parser'
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
class StructuredOutputParser implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'Structured Output Parser'
this.name = 'structuredOutputParser'
this.version = 1.0
this.type = 'StructuredOutputParser'
this.description = 'Parse the output of an LLM call into a given (JSON) structure.'
this.icon = 'structure.png'
this.category = 'Output Parser'
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)]
this.inputs = [
{
label: 'Structure Type',
name: 'structureType',
type: 'options',
options: [
{
label: 'Names And Descriptions',
name: 'fromNamesAndDescriptions'
},
{
label: 'Zod Schema',
name: 'fromZodSchema'
}
],
default: 'fromNamesAndDescriptions'
},
{
label: 'Structure',
name: 'structure',
type: 'string',
rows: 4,
placeholder:
'{' +
' answer: "answer to the question",\n' +
' source: "source used to answer the question, should be a website.",\n' +
'}'
}
]
}
// eslint-disable-next-line unused-imports/no-unused-vars
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const structureType = nodeData.inputs?.structureType as string
const structure = nodeData.inputs?.structure as string
let parsedStructure: any | undefined = undefined
if (structure) {
try {
parsedStructure = JSON.parse(structure)
} catch (exception) {
throw new Error('Invalid JSON in StructuredOutputParser: ' + exception)
}
}
if (structureType === 'fromZodSchema') {
return LangchainStructuredOutputParser.fromZodSchema(parsedStructure)
} else {
return LangchainStructuredOutputParser.fromNamesAndDescriptions(parsedStructure)
}
}
}
module.exports = { nodeClass: StructuredOutputParser }
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB