UNPKG

@llumiverse/core

Version:

Provide an universal API to LLMs. Support for existing LLMs can be added by writing a driver.

27 lines (23 loc) 719 B
import { JSONValue } from "@llumiverse/common"; import { jsonrepair } from 'jsonrepair'; function extractJsonFromText(text: string): string { const start = text.indexOf("{"); const end = text.lastIndexOf("}"); return text.substring(start, end + 1); } export function extractAndParseJSON(text: string): JSONValue { return parseJSON(extractJsonFromText(text)); } export function parseJSON(text: string): JSONValue { text = text.trim(); try { return JSON.parse(text); } catch (err: any) { // use a relaxed parser try { return JSON.parse(jsonrepair(text)); } catch (err2: any) { // throw the original error throw err; } } }