From c77b150c28ea07c059d6f27378cc5e69fd40563c Mon Sep 17 00:00:00 2001 From: Elliot Kang <kkang2097@gmail.com> Date: Mon, 11 Sep 2023 15:24:07 -0700 Subject: [PATCH] hardcoding single JSON object case --- packages/core/src/OutputParser.ts | 6 +++- packages/core/src/tests/OutputParser.test.ts | 38 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/core/src/OutputParser.ts b/packages/core/src/OutputParser.ts index 1498f3f00..4f9efc17c 100644 --- a/packages/core/src/OutputParser.ts +++ b/packages/core/src/OutputParser.ts @@ -56,11 +56,15 @@ class OutputParserError extends Error { function parseJsonMarkdown(text: string) { text = text.trim(); - //This code is more general than the previous version, and should be faster. const beginIndex = text.indexOf("["); const endIndex = text.lastIndexOf("]"); const jsonText = text.substring(beginIndex, endIndex + 1); try { + //Single JSON object case. + if(beginIndex === -1 || endIndex === -1){ + return [JSON.parse(text)]; + } + //Multiple JSON object case. return JSON.parse(jsonText); } catch (e) { throw new OutputParserError("Not a json markdown", { output: text }); diff --git a/packages/core/src/tests/OutputParser.test.ts b/packages/core/src/tests/OutputParser.test.ts index e11fc6aee..6ec61d4dc 100644 --- a/packages/core/src/tests/OutputParser.test.ts +++ b/packages/core/src/tests/OutputParser.test.ts @@ -49,4 +49,42 @@ ${data_str} expect(parser.parse(JSON.stringify(data))).toEqual(real_answer); }); + + test("parses null single response", () => { + const parser = new SubQuestionOutputParser(); + const data_str = + "[\n" + + " {\n" + + ` "subQuestion": "Sorry, I don't have any relevant information to answer your question",\n` + + ' "toolName": ""\n' + + " }\n" + + "]"; + const data = [ + { + subQuestion: + "Sorry, I don't have any relevant information to answer your question", + toolName: "", + }, + ]; + const real_answer = { parsedOutput: data, rawOutput: data_str }; + expect(parser.parse(data_str)).toEqual(real_answer); + }); + + test("Single JSON object case", () => { + const parser = new SubQuestionOutputParser(); + const data_str = + " {\n" + + ` "subQuestion": "Sorry, I don't have any relevant information to answer your question",\n` + + ' "toolName": ""\n' + + " }\n"; + const data = [ + { + subQuestion: + "Sorry, I don't have any relevant information to answer your question", + toolName: "", + }, + ]; + const real_answer = { parsedOutput: data, rawOutput: data_str }; + expect(parser.parse(data_str)).toEqual(real_answer); + }); }); -- GitLab