@solvers-hub/llm-json 0.1.5 → 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.
@@ -0,0 +1,69 @@
1
+ import LlmJson from '../src/index';
2
+
3
+ /**
4
+ * Example demonstrating how to use the LLM-JSON library for arrays.
5
+ */
6
+ function runArrayExample() {
7
+ // Create an instance with auto-correction enabled
8
+ const llmJson = new LlmJson({ attemptCorrection: true });
9
+
10
+ // Simple array example
11
+ const simpleArrayInput = `Here is a list of names: ["John", "Jane", "Bob"]`;
12
+
13
+ console.log("=== Simple Array Example ===");
14
+
15
+ // Using extract - might not properly handle standalone arrays
16
+ const simpleExtractResult = llmJson.extract(simpleArrayInput);
17
+ console.log("With extract():");
18
+ console.log("Text:", simpleExtractResult.text);
19
+ console.log("JSON:", JSON.stringify(simpleExtractResult.json, null, 2));
20
+ console.log();
21
+
22
+ // Using extractAll - properly handles arrays
23
+ const simpleExtractAllResult = llmJson.extractAll(simpleArrayInput);
24
+ console.log("With extractAll():");
25
+ console.log("Text:", simpleExtractAllResult.text);
26
+ console.log("JSON:", JSON.stringify(simpleExtractAllResult.json, null, 2));
27
+ console.log();
28
+
29
+ // Complex array example
30
+ const complexArrayInput = `Here's an array of users:
31
+ [
32
+ {
33
+ "name": "John Doe",
34
+ "age": 30,
35
+ "skills": ["JavaScript", "TypeScript"]
36
+ },
37
+ {
38
+ "name": "Jane Smith",
39
+ "age": 28,
40
+ "skills": ["Python", "Machine Learning"]
41
+ }
42
+ ]`;
43
+
44
+ console.log("=== Complex Array Example ===");
45
+
46
+ // Using extractAll - properly handles complex arrays
47
+ const complexResult = llmJson.extractAll(complexArrayInput);
48
+ console.log("With extractAll():");
49
+ console.log("Text:", complexResult.text);
50
+ console.log("JSON:", JSON.stringify(complexResult.json, null, 2));
51
+ console.log();
52
+
53
+ // Multiple JSON structures example
54
+ const mixedInput = `Here's an object: {"name": "John", "age": 30}
55
+
56
+ And here's an array: [1, 2, 3, 4, 5]
57
+
58
+ And another object: {"city": "New York", "country": "USA"}`;
59
+
60
+ console.log("=== Mixed JSON Types Example ===");
61
+
62
+ // Using extractAll - handles both objects and arrays
63
+ const mixedResult = llmJson.extractAll(mixedInput);
64
+ console.log("With extractAll():");
65
+ console.log("Text:", mixedResult.text);
66
+ console.log("JSON:", JSON.stringify(mixedResult.json, null, 2));
67
+ }
68
+
69
+ runArrayExample();
@@ -0,0 +1,101 @@
1
+ import LlmJson from '../src/index';
2
+
3
+ /**
4
+ * Example demonstrating how to use the LLM-JSON library.
5
+ */
6
+ function runExample() {
7
+ // Create an instance with auto-correction enabled
8
+ const llmJson = new LlmJson({ attemptCorrection: true });
9
+
10
+ // Example from the requirements
11
+ const input = `<research_planning>
12
+ a. Summary: The given organization is unnamed and works in the area of "something new," which suggests an innovative or emerging field. This could involve novel technologies, fresh market approaches, or unexplored domains. Without specific details, assumptions about the sector or industry may involve startups, tech innovation, or trendsetting industries. The focus and goals may lean toward exploration, user adoption, and refinement of novel concepts.
13
+
14
+ b. Potential Product Features:
15
+ - Exploration of new technologies (VR/AR interfaces, IoT integration)
16
+ - User onboarding and education tools
17
+ - Novel interaction models or user interfaces
18
+ - Feedback and improvement loops
19
+ - Community engagement and collaboration spaces
20
+
21
+ c. User Persona: Considering the organization's innovative nature, the primary user could be an early adopter, tech-savvy individual who is curious and willing to explore new technologies. This persona is likely someone who enjoys experimenting with novel ideas and is motivated by the excitement of participating in pioneering efforts.
22
+ Study Name: "Demo - Innovator Insight"
23
+
24
+ d. Potential Research Objectives:
25
+ - Evaluate user onboarding process effectiveness in helping users understand the product's novel features.
26
+ - Assess user engagement with community collaboration spaces to identify areas for increased interaction.
27
+ - Verify the intuitiveness of new interaction models and user interfaces.
28
+ - Explore user satisfaction with feedback and improvement loops.
29
+ - Measure the impact of educational tools on user empowerment and confidence.
30
+ - Analyze user behavior patterns to refine product workflows.
31
+ - Investigate potential barriers to user adoption and retention.
32
+
33
+ e. Narrowing Down Objectives:
34
+ After considering the potential research objectives, the focus shifted towards objectives that can be directly evaluated through a live web application. The final objectives chosen were geared towards user onboarding, interaction intuitiveness, and community engagement, as they align with the persona of an early adopter and focus on improving user experience in areas relevant to the organization's innovative nature.
35
+ </research_planning>
36
+
37
+ \`\`\`json
38
+ {
39
+ "studyName": "Demo - Innovator Insight",
40
+ "userPersona": "Tech-savvy early adopter exploring new innovations.",
41
+ "objectives": [
42
+ {
43
+ "objectiveTitle": "Onboarding Process Evaluation",
44
+ "objectiveDescription": "Assess the effectiveness of the user onboarding process in enabling users to grasp the novel features of the product quickly and efficiently, ensuring that it enhances initial user engagement and reduces learning curves."
45
+ },
46
+ {
47
+ "objectiveTitle": "Community Interaction Analysis",
48
+ "objectiveDescription": "Investigate user engagement within community collaboration spaces, identifying potential improvements to foster more interaction, sharing, and collaboration among users, enhancing overall community dynamics."
49
+ },
50
+ {
51
+ "objectiveTitle": "Interface Intuition Verification",
52
+ "objectiveDescription": "Verify the intuitiveness of new interaction models and user interfaces, focusing on how users adapt and navigate through the product, aiming to identify any areas needing refinement for better usability."
53
+ }
54
+ ]
55
+ }
56
+ \`\`\``;
57
+
58
+ // Extract JSON and text
59
+ const result = llmJson.extract(input);
60
+
61
+ console.log("Extracted text:");
62
+ console.log("------------------");
63
+ result.text.forEach((text, index) => {
64
+ console.log(`Text block ${index + 1}:`);
65
+ console.log(text);
66
+ console.log();
67
+ });
68
+
69
+ console.log("Extracted JSON:");
70
+ console.log("------------------");
71
+ result.json.forEach((json, index) => {
72
+ console.log(`JSON object ${index + 1}:`);
73
+ console.log(JSON.stringify(json, null, 2));
74
+ console.log();
75
+ });
76
+
77
+ // Example with malformed JSON
78
+ console.log("\nExample with malformed JSON:");
79
+ console.log("---------------------------");
80
+
81
+ const malformedInput = `Here is some information:
82
+
83
+ {
84
+ name: "John",
85
+ age: 30,
86
+ skills: ["JavaScript", "TypeScript"],
87
+ preferences: {
88
+ theme: "dark",
89
+ notifications: true,
90
+ }
91
+ }`;
92
+
93
+ const malformedResult = llmJson.extract(malformedInput);
94
+
95
+ console.log("Text:");
96
+ console.log(malformedResult.text[0]);
97
+ console.log("\nCorrected JSON:");
98
+ console.log(JSON.stringify(malformedResult.json[0], null, 2));
99
+ }
100
+
101
+ runExample();
@@ -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,49 +1,53 @@
1
- {
2
- "name": "@solvers-hub/llm-json",
3
- "version": "0.1.5",
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 examples/example.ts",
16
- "build:example": "tsc && node dist/examples/example.js",
17
- "docs": "typedoc --out docs src",
18
- "docs:md": "typedoc --out docs-md --plugin typedoc-plugin-markdown src"
19
- },
20
- "repository": {
21
- "type": "git",
22
- "url": "https://github.com/solvers-hub/llm-json.git"
23
- },
24
- "bugs": {
25
- "url": "https://github.com/solvers-hub/llm-json/issues"
26
- },
27
- "homepage": "https://github.com/solvers-hub/llm-json#readme",
28
- "keywords": [
29
- "llm",
30
- "json",
31
- "extractor",
32
- "parser"
33
- ],
34
- "devDependencies": {
35
- "@types/jest": "^29.5.0",
36
- "@types/node": "^18.15.11",
37
- "jest": "^29.5.0",
38
- "ts-jest": "^29.1.0",
39
- "ts-node": "^10.9.1",
40
- "typedoc": "^0.25.12",
41
- "typedoc-plugin-markdown": "^3.17.1",
42
- "typescript": "^5.0.4"
43
- },
44
- "files": [
45
- "dist/**/*",
46
- "docs/**/*",
47
- "docs-md/**/*"
48
- ]
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
+ ]
49
53
  }