Improve flexibility of the structured output parser by allowing the user to input an example JSON and automatically converting it to a zod scheme

This commit is contained in:
Kenny Vaneetvelde
2024-02-03 19:43:44 +01:00
parent 6013743705
commit 288e451161
3 changed files with 64 additions and 99 deletions
@@ -1,8 +1,9 @@
import { convertSchemaToZod, getBaseClasses, INode, INodeData, INodeParams } from '../../../src'
import { 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'
import { jsonToZod } from 'json-to-zod'
class StructuredOutputParser implements INode {
label: string
@@ -34,44 +35,31 @@ class StructuredOutputParser implements INode {
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`
}
],
label: 'Example JSON',
name: 'exampleJson',
type: 'string',
description: 'Example JSON structure for LLM to return',
rows: 10,
default: '{"answer": "the answer", "followupQuestions": ["question1", "question2"]}',
additionalParams: true
}
]
}
async init(nodeData: INodeData): Promise<any> {
const jsonStructure = nodeData.inputs?.jsonStructure as string
const exampleJson = nodeData.inputs?.exampleJson as string
const autoFix = nodeData.inputs?.autofixParser as boolean
const jsonToZodString = jsonToZod(JSON.parse(exampleJson))
const splitString = jsonToZodString.split('const schema = ')
const schemaString = splitString[1].trim()
const fnString = `function proxyFn(z){ return ${schemaString} }`
const zodSchemaFunction = new Function('z', `return ${schemaString}`)
const zodSchema = zodSchemaFunction(z)
try {
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(z.object(convertSchemaToZod(jsonStructure)))
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(zodSchema)
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
Object.defineProperty(structuredOutputParser, 'autoFix', {
+1
View File
@@ -56,6 +56,7 @@
"html-to-text": "^9.0.5",
"husky": "^8.0.3",
"ioredis": "^5.3.2",
"json-to-zod": "1.1.2",
"langchain": "^0.0.214",
"langfuse": "2.0.2",
"langfuse-langchain": "2.3.3",