@visactor/vmind
Version:
<div align="center"> <a href="https://github.com/VisActor#gh-light-mode-only" target="_blank"> <img alt="VisActor Logo" width="200" src="https://github.com/VisActor/.github/blob/main/profile/logo_500_200_light.svg"/> </a> <a href="https://githu
41 lines (36 loc) • 1.34 kB
JavaScript
import JSON5 from "json5";
import { jsonrepair } from "jsonrepair";
import { replaceAll } from "./text";
export const matchJSONStr = str => {
const first = str.indexOf("{"), last = str.lastIndexOf("}"), result = str.substring(first, last + 1);
return result && result.length > 0 ? replaceAll(result, "\n", " ") : str;
};
export const revisedJSONStr = str => {
let res = str;
return str.includes('"unit":"%""') && (res = res.replaceAll('"unit":"%""', "unit:'%'")),
str.includes('"unit": "%""') && (res = res.replaceAll('"unit": "%""', "unit:'%'")),
res;
};
export const parseLLMJson = (JsonStr, prefix) => {
const parseNoPrefixStr = str => {
try {
return JSON5.parse(str);
} catch (err) {
try {
return JSON5.parse(jsonrepair(str));
} catch (err) {
return {
error: `LLM Result is not Valid JSON; llm res: ${JsonStr}`,
errorInfo: err
};
}
}
};
if (prefix) {
const splitArr = JsonStr.split(prefix), res = parseNoPrefixStr(splitArr[splitArr.length - 2]);
if (!res.error) return res;
}
const res2 = parseNoPrefixStr(JsonStr);
return res2.error && console.error(res2.errorInfo), res2;
};
//# sourceMappingURL=json.js.map