@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
@@ -0,0 +1,140 @@
|
|
1
|
+
import LlmJson from '../src/index';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Example demonstrating how to use schema validation with the LLM-JSON library.
|
5
|
+
*/
|
6
|
+
function runSchemaValidationExample() {
|
7
|
+
console.log('Schema Validation Example');
|
8
|
+
console.log('=========================\n');
|
9
|
+
|
10
|
+
// Define some schemas for validation
|
11
|
+
const schemas = [
|
12
|
+
{
|
13
|
+
name: 'person',
|
14
|
+
schema: {
|
15
|
+
type: 'object',
|
16
|
+
properties: {
|
17
|
+
name: { type: 'string' },
|
18
|
+
age: { type: 'integer' },
|
19
|
+
email: { type: 'string' }
|
20
|
+
},
|
21
|
+
required: ['name', 'age']
|
22
|
+
}
|
23
|
+
},
|
24
|
+
{
|
25
|
+
name: 'product',
|
26
|
+
schema: {
|
27
|
+
type: 'object',
|
28
|
+
properties: {
|
29
|
+
id: { type: 'string' },
|
30
|
+
name: { type: 'string' },
|
31
|
+
price: { type: 'number' }
|
32
|
+
},
|
33
|
+
required: ['id', 'name', 'price']
|
34
|
+
}
|
35
|
+
}
|
36
|
+
];
|
37
|
+
|
38
|
+
// Create an instance with auto-correction and schemas
|
39
|
+
const llmJson = new LlmJson({
|
40
|
+
attemptCorrection: true,
|
41
|
+
schemas
|
42
|
+
});
|
43
|
+
|
44
|
+
// Example 1: Valid JSON matching a schema
|
45
|
+
console.log('Example 1: Valid JSON matching a schema');
|
46
|
+
console.log('----------------------------------------');
|
47
|
+
|
48
|
+
const input1 = `Here's some customer information:
|
49
|
+
|
50
|
+
{
|
51
|
+
"name": "John Doe",
|
52
|
+
"age": 32,
|
53
|
+
"email": "john.doe@example.com"
|
54
|
+
}
|
55
|
+
|
56
|
+
Please process this customer data.`;
|
57
|
+
|
58
|
+
const result1 = llmJson.extract(input1);
|
59
|
+
|
60
|
+
console.log('Extracted JSON:');
|
61
|
+
console.log(JSON.stringify(result1.json, null, 2));
|
62
|
+
console.log('\nValidation Results:');
|
63
|
+
console.log(JSON.stringify(result1.validatedJson, null, 2));
|
64
|
+
console.log('\n');
|
65
|
+
|
66
|
+
// Example 2: Valid JSON but doesn't match any schema
|
67
|
+
console.log('Example 2: Valid JSON but doesn\'t match any schema');
|
68
|
+
console.log('--------------------------------------------------');
|
69
|
+
|
70
|
+
const input2 = `Here's some location data:
|
71
|
+
|
72
|
+
{
|
73
|
+
"latitude": 37.7749,
|
74
|
+
"longitude": -122.4194,
|
75
|
+
"city": "San Francisco"
|
76
|
+
}`;
|
77
|
+
|
78
|
+
const result2 = llmJson.extract(input2);
|
79
|
+
|
80
|
+
console.log('Extracted JSON:');
|
81
|
+
console.log(JSON.stringify(result2.json, null, 2));
|
82
|
+
console.log('\nValidation Results:');
|
83
|
+
console.log(JSON.stringify(result2.validatedJson, null, 2));
|
84
|
+
console.log('\n');
|
85
|
+
|
86
|
+
// Example 3: Multiple JSON objects with some matching schemas
|
87
|
+
console.log('Example 3: Multiple JSON objects with some matching schemas');
|
88
|
+
console.log('--------------------------------------------------------');
|
89
|
+
|
90
|
+
const input3 = `Here are two different objects:
|
91
|
+
|
92
|
+
First, a person:
|
93
|
+
{
|
94
|
+
"name": "Jane Smith",
|
95
|
+
"age": 28
|
96
|
+
}
|
97
|
+
|
98
|
+
And a product:
|
99
|
+
{
|
100
|
+
"id": "prod-123",
|
101
|
+
"name": "Laptop",
|
102
|
+
"price": 999.99
|
103
|
+
}
|
104
|
+
|
105
|
+
And something that doesn't match our schemas:
|
106
|
+
{
|
107
|
+
"title": "Meeting Notes",
|
108
|
+
"date": "2023-05-15"
|
109
|
+
}`;
|
110
|
+
|
111
|
+
const result3 = llmJson.extract(input3);
|
112
|
+
|
113
|
+
console.log('Extracted JSON:');
|
114
|
+
console.log(JSON.stringify(result3.json, null, 2));
|
115
|
+
console.log('\nValidation Results:');
|
116
|
+
console.log(JSON.stringify(result3.validatedJson, null, 2));
|
117
|
+
console.log('\n');
|
118
|
+
|
119
|
+
// Example 4: Malformed JSON that gets corrected and validated
|
120
|
+
console.log('Example 4: Malformed JSON that gets corrected and validated');
|
121
|
+
console.log('----------------------------------------------------------');
|
122
|
+
|
123
|
+
const input4 = `This JSON has errors but should be fixed:
|
124
|
+
|
125
|
+
{
|
126
|
+
name: "Alex Johnson",
|
127
|
+
age: 42,
|
128
|
+
email: "alex@example.com"
|
129
|
+
}`;
|
130
|
+
|
131
|
+
const result4 = llmJson.extract(input4);
|
132
|
+
|
133
|
+
console.log('Extracted JSON:');
|
134
|
+
console.log(JSON.stringify(result4.json, null, 2));
|
135
|
+
console.log('\nValidation Results:');
|
136
|
+
console.log(JSON.stringify(result4.validatedJson, null, 2));
|
137
|
+
}
|
138
|
+
|
139
|
+
// Run the example
|
140
|
+
runSchemaValidationExample();
|
@@ -0,0 +1,248 @@
|
|
1
|
+
import LlmJson from '../src/index';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Comprehensive examples of schema validation with LLM-JSON
|
5
|
+
*/
|
6
|
+
function runSchemaValidationExamples() {
|
7
|
+
// Example 1: Basic schema with required and optional fields
|
8
|
+
|
9
|
+
const input1 = `Here's a user profile:
|
10
|
+
{
|
11
|
+
"name": "John Doe",
|
12
|
+
"age": 30,
|
13
|
+
"email": "john@example.com",
|
14
|
+
"location": "New York"
|
15
|
+
}`;
|
16
|
+
|
17
|
+
const userSchema = {
|
18
|
+
name: 'user',
|
19
|
+
schema: {
|
20
|
+
type: 'object',
|
21
|
+
required: ['name', 'email'], // These fields must be present
|
22
|
+
properties: {
|
23
|
+
name: { type: 'string' },
|
24
|
+
email: { type: 'string', format: 'email' },
|
25
|
+
age: { type: 'number' }, // Optional field
|
26
|
+
location: { type: 'string' } // Optional field
|
27
|
+
}
|
28
|
+
}
|
29
|
+
};
|
30
|
+
|
31
|
+
// Example 2: Complex schema with nested objects and arrays
|
32
|
+
|
33
|
+
const input2 = `Here's a product catalog:
|
34
|
+
{
|
35
|
+
"product": {
|
36
|
+
"id": "123",
|
37
|
+
"name": "Smartphone",
|
38
|
+
"price": 599.99,
|
39
|
+
"specifications": {
|
40
|
+
"brand": "TechCo",
|
41
|
+
"model": "X100",
|
42
|
+
"features": ["5G", "Wireless Charging", "Dual Camera"]
|
43
|
+
},
|
44
|
+
"inventory": {
|
45
|
+
"inStock": true,
|
46
|
+
"quantity": 50,
|
47
|
+
"locations": [
|
48
|
+
{
|
49
|
+
"store": "Main Store",
|
50
|
+
"quantity": 30
|
51
|
+
},
|
52
|
+
{
|
53
|
+
"store": "Branch Store",
|
54
|
+
"quantity": 20
|
55
|
+
}
|
56
|
+
]
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}`;
|
60
|
+
|
61
|
+
const productSchema = {
|
62
|
+
name: 'product',
|
63
|
+
schema: {
|
64
|
+
type: 'object',
|
65
|
+
required: ['product'],
|
66
|
+
properties: {
|
67
|
+
product: {
|
68
|
+
type: 'object',
|
69
|
+
required: ['id', 'name', 'price', 'specifications', 'inventory'],
|
70
|
+
properties: {
|
71
|
+
id: { type: 'string' },
|
72
|
+
name: { type: 'string' },
|
73
|
+
price: { type: 'number', minimum: 0 },
|
74
|
+
specifications: {
|
75
|
+
type: 'object',
|
76
|
+
required: ['brand', 'model', 'features'],
|
77
|
+
properties: {
|
78
|
+
brand: { type: 'string' },
|
79
|
+
model: { type: 'string' },
|
80
|
+
features: {
|
81
|
+
type: 'array',
|
82
|
+
items: { type: 'string' }
|
83
|
+
}
|
84
|
+
}
|
85
|
+
},
|
86
|
+
inventory: {
|
87
|
+
type: 'object',
|
88
|
+
required: ['inStock', 'quantity', 'locations'],
|
89
|
+
properties: {
|
90
|
+
inStock: { type: 'boolean' },
|
91
|
+
quantity: { type: 'integer', minimum: 0 },
|
92
|
+
locations: {
|
93
|
+
type: 'array',
|
94
|
+
items: {
|
95
|
+
type: 'object',
|
96
|
+
required: ['store', 'quantity'],
|
97
|
+
properties: {
|
98
|
+
store: { type: 'string' },
|
99
|
+
quantity: { type: 'integer', minimum: 0 }
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
};
|
110
|
+
|
111
|
+
// Example 3: Multiple schemas for different types of data
|
112
|
+
|
113
|
+
const input3 = `Here are different types of records:
|
114
|
+
{
|
115
|
+
"employee": {
|
116
|
+
"id": "E123",
|
117
|
+
"name": "Jane Smith",
|
118
|
+
"department": "Engineering",
|
119
|
+
"skills": ["JavaScript", "TypeScript", "React"],
|
120
|
+
"contact": {
|
121
|
+
"email": "jane@company.com",
|
122
|
+
"phone": "+1-234-567-8900"
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
{
|
128
|
+
"project": {
|
129
|
+
"id": "P789",
|
130
|
+
"name": "Website Redesign",
|
131
|
+
"status": "in-progress",
|
132
|
+
"deadline": "2024-12-31",
|
133
|
+
"teamSize": 5,
|
134
|
+
"budget": 50000
|
135
|
+
}
|
136
|
+
}`;
|
137
|
+
|
138
|
+
const employeeSchema = {
|
139
|
+
name: 'employee',
|
140
|
+
schema: {
|
141
|
+
type: 'object',
|
142
|
+
required: ['employee'],
|
143
|
+
properties: {
|
144
|
+
employee: {
|
145
|
+
type: 'object',
|
146
|
+
required: ['id', 'name', 'department', 'skills', 'contact'],
|
147
|
+
properties: {
|
148
|
+
id: { type: 'string', pattern: '^E\\d+$' }, // Must start with E followed by numbers
|
149
|
+
name: { type: 'string' },
|
150
|
+
department: { type: 'string' },
|
151
|
+
skills: {
|
152
|
+
type: 'array',
|
153
|
+
items: { type: 'string' },
|
154
|
+
minItems: 1 // Must have at least one skill
|
155
|
+
},
|
156
|
+
contact: {
|
157
|
+
type: 'object',
|
158
|
+
required: ['email'],
|
159
|
+
properties: {
|
160
|
+
email: { type: 'string', format: 'email' },
|
161
|
+
phone: { type: 'string' } // Optional
|
162
|
+
}
|
163
|
+
}
|
164
|
+
}
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
};
|
169
|
+
|
170
|
+
const projectSchema = {
|
171
|
+
name: 'project',
|
172
|
+
schema: {
|
173
|
+
type: 'object',
|
174
|
+
required: ['project'],
|
175
|
+
properties: {
|
176
|
+
project: {
|
177
|
+
type: 'object',
|
178
|
+
required: ['id', 'name', 'status', 'deadline', 'teamSize'],
|
179
|
+
properties: {
|
180
|
+
id: { type: 'string', pattern: '^P\\d+$' }, // Must start with P followed by numbers
|
181
|
+
name: { type: 'string' },
|
182
|
+
status: {
|
183
|
+
type: 'string',
|
184
|
+
enum: ['not-started', 'in-progress', 'completed'] // Only these values allowed
|
185
|
+
},
|
186
|
+
deadline: {
|
187
|
+
type: 'string',
|
188
|
+
format: 'date' // Must be a valid date string
|
189
|
+
},
|
190
|
+
teamSize: {
|
191
|
+
type: 'integer',
|
192
|
+
minimum: 1,
|
193
|
+
maximum: 20 // Team size constraints
|
194
|
+
},
|
195
|
+
budget: {
|
196
|
+
type: 'number',
|
197
|
+
minimum: 0 // Optional but must be positive
|
198
|
+
}
|
199
|
+
}
|
200
|
+
}
|
201
|
+
}
|
202
|
+
}
|
203
|
+
};
|
204
|
+
|
205
|
+
// Create LlmJson instances with different schema combinations
|
206
|
+
const llmJson1 = new LlmJson({ attemptCorrection: true, schemas: [userSchema] });
|
207
|
+
const llmJson2 = new LlmJson({ attemptCorrection: true, schemas: [productSchema] });
|
208
|
+
const llmJson3 = new LlmJson({ attemptCorrection: true, schemas: [employeeSchema, projectSchema] });
|
209
|
+
|
210
|
+
// Run the examples
|
211
|
+
const result1 = llmJson1.extract(input1);
|
212
|
+
console.log('User Schema Validation Result:');
|
213
|
+
result1.json.forEach((json, i) => {
|
214
|
+
console.log(`JSON block ${i + 1}:`);
|
215
|
+
console.log(JSON.stringify(json, null, 2));
|
216
|
+
if (result1.validatedJson) {
|
217
|
+
console.log('Validation:');
|
218
|
+
console.log(JSON.stringify(result1.validatedJson[i], null, 2));
|
219
|
+
}
|
220
|
+
console.log();
|
221
|
+
});
|
222
|
+
|
223
|
+
const result2 = llmJson2.extract(input2);
|
224
|
+
console.log('\nProduct Schema Validation Result:');
|
225
|
+
result2.json.forEach((json, i) => {
|
226
|
+
console.log(`JSON block ${i + 1}:`);
|
227
|
+
console.log(JSON.stringify(json, null, 2));
|
228
|
+
if (result2.validatedJson) {
|
229
|
+
console.log('Validation:');
|
230
|
+
console.log(JSON.stringify(result2.validatedJson[i], null, 2));
|
231
|
+
}
|
232
|
+
console.log();
|
233
|
+
});
|
234
|
+
|
235
|
+
const result3 = llmJson3.extract(input3);
|
236
|
+
console.log('\nMultiple Schemas Validation Result:');
|
237
|
+
result3.json.forEach((json, i) => {
|
238
|
+
console.log(`JSON block ${i + 1}:`);
|
239
|
+
console.log(JSON.stringify(json, null, 2));
|
240
|
+
if (result3.validatedJson) {
|
241
|
+
console.log('Validation:');
|
242
|
+
console.log(JSON.stringify(result3.validatedJson[i], null, 2));
|
243
|
+
}
|
244
|
+
console.log();
|
245
|
+
});
|
246
|
+
}
|
247
|
+
|
248
|
+
runSchemaValidationExamples();
|
package/package.json
CHANGED
@@ -1,53 +1,53 @@
|
|
1
|
-
{
|
2
|
-
"name": "@solvers-hub/llm-json",
|
3
|
-
"version": "0.1.
|
4
|
-
"description": "A TypeScript SDK to extract and correct JSON from LLM outputs",
|
5
|
-
"main": "dist/index.js",
|
6
|
-
"types": "dist/index.d.ts",
|
7
|
-
"author": "Solvers Hub",
|
8
|
-
"license": "MIT",
|
9
|
-
"scripts": {
|
10
|
-
"build": "tsc",
|
11
|
-
"test": "jest",
|
12
|
-
"test:coverage": "jest --coverage",
|
13
|
-
"lint": "eslint src/**/*.ts",
|
14
|
-
"prepublishOnly": "npm run build && npm run docs",
|
15
|
-
"example": "ts-node
|
16
|
-
"
|
17
|
-
"
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
"
|
31
|
-
"
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
"
|
37
|
-
"
|
38
|
-
"
|
39
|
-
"
|
40
|
-
"
|
41
|
-
"
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
"
|
50
|
-
"docs/**/*",
|
51
|
-
"
|
52
|
-
]
|
1
|
+
{
|
2
|
+
"name": "@solvers-hub/llm-json",
|
3
|
+
"version": "0.1.7",
|
4
|
+
"description": "A TypeScript SDK to extract and correct JSON from LLM outputs",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
|
+
"author": "Solvers Hub",
|
8
|
+
"license": "MIT",
|
9
|
+
"scripts": {
|
10
|
+
"build": "tsc",
|
11
|
+
"test": "jest",
|
12
|
+
"test:coverage": "jest --coverage",
|
13
|
+
"lint": "eslint src/**/*.ts",
|
14
|
+
"prepublishOnly": "npm run build && npm run docs",
|
15
|
+
"example:run": "ts-node",
|
16
|
+
"docs": "typedoc --out docs src",
|
17
|
+
"docs:md": "typedoc --out docs-md --plugin typedoc-plugin-markdown src"
|
18
|
+
},
|
19
|
+
"repository": {
|
20
|
+
"type": "git",
|
21
|
+
"url": "https://github.com/solvers-hub/llm-json.git"
|
22
|
+
},
|
23
|
+
"bugs": {
|
24
|
+
"url": "https://github.com/solvers-hub/llm-json/issues"
|
25
|
+
},
|
26
|
+
"homepage": "https://github.com/solvers-hub/llm-json#readme",
|
27
|
+
"keywords": [
|
28
|
+
"llm",
|
29
|
+
"json",
|
30
|
+
"extractor",
|
31
|
+
"parser"
|
32
|
+
],
|
33
|
+
"devDependencies": {
|
34
|
+
"@types/jest": "^29.5.0",
|
35
|
+
"@types/node": "^18.15.11",
|
36
|
+
"jest": "^29.5.0",
|
37
|
+
"ts-jest": "^29.1.0",
|
38
|
+
"ts-node": "^10.9.1",
|
39
|
+
"typedoc": "^0.25.12",
|
40
|
+
"typedoc-plugin-markdown": "^3.17.1",
|
41
|
+
"typescript": "^5.0.4"
|
42
|
+
},
|
43
|
+
"dependencies": {
|
44
|
+
"ajv": "^8.17.1",
|
45
|
+
"ajv-formats": "^3.0.1"
|
46
|
+
},
|
47
|
+
"files": [
|
48
|
+
"dist/**/*",
|
49
|
+
"docs/**/*",
|
50
|
+
"docs-md/**/*",
|
51
|
+
"examples/**/*"
|
52
|
+
]
|
53
53
|
}
|