mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 21:00:58 +03:00
[Feature] Repair JSON from LLM in StructuredOutputParserAdvanced (#3723)
Repair JSON from LLM in StructuredOutputParserAdvanced Co-authored-by: Henry <hzj94@hotmail.com>
This commit is contained in:
+9
@@ -3,6 +3,7 @@ import { BaseOutputParser } from '@langchain/core/output_parsers'
|
||||
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
|
||||
import { CATEGORY } from '../OutputParserHelpers'
|
||||
import { convertSchemaToZod, getBaseClasses, INode, INodeData, INodeParams } from '../../../src'
|
||||
import { jsonrepair } from 'jsonrepair'
|
||||
|
||||
class StructuredOutputParser implements INode {
|
||||
label: string
|
||||
@@ -74,6 +75,14 @@ class StructuredOutputParser implements INode {
|
||||
const zodSchema = z.object(convertSchemaToZod(jsonStructure)) as any
|
||||
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(zodSchema)
|
||||
|
||||
const baseParse = structuredOutputParser.parse
|
||||
|
||||
// Fix broken JSON from LLM
|
||||
structuredOutputParser.parse = (text) => {
|
||||
const jsonString = text.includes('```') ? text.trim().split(/```(?:json)?/)[1] : text.trim()
|
||||
return baseParse.call(structuredOutputParser, jsonrepair(jsonString))
|
||||
}
|
||||
|
||||
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
|
||||
Object.defineProperty(structuredOutputParser, 'autoFix', {
|
||||
enumerable: true,
|
||||
|
||||
+9
@@ -3,6 +3,7 @@ import { BaseOutputParser } from '@langchain/core/output_parsers'
|
||||
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
|
||||
import { CATEGORY } from '../OutputParserHelpers'
|
||||
import { z } from 'zod'
|
||||
import { jsonrepair } from 'jsonrepair'
|
||||
|
||||
class AdvancedStructuredOutputParser implements INode {
|
||||
label: string
|
||||
@@ -62,6 +63,14 @@ class AdvancedStructuredOutputParser implements INode {
|
||||
try {
|
||||
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(zodSchema)
|
||||
|
||||
const baseParse = structuredOutputParser.parse
|
||||
|
||||
// Fix broken JSON from LLM
|
||||
structuredOutputParser.parse = (text) => {
|
||||
const jsonString = text.includes('```') ? text.trim().split(/```(?:json)?/)[1] : text.trim()
|
||||
return baseParse.call(structuredOutputParser, jsonrepair(jsonString))
|
||||
}
|
||||
|
||||
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
|
||||
Object.defineProperty(structuredOutputParser, 'autoFix', {
|
||||
enumerable: true,
|
||||
|
||||
@@ -92,6 +92,7 @@
|
||||
"ioredis": "^5.3.2",
|
||||
"jsdom": "^22.1.0",
|
||||
"jsonpointer": "^5.0.1",
|
||||
"jsonrepair": "^3.11.1",
|
||||
"langchain": "^0.3.5",
|
||||
"langfuse": "3.3.4",
|
||||
"langfuse-langchain": "^3.3.4",
|
||||
|
||||
Reference in New Issue
Block a user