From 62f70ab6efe46f6be2d2cddefc2cc12d8d008672 Mon Sep 17 00:00:00 2001 From: Kenny Vaneetvelde Date: Sat, 3 Feb 2024 21:25:26 +0100 Subject: [PATCH] Revert the changes to the original parser & template --- .../StructuredOutputParser.ts | 48 +++++--- .../chatflows/Structured Output Parser.json | 114 +++++++++++------- 2 files changed, 99 insertions(+), 63 deletions(-) diff --git a/packages/components/nodes/outputparsers/StructuredOutputParser/StructuredOutputParser.ts b/packages/components/nodes/outputparsers/StructuredOutputParser/StructuredOutputParser.ts index 849d825d6..fc28fd1c2 100644 --- a/packages/components/nodes/outputparsers/StructuredOutputParser/StructuredOutputParser.ts +++ b/packages/components/nodes/outputparsers/StructuredOutputParser/StructuredOutputParser.ts @@ -1,9 +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' -import { jsonToZod } from 'json-to-zod' class StructuredOutputParser implements INode { label: string @@ -35,31 +34,44 @@ 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: 'Example JSON', - name: 'exampleJson', - type: 'string', - description: 'Example JSON structure for LLM to return', - rows: 10, - default: '{"answer": "the answer", "followupQuestions": ["question1", "question2"]}', + 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 { - const exampleJson = nodeData.inputs?.exampleJson as string + const jsonStructure = nodeData.inputs?.jsonStructure 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(zodSchema) + 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 Object.defineProperty(structuredOutputParser, 'autoFix', { diff --git a/packages/server/marketplaces/chatflows/Structured Output Parser.json b/packages/server/marketplaces/chatflows/Structured Output Parser.json index 93bb96bb9..923364432 100644 --- a/packages/server/marketplaces/chatflows/Structured Output Parser.json +++ b/packages/server/marketplaces/chatflows/Structured Output Parser.json @@ -4,7 +4,7 @@ "nodes": [ { "width": 300, - "height": 576, + "height": 574, "id": "chatOpenAI_0", "position": { "x": 845.3961479115309, @@ -17,12 +17,7 @@ "version": 3, "name": "chatOpenAI", "type": "ChatOpenAI", - "baseClasses": [ - "ChatOpenAI", - "BaseChatModel", - "BaseLanguageModel", - "Runnable" - ], + "baseClasses": ["ChatOpenAI", "BaseChatModel", "BaseLanguageModel", "Runnable"], "category": "Chat Models", "description": "Wrapper around OpenAI large language models that use the Chat endpoint", "inputParams": [ @@ -30,9 +25,7 @@ "label": "Connect Credential", "name": "credential", "type": "credential", - "credentialNames": [ - "openAIApi" - ], + "credentialNames": ["openAIApi"], "id": "chatOpenAI_0-input-credential-credential" }, { @@ -209,7 +202,7 @@ }, { "width": 300, - "height": 508, + "height": 456, "id": "llmChain_0", "position": { "x": 1229.1699649849293, @@ -222,11 +215,7 @@ "version": 3, "name": "llmChain", "type": "LLMChain", - "baseClasses": [ - "LLMChain", - "BaseChain", - "Runnable" - ], + "baseClasses": ["LLMChain", "BaseChain", "Runnable"], "category": "Chains", "description": "Chain to run queries against LLMs", "inputParams": [ @@ -311,7 +300,7 @@ }, { "width": 300, - "height": 690, + "height": 652, "id": "chatPromptTemplate_0", "position": { "x": 501.1597501123828, @@ -324,12 +313,7 @@ "version": 1, "name": "chatPromptTemplate", "type": "ChatPromptTemplate", - "baseClasses": [ - "ChatPromptTemplate", - "BaseChatPromptTemplate", - "BasePromptTemplate", - "Runnable" - ], + "baseClasses": ["ChatPromptTemplate", "BaseChatPromptTemplate", "BasePromptTemplate", "Runnable"], "category": "Prompts", "description": "Schema to represent a chat prompt", "inputParams": [ @@ -385,11 +369,11 @@ }, { "width": 300, - "height": 329, + "height": 328, "id": "structuredOutputParser_0", "position": { - "x": 498.2326128526694, - "y": 566.5473204649535 + "x": 170.3869571939727, + "y": 343.9298288967859 }, "type": "customNode", "data": { @@ -398,11 +382,7 @@ "version": 1, "name": "structuredOutputParser", "type": "StructuredOutputParser", - "baseClasses": [ - "StructuredOutputParser", - "BaseLLMOutputParser", - "Runnable" - ], + "baseClasses": ["StructuredOutputParser", "BaseLLMOutputParser", "Runnable"], "category": "Output Parsers", "description": "Parse the output of an LLM call into a given (JSON) structure.", "inputParams": [ @@ -415,20 +395,61 @@ "id": "structuredOutputParser_0-input-autofixParser-boolean" }, { - "label": "Example JSON", - "name": "exampleJson", - "type": "string", - "description": "Example JSON structure for LLM to return", - "rows": 10, - "default": "{\"answer\": \"the answer\", \"followupQuestions\": [\"question1\", \"question2\"]}", + "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, - "id": "structuredOutputParser_0-input-exampleJson-string" + "id": "structuredOutputParser_0-input-jsonStructure-datagrid" } ], "inputAnchors": [], "inputs": { "autofixParser": true, - "exampleJson": "{\"answer\": \"the answer\", \"followupQuestions\": [\"question1\", \"question2\"]}" + "jsonStructure": [ + { + "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": [ { @@ -442,11 +463,11 @@ "selected": false }, "selected": false, - "dragging": false, "positionAbsolute": { - "x": 498.2326128526694, - "y": 566.5473204649535 - } + "x": 170.3869571939727, + "y": 343.9298288967859 + }, + "dragging": false } ], "edges": [ @@ -478,7 +499,10 @@ "target": "llmChain_0", "targetHandle": "llmChain_0-input-outputParser-BaseLLMOutputParser", "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": "" + } } ] -} \ No newline at end of file +}