From 99d605f0c447fc74eb2c44e7e81fc100ac321d02 Mon Sep 17 00:00:00 2001 From: Sam Estrin Date: Sat, 17 Aug 2024 16:17:23 -0700 Subject: [PATCH] feat: improved unescapeString for json repair --- README.md | 2 +- package.json | 2 +- src/utils/utils.js | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 73ffe2e..ee9e534 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Star on GitHub](https://img.shields.io/github/stars/samestrin/llm-interface?style=social)](https://github.com/samestrin/llm-interface/stargazers) [![Fork on GitHub](https://img.shields.io/github/forks/samestrin/llm-interface?style=social)](https://github.com/samestrin/llm-interface/network/members) [![Watch on GitHub](https://img.shields.io/github/watchers/samestrin/llm-interface?style=social)](https://github.com/samestrin/llm-interface/watchers) -![Version 2.0.1493](https://img.shields.io/badge/Version-2.0.1493-blue) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Built with Node.js](https://img.shields.io/badge/Built%20with-Node.js-green)](https://nodejs.org/) +![Version 2.0.1494](https://img.shields.io/badge/Version-2.0.1494-blue) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Built with Node.js](https://img.shields.io/badge/Built%20with-Node.js-green)](https://nodejs.org/) ## Introduction diff --git a/package.json b/package.json index ac8ffdd..0afb792 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "llm-interface", - "version": "2.0.1493", + "version": "2.0.1494", "main": "src/index.js", "description": "A simple, unified NPM-based interface for interacting with multiple Large Language Model (LLM) APIs, including OpenAI, AI21 Studio, Anthropic, Cloudflare AI, Cohere, Fireworks AI, Google Gemini, Goose AI, Groq, Hugging Face, Mistral AI, Perplexity, Reka AI, watsonx.ai, and LLaMA.cpp.", "type": "commonjs", diff --git a/src/utils/utils.js b/src/utils/utils.js index 3d5f48c..d5a45d3 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -99,6 +99,20 @@ function extractCodeFromResponse(json, attemptRepair) { return json; } +function unescapeString(escapedStr) { + return escapedStr + .replace(/\\n/g, '\n') + .replace(/\\t/g, '\t') + .replace(/\\r/g, '\r') + .replace(/\\b/g, '\b') + .replace(/\\f/g, '\f') + .replace(/\\v/g, '\v') + .replace(/\\0/g, '\0') + .replace(/\\\\/g, '\\') + .replace(/\\"/g, '"') + .replace(/\\'/g, "'"); +} + /** * Attempts to parse a JSON string. If parsing fails and attemptRepair is true, * it uses jsonrepair to try repairing the JSON string. @@ -113,6 +127,7 @@ async function parseJSON(json, attemptRepair) { const regex = new RegExp(subString, 'ig'); // Added 'g' flag for global replacement if (typeof json === 'string') { + json = unescapeString(json); if (regex.test(json)) { json = extractCodeFromResponse(json, true); } else {