diff --git a/langchain/src/experimental/autogpt/output_parser.ts b/langchain/src/experimental/autogpt/output_parser.ts index 04259526e475..603486c98330 100644 --- a/langchain/src/experimental/autogpt/output_parser.ts +++ b/langchain/src/experimental/autogpt/output_parser.ts @@ -2,18 +2,24 @@ import { BaseOutputParser } from "../../schema/output_parser.js"; import { AutoGPTAction } from "./schema.js"; /** - * Utility function used to preprocess a string to be parsed as JSON. It - * replaces single backslashes with double backslashes, while leaving + * Utility function used to preprocess a string to be parsed as JSON. + * It replaces single backslashes with double backslashes, while leaving * already escaped ones intact. + * It also extracts the json code if it is inside a code block */ export function preprocessJsonInput(inputStr: string): string { - // Replace single backslashes with double backslashes, - // while leaving already escaped ones intact const correctedStr = inputStr.replace( /(?[\w\W\n]+)(\r\n|\r|\n)```/ + ); + if (match?.groups?.code) { + return match.groups.code.trim(); + } else { + return correctedStr; + } } /** diff --git a/langchain/src/experimental/autogpt/tests/output_parser.test.ts b/langchain/src/experimental/autogpt/tests/output_parser.test.ts new file mode 100644 index 000000000000..979107374993 --- /dev/null +++ b/langchain/src/experimental/autogpt/tests/output_parser.test.ts @@ -0,0 +1,12 @@ +import { test, expect } from "@jest/globals"; +import { preprocessJsonInput } from "../output_parser.js"; + +test("should parse outputs correctly", () => { + expect(preprocessJsonInput("{'escaped':'\\a'}")).toBe("{'escaped':'\\\\a'}"); + + expect(preprocessJsonInput("```\n{}\n```")).toBe("{}"); + expect(preprocessJsonInput("```json\n{}\n```")).toBe("{}"); + expect( + preprocessJsonInput("I will do the following:\n\n```json\n{}\n```") + ).toBe("{}"); +});