[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:
Jérémy JOURDIN
2024-12-21 13:51:56 +01:00
committed by GitHub
parent 94f67c0212
commit 0381a99c4d
4 changed files with 28 additions and 0 deletions
@@ -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,
@@ -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,