mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 03:01:10 +03:00
add datagrid field type
This commit is contained in:
+45
-44
@@ -1,7 +1,8 @@
|
||||
import { getBaseClasses, INode, INodeData, INodeParams } from '../../../src'
|
||||
import { convertSchemaToZod, getBaseClasses, INode, INodeData, INodeParams } from '../../../src'
|
||||
import { BaseOutputParser } from 'langchain/schema/output_parser'
|
||||
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
|
||||
import { CATEGORY } from '../OutputParserHelpers'
|
||||
import { z } from 'zod'
|
||||
|
||||
class StructuredOutputParser implements INode {
|
||||
label: string
|
||||
@@ -24,65 +25,65 @@ class StructuredOutputParser implements INode {
|
||||
this.icon = 'structure.png'
|
||||
this.category = CATEGORY
|
||||
this.baseClasses = [this.type, ...getBaseClasses(BaseOutputParser)]
|
||||
//TODO: To extend the structureType to ZodSchema
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Structure Type',
|
||||
name: 'structureType',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
label: 'Names And Descriptions',
|
||||
name: 'fromNamesAndDescriptions'
|
||||
}
|
||||
],
|
||||
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' +
|
||||
'}'
|
||||
},
|
||||
{
|
||||
label: 'Autofix',
|
||||
name: 'autofixParser',
|
||||
type: 'boolean',
|
||||
optional: true,
|
||||
description: 'In the event that the first call fails, will make another call to the model to fix any errors.'
|
||||
},
|
||||
{
|
||||
label: 'JSON Structure',
|
||||
name: 'jsonStructure',
|
||||
type: 'datagrid',
|
||||
description: 'JSON structure for LLM to return',
|
||||
datagrid: [
|
||||
{ field: 'property', headerName: 'Property', editable: true },
|
||||
{
|
||||
field: 'type',
|
||||
headerName: 'Type',
|
||||
type: 'singleSelect',
|
||||
valueOptions: ['string', 'number', 'boolean'],
|
||||
editable: true
|
||||
},
|
||||
{ field: 'description', headerName: 'Description', editable: true, flex: 1 }
|
||||
],
|
||||
default: [
|
||||
{
|
||||
property: 'answer',
|
||||
type: 'string',
|
||||
description: `answer to the user's question`
|
||||
},
|
||||
{
|
||||
property: 'source',
|
||||
type: 'string',
|
||||
description: `sources used to answer the question, should be websites`
|
||||
}
|
||||
],
|
||||
additionalParams: true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData): Promise<any> {
|
||||
const structureType = nodeData.inputs?.structureType as string
|
||||
const structure = nodeData.inputs?.structure as string
|
||||
const jsonStructure = nodeData.inputs?.jsonStructure as string
|
||||
const autoFix = nodeData.inputs?.autofixParser as boolean
|
||||
|
||||
let parsedStructure: any | undefined = undefined
|
||||
if (structure && structureType === 'fromNamesAndDescriptions') {
|
||||
try {
|
||||
parsedStructure = JSON.parse(structure)
|
||||
try {
|
||||
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(z.object(convertSchemaToZod(jsonStructure)))
|
||||
|
||||
// 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)
|
||||
}
|
||||
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
|
||||
Object.defineProperty(structuredOutputParser, 'autoFix', {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: autoFix
|
||||
})
|
||||
return structuredOutputParser
|
||||
} catch (exception) {
|
||||
throw new Error('Invalid JSON in StructuredOutputParser: ' + exception)
|
||||
}
|
||||
throw new Error('Error creating OutputParser.')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses } from '../../../src/utils'
|
||||
import { convertSchemaToZod, getBaseClasses } from '../../../src/utils'
|
||||
import { DynamicStructuredTool } from './core'
|
||||
import { z } from 'zod'
|
||||
import { DataSource } from 'typeorm'
|
||||
@@ -87,26 +87,4 @@ class CustomTool_Tools implements INode {
|
||||
}
|
||||
}
|
||||
|
||||
const convertSchemaToZod = (schema: string) => {
|
||||
try {
|
||||
const parsedSchema = JSON.parse(schema)
|
||||
const zodObj: any = {}
|
||||
for (const sch of parsedSchema) {
|
||||
if (sch.type === 'string') {
|
||||
if (sch.required) z.string({ required_error: `${sch.property} required` }).describe(sch.description)
|
||||
zodObj[sch.property] = z.string().describe(sch.description)
|
||||
} else if (sch.type === 'number') {
|
||||
if (sch.required) z.number({ required_error: `${sch.property} required` }).describe(sch.description)
|
||||
zodObj[sch.property] = z.number().describe(sch.description)
|
||||
} else if (sch.type === 'boolean') {
|
||||
if (sch.required) z.boolean({ required_error: `${sch.property} required` }).describe(sch.description)
|
||||
zodObj[sch.property] = z.boolean().describe(sch.description)
|
||||
}
|
||||
}
|
||||
return zodObj
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: CustomTool_Tools }
|
||||
|
||||
Reference in New Issue
Block a user