Skip to content

Commit 99d605f

Browse files
committed
feat: improved unescapeString for json repair
1 parent cc5b8c9 commit 99d605f

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![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)
44

5-
![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/)
5+
![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/)
66

77
## Introduction
88

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "llm-interface",
3-
"version": "2.0.1493",
3+
"version": "2.0.1494",
44
"main": "src/index.js",
55
"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.",
66
"type": "commonjs",

src/utils/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ function extractCodeFromResponse(json, attemptRepair) {
9999
return json;
100100
}
101101

102+
function unescapeString(escapedStr) {
103+
return escapedStr
104+
.replace(/\\n/g, '\n')
105+
.replace(/\\t/g, '\t')
106+
.replace(/\\r/g, '\r')
107+
.replace(/\\b/g, '\b')
108+
.replace(/\\f/g, '\f')
109+
.replace(/\\v/g, '\v')
110+
.replace(/\\0/g, '\0')
111+
.replace(/\\\\/g, '\\')
112+
.replace(/\\"/g, '"')
113+
.replace(/\\'/g, "'");
114+
}
115+
102116
/**
103117
* Attempts to parse a JSON string. If parsing fails and attemptRepair is true,
104118
* it uses jsonrepair to try repairing the JSON string.
@@ -113,6 +127,7 @@ async function parseJSON(json, attemptRepair) {
113127
const regex = new RegExp(subString, 'ig'); // Added 'g' flag for global replacement
114128

115129
if (typeof json === 'string') {
130+
json = unescapeString(json);
116131
if (regex.test(json)) {
117132
json = extractCodeFromResponse(json, true);
118133
} else {

0 commit comments

Comments
 (0)