From b75e2d23a209c91403dbbaaa6d323091f4eacc0a Mon Sep 17 00:00:00 2001
From: Elliot Kang <kkang2097@gmail.com>
Date: Mon, 11 Sep 2023 01:16:31 -0700
Subject: [PATCH] re-ordering logic for parser

- previous iteration ran the computation twice if we had an unexpected output format
- added comment for future use
---
 packages/core/src/OutputParser.ts            | 11 +++++------
 packages/core/src/tests/OutputParser.test.ts |  2 ++
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/packages/core/src/OutputParser.ts b/packages/core/src/OutputParser.ts
index c067297d5..11bd7f023 100644
--- a/packages/core/src/OutputParser.ts
+++ b/packages/core/src/OutputParser.ts
@@ -64,14 +64,13 @@ function parseJsonMarkdown(text: string) {
     endDelimiter,
     beginIndex + beginDelimiter.length,
   );
-
-  const jsonText = text.substring(beginIndex + beginDelimiter.length, endIndex);
-
   //Scenario 1: LLM follows instruction format. However, it doesn't always do this.
-  try {
+  if (!(beginIndex === -1 || endIndex === -1)) {
+    const jsonText = text.substring(
+      beginIndex + beginDelimiter.length,
+      endIndex,
+    );
     return JSON.parse(jsonText);
-  } catch (e) {
-    //Fall through
   }
 
   //Scenario 2: LLM follows instruction format roughly, but doesn't do this exactly.
diff --git a/packages/core/src/tests/OutputParser.test.ts b/packages/core/src/tests/OutputParser.test.ts
index b088f5bd3..a4c938b55 100644
--- a/packages/core/src/tests/OutputParser.test.ts
+++ b/packages/core/src/tests/OutputParser.test.ts
@@ -1,5 +1,7 @@
 import { SubQuestionOutputParser } from "../OutputParser";
 
+//This parser is really important, so make sure to add tests
+// as the parser sees through more iterations.
 describe("SubQuestionOutputParser", () => {
   test("parses expected", () => {
     const parser = new SubQuestionOutputParser();
-- 
GitLab