@solvers-hub/llm-json 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +116 -108
- package/dist/src/corrector.js +11 -12
- package/dist/src/extractor.d.ts +1 -1
- package/dist/src/extractor.js +12 -23
- package/dist/src/validator.js +3 -0
- package/docs/classes/LlmJson.html +5 -5
- package/docs/index.html +7 -3
- package/docs/interfaces/ExtractOptions.html +3 -3
- package/docs/interfaces/ExtractResult.html +4 -4
- package/docs/interfaces/JsonBlock.html +6 -6
- package/docs/interfaces/JsonParseError.html +4 -4
- package/docs/interfaces/SchemaDefinition.html +3 -3
- package/docs/interfaces/ValidationResult.html +5 -5
- package/docs-md/README.md +71 -22
- package/docs-md/classes/LlmJson.md +7 -7
- package/docs-md/interfaces/ExtractOptions.md +21 -1
- package/docs-md/interfaces/ExtractResult.md +15 -2
- package/docs-md/interfaces/JsonBlock.md +5 -5
- package/docs-md/interfaces/JsonParseError.md +3 -3
- package/docs-md/interfaces/SchemaDefinition.md +36 -0
- package/docs-md/interfaces/ValidationResult.md +62 -0
- package/docs-md/modules.md +2 -0
- package/examples/array-example.ts +69 -0
- package/examples/example.ts +101 -0
- package/examples/schema-validation-example.ts +140 -0
- package/examples/schema-validation-examples.ts +248 -0
- package/package.json +52 -52
package/README.md
CHANGED
@@ -1,108 +1,116 @@
|
|
1
|
-
# LLM-JSON Extractor
|
2
|
-
|
3
|
-
A TypeScript SDK for extracting and correcting JSON data from LLM outputs.
|
4
|
-
|
5
|
-
[](https://badge.fury.io/js/llm-json)
|
6
|
-
[](https://opensource.org/licenses/MIT)
|
7
|
-
|
8
|
-
## Overview
|
9
|
-
|
10
|
-
LLM-JSON is a lightweight library designed to parse and extract JSON objects from large language model (LLM) outputs. It can handle multiple JSON objects within text, extract text separately from JSON, and even attempt to fix malformed JSON.
|
11
|
-
|
12
|
-
## Key Features
|
13
|
-
|
14
|
-
- **Text/JSON Separation**: Cleanly separates text content from JSON data in LLM outputs
|
15
|
-
- **Multiple JSON Support**: Extracts multiple JSON objects or arrays from a single input
|
16
|
-
- **JSON Validation & Correction**: Automatically fixes common JSON formatting errors from LLMs
|
17
|
-
- **Code Block Support**: Extracts JSON from markdown code blocks (```json)
|
18
|
-
- **Schema Validation**: Validates extracted JSON against provided schemas
|
19
|
-
- **TypeScript Support**: Written in TypeScript with full type definitions
|
20
|
-
|
21
|
-
## Quick Start
|
22
|
-
|
23
|
-
### Installation
|
24
|
-
|
25
|
-
```bash
|
26
|
-
npm install @solvers-hub/llm-json
|
27
|
-
```
|
28
|
-
|
29
|
-
### Basic Usage
|
30
|
-
|
31
|
-
```typescript
|
32
|
-
import { LlmJson } from '@solvers-hub/llm-json';
|
33
|
-
|
34
|
-
const llmOutput = `Here's some text followed by JSON:
|
35
|
-
|
36
|
-
{
|
37
|
-
"name": "John",
|
38
|
-
"age": 30,
|
39
|
-
"skills": ["JavaScript", "TypeScript", "React"]
|
40
|
-
}`;
|
41
|
-
|
42
|
-
const llmJson = new LlmJson({ attemptCorrection: true });
|
43
|
-
const { text, json } = llmJson.extract(llmOutput);
|
44
|
-
|
45
|
-
console.log(text); // ['Here\'s some text followed by JSON:']
|
46
|
-
console.log(json); // [{ name: 'John', age: 30, skills: ['JavaScript', 'TypeScript', 'React'] }]
|
47
|
-
```
|
48
|
-
|
49
|
-
### Schema Validation
|
50
|
-
|
51
|
-
You can validate extracted JSON against schemas:
|
52
|
-
|
53
|
-
```typescript
|
54
|
-
import { LlmJson } from '@solvers-hub/llm-json';
|
55
|
-
|
56
|
-
const schemas = [
|
57
|
-
{
|
58
|
-
name: 'person',
|
59
|
-
schema: {
|
60
|
-
type: 'object',
|
61
|
-
properties: {
|
62
|
-
name: { type: 'string' },
|
63
|
-
age: { type: 'integer' }
|
64
|
-
},
|
65
|
-
required: ['name', 'age']
|
66
|
-
}
|
67
|
-
}
|
68
|
-
];
|
69
|
-
|
70
|
-
const llmJson = new LlmJson({
|
71
|
-
attemptCorrection: true,
|
72
|
-
schemas
|
73
|
-
});
|
74
|
-
|
75
|
-
const llmOutput = `Here's a person: {"name": "John", "age": 30}
|
76
|
-
And some other data: {"title": "Meeting notes"}`;
|
77
|
-
const result = llmJson.extract(llmOutput);
|
78
|
-
|
79
|
-
// Note: All extracted JSON objects are included in the json array
|
80
|
-
console.log(result.json);
|
81
|
-
// [
|
82
|
-
// { name: 'John', age: 30 },
|
83
|
-
// { title: 'Meeting notes' }
|
84
|
-
// ]
|
85
|
-
|
86
|
-
// The validatedJson array includes validation results for each JSON object
|
87
|
-
console.log(result.validatedJson);
|
88
|
-
// [
|
89
|
-
// {
|
90
|
-
// json: { name: 'John', age: 30 },
|
91
|
-
// matchedSchema: 'person',
|
92
|
-
// isValid: true
|
93
|
-
// },
|
94
|
-
// {
|
95
|
-
// json: { title: 'Meeting notes' },
|
96
|
-
// matchedSchema: null,
|
97
|
-
// isValid: false,
|
98
|
-
// validationErrors: [...] // Validation errors
|
99
|
-
// }
|
100
|
-
// ]
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
1
|
+
# LLM-JSON Extractor
|
2
|
+
|
3
|
+
A TypeScript SDK for extracting and correcting JSON data from LLM outputs.
|
4
|
+
|
5
|
+
[](https://badge.fury.io/js/llm-json)
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
7
|
+
|
8
|
+
## Overview
|
9
|
+
|
10
|
+
LLM-JSON is a lightweight library designed to parse and extract JSON objects from large language model (LLM) outputs. It can handle multiple JSON objects within text, extract text separately from JSON, and even attempt to fix malformed JSON.
|
11
|
+
|
12
|
+
## Key Features
|
13
|
+
|
14
|
+
- **Text/JSON Separation**: Cleanly separates text content from JSON data in LLM outputs
|
15
|
+
- **Multiple JSON Support**: Extracts multiple JSON objects or arrays from a single input
|
16
|
+
- **JSON Validation & Correction**: Automatically fixes common JSON formatting errors from LLMs
|
17
|
+
- **Code Block Support**: Extracts JSON from markdown code blocks (```json)
|
18
|
+
- **Schema Validation**: Validates extracted JSON against provided schemas
|
19
|
+
- **TypeScript Support**: Written in TypeScript with full type definitions
|
20
|
+
|
21
|
+
## Quick Start
|
22
|
+
|
23
|
+
### Installation
|
24
|
+
|
25
|
+
```bash
|
26
|
+
npm install @solvers-hub/llm-json
|
27
|
+
```
|
28
|
+
|
29
|
+
### Basic Usage
|
30
|
+
|
31
|
+
```typescript
|
32
|
+
import { LlmJson } from '@solvers-hub/llm-json';
|
33
|
+
|
34
|
+
const llmOutput = `Here's some text followed by JSON:
|
35
|
+
|
36
|
+
{
|
37
|
+
"name": "John",
|
38
|
+
"age": 30,
|
39
|
+
"skills": ["JavaScript", "TypeScript", "React"]
|
40
|
+
}`;
|
41
|
+
|
42
|
+
const llmJson = new LlmJson({ attemptCorrection: true });
|
43
|
+
const { text, json } = llmJson.extract(llmOutput);
|
44
|
+
|
45
|
+
console.log(text); // ['Here\'s some text followed by JSON:']
|
46
|
+
console.log(json); // [{ name: 'John', age: 30, skills: ['JavaScript', 'TypeScript', 'React'] }]
|
47
|
+
```
|
48
|
+
|
49
|
+
### Schema Validation
|
50
|
+
|
51
|
+
You can validate extracted JSON against schemas:
|
52
|
+
|
53
|
+
```typescript
|
54
|
+
import { LlmJson } from '@solvers-hub/llm-json';
|
55
|
+
|
56
|
+
const schemas = [
|
57
|
+
{
|
58
|
+
name: 'person',
|
59
|
+
schema: {
|
60
|
+
type: 'object',
|
61
|
+
properties: {
|
62
|
+
name: { type: 'string' },
|
63
|
+
age: { type: 'integer' }
|
64
|
+
},
|
65
|
+
required: ['name', 'age']
|
66
|
+
}
|
67
|
+
}
|
68
|
+
];
|
69
|
+
|
70
|
+
const llmJson = new LlmJson({
|
71
|
+
attemptCorrection: true,
|
72
|
+
schemas
|
73
|
+
});
|
74
|
+
|
75
|
+
const llmOutput = `Here's a person: {"name": "John", "age": 30}
|
76
|
+
And some other data: {"title": "Meeting notes"}`;
|
77
|
+
const result = llmJson.extract(llmOutput);
|
78
|
+
|
79
|
+
// Note: All extracted JSON objects are included in the json array
|
80
|
+
console.log(result.json);
|
81
|
+
// [
|
82
|
+
// { name: 'John', age: 30 },
|
83
|
+
// { title: 'Meeting notes' }
|
84
|
+
// ]
|
85
|
+
|
86
|
+
// The validatedJson array includes validation results for each JSON object
|
87
|
+
console.log(result.validatedJson);
|
88
|
+
// [
|
89
|
+
// {
|
90
|
+
// json: { name: 'John', age: 30 },
|
91
|
+
// matchedSchema: 'person',
|
92
|
+
// isValid: true
|
93
|
+
// },
|
94
|
+
// {
|
95
|
+
// json: { title: 'Meeting notes' },
|
96
|
+
// matchedSchema: null,
|
97
|
+
// isValid: false,
|
98
|
+
// validationErrors: [...] // Validation errors
|
99
|
+
// }
|
100
|
+
// ]
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
## Examples
|
105
|
+
|
106
|
+
See the [examples](examples) directory for more examples of how to use the library.
|
107
|
+
|
108
|
+
How to run the examples:
|
109
|
+
|
110
|
+
```bash
|
111
|
+
npm run example:run -- examples/example.ts
|
112
|
+
```
|
113
|
+
|
114
|
+
## License
|
115
|
+
|
116
|
+
MIT © 2025
|
package/dist/src/corrector.js
CHANGED
@@ -30,28 +30,27 @@ class JsonCorrector {
|
|
30
30
|
this.fixSingleQuotes,
|
31
31
|
this.fixExtraCommas
|
32
32
|
];
|
33
|
+
let attemptedCorrection = jsonString;
|
34
|
+
let wasCorrected = wasCommentsRemoved;
|
35
|
+
// Trying both single strategies and cumulative fixes
|
33
36
|
for (const strategy of strategies) {
|
34
|
-
|
37
|
+
// Try single strategy
|
38
|
+
const singleStrategyResult = strategy(jsonString);
|
35
39
|
try {
|
36
|
-
JSON.parse(
|
37
|
-
return { corrected:
|
40
|
+
JSON.parse(singleStrategyResult);
|
41
|
+
return { corrected: singleStrategyResult, wasCorrected: true };
|
38
42
|
}
|
39
43
|
catch (e) {
|
40
|
-
//
|
44
|
+
// Single strategy failed, continue with cumulative approach
|
41
45
|
}
|
42
|
-
|
43
|
-
// If all strategies failed, try a combination of them
|
44
|
-
let attemptedCorrection = jsonString;
|
45
|
-
let wasCorrected = wasCommentsRemoved;
|
46
|
-
for (const strategy of strategies) {
|
46
|
+
// Apply current strategy to our cumulative attempt
|
47
47
|
attemptedCorrection = strategy(attemptedCorrection);
|
48
48
|
try {
|
49
49
|
JSON.parse(attemptedCorrection);
|
50
|
-
wasCorrected
|
51
|
-
break;
|
50
|
+
return { corrected: attemptedCorrection, wasCorrected: true };
|
52
51
|
}
|
53
52
|
catch (e) {
|
54
|
-
// Continue with
|
53
|
+
// Continue with next strategy
|
55
54
|
}
|
56
55
|
}
|
57
56
|
return {
|
package/dist/src/extractor.d.ts
CHANGED
@@ -22,7 +22,7 @@ export declare class JsonExtractor {
|
|
22
22
|
* @param jsonObjects - The JSON objects to validate.
|
23
23
|
* @returns Array of validation results.
|
24
24
|
*/
|
25
|
-
protected validateJson(jsonObjects:
|
25
|
+
protected validateJson(jsonObjects: JsonBlock[]): ValidationResult[];
|
26
26
|
/**
|
27
27
|
* Extract JSON from markdown code blocks.
|
28
28
|
* @param input - The input string that may contain code blocks.
|
package/dist/src/extractor.js
CHANGED
@@ -72,7 +72,7 @@ class JsonExtractor {
|
|
72
72
|
return {
|
73
73
|
text: textBlocks,
|
74
74
|
json: extractedJson,
|
75
|
-
|
75
|
+
validatedJson
|
76
76
|
};
|
77
77
|
}
|
78
78
|
/**
|
@@ -81,7 +81,7 @@ class JsonExtractor {
|
|
81
81
|
* @returns Array of validation results.
|
82
82
|
*/
|
83
83
|
validateJson(jsonObjects) {
|
84
|
-
if (!this.schemaValidator || !this.options.schemas || !jsonObjects.length) {
|
84
|
+
if (!this.schemaValidator || !this.options.schemas || this.options.schemas.length === 0 || !jsonObjects.length) {
|
85
85
|
return [];
|
86
86
|
}
|
87
87
|
return this.schemaValidator.validateAll(jsonObjects, this.options.schemas);
|
@@ -189,18 +189,23 @@ class JsonExtractor {
|
|
189
189
|
* @returns Array of parsed JSON blocks.
|
190
190
|
*/
|
191
191
|
parseJsonBlocks(blocks) {
|
192
|
-
return blocks
|
192
|
+
// Only return only the blocks that were successfully parsed
|
193
|
+
const parsedBlocks = [];
|
194
|
+
for (const block of blocks) {
|
193
195
|
try {
|
194
196
|
block.parsed = JSON.parse(block.raw);
|
195
|
-
|
197
|
+
parsedBlocks.push(block);
|
196
198
|
}
|
197
199
|
catch (error) {
|
198
200
|
if (this.options.attemptCorrection) {
|
199
|
-
|
201
|
+
const correctedBlock = this.attemptJsonCorrection(block, error);
|
202
|
+
if (correctedBlock.parsed) {
|
203
|
+
parsedBlocks.push(correctedBlock);
|
204
|
+
}
|
200
205
|
}
|
201
|
-
return block;
|
202
206
|
}
|
203
|
-
}
|
207
|
+
}
|
208
|
+
return parsedBlocks;
|
204
209
|
}
|
205
210
|
/**
|
206
211
|
* Attempt to correct malformed JSON.
|
@@ -251,22 +256,6 @@ class JsonExtractor {
|
|
251
256
|
textBlocks.push(lastBlock);
|
252
257
|
}
|
253
258
|
}
|
254
|
-
// Handle case where no text blocks were found but we need to maintain structure
|
255
|
-
// for tests expecting a certain number of text segments (like separators)
|
256
|
-
if (textBlocks.length === 0 && sortedBlocks.length > 0) {
|
257
|
-
// If multiple JSON blocks, we need to infer text segments between them
|
258
|
-
if (sortedBlocks.length > 1) {
|
259
|
-
// Add placeholder text segments between JSON blocks
|
260
|
-
for (let i = 0; i < sortedBlocks.length - 1; i++) {
|
261
|
-
const currentBlock = sortedBlocks[i];
|
262
|
-
const nextBlock = sortedBlocks[i + 1];
|
263
|
-
const inBetweenText = input.substring(currentBlock.endIndex + 1, nextBlock.startIndex).trim();
|
264
|
-
if (inBetweenText) {
|
265
|
-
textBlocks.push(inBetweenText);
|
266
|
-
}
|
267
|
-
}
|
268
|
-
}
|
269
|
-
}
|
270
259
|
return textBlocks;
|
271
260
|
}
|
272
261
|
}
|
package/dist/src/validator.js
CHANGED
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.SchemaValidator = void 0;
|
7
7
|
const ajv_1 = __importDefault(require("ajv"));
|
8
|
+
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
8
9
|
/**
|
9
10
|
* SchemaValidator class for validating JSON against schemas.
|
10
11
|
*/
|
@@ -17,6 +18,8 @@ class SchemaValidator {
|
|
17
18
|
allErrors: true,
|
18
19
|
verbose: true
|
19
20
|
});
|
21
|
+
// Add format validation support
|
22
|
+
(0, ajv_formats_1.default)(this.ajv);
|
20
23
|
}
|
21
24
|
/**
|
22
25
|
* Validates a JSON object against a set of schemas.
|
@@ -1,5 +1,5 @@
|
|
1
1
|
<!DOCTYPE html><html class="default" lang="en"><head><meta charSet="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>LlmJson | @solvers-hub/llm-json</title><meta name="description" content="Documentation for @solvers-hub/llm-json"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><div class="table-cell" id="tsd-search" data-base=".."><div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="../assets/icons.svg#icon-search"></use></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div><div class="field"><div id="tsd-toolbar-links"></div></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="../index.html" class="title">@solvers-hub/llm-json</a></div><div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb"><li><a href="../modules.html">@solvers-hub/llm-json</a></li><li><a href="LlmJson.html">LlmJson</a></li></ul><h1>Class LlmJson</h1></div><section class="tsd-panel tsd-comment"><div class="tsd-comment tsd-typography"><p>Main factory class for the LLM-JSON extractor SDK.</p>
|
2
|
-
</div><div class="tsd-comment tsd-typography"></div></section><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/
|
2
|
+
</div><div class="tsd-comment tsd-typography"></div></section><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L8">index.ts:8</a></li></ul></aside><section class="tsd-panel-group tsd-index-group"><section class="tsd-panel tsd-index-panel"><details class="tsd-index-content tsd-index-accordion" open><summary class="tsd-accordion-summary tsd-index-summary"><h5 class="tsd-index-heading uppercase" role="button" aria-expanded="false" tabIndex="0"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="../assets/icons.svg#icon-chevronSmall"></use></svg> Index</h5></summary><div class="tsd-accordion-details"><section class="tsd-index-section"><h3 class="tsd-index-heading">Constructors</h3><div class="tsd-index-list"><a href="LlmJson.html#constructor" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-512"></use></svg><span>constructor</span></a>
|
3
3
|
</div></section><section class="tsd-index-section"><h3 class="tsd-index-heading">Properties</h3><div class="tsd-index-list"><a href="LlmJson.html#arrayExtractor" class="tsd-index-link tsd-is-private"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1024"></use></svg><span>array<wbr/>Extractor</span></a>
|
4
4
|
<a href="LlmJson.html#objectExtractor" class="tsd-index-link tsd-is-private"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1024"></use></svg><span>object<wbr/>Extractor</span></a>
|
5
5
|
<a href="LlmJson.html#instance" class="tsd-index-link tsd-is-private"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1024"></use></svg><span>instance</span></a>
|
@@ -8,13 +8,13 @@
|
|
8
8
|
<a href="LlmJson.html#getInstance" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-2048"></use></svg><span>get<wbr/>Instance</span></a>
|
9
9
|
</div></section></div></details></section></section><section class="tsd-panel-group tsd-member-group"><h2>Constructors</h2><section class="tsd-panel tsd-member"><a id="constructor" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>constructor</span><a href="#constructor" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="constructor.new_LlmJson" class="tsd-anchor"></a><span class="tsd-kind-constructor-signature">new <wbr/>Llm<wbr/>Json</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="LlmJson.html" class="tsd-signature-type tsd-kind-class">LlmJson</a><a href="#constructor.new_LlmJson" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Creates a new LlmJson instance with the specified options.</p>
|
10
10
|
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">options</span>: <a href="../interfaces/ExtractOptions.html" class="tsd-signature-type tsd-kind-interface">ExtractOptions</a><span class="tsd-signature-symbol"> = {}</span></span><div class="tsd-comment tsd-typography"><p>Configuration options for extraction.</p>
|
11
|
-
</div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="LlmJson.html" class="tsd-signature-type tsd-kind-class">LlmJson</a></h4><div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/
|
11
|
+
</div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="LlmJson.html" class="tsd-signature-type tsd-kind-class">LlmJson</a></h4><div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L17">index.ts:17</a></li></ul></aside></li></ul></section></section><section class="tsd-panel-group tsd-member-group"><h2>Properties</h2><section class="tsd-panel tsd-member tsd-is-private"><a id="arrayExtractor" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagPrivate">Private</code> <span>array<wbr/>Extractor</span><a href="#arrayExtractor" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><div class="tsd-signature"><span class="tsd-kind-property">array<wbr/>Extractor</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">JsonArrayExtractor</span></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L11">index.ts:11</a></li></ul></aside></section><section class="tsd-panel tsd-member tsd-is-private"><a id="objectExtractor" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagPrivate">Private</code> <span>object<wbr/>Extractor</span><a href="#objectExtractor" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><div class="tsd-signature"><span class="tsd-kind-property">object<wbr/>Extractor</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">JsonExtractor</span></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L10">index.ts:10</a></li></ul></aside></section><section class="tsd-panel tsd-member tsd-is-private"><a id="instance" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagStatic">Static</code> <code class="tsd-tag ts-flagPrivate">Private</code> <span>instance</span><a href="#instance" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><div class="tsd-signature"><span class="tsd-kind-property">instance</span><span class="tsd-signature-symbol">:</span> <a href="LlmJson.html" class="tsd-signature-type tsd-kind-class">LlmJson</a></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L9">index.ts:9</a></li></ul></aside></section></section><section class="tsd-panel-group tsd-member-group"><h2>Methods</h2><section class="tsd-panel tsd-member"><a id="extract" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>extract</span><a href="#extract" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="extract.extract-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">extract</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">input</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="../interfaces/ExtractResult.html" class="tsd-signature-type tsd-kind-interface">ExtractResult</a><a href="#extract.extract-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Extract JSON objects and text from a string input.</p>
|
12
12
|
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">input</span>: <span class="tsd-signature-type">string</span></span><div class="tsd-comment tsd-typography"><p>The input string that may contain JSON.</p>
|
13
13
|
</div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="../interfaces/ExtractResult.html" class="tsd-signature-type tsd-kind-interface">ExtractResult</a></h4><p>An object containing arrays of extracted text and JSON.</p>
|
14
|
-
<div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/
|
14
|
+
<div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L39">index.ts:39</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="extractAll" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><span>extract<wbr/>All</span><a href="#extractAll" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="extractAll.extractAll-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">extract<wbr/>All</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">input</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="../interfaces/ExtractResult.html" class="tsd-signature-type tsd-kind-interface">ExtractResult</a><a href="#extractAll.extractAll-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Extract JSON objects, arrays, and text from a string input.</p>
|
15
15
|
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">input</span>: <span class="tsd-signature-type">string</span></span><div class="tsd-comment tsd-typography"><p>The input string that may contain JSON.</p>
|
16
16
|
</div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="../interfaces/ExtractResult.html" class="tsd-signature-type tsd-kind-interface">ExtractResult</a></h4><p>An object containing arrays of extracted text and JSON.</p>
|
17
|
-
<div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/
|
17
|
+
<div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L48">index.ts:48</a></li></ul></aside></li></ul></section><section class="tsd-panel tsd-member"><a id="getInstance" class="tsd-anchor"></a><h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagStatic">Static</code> <span>get<wbr/>Instance</span><a href="#getInstance" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="getInstance.getInstance-1" class="tsd-anchor"></a><span class="tsd-kind-call-signature">get<wbr/>Instance</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">options</span><span class="tsd-signature-symbol">?</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="LlmJson.html" class="tsd-signature-type tsd-kind-class">LlmJson</a><a href="#getInstance.getInstance-1" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-comment tsd-typography"><p>Get or create a singleton instance of LlmJson.</p>
|
18
18
|
</div><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">options</span>: <a href="../interfaces/ExtractOptions.html" class="tsd-signature-type tsd-kind-interface">ExtractOptions</a><span class="tsd-signature-symbol"> = {}</span></span><div class="tsd-comment tsd-typography"><p>Configuration options for extraction.</p>
|
19
19
|
</div><div class="tsd-comment tsd-typography"></div></li></ul></div><h4 class="tsd-returns-title">Returns <a href="LlmJson.html" class="tsd-signature-type tsd-kind-class">LlmJson</a></h4><p>The LlmJson singleton instance.</p>
|
20
|
-
<div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/
|
20
|
+
<div class="tsd-comment tsd-typography"></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/solvers-hub/llm-json/blob/c66e07bf56dff5952768d1bbe7ca7221c8d2192d/src/index.ts#L27">index.ts:27</a></li></ul></aside></li></ul></section></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-index-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><h4 class="uppercase">Member Visibility</h4><form><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-private" name="private"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Private</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></form></div><div class="tsd-theme-toggle"><h4 class="uppercase">Theme</h4><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-index-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#constructor" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-512"></use></svg><span>constructor</span></a><a href="#arrayExtractor" class="tsd-is-private"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1024"></use></svg><span>array<wbr/>Extractor</span></a><a href="#objectExtractor" class="tsd-is-private"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1024"></use></svg><span>object<wbr/>Extractor</span></a><a href="#instance" class="tsd-is-private"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1024"></use></svg><span>instance</span></a><a href="#extract" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-2048"></use></svg><span>extract</span></a><a href="#extractAll" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-2048"></use></svg><span>extract<wbr/>All</span></a><a href="#getInstance" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-2048"></use></svg><span>get<wbr/>Instance</span></a></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1"></use></svg><span>@solvers-hub/llm-json</span></a><ul class="tsd-small-nested-navigation" id="tsd-nav-container" data-base=".."><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|
package/docs/index.html
CHANGED
@@ -15,7 +15,11 @@
|
|
15
15
|
<a id="md:basic-usage" class="tsd-anchor"></a><h3><a href="#md:basic-usage">Basic Usage</a></h3><pre><code class="language-typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">LlmJson</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">'@solvers-hub/llm-json'</span><span class="hl-1">;</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">llmOutput</span><span class="hl-1"> = </span><span class="hl-2">`Here's some text followed by JSON:</span><br/><br/><span class="hl-2">{</span><br/><span class="hl-2"> "name": "John",</span><br/><span class="hl-2"> "age": 30,</span><br/><span class="hl-2"> "skills": ["JavaScript", "TypeScript", "React"]</span><br/><span class="hl-2">}`</span><span class="hl-1">;</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">llmJson</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">LlmJson</span><span class="hl-1">({ </span><span class="hl-4">attemptCorrection:</span><span class="hl-1"> </span><span class="hl-5">true</span><span class="hl-1"> });</span><br/><span class="hl-5">const</span><span class="hl-1"> { </span><span class="hl-6">text</span><span class="hl-1">, </span><span class="hl-6">json</span><span class="hl-1"> } = </span><span class="hl-4">llmJson</span><span class="hl-1">.</span><span class="hl-0">extract</span><span class="hl-1">(</span><span class="hl-4">llmOutput</span><span class="hl-1">);</span><br/><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">text</span><span class="hl-1">); </span><span class="hl-7">// ['Here\'s some text followed by JSON:']</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">json</span><span class="hl-1">); </span><span class="hl-7">// [{ name: 'John', age: 30, skills: ['JavaScript', 'TypeScript', 'React'] }]</span>
|
16
16
|
</code><button>Copy</button></pre>
|
17
17
|
<a id="md:schema-validation" class="tsd-anchor"></a><h3><a href="#md:schema-validation">Schema Validation</a></h3><p>You can validate extracted JSON against schemas:</p>
|
18
|
-
<pre><code class="language-typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">LlmJson</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">'@solvers-hub/llm-json'</span><span class="hl-1">;</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">schemas</span><span class="hl-1"> = [</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> </span><span class="hl-2">'person'</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">schema:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-2">'object'</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">properties:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> { </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-2">'string'</span><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">age:</span><span class="hl-1"> { </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-2">'integer'</span><span class="hl-1"> }</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">required:</span><span class="hl-1"> [</span><span class="hl-2">'name'</span><span class="hl-1">, </span><span class="hl-2">'age'</span><span class="hl-1">]</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">];</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">llmJson</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">LlmJson</span><span class="hl-1">({ </span><br/><span class="hl-1"> </span><span class="hl-4">attemptCorrection:</span><span class="hl-1"> </span><span class="hl-5">true</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">schemas</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">llmOutput</span><span class="hl-1"> = </span><span class="hl-2">`Here's a person: {"name": "John", "age": 30}</span><br/><span class="hl-2">And some other data: {"title": "Meeting notes"}`</span><span class="hl-1">;</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">result</span><span class="hl-1"> = </span><span class="hl-4">llmJson</span><span class="hl-1">.</span><span class="hl-0">extract</span><span class="hl-1">(</span><span class="hl-4">llmOutput</span><span class="hl-1">);</span><br/><br/><span class="hl-7">// Note: All extracted JSON objects are included in the json array</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">result</span><span class="hl-1">.</span><span class="hl-4">json</span><span class="hl-1">);</span><br/><span class="hl-7">// [</span><br/><span class="hl-7">// { name: 'John', age: 30 },</span><br/><span class="hl-7">// { title: 'Meeting notes' }</span><br/><span class="hl-7">// ]</span><br/><br/><span class="hl-7">// The validatedJson array includes validation results for each JSON object</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">result</span><span class="hl-1">.</span><span class="hl-4">validatedJson</span><span class="hl-1">);</span><br/><span class="hl-7">// [</span><br/><span class="hl-7">// {</span><br/><span class="hl-7">// json: { name: 'John', age: 30 },</span><br/><span class="hl-7">// matchedSchema: 'person',</span><br/><span class="hl-7">// isValid: true</span><br/><span class="hl-7">// },</span><br/><span class="hl-7">// {</span><br/><span class="hl-7">// json: { title: 'Meeting notes' },</span><br/><span class="hl-7">// matchedSchema: null,</span><br/><span class="hl-7">// isValid: false,</span><br/><span class="hl-7">// validationErrors: [...] // Validation errors</span><br/><span class="hl-7">// }</span><br/><span class="hl-7">// ]</span><br
|
18
|
+
<pre><code class="language-typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">LlmJson</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">'@solvers-hub/llm-json'</span><span class="hl-1">;</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">schemas</span><span class="hl-1"> = [</span><br/><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> </span><span class="hl-2">'person'</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">schema:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-2">'object'</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">properties:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> { </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-2">'string'</span><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">age:</span><span class="hl-1"> { </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-2">'integer'</span><span class="hl-1"> }</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> </span><span class="hl-4">required:</span><span class="hl-1"> [</span><span class="hl-2">'name'</span><span class="hl-1">, </span><span class="hl-2">'age'</span><span class="hl-1">]</span><br/><span class="hl-1"> }</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">];</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">llmJson</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">LlmJson</span><span class="hl-1">({ </span><br/><span class="hl-1"> </span><span class="hl-4">attemptCorrection:</span><span class="hl-1"> </span><span class="hl-5">true</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">schemas</span><br/><span class="hl-1">});</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">llmOutput</span><span class="hl-1"> = </span><span class="hl-2">`Here's a person: {"name": "John", "age": 30}</span><br/><span class="hl-2">And some other data: {"title": "Meeting notes"}`</span><span class="hl-1">;</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">result</span><span class="hl-1"> = </span><span class="hl-4">llmJson</span><span class="hl-1">.</span><span class="hl-0">extract</span><span class="hl-1">(</span><span class="hl-4">llmOutput</span><span class="hl-1">);</span><br/><br/><span class="hl-7">// Note: All extracted JSON objects are included in the json array</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">result</span><span class="hl-1">.</span><span class="hl-4">json</span><span class="hl-1">);</span><br/><span class="hl-7">// [</span><br/><span class="hl-7">// { name: 'John', age: 30 },</span><br/><span class="hl-7">// { title: 'Meeting notes' }</span><br/><span class="hl-7">// ]</span><br/><br/><span class="hl-7">// The validatedJson array includes validation results for each JSON object</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">result</span><span class="hl-1">.</span><span class="hl-4">validatedJson</span><span class="hl-1">);</span><br/><span class="hl-7">// [</span><br/><span class="hl-7">// {</span><br/><span class="hl-7">// json: { name: 'John', age: 30 },</span><br/><span class="hl-7">// matchedSchema: 'person',</span><br/><span class="hl-7">// isValid: true</span><br/><span class="hl-7">// },</span><br/><span class="hl-7">// {</span><br/><span class="hl-7">// json: { title: 'Meeting notes' },</span><br/><span class="hl-7">// matchedSchema: null,</span><br/><span class="hl-7">// isValid: false,</span><br/><span class="hl-7">// validationErrors: [...] // Validation errors</span><br/><span class="hl-7">// }</span><br/><span class="hl-7">// ]</span><br/>
|
19
19
|
</code><button>Copy</button></pre>
|
20
|
-
<a id="md:
|
21
|
-
|
20
|
+
<a id="md:examples" class="tsd-anchor"></a><h2><a href="#md:examples">Examples</a></h2><p>See the <a href="examples">examples</a> directory for more examples of how to use the library.</p>
|
21
|
+
<p>How to run the examples:</p>
|
22
|
+
<pre><code class="language-bash"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">run</span><span class="hl-1"> </span><span class="hl-2">example:run</span><span class="hl-1"> </span><span class="hl-5">--</span><span class="hl-1"> </span><span class="hl-2">examples/example.ts</span>
|
23
|
+
</code><button>Copy</button></pre>
|
24
|
+
<a id="md:license" class="tsd-anchor"></a><h2><a href="#md:license">License</a></h2><p>MIT © 2025</p>
|
25
|
+
</div></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-index-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><h4 class="uppercase">Member Visibility</h4><form><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-private" name="private"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Private</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></form></div><div class="tsd-theme-toggle"><h4 class="uppercase">Theme</h4><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-index-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="assets/icons.svg#icon-chevronDown"></use></svg>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#md:llm-json-extractor"><span>LLM-<wbr/>JSON <wbr/>Extractor</span></a><ul><li><a href="#md:overview"><span>Overview</span></a></li><li><a href="#md:key-features"><span>Key <wbr/>Features</span></a></li><li><a href="#md:quick-start"><span>Quick <wbr/>Start</span></a></li><li><ul><li><a href="#md:installation"><span>Installation</span></a></li><li><a href="#md:basic-usage"><span>Basic <wbr/>Usage</span></a></li><li><a href="#md:schema-validation"><span>Schema <wbr/>Validation</span></a></li></ul></li><li><a href="#md:examples"><span>Examples</span></a></li><li><a href="#md:license"><span>License</span></a></li></ul></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html" class="current"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="assets/icons.svg#icon-1"></use></svg><span>@solvers-hub/llm-json</span></a><ul class="tsd-small-nested-navigation" id="tsd-nav-container" data-base="."><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|