From 7c6837444ded6c3ebf88020050d2d0434c049c51 Mon Sep 17 00:00:00 2001 From: Michael Robinson Date: Thu, 18 Apr 2024 14:02:10 +0800 Subject: [PATCH] Fix parsing failures whtn ChatGPT returns JSON with trailing commas --- typescript/src/ts/strip-whitespace.ts | 38 +++++++++++++++++++++++++++ typescript/src/typechat.ts | 3 ++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 typescript/src/ts/strip-whitespace.ts diff --git a/typescript/src/ts/strip-whitespace.ts b/typescript/src/ts/strip-whitespace.ts new file mode 100644 index 00000000..b6a36fc6 --- /dev/null +++ b/typescript/src/ts/strip-whitespace.ts @@ -0,0 +1,38 @@ +/** + * The following code snippet is copied from the index.ts file of the strip-json-trailing-commas project + * (commit 3cfa16a2aa6bbef8ed740c346321ef6c2b42237d), + * available at: https://github.com/nokazn/strip-json-trailing-commas/blob/3cfa16a2aa6bbef8ed740c346321ef6c2b42237d/src/index.ts + * + * The original code is licensed under the MIT License: + * + * MIT License + * + * Copyright 2021 https://github.com/nokazn + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +export default function stripJsonTrailingCommas(content: string): string { + /** + * preceded by number or string or boolean (true/false) or null or '}' or ']' (and with 0 or more spaces) + * match with ',' + * followed by '}' or ']' + */ + return content.replace(/(?<=(true|false|null|["\d}\]])\s*),(?=\s*[}\]])/g, ''); +} \ No newline at end of file diff --git a/typescript/src/typechat.ts b/typescript/src/typechat.ts index 8d7fd45c..b832f4f5 100644 --- a/typescript/src/typechat.ts +++ b/typescript/src/typechat.ts @@ -1,5 +1,6 @@ import { Result, success, error } from "./result"; import { TypeChatLanguageModel, PromptSection } from "./model"; +import stripJsonTrailingCommas from "./ts/strip-whitespace"; /** * Represents an object that can translate natural language requests in JSON objects of the given type. @@ -136,7 +137,7 @@ export function createJsonTranslator(model: TypeChatLanguageMo if (!(startIndex >= 0 && endIndex > startIndex)) { return error(`Response is not JSON:\n${responseText}`); } - const jsonText = responseText.slice(startIndex, endIndex + 1); + const jsonText = stripJsonTrailingCommas(responseText.slice(startIndex, endIndex + 1)); let jsonObject; try { jsonObject = JSON.parse(jsonText) as object;