Revert the changes to the original parser & template
This commit is contained in:
parent
95b251f02a
commit
62f70ab6ef
|
|
@ -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 { 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
|
||||||
|
|
@ -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.'
|
description: 'In the event that the first call fails, will make another call to the model to fix any errors.'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'Example JSON',
|
label: 'JSON Structure',
|
||||||
name: 'exampleJson',
|
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',
|
type: 'string',
|
||||||
description: 'Example JSON structure for LLM to return',
|
description: `answer to the user's question`
|
||||||
rows: 10,
|
},
|
||||||
default: '{"answer": "the answer", "followupQuestions": ["question1", "question2"]}',
|
{
|
||||||
|
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 exampleJson = nodeData.inputs?.exampleJson as string
|
const jsonStructure = nodeData.inputs?.jsonStructure 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(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
|
// 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', {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
"nodes": [
|
"nodes": [
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 576,
|
"height": 574,
|
||||||
"id": "chatOpenAI_0",
|
"id": "chatOpenAI_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 845.3961479115309,
|
"x": 845.3961479115309,
|
||||||
|
|
@ -17,12 +17,7 @@
|
||||||
"version": 3,
|
"version": 3,
|
||||||
"name": "chatOpenAI",
|
"name": "chatOpenAI",
|
||||||
"type": "ChatOpenAI",
|
"type": "ChatOpenAI",
|
||||||
"baseClasses": [
|
"baseClasses": ["ChatOpenAI", "BaseChatModel", "BaseLanguageModel", "Runnable"],
|
||||||
"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": [
|
||||||
|
|
@ -30,9 +25,7 @@
|
||||||
"label": "Connect Credential",
|
"label": "Connect Credential",
|
||||||
"name": "credential",
|
"name": "credential",
|
||||||
"type": "credential",
|
"type": "credential",
|
||||||
"credentialNames": [
|
"credentialNames": ["openAIApi"],
|
||||||
"openAIApi"
|
|
||||||
],
|
|
||||||
"id": "chatOpenAI_0-input-credential-credential"
|
"id": "chatOpenAI_0-input-credential-credential"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -209,7 +202,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 508,
|
"height": 456,
|
||||||
"id": "llmChain_0",
|
"id": "llmChain_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 1229.1699649849293,
|
"x": 1229.1699649849293,
|
||||||
|
|
@ -222,11 +215,7 @@
|
||||||
"version": 3,
|
"version": 3,
|
||||||
"name": "llmChain",
|
"name": "llmChain",
|
||||||
"type": "LLMChain",
|
"type": "LLMChain",
|
||||||
"baseClasses": [
|
"baseClasses": ["LLMChain", "BaseChain", "Runnable"],
|
||||||
"LLMChain",
|
|
||||||
"BaseChain",
|
|
||||||
"Runnable"
|
|
||||||
],
|
|
||||||
"category": "Chains",
|
"category": "Chains",
|
||||||
"description": "Chain to run queries against LLMs",
|
"description": "Chain to run queries against LLMs",
|
||||||
"inputParams": [
|
"inputParams": [
|
||||||
|
|
@ -311,7 +300,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 690,
|
"height": 652,
|
||||||
"id": "chatPromptTemplate_0",
|
"id": "chatPromptTemplate_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 501.1597501123828,
|
"x": 501.1597501123828,
|
||||||
|
|
@ -324,12 +313,7 @@
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"name": "chatPromptTemplate",
|
"name": "chatPromptTemplate",
|
||||||
"type": "ChatPromptTemplate",
|
"type": "ChatPromptTemplate",
|
||||||
"baseClasses": [
|
"baseClasses": ["ChatPromptTemplate", "BaseChatPromptTemplate", "BasePromptTemplate", "Runnable"],
|
||||||
"ChatPromptTemplate",
|
|
||||||
"BaseChatPromptTemplate",
|
|
||||||
"BasePromptTemplate",
|
|
||||||
"Runnable"
|
|
||||||
],
|
|
||||||
"category": "Prompts",
|
"category": "Prompts",
|
||||||
"description": "Schema to represent a chat prompt",
|
"description": "Schema to represent a chat prompt",
|
||||||
"inputParams": [
|
"inputParams": [
|
||||||
|
|
@ -385,11 +369,11 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"width": 300,
|
"width": 300,
|
||||||
"height": 329,
|
"height": 328,
|
||||||
"id": "structuredOutputParser_0",
|
"id": "structuredOutputParser_0",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 498.2326128526694,
|
"x": 170.3869571939727,
|
||||||
"y": 566.5473204649535
|
"y": 343.9298288967859
|
||||||
},
|
},
|
||||||
"type": "customNode",
|
"type": "customNode",
|
||||||
"data": {
|
"data": {
|
||||||
|
|
@ -398,11 +382,7 @@
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"name": "structuredOutputParser",
|
"name": "structuredOutputParser",
|
||||||
"type": "StructuredOutputParser",
|
"type": "StructuredOutputParser",
|
||||||
"baseClasses": [
|
"baseClasses": ["StructuredOutputParser", "BaseLLMOutputParser", "Runnable"],
|
||||||
"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": [
|
||||||
|
|
@ -415,20 +395,61 @@
|
||||||
"id": "structuredOutputParser_0-input-autofixParser-boolean"
|
"id": "structuredOutputParser_0-input-autofixParser-boolean"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "Example JSON",
|
"label": "JSON Structure",
|
||||||
"name": "exampleJson",
|
"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",
|
"type": "string",
|
||||||
"description": "Example JSON structure for LLM to return",
|
"description": "answer to the user's question"
|
||||||
"rows": 10,
|
},
|
||||||
"default": "{\"answer\": \"the answer\", \"followupQuestions\": [\"question1\", \"question2\"]}",
|
{
|
||||||
|
"property": "source",
|
||||||
|
"type": "string",
|
||||||
|
"description": "sources used to answer the question, should be websites"
|
||||||
|
}
|
||||||
|
],
|
||||||
"additionalParams": true,
|
"additionalParams": true,
|
||||||
"id": "structuredOutputParser_0-input-exampleJson-string"
|
"id": "structuredOutputParser_0-input-jsonStructure-datagrid"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"inputAnchors": [],
|
"inputAnchors": [],
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"autofixParser": true,
|
"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": [
|
"outputAnchors": [
|
||||||
{
|
{
|
||||||
|
|
@ -442,11 +463,11 @@
|
||||||
"selected": false
|
"selected": false
|
||||||
},
|
},
|
||||||
"selected": false,
|
"selected": false,
|
||||||
"dragging": false,
|
|
||||||
"positionAbsolute": {
|
"positionAbsolute": {
|
||||||
"x": 498.2326128526694,
|
"x": 170.3869571939727,
|
||||||
"y": 566.5473204649535
|
"y": 343.9298288967859
|
||||||
}
|
},
|
||||||
|
"dragging": false
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"edges": [
|
"edges": [
|
||||||
|
|
@ -478,7 +499,10 @@
|
||||||
"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": ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue