UNPKG

llamaindex

Version:

<p align="center"> <img height="100" width="100" alt="LlamaIndex logo" src="https://ts.llamaindex.ai/square.svg" /> </p> <h1 align="center">LlamaIndex.TS</h1> <h3 align="center"> Data framework for your LLM application. </h3>

39 lines (37 loc) 926 B
import { parseJsonMarkdown } from "../OutputParser.js"; const formatStr = `The output should be ONLY JSON formatted as a JSON instance. Here is an example: [ { "choice": 1, "reason": "<insert reason for choice>" }, ... ] `; /* * An OutputParser is used to extract structured data from the raw output of the LLM. */ export class SelectionOutputParser { /** * * @param output */ parse(output) { let parsed; try { parsed = parseJsonMarkdown(output); } catch (e) { try { parsed = JSON.parse(output); } catch (e) { throw new Error(`Got invalid JSON object. Error: ${e}. Got JSON string: ${output}`); } } return { rawOutput: output, parsedOutput: parsed }; } format(output) { return output + "\n\n" + formatStr; } }