UNPKG

@solvers-hub/llm-json

Version:

A TypeScript SDK to extract and correct JSON from LLM outputs

59 lines (58 loc) 2.29 kB
import { ExtractOptions, ExtractResult, JsonBlock, ValidationResult } from './types'; import { SchemaValidator } from './validator'; /** * JsonExtractor class for extracting JSON from text input. */ export declare class JsonExtractor { protected options: ExtractOptions; protected schemaValidator: SchemaValidator | null; /** * Creates a new instance of JsonExtractor. * @param options - Configuration options for extraction. */ constructor(options?: ExtractOptions); /** * Extract JSON and text from a string input. * @param input - The input string that may contain JSON. * @returns An object containing arrays of extracted text and JSON. */ extract(input: string): ExtractResult; /** * Validates JSON objects against provided schemas. * @param jsonObjects - The JSON objects to validate. * @returns Array of validation results. */ protected validateJson(jsonObjects: JsonBlock[]): ValidationResult[]; /** * Extract JSON from markdown code blocks. * @param input - The input string that may contain code blocks. * @returns An object containing arrays of extracted text and JSON. */ protected extractJsonFromCodeBlocks(input: string): ExtractResult; /** * Find potential JSON blocks in the input string. * @param input - The input string to search for JSON. * @returns Array of detected JSON blocks. */ protected findJsonBlocks(input: string): JsonBlock[]; /** * Parse the JSON blocks and attempt correction if enabled. * @param blocks - The JSON blocks to parse. * @returns Array of parsed JSON blocks. */ protected parseJsonBlocks(blocks: JsonBlock[]): JsonBlock[]; /** * Attempt to correct malformed JSON. * @param block - The JSON block to correct. * @param error - The parsing error. * @returns The corrected JSON block if possible. */ private attemptJsonCorrection; /** * Extract text blocks from the input, excluding JSON blocks. * @param input - The original input string. * @param jsonBlocks - The JSON blocks to exclude. * @returns Array of text blocks. */ protected extractTextBlocks(input: string, jsonBlocks: JsonBlock[]): string[]; }