@llumiverse/core 0.22.0-dev.1 → 0.22.1

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/package.json CHANGED
@@ -1,90 +1,91 @@
1
1
  {
2
- "name": "@llumiverse/core",
3
- "version": "0.22.0-dev.1",
4
- "type": "module",
5
- "description": "Provide an universal API to LLMs. Support for existing LLMs can be added by writing a driver.",
6
- "files": [
7
- "lib",
8
- "src"
9
- ],
10
- "keywords": [
11
- "llm",
12
- "ai",
13
- "prompt",
14
- "prompt engineering",
15
- "ml",
16
- "machine learning",
17
- "embeddings",
18
- "training",
19
- "model",
20
- "universal",
21
- "api",
22
- "chatgpt",
23
- "openai",
24
- "vertexai",
25
- "bedrock",
26
- "replicate",
27
- "huggingface",
28
- "togetherai"
29
- ],
30
- "types": "./lib/types/index.d.ts",
31
- "typesVersions": {
32
- "*": {
33
- "async": [
34
- "./lib/types/async.d.ts"
35
- ],
36
- "formatters": [
37
- "./lib/types/formatters/index.d.ts"
38
- ]
39
- }
40
- },
41
- "exports": {
42
- ".": {
43
- "types": "./lib/types/index.d.ts",
44
- "import": "./lib/esm/index.js",
45
- "require": "./lib/cjs/index.js"
46
- },
47
- "./async": {
48
- "types": "./lib/types/async.d.ts",
49
- "import": "./lib/esm/async.js",
50
- "require": "./lib/cjs/async.js"
2
+ "name": "@llumiverse/core",
3
+ "version": "0.22.1",
4
+ "type": "module",
5
+ "description": "Provide an universal API to LLMs. Support for existing LLMs can be added by writing a driver.",
6
+ "files": [
7
+ "lib",
8
+ "src"
9
+ ],
10
+ "keywords": [
11
+ "llm",
12
+ "ai",
13
+ "prompt",
14
+ "prompt engineering",
15
+ "ml",
16
+ "machine learning",
17
+ "embeddings",
18
+ "training",
19
+ "model",
20
+ "universal",
21
+ "api",
22
+ "chatgpt",
23
+ "openai",
24
+ "vertexai",
25
+ "bedrock",
26
+ "replicate",
27
+ "huggingface",
28
+ "togetherai"
29
+ ],
30
+ "types": "./lib/types/index.d.ts",
31
+ "typesVersions": {
32
+ "*": {
33
+ "async": [
34
+ "./lib/types/async.d.ts"
35
+ ],
36
+ "formatters": [
37
+ "./lib/types/formatters/index.d.ts"
38
+ ]
39
+ }
51
40
  },
52
- "./formatters": {
53
- "types": "./lib/types/formatters/index.d.ts",
54
- "import": "./lib/esm/formatters/index.js",
55
- "require": "./lib/cjs/formatters/index.js"
56
- }
57
- },
58
- "author": "Llumiverse",
59
- "license": "Apache-2.0",
60
- "homepage": "https://github.com/vertesia/llumiverse",
61
- "repository": {
62
- "type": "git",
63
- "url": "git+ssh://git@github.com/vertesia/llumiverse.git"
64
- },
65
- "devDependencies": {
66
- "@vertesia/api-fetch-client": "^0.78.0",
67
- "rimraf": "^6.0.1",
68
- "ts-dual-module": "^0.6.3",
69
- "typescript": "^5.9.2",
70
- "vitest": "^3.2.4"
71
- },
72
- "dependencies": {
73
- "@types/node": "^22.18.6",
74
- "ajv": "^8.17.1",
75
- "ajv-formats": "^3.0.1",
76
- "@llumiverse/common": "0.22.0-dev.1"
77
- },
78
- "ts_dual_module": {
79
- "outDir": "lib",
80
41
  "exports": {
81
- "async": "async.js",
82
- "formatters": "formatters/index.js"
42
+ ".": {
43
+ "types": "./lib/types/index.d.ts",
44
+ "import": "./lib/esm/index.js",
45
+ "require": "./lib/cjs/index.js"
46
+ },
47
+ "./async": {
48
+ "types": "./lib/types/async.d.ts",
49
+ "import": "./lib/esm/async.js",
50
+ "require": "./lib/cjs/async.js"
51
+ },
52
+ "./formatters": {
53
+ "types": "./lib/types/formatters/index.d.ts",
54
+ "import": "./lib/esm/formatters/index.js",
55
+ "require": "./lib/cjs/formatters/index.js"
56
+ }
57
+ },
58
+ "scripts": {
59
+ "test": "vitest run",
60
+ "build": "pnpm exec tsmod build",
61
+ "clean": "rimraf ./lib tsconfig.tsbuildinfo"
62
+ },
63
+ "author": "Llumiverse",
64
+ "license": "Apache-2.0",
65
+ "homepage": "https://github.com/vertesia/llumiverse",
66
+ "repository": {
67
+ "type": "git",
68
+ "url": "git+ssh://git@github.com/vertesia/llumiverse.git"
69
+ },
70
+ "devDependencies": {
71
+ "@vertesia/api-fetch-client": "^0.74.0",
72
+ "rimraf": "^6.0.1",
73
+ "ts-dual-module": "^0.6.3",
74
+ "typescript": "^5.9.2",
75
+ "vitest": "^3.2.4"
76
+ },
77
+ "dependencies": {
78
+ "@llumiverse/common": "workspace:*",
79
+ "@types/node": "^22.17.1",
80
+ "ajv": "^8.17.1",
81
+ "ajv-formats": "^3.0.1",
82
+ "jsonrepair": "^3.13.1"
83
+ },
84
+ "ts_dual_module": {
85
+ "outDir": "lib",
86
+ "exports": {
87
+ "async": "async.js",
88
+ "formatters": "formatters/index.js"
89
+ }
83
90
  }
84
- },
85
- "scripts": {
86
- "test": "vitest run",
87
- "build": "pnpm exec tsmod build",
88
- "clean": "rimraf ./lib tsconfig.tsbuildinfo"
89
- }
90
- }
91
+ }
package/src/json.ts CHANGED
@@ -1,181 +1,16 @@
1
1
  import { JSONValue } from "@llumiverse/common";
2
+ import { jsonrepair } from 'jsonrepair';
2
3
 
3
4
  function extractJsonFromText(text: string): string {
4
5
  const start = text.indexOf("{");
5
6
  const end = text.lastIndexOf("}");
6
- text = text.substring(start, end + 1);
7
- return text.replace(/\\n/g, "");
7
+ return text.substring(start, end + 1);
8
8
  }
9
9
 
10
10
  export function extractAndParseJSON(text: string): JSONValue {
11
11
  return parseJSON(extractJsonFromText(text));
12
12
  }
13
13
 
14
- const RX_DQUOTE = /^"([^"\\]|\\.)*"/us;
15
- const RX_SQUOTE = /^'([^'\\]|\\.)*'/us;
16
- const RX_NUMBER = /^-?\d+(\.\d+)?/;
17
- const RX_BOOLEAN = /^true|false/;
18
- const RX_NULL = /^null/;
19
- const RX_KEY = /^[$_a-zA-Z][$_a-zA-Z0-9]*/;
20
- const RX_PUNCTUATION = /^\s*([\[\]{}:,])\s*/;
21
-
22
- function fixText(value: string) {
23
- return value.replaceAll('\n', '\\n').replaceAll('\r', '\\r');
24
- }
25
-
26
- function decodeSingleQuotedString(value: string) {
27
- return JSON.parse('"' + value.slice(1, -1).replaceAll(/(?<!\\)"/g, '\\"') + '"');
28
- }
29
-
30
- export class JsonParser {
31
- pos: number = 0;
32
-
33
- constructor(public text: string) { }
34
-
35
- skip(n: number) {
36
- this.text = this.text.substring(n);
37
- this.pos += n;
38
- }
39
-
40
- tryReadPunctuation() {
41
- const m = RX_PUNCTUATION.exec(this.text);
42
- if (m) {
43
- this.skip(m[0].length);
44
- return m[1];
45
- }
46
- }
47
-
48
- readKey() {
49
- const first = this.text.charCodeAt(0);
50
- if (first === 34) { // "
51
- const m = RX_DQUOTE.exec(this.text);
52
- if (m) {
53
- this.skip(m[0].length);
54
- return JSON.parse(m[0]);
55
- }
56
- } else if (first === 39) { // '
57
- const m = RX_SQUOTE.exec(this.text);
58
- if (m) {
59
- this.skip(m[0].length);
60
- return decodeSingleQuotedString(m[0]);
61
- }
62
- } else {
63
- const m = RX_KEY.exec(this.text);
64
- if (m) {
65
- this.skip(m[0].length);
66
- return m[0];
67
- }
68
- }
69
- throw new Error('Expected a key at position ' + this.pos + ' but found ' + this.text);
70
- }
71
-
72
- readScalar() {
73
- const first = this.text.charCodeAt(0);
74
- if (first === 34) { // "
75
- const m = RX_DQUOTE.exec(this.text);
76
- if (m) {
77
- this.skip(m[0].length);
78
- return JSON.parse(fixText(m[0]));
79
- }
80
- } else if (first === 39) { // '
81
- const m = RX_SQUOTE.exec(this.text);
82
- if (m) {
83
- this.skip(m[0].length);
84
- return decodeSingleQuotedString(fixText(m[0]));
85
- }
86
- } else {
87
- let m = RX_NUMBER.exec(this.text);
88
- if (m) {
89
- this.skip(m[0].length);
90
- return parseFloat(m[0]);
91
- }
92
- m = RX_BOOLEAN.exec(this.text);
93
- if (m) {
94
- this.skip(m[0].length);
95
- return m[0] === 'true';
96
- }
97
- m = RX_NULL.exec(this.text);
98
- if (m) {
99
- this.skip(m[0].length);
100
- return null;
101
- }
102
- }
103
- throw new Error('Expected a value at position ' + this.pos + ' but found ' + this.text);
104
- }
105
-
106
- readObject() {
107
- let key: string | undefined;
108
- const obj: any = {};
109
- while (true) {
110
- if (!key) { // read key
111
- const p = this.tryReadPunctuation();
112
- if (p === '}') {
113
- return obj;
114
- } else if (p === ',') {
115
- continue;
116
- } else if (p) {
117
- throw new Error('Expected a key at position ' + this.pos + ' but found ' + this.text);
118
- }
119
- key = this.readKey();
120
- if (!key) {
121
- throw new Error('Expected a key at position ' + this.pos + ' but found ' + this.text);
122
- }
123
- if (this.tryReadPunctuation() !== ':') {
124
- throw new Error('Expected a colon at position ' + this.pos + ' but found ' + this.text);
125
- };
126
- } else { // read value
127
- const value = this.readValue();
128
- if (value === undefined) {
129
- throw new Error('Expected a value at position ' + this.pos + ' but found ' + this.text);
130
- }
131
- obj[key] = value;
132
- key = undefined;
133
- }
134
- }
135
- }
136
-
137
- readArray() {
138
- const ar: any[] = [];
139
- while (true) {
140
- const p = this.tryReadPunctuation();
141
- if (p === ',') {
142
- continue;
143
- } else if (p === ']') {
144
- return ar;
145
- } else if (p === '[') {
146
- ar.push(this.readArray());
147
- } else if (p === '{') {
148
- ar.push(this.readObject());
149
- } else if (!p) {
150
- ar.push(this.readScalar());
151
- } else {
152
- throw new Error('Expected a value at position ' + this.pos + ' but found ' + this.text);
153
- }
154
- }
155
- }
156
-
157
- readValue() {
158
- const p = this.tryReadPunctuation();
159
- if (p === '{') {
160
- return this.readObject();
161
- } else if (p === '[') {
162
- return this.readArray();
163
- } else if (!p) {
164
- return this.readScalar();
165
- }
166
- }
167
-
168
- static parse(text: string) {
169
- const parser = new JsonParser(text);
170
- const r = parser.readValue();
171
- if (r === undefined) {
172
- throw new Error('Not a valid JSON');
173
- }
174
- return r;
175
- }
176
- }
177
-
178
-
179
14
  export function parseJSON(text: string): JSONValue {
180
15
  text = text.trim();
181
16
  try {
@@ -183,7 +18,7 @@ export function parseJSON(text: string): JSONValue {
183
18
  } catch (err: any) {
184
19
  // use a relaxed parser
185
20
  try {
186
- return JsonParser.parse(text);
21
+ return JSON.parse(jsonrepair(text));
187
22
  } catch (err2: any) { // throw the original error
188
23
  throw err;
189
24
  }
package/src/validation.ts CHANGED
@@ -1,8 +1,8 @@
1
+ import { CompletionResult, ResultValidationError } from "@llumiverse/common";
1
2
  import { Ajv } from 'ajv';
2
3
  import addFormats from 'ajv-formats';
3
4
  import { extractAndParseJSON } from "./json.js";
4
5
  import { resolveField } from './resolver.js';
5
- import { CompletionResult, completionResultToString, ResultValidationError } from "@llumiverse/common";
6
6
 
7
7
 
8
8
  const ajv = new Ajv({
@@ -28,6 +28,25 @@ export class ValidationError extends Error implements ResultValidationError {
28
28
  }
29
29
  }
30
30
 
31
+ function parseCompletionAsJson(data: CompletionResult[]) {
32
+ let lastError: ValidationError | undefined;
33
+ for (const part of data) {
34
+ if (part.type === "text") {
35
+ const text = part.value.trim();
36
+ try {
37
+ return extractAndParseJSON(text);
38
+ } catch (error: any) {
39
+ lastError = new ValidationError("json_error", error.message);
40
+ }
41
+ }
42
+ }
43
+ if (!lastError) {
44
+ lastError = new ValidationError("json_error", "No JSON compatible response found in completion result");
45
+ }
46
+ throw lastError;
47
+ }
48
+
49
+
31
50
  export function validateResult(data: CompletionResult[], schema: Object): CompletionResult[] {
32
51
  let json;
33
52
  if (Array.isArray(data)) {
@@ -35,9 +54,8 @@ export function validateResult(data: CompletionResult[], schema: Object): Comple
35
54
  if (jsonResults.length > 0) {
36
55
  json = jsonResults[0].value;
37
56
  } else {
38
- const stringResult = data.map(completionResultToString).join("");
39
57
  try {
40
- json = extractAndParseJSON(stringResult);
58
+ json = parseCompletionAsJson(data);
41
59
  } catch (error: any) {
42
60
  throw new ValidationError("json_error", error.message)
43
61
  }