mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 17:01:00 +03:00
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:
+18
-30
@@ -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 { BaseOutputParser } from 'langchain/schema/output_parser'
|
||||||
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
|
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
|
||||||
import { CATEGORY } from '../OutputParserHelpers'
|
import { CATEGORY } from '../OutputParserHelpers'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import { jsonToZod } from 'json-to-zod'
|
||||||
|
|
||||||
class StructuredOutputParser implements INode {
|
class StructuredOutputParser implements INode {
|
||||||
label: string
|
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.'
|
description: 'In the event that the first call fails, will make another call to the model to fix any errors.'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'JSON Structure',
|
label: 'Example JSON',
|
||||||
name: 'jsonStructure',
|
name: 'exampleJson',
|
||||||
type: 'datagrid',
|
type: 'string',
|
||||||
description: 'JSON structure for LLM to return',
|
description: 'Example JSON structure for LLM to return',
|
||||||
datagrid: [
|
rows: 10,
|
||||||
{ field: 'property', headerName: 'Property', editable: true },
|
default: '{"answer": "the answer", "followupQuestions": ["question1", "question2"]}',
|
||||||
{
|
|
||||||
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
|
additionalParams: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(nodeData: INodeData): Promise<any> {
|
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 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 {
|
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
|
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
|
||||||
Object.defineProperty(structuredOutputParser, 'autoFix', {
|
Object.defineProperty(structuredOutputParser, 'autoFix', {
|
||||||
|
|||||||
@@ -56,6 +56,7 @@
|
|||||||
"html-to-text": "^9.0.5",
|
"html-to-text": "^9.0.5",
|
||||||
"husky": "^8.0.3",
|
"husky": "^8.0.3",
|
||||||
"ioredis": "^5.3.2",
|
"ioredis": "^5.3.2",
|
||||||
|
"json-to-zod": "1.1.2",
|
||||||
"langchain": "^0.0.214",
|
"langchain": "^0.0.214",
|
||||||
"langfuse": "2.0.2",
|
"langfuse": "2.0.2",
|
||||||
"langfuse-langchain": "2.3.3",
|
"langfuse-langchain": "2.3.3",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"nodes": [
|
"nodes": [
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 574,
|
"height": 576,
|
||||||
"id": "chatOpenAI_0",
|
"id": "chatOpenAI_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 845.3961479115309,
|
"x": 845.3961479115309,
|
||||||
@@ -17,7 +17,12 @@
|
|||||||
"version": 3,
|
"version": 3,
|
||||||
"name": "chatOpenAI",
|
"name": "chatOpenAI",
|
||||||
"type": "ChatOpenAI",
|
"type": "ChatOpenAI",
|
||||||
"baseClasses": ["ChatOpenAI", "BaseChatModel", "BaseLanguageModel", "Runnable"],
|
"baseClasses": [
|
||||||
|
"ChatOpenAI",
|
||||||
|
"BaseChatModel",
|
||||||
|
"BaseLanguageModel",
|
||||||
|
"Runnable"
|
||||||
|
],
|
||||||
"category": "Chat Models",
|
"category": "Chat Models",
|
||||||
"description": "Wrapper around OpenAI large language models that use the Chat endpoint",
|
"description": "Wrapper around OpenAI large language models that use the Chat endpoint",
|
||||||
"inputParams": [
|
"inputParams": [
|
||||||
@@ -25,7 +30,9 @@
|
|||||||
"label": "Connect Credential",
|
"label": "Connect Credential",
|
||||||
"name": "credential",
|
"name": "credential",
|
||||||
"type": "credential",
|
"type": "credential",
|
||||||
"credentialNames": ["openAIApi"],
|
"credentialNames": [
|
||||||
|
"openAIApi"
|
||||||
|
],
|
||||||
"id": "chatOpenAI_0-input-credential-credential"
|
"id": "chatOpenAI_0-input-credential-credential"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -202,7 +209,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 456,
|
"height": 508,
|
||||||
"id": "llmChain_0",
|
"id": "llmChain_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 1229.1699649849293,
|
"x": 1229.1699649849293,
|
||||||
@@ -215,7 +222,11 @@
|
|||||||
"version": 3,
|
"version": 3,
|
||||||
"name": "llmChain",
|
"name": "llmChain",
|
||||||
"type": "LLMChain",
|
"type": "LLMChain",
|
||||||
"baseClasses": ["LLMChain", "BaseChain", "Runnable"],
|
"baseClasses": [
|
||||||
|
"LLMChain",
|
||||||
|
"BaseChain",
|
||||||
|
"Runnable"
|
||||||
|
],
|
||||||
"category": "Chains",
|
"category": "Chains",
|
||||||
"description": "Chain to run queries against LLMs",
|
"description": "Chain to run queries against LLMs",
|
||||||
"inputParams": [
|
"inputParams": [
|
||||||
@@ -300,7 +311,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 652,
|
"height": 690,
|
||||||
"id": "chatPromptTemplate_0",
|
"id": "chatPromptTemplate_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 501.1597501123828,
|
"x": 501.1597501123828,
|
||||||
@@ -313,7 +324,12 @@
|
|||||||
"version": 1,
|
"version": 1,
|
||||||
"name": "chatPromptTemplate",
|
"name": "chatPromptTemplate",
|
||||||
"type": "ChatPromptTemplate",
|
"type": "ChatPromptTemplate",
|
||||||
"baseClasses": ["ChatPromptTemplate", "BaseChatPromptTemplate", "BasePromptTemplate", "Runnable"],
|
"baseClasses": [
|
||||||
|
"ChatPromptTemplate",
|
||||||
|
"BaseChatPromptTemplate",
|
||||||
|
"BasePromptTemplate",
|
||||||
|
"Runnable"
|
||||||
|
],
|
||||||
"category": "Prompts",
|
"category": "Prompts",
|
||||||
"description": "Schema to represent a chat prompt",
|
"description": "Schema to represent a chat prompt",
|
||||||
"inputParams": [
|
"inputParams": [
|
||||||
@@ -369,11 +385,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 328,
|
"height": 329,
|
||||||
"id": "structuredOutputParser_0",
|
"id": "structuredOutputParser_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 170.3869571939727,
|
"x": 498.2326128526694,
|
||||||
"y": 343.9298288967859
|
"y": 566.5473204649535
|
||||||
},
|
},
|
||||||
"type": "customNode",
|
"type": "customNode",
|
||||||
"data": {
|
"data": {
|
||||||
@@ -382,7 +398,11 @@
|
|||||||
"version": 1,
|
"version": 1,
|
||||||
"name": "structuredOutputParser",
|
"name": "structuredOutputParser",
|
||||||
"type": "StructuredOutputParser",
|
"type": "StructuredOutputParser",
|
||||||
"baseClasses": ["StructuredOutputParser", "BaseLLMOutputParser", "Runnable"],
|
"baseClasses": [
|
||||||
|
"StructuredOutputParser",
|
||||||
|
"BaseLLMOutputParser",
|
||||||
|
"Runnable"
|
||||||
|
],
|
||||||
"category": "Output Parsers",
|
"category": "Output Parsers",
|
||||||
"description": "Parse the output of an LLM call into a given (JSON) structure.",
|
"description": "Parse the output of an LLM call into a given (JSON) structure.",
|
||||||
"inputParams": [
|
"inputParams": [
|
||||||
@@ -395,61 +415,20 @@
|
|||||||
"id": "structuredOutputParser_0-input-autofixParser-boolean"
|
"id": "structuredOutputParser_0-input-autofixParser-boolean"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "JSON Structure",
|
"label": "Example JSON",
|
||||||
"name": "jsonStructure",
|
"name": "exampleJson",
|
||||||
"type": "datagrid",
|
"type": "string",
|
||||||
"description": "JSON structure for LLM to return",
|
"description": "Example JSON structure for LLM to return",
|
||||||
"datagrid": [
|
"rows": 10,
|
||||||
{
|
"default": "{\"answer\": \"the answer\", \"followupQuestions\": [\"question1\", \"question2\"]}",
|
||||||
"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,
|
"additionalParams": true,
|
||||||
"id": "structuredOutputParser_0-input-jsonStructure-datagrid"
|
"id": "structuredOutputParser_0-input-exampleJson-string"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"inputAnchors": [],
|
"inputAnchors": [],
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"autofixParser": true,
|
"autofixParser": true,
|
||||||
"jsonStructure": [
|
"exampleJson": "{\"answer\": \"the answer\", \"followupQuestions\": [\"question1\", \"question2\"]}"
|
||||||
{
|
|
||||||
"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"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"outputAnchors": [
|
"outputAnchors": [
|
||||||
{
|
{
|
||||||
@@ -463,11 +442,11 @@
|
|||||||
"selected": false
|
"selected": false
|
||||||
},
|
},
|
||||||
"selected": false,
|
"selected": false,
|
||||||
|
"dragging": false,
|
||||||
"positionAbsolute": {
|
"positionAbsolute": {
|
||||||
"x": 170.3869571939727,
|
"x": 498.2326128526694,
|
||||||
"y": 343.9298288967859
|
"y": 566.5473204649535
|
||||||
},
|
}
|
||||||
"dragging": false
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"edges": [
|
"edges": [
|
||||||
@@ -499,10 +478,7 @@
|
|||||||
"target": "llmChain_0",
|
"target": "llmChain_0",
|
||||||
"targetHandle": "llmChain_0-input-outputParser-BaseLLMOutputParser",
|
"targetHandle": "llmChain_0-input-outputParser-BaseLLMOutputParser",
|
||||||
"type": "buttonedge",
|
"type": "buttonedge",
|
||||||
"id": "structuredOutputParser_0-structuredOutputParser_0-output-structuredOutputParser-StructuredOutputParser|BaseLLMOutputParser|Runnable-llmChain_0-llmChain_0-input-outputParser-BaseLLMOutputParser",
|
"id": "structuredOutputParser_0-structuredOutputParser_0-output-structuredOutputParser-StructuredOutputParser|BaseLLMOutputParser|Runnable-llmChain_0-llmChain_0-input-outputParser-BaseLLMOutputParser"
|
||||||
"data": {
|
|
||||||
"label": ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user