Skip to content

Commit

Permalink
feat: improved unescapeString for json repair
Browse files Browse the repository at this point in the history
  • Loading branch information
samestrin committed Aug 17, 2024
1 parent cc5b8c9 commit 99d605f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 15 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down

0 comments on commit 99d605f

Please sign in to comment.