From 6e8a5c113f4c76bf24668c38fe52086e4b62d7f2 Mon Sep 17 00:00:00 2001 From: Maytee Chinavanichkit Date: Fri, 15 Dec 2023 00:43:17 +0900 Subject: [PATCH 1/2] Improve output_parser to extract JSON code block Closes #3655 --- .../src/experimental/autogpt/output_parser.ts | 22 +++++++++++-------- .../autogpt/tests/output_parser.test.ts | 12 ++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 langchain/src/experimental/autogpt/tests/output_parser.test.ts diff --git a/langchain/src/experimental/autogpt/output_parser.ts b/langchain/src/experimental/autogpt/output_parser.ts index 04259526e475..134102a73de7 100644 --- a/langchain/src/experimental/autogpt/output_parser.ts +++ b/langchain/src/experimental/autogpt/output_parser.ts @@ -2,18 +2,22 @@ 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..203d83e5ad1d --- /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("{}") + + +}) From ce67a0532b401a866eac412a54b73d7888d9db46 Mon Sep 17 00:00:00 2001 From: Maytee Chinavanichkit Date: Fri, 15 Dec 2023 09:04:42 +0900 Subject: [PATCH 2/2] Ran yarn format and lint --- .../src/experimental/autogpt/output_parser.ts | 22 ++++++++++--------- .../autogpt/tests/output_parser.test.ts | 18 +++++++-------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/langchain/src/experimental/autogpt/output_parser.ts b/langchain/src/experimental/autogpt/output_parser.ts index 134102a73de7..603486c98330 100644 --- a/langchain/src/experimental/autogpt/output_parser.ts +++ b/langchain/src/experimental/autogpt/output_parser.ts @@ -8,16 +8,18 @@ import { AutoGPTAction } from "./schema.js"; * It also extracts the json code if it is inside a code block */ export function preprocessJsonInput(inputStr: string): string { - const correctedStr = inputStr.replace( - /(?[\w\W\n]+)(\r\n|\r|\n)```/) - if (match?.groups?.code) { - return match.groups.code.trim() - } else { - return correctedStr - } + 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 index 203d83e5ad1d..979107374993 100644 --- a/langchain/src/experimental/autogpt/tests/output_parser.test.ts +++ b/langchain/src/experimental/autogpt/tests/output_parser.test.ts @@ -1,12 +1,12 @@ import { test, expect } from "@jest/globals"; -import {preprocessJsonInput} from "../output_parser.js"; +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("{}") - - -}) + 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("{}"); +});