@llumiverse/core
Version:
Provide an universal API to LLMs. Support for existing LLMs can be added by writing a driver.
25 lines • 648 B
JavaScript
import { jsonrepair } from 'jsonrepair';
function extractJsonFromText(text) {
const start = text.indexOf("{");
const end = text.lastIndexOf("}");
return text.substring(start, end + 1);
}
export function extractAndParseJSON(text) {
return parseJSON(extractJsonFromText(text));
}
export function parseJSON(text) {
text = text.trim();
try {
return JSON.parse(text);
}
catch (err) {
// use a relaxed parser
try {
return JSON.parse(jsonrepair(text));
}
catch (err2) { // throw the original error
throw err;
}
}
}
//# sourceMappingURL=json.js.map