@promptbook/markdown-utils 0.92.0-11 → 0.92.0-12
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/esm/index.es.js +221 -199
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/browser.index.d.ts +2 -0
- package/esm/typings/src/_packages/core.index.d.ts +6 -4
- package/esm/typings/src/_packages/types.index.d.ts +2 -2
- package/esm/typings/src/execution/PipelineExecutorResult.d.ts +3 -1
- package/esm/typings/src/execution/createPipelineExecutor/computeCosineSimilarity.d.ts +13 -0
- package/esm/typings/src/execution/utils/checkExpectations.d.ts +1 -1
- package/esm/typings/src/formats/_common/{FormatDefinition.d.ts → FormatParser.d.ts} +3 -3
- package/esm/typings/src/formats/_common/{FormatSubvalueDefinition.d.ts → FormatSubvalueParser.d.ts} +1 -1
- package/esm/typings/src/formats/csv/CsvFormatParser.d.ts +17 -0
- package/esm/typings/src/formats/index.d.ts +2 -2
- package/esm/typings/src/formats/json/{JsonFormatDefinition.d.ts → JsonFormatParser.d.ts} +6 -6
- package/esm/typings/src/formats/text/{TextFormatDefinition.d.ts → TextFormatParser.d.ts} +7 -7
- package/esm/typings/src/formats/xml/XmlFormatParser.d.ts +19 -0
- package/esm/typings/src/postprocessing/utils/extractJsonBlock.d.ts +1 -1
- package/esm/typings/src/storage/local-storage/getIndexedDbStorage.d.ts +10 -0
- package/esm/typings/src/storage/local-storage/utils/makePromptbookStorageFromIndexedDb.d.ts +7 -0
- package/esm/typings/src/utils/expectation-counters/index.d.ts +1 -1
- package/package.json +1 -1
- package/umd/index.umd.js +221 -199
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/formats/csv/CsvFormatDefinition.d.ts +0 -17
- package/esm/typings/src/formats/xml/XmlFormatDefinition.d.ts +0 -19
package/esm/index.es.js
CHANGED
|
@@ -25,7 +25,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
25
25
|
* @generated
|
|
26
26
|
* @see https://github.com/webgptorg/promptbook
|
|
27
27
|
*/
|
|
28
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.92.0-
|
|
28
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.92.0-12';
|
|
29
29
|
/**
|
|
30
30
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
31
31
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -517,7 +517,7 @@ function extractJsonBlock(markdown) {
|
|
|
517
517
|
}
|
|
518
518
|
/**
|
|
519
519
|
* TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
|
|
520
|
-
* TODO: [🏢] Make this logic part of `
|
|
520
|
+
* TODO: [🏢] Make this logic part of `JsonFormatParser` or `isValidJsonString`
|
|
521
521
|
*/
|
|
522
522
|
|
|
523
523
|
/**
|
|
@@ -1797,75 +1797,6 @@ function isPipelinePrepared(pipeline) {
|
|
|
1797
1797
|
* - [♨] Are tasks prepared
|
|
1798
1798
|
*/
|
|
1799
1799
|
|
|
1800
|
-
/**
|
|
1801
|
-
* Converts a JavaScript Object Notation (JSON) string into an object.
|
|
1802
|
-
*
|
|
1803
|
-
* Note: This is wrapper around `JSON.parse()` with better error and type handling
|
|
1804
|
-
*
|
|
1805
|
-
* @public exported from `@promptbook/utils`
|
|
1806
|
-
*/
|
|
1807
|
-
function jsonParse(value) {
|
|
1808
|
-
if (value === undefined) {
|
|
1809
|
-
throw new Error(`Can not parse JSON from undefined value.`);
|
|
1810
|
-
}
|
|
1811
|
-
else if (typeof value !== 'string') {
|
|
1812
|
-
console.error('Can not parse JSON from non-string value.', { text: value });
|
|
1813
|
-
throw new Error(spaceTrim(`
|
|
1814
|
-
Can not parse JSON from non-string value.
|
|
1815
|
-
|
|
1816
|
-
The value type: ${typeof value}
|
|
1817
|
-
See more in console.
|
|
1818
|
-
`));
|
|
1819
|
-
}
|
|
1820
|
-
try {
|
|
1821
|
-
return JSON.parse(value);
|
|
1822
|
-
}
|
|
1823
|
-
catch (error) {
|
|
1824
|
-
if (!(error instanceof Error)) {
|
|
1825
|
-
throw error;
|
|
1826
|
-
}
|
|
1827
|
-
throw new Error(spaceTrim((block) => `
|
|
1828
|
-
${block(error.message)}
|
|
1829
|
-
|
|
1830
|
-
The JSON text:
|
|
1831
|
-
${block(value)}
|
|
1832
|
-
`));
|
|
1833
|
-
}
|
|
1834
|
-
}
|
|
1835
|
-
/**
|
|
1836
|
-
* TODO: !!!! Use in Promptbook.studio
|
|
1837
|
-
*/
|
|
1838
|
-
|
|
1839
|
-
/**
|
|
1840
|
-
* Recursively converts JSON strings to JSON objects
|
|
1841
|
-
|
|
1842
|
-
* @public exported from `@promptbook/utils`
|
|
1843
|
-
*/
|
|
1844
|
-
function jsonStringsToJsons(object) {
|
|
1845
|
-
if (object === null) {
|
|
1846
|
-
return object;
|
|
1847
|
-
}
|
|
1848
|
-
if (Array.isArray(object)) {
|
|
1849
|
-
return object.map(jsonStringsToJsons);
|
|
1850
|
-
}
|
|
1851
|
-
if (typeof object !== 'object') {
|
|
1852
|
-
return object;
|
|
1853
|
-
}
|
|
1854
|
-
const newObject = { ...object };
|
|
1855
|
-
for (const [key, value] of Object.entries(object)) {
|
|
1856
|
-
if (typeof value === 'string' && isValidJsonString(value)) {
|
|
1857
|
-
newObject[key] = jsonParse(value);
|
|
1858
|
-
}
|
|
1859
|
-
else {
|
|
1860
|
-
newObject[key] = jsonStringsToJsons(value);
|
|
1861
|
-
}
|
|
1862
|
-
}
|
|
1863
|
-
return newObject;
|
|
1864
|
-
}
|
|
1865
|
-
/**
|
|
1866
|
-
* TODO: Type the return type correctly
|
|
1867
|
-
*/
|
|
1868
|
-
|
|
1869
1800
|
/**
|
|
1870
1801
|
* This error indicates problems parsing the format value
|
|
1871
1802
|
*
|
|
@@ -2075,6 +2006,104 @@ const ALL_ERRORS = {
|
|
|
2075
2006
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
2076
2007
|
*/
|
|
2077
2008
|
|
|
2009
|
+
/**
|
|
2010
|
+
* Serializes an error into a [🚉] JSON-serializable object
|
|
2011
|
+
*
|
|
2012
|
+
* @public exported from `@promptbook/utils`
|
|
2013
|
+
*/
|
|
2014
|
+
function serializeError(error) {
|
|
2015
|
+
const { name, message, stack } = error;
|
|
2016
|
+
const { id } = error;
|
|
2017
|
+
if (!Object.keys(ALL_ERRORS).includes(name)) {
|
|
2018
|
+
console.error(spaceTrim((block) => `
|
|
2019
|
+
|
|
2020
|
+
Cannot serialize error with name "${name}"
|
|
2021
|
+
|
|
2022
|
+
Authors of Promptbook probably forgot to add this error into the list of errors:
|
|
2023
|
+
https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
|
|
2024
|
+
|
|
2025
|
+
|
|
2026
|
+
${block(stack || message)}
|
|
2027
|
+
|
|
2028
|
+
`));
|
|
2029
|
+
}
|
|
2030
|
+
return {
|
|
2031
|
+
name: name,
|
|
2032
|
+
message,
|
|
2033
|
+
stack,
|
|
2034
|
+
id, // Include id in the serialized object
|
|
2035
|
+
};
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
/**
|
|
2039
|
+
* Converts a JavaScript Object Notation (JSON) string into an object.
|
|
2040
|
+
*
|
|
2041
|
+
* Note: This is wrapper around `JSON.parse()` with better error and type handling
|
|
2042
|
+
*
|
|
2043
|
+
* @public exported from `@promptbook/utils`
|
|
2044
|
+
*/
|
|
2045
|
+
function jsonParse(value) {
|
|
2046
|
+
if (value === undefined) {
|
|
2047
|
+
throw new Error(`Can not parse JSON from undefined value.`);
|
|
2048
|
+
}
|
|
2049
|
+
else if (typeof value !== 'string') {
|
|
2050
|
+
console.error('Can not parse JSON from non-string value.', { text: value });
|
|
2051
|
+
throw new Error(spaceTrim(`
|
|
2052
|
+
Can not parse JSON from non-string value.
|
|
2053
|
+
|
|
2054
|
+
The value type: ${typeof value}
|
|
2055
|
+
See more in console.
|
|
2056
|
+
`));
|
|
2057
|
+
}
|
|
2058
|
+
try {
|
|
2059
|
+
return JSON.parse(value);
|
|
2060
|
+
}
|
|
2061
|
+
catch (error) {
|
|
2062
|
+
if (!(error instanceof Error)) {
|
|
2063
|
+
throw error;
|
|
2064
|
+
}
|
|
2065
|
+
throw new Error(spaceTrim((block) => `
|
|
2066
|
+
${block(error.message)}
|
|
2067
|
+
|
|
2068
|
+
The JSON text:
|
|
2069
|
+
${block(value)}
|
|
2070
|
+
`));
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
/**
|
|
2074
|
+
* TODO: !!!! Use in Promptbook.studio
|
|
2075
|
+
*/
|
|
2076
|
+
|
|
2077
|
+
/**
|
|
2078
|
+
* Recursively converts JSON strings to JSON objects
|
|
2079
|
+
|
|
2080
|
+
* @public exported from `@promptbook/utils`
|
|
2081
|
+
*/
|
|
2082
|
+
function jsonStringsToJsons(object) {
|
|
2083
|
+
if (object === null) {
|
|
2084
|
+
return object;
|
|
2085
|
+
}
|
|
2086
|
+
if (Array.isArray(object)) {
|
|
2087
|
+
return object.map(jsonStringsToJsons);
|
|
2088
|
+
}
|
|
2089
|
+
if (typeof object !== 'object') {
|
|
2090
|
+
return object;
|
|
2091
|
+
}
|
|
2092
|
+
const newObject = { ...object };
|
|
2093
|
+
for (const [key, value] of Object.entries(object)) {
|
|
2094
|
+
if (typeof value === 'string' && isValidJsonString(value)) {
|
|
2095
|
+
newObject[key] = jsonParse(value);
|
|
2096
|
+
}
|
|
2097
|
+
else {
|
|
2098
|
+
newObject[key] = jsonStringsToJsons(value);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
return newObject;
|
|
2102
|
+
}
|
|
2103
|
+
/**
|
|
2104
|
+
* TODO: Type the return type correctly
|
|
2105
|
+
*/
|
|
2106
|
+
|
|
2078
2107
|
/**
|
|
2079
2108
|
* Deserializes the error object
|
|
2080
2109
|
*
|
|
@@ -2240,64 +2269,6 @@ function createTask(options) {
|
|
|
2240
2269
|
* TODO: [🐚] Split into more files and make `PrepareTask` & `RemoteTask` + split the function
|
|
2241
2270
|
*/
|
|
2242
2271
|
|
|
2243
|
-
/**
|
|
2244
|
-
* Serializes an error into a [🚉] JSON-serializable object
|
|
2245
|
-
*
|
|
2246
|
-
* @public exported from `@promptbook/utils`
|
|
2247
|
-
*/
|
|
2248
|
-
function serializeError(error) {
|
|
2249
|
-
const { name, message, stack } = error;
|
|
2250
|
-
const { id } = error;
|
|
2251
|
-
if (!Object.keys(ALL_ERRORS).includes(name)) {
|
|
2252
|
-
console.error(spaceTrim((block) => `
|
|
2253
|
-
|
|
2254
|
-
Cannot serialize error with name "${name}"
|
|
2255
|
-
|
|
2256
|
-
Authors of Promptbook probably forgot to add this error into the list of errors:
|
|
2257
|
-
https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
${block(stack || message)}
|
|
2261
|
-
|
|
2262
|
-
`));
|
|
2263
|
-
}
|
|
2264
|
-
return {
|
|
2265
|
-
name: name,
|
|
2266
|
-
message,
|
|
2267
|
-
stack,
|
|
2268
|
-
id, // Include id in the serialized object
|
|
2269
|
-
};
|
|
2270
|
-
}
|
|
2271
|
-
|
|
2272
|
-
/**
|
|
2273
|
-
* Async version of Array.forEach
|
|
2274
|
-
*
|
|
2275
|
-
* @param array - Array to iterate over
|
|
2276
|
-
* @param options - Options for the function
|
|
2277
|
-
* @param callbackfunction - Function to call for each item
|
|
2278
|
-
* @public exported from `@promptbook/utils`
|
|
2279
|
-
* @deprecated [🪂] Use queues instead
|
|
2280
|
-
*/
|
|
2281
|
-
async function forEachAsync(array, options, callbackfunction) {
|
|
2282
|
-
const { maxParallelCount = Infinity } = options;
|
|
2283
|
-
let index = 0;
|
|
2284
|
-
let runningTasks = [];
|
|
2285
|
-
const tasks = [];
|
|
2286
|
-
for (const item of array) {
|
|
2287
|
-
const currentIndex = index++;
|
|
2288
|
-
const task = callbackfunction(item, currentIndex, array);
|
|
2289
|
-
tasks.push(task);
|
|
2290
|
-
runningTasks.push(task);
|
|
2291
|
-
/* not await */ Promise.resolve(task).then(() => {
|
|
2292
|
-
runningTasks = runningTasks.filter((t) => t !== task);
|
|
2293
|
-
});
|
|
2294
|
-
if (maxParallelCount < runningTasks.length) {
|
|
2295
|
-
await Promise.race(runningTasks);
|
|
2296
|
-
}
|
|
2297
|
-
}
|
|
2298
|
-
await Promise.all(tasks);
|
|
2299
|
-
}
|
|
2300
|
-
|
|
2301
2272
|
/**
|
|
2302
2273
|
* Represents the uncertain value
|
|
2303
2274
|
*
|
|
@@ -2341,7 +2312,7 @@ const ZERO_USAGE = $deepFreeze({
|
|
|
2341
2312
|
*
|
|
2342
2313
|
* @public exported from `@promptbook/core`
|
|
2343
2314
|
*/
|
|
2344
|
-
$deepFreeze({
|
|
2315
|
+
const UNCERTAIN_USAGE = $deepFreeze({
|
|
2345
2316
|
price: UNCERTAIN_ZERO_VALUE,
|
|
2346
2317
|
input: {
|
|
2347
2318
|
tokensCount: UNCERTAIN_ZERO_VALUE,
|
|
@@ -2366,6 +2337,35 @@ $deepFreeze({
|
|
|
2366
2337
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
2367
2338
|
*/
|
|
2368
2339
|
|
|
2340
|
+
/**
|
|
2341
|
+
* Async version of Array.forEach
|
|
2342
|
+
*
|
|
2343
|
+
* @param array - Array to iterate over
|
|
2344
|
+
* @param options - Options for the function
|
|
2345
|
+
* @param callbackfunction - Function to call for each item
|
|
2346
|
+
* @public exported from `@promptbook/utils`
|
|
2347
|
+
* @deprecated [🪂] Use queues instead
|
|
2348
|
+
*/
|
|
2349
|
+
async function forEachAsync(array, options, callbackfunction) {
|
|
2350
|
+
const { maxParallelCount = Infinity } = options;
|
|
2351
|
+
let index = 0;
|
|
2352
|
+
let runningTasks = [];
|
|
2353
|
+
const tasks = [];
|
|
2354
|
+
for (const item of array) {
|
|
2355
|
+
const currentIndex = index++;
|
|
2356
|
+
const task = callbackfunction(item, currentIndex, array);
|
|
2357
|
+
tasks.push(task);
|
|
2358
|
+
runningTasks.push(task);
|
|
2359
|
+
/* not await */ Promise.resolve(task).then(() => {
|
|
2360
|
+
runningTasks = runningTasks.filter((t) => t !== task);
|
|
2361
|
+
});
|
|
2362
|
+
if (maxParallelCount < runningTasks.length) {
|
|
2363
|
+
await Promise.race(runningTasks);
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
await Promise.all(tasks);
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
2369
|
/**
|
|
2370
2370
|
* Function `addUsage` will add multiple usages into one
|
|
2371
2371
|
*
|
|
@@ -4159,6 +4159,24 @@ const MANDATORY_CSV_SETTINGS = Object.freeze({
|
|
|
4159
4159
|
// encoding: 'utf-8',
|
|
4160
4160
|
});
|
|
4161
4161
|
|
|
4162
|
+
/**
|
|
4163
|
+
* Converts a CSV string into an object
|
|
4164
|
+
*
|
|
4165
|
+
* Note: This is wrapper around `papaparse.parse()` with better autohealing
|
|
4166
|
+
*
|
|
4167
|
+
* @private - for now until `@promptbook/csv` is released
|
|
4168
|
+
*/
|
|
4169
|
+
function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
|
|
4170
|
+
settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
|
|
4171
|
+
// Note: Autoheal invalid '\n' characters
|
|
4172
|
+
if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
|
|
4173
|
+
console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
|
|
4174
|
+
value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
4175
|
+
}
|
|
4176
|
+
const csv = parse(value, settings);
|
|
4177
|
+
return csv;
|
|
4178
|
+
}
|
|
4179
|
+
|
|
4162
4180
|
/**
|
|
4163
4181
|
* Function to check if a string is valid CSV
|
|
4164
4182
|
*
|
|
@@ -4181,31 +4199,13 @@ function isValidCsvString(value) {
|
|
|
4181
4199
|
}
|
|
4182
4200
|
}
|
|
4183
4201
|
|
|
4184
|
-
/**
|
|
4185
|
-
* Converts a CSV string into an object
|
|
4186
|
-
*
|
|
4187
|
-
* Note: This is wrapper around `papaparse.parse()` with better autohealing
|
|
4188
|
-
*
|
|
4189
|
-
* @private - for now until `@promptbook/csv` is released
|
|
4190
|
-
*/
|
|
4191
|
-
function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
|
|
4192
|
-
settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
|
|
4193
|
-
// Note: Autoheal invalid '\n' characters
|
|
4194
|
-
if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
|
|
4195
|
-
console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
|
|
4196
|
-
value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
4197
|
-
}
|
|
4198
|
-
const csv = parse(value, settings);
|
|
4199
|
-
return csv;
|
|
4200
|
-
}
|
|
4201
|
-
|
|
4202
4202
|
/**
|
|
4203
4203
|
* Definition for CSV spreadsheet
|
|
4204
4204
|
*
|
|
4205
4205
|
* @public exported from `@promptbook/core`
|
|
4206
4206
|
* <- TODO: [🏢] Export from package `@promptbook/csv`
|
|
4207
4207
|
*/
|
|
4208
|
-
const
|
|
4208
|
+
const CsvFormatParser = {
|
|
4209
4209
|
formatName: 'CSV',
|
|
4210
4210
|
aliases: ['SPREADSHEET', 'TABLE'],
|
|
4211
4211
|
isValid(value, settings, schema) {
|
|
@@ -4217,7 +4217,7 @@ const CsvFormatDefinition = {
|
|
|
4217
4217
|
heal(value, settings, schema) {
|
|
4218
4218
|
throw new Error('Not implemented');
|
|
4219
4219
|
},
|
|
4220
|
-
|
|
4220
|
+
subvalueParsers: [
|
|
4221
4221
|
{
|
|
4222
4222
|
subvalueName: 'ROW',
|
|
4223
4223
|
async mapValues(value, outputParameterName, settings, mapCallback) {
|
|
@@ -4278,10 +4278,10 @@ const CsvFormatDefinition = {
|
|
|
4278
4278
|
],
|
|
4279
4279
|
};
|
|
4280
4280
|
/**
|
|
4281
|
-
* TODO: [🍓] In `
|
|
4282
|
-
* TODO: [🍓] In `
|
|
4283
|
-
* TODO: [🍓] In `
|
|
4284
|
-
* TODO: [🍓] In `
|
|
4281
|
+
* TODO: [🍓] In `CsvFormatParser` implement simple `isValid`
|
|
4282
|
+
* TODO: [🍓] In `CsvFormatParser` implement partial `canBeValid`
|
|
4283
|
+
* TODO: [🍓] In `CsvFormatParser` implement `heal
|
|
4284
|
+
* TODO: [🍓] In `CsvFormatParser` implement `subvalueParsers`
|
|
4285
4285
|
* TODO: [🏢] Allow to expect something inside CSV objects and other formats
|
|
4286
4286
|
*/
|
|
4287
4287
|
|
|
@@ -4290,7 +4290,7 @@ const CsvFormatDefinition = {
|
|
|
4290
4290
|
*
|
|
4291
4291
|
* @private still in development [🏢]
|
|
4292
4292
|
*/
|
|
4293
|
-
const
|
|
4293
|
+
const JsonFormatParser = {
|
|
4294
4294
|
formatName: 'JSON',
|
|
4295
4295
|
mimeType: 'application/json',
|
|
4296
4296
|
isValid(value, settings, schema) {
|
|
@@ -4302,28 +4302,28 @@ const JsonFormatDefinition = {
|
|
|
4302
4302
|
heal(value, settings, schema) {
|
|
4303
4303
|
throw new Error('Not implemented');
|
|
4304
4304
|
},
|
|
4305
|
-
|
|
4305
|
+
subvalueParsers: [],
|
|
4306
4306
|
};
|
|
4307
4307
|
/**
|
|
4308
4308
|
* TODO: [🧠] Maybe propper instance of object
|
|
4309
4309
|
* TODO: [0] Make string_serialized_json
|
|
4310
4310
|
* TODO: [1] Make type for JSON Settings and Schema
|
|
4311
4311
|
* TODO: [🧠] What to use for validating JSONs - JSON Schema, ZoD, typescript types/interfaces,...?
|
|
4312
|
-
* TODO: [🍓] In `
|
|
4313
|
-
* TODO: [🍓] In `
|
|
4314
|
-
* TODO: [🍓] In `
|
|
4315
|
-
* TODO: [🍓] In `
|
|
4312
|
+
* TODO: [🍓] In `JsonFormatParser` implement simple `isValid`
|
|
4313
|
+
* TODO: [🍓] In `JsonFormatParser` implement partial `canBeValid`
|
|
4314
|
+
* TODO: [🍓] In `JsonFormatParser` implement `heal
|
|
4315
|
+
* TODO: [🍓] In `JsonFormatParser` implement `subvalueParsers`
|
|
4316
4316
|
* TODO: [🏢] Allow to expect something inside JSON objects and other formats
|
|
4317
4317
|
*/
|
|
4318
4318
|
|
|
4319
4319
|
/**
|
|
4320
4320
|
* Definition for any text - this will be always valid
|
|
4321
4321
|
*
|
|
4322
|
-
* Note: This is not useful for validation, but for splitting and mapping with `
|
|
4322
|
+
* Note: This is not useful for validation, but for splitting and mapping with `subvalueParsers`
|
|
4323
4323
|
*
|
|
4324
4324
|
* @public exported from `@promptbook/core`
|
|
4325
4325
|
*/
|
|
4326
|
-
const
|
|
4326
|
+
const TextFormatParser = {
|
|
4327
4327
|
formatName: 'TEXT',
|
|
4328
4328
|
isValid(value) {
|
|
4329
4329
|
return typeof value === 'string';
|
|
@@ -4332,9 +4332,9 @@ const TextFormatDefinition = {
|
|
|
4332
4332
|
return typeof partialValue === 'string';
|
|
4333
4333
|
},
|
|
4334
4334
|
heal() {
|
|
4335
|
-
throw new UnexpectedError('It does not make sense to call `
|
|
4335
|
+
throw new UnexpectedError('It does not make sense to call `TextFormatParser.heal`');
|
|
4336
4336
|
},
|
|
4337
|
-
|
|
4337
|
+
subvalueParsers: [
|
|
4338
4338
|
{
|
|
4339
4339
|
subvalueName: 'LINE',
|
|
4340
4340
|
async mapValues(value, outputParameterName, settings, mapCallback) {
|
|
@@ -4354,10 +4354,10 @@ const TextFormatDefinition = {
|
|
|
4354
4354
|
/**
|
|
4355
4355
|
* TODO: [1] Make type for XML Text and Schema
|
|
4356
4356
|
* TODO: [🧠][🤠] Here should be all words, characters, lines, paragraphs, pages available as subvalues
|
|
4357
|
-
* TODO: [🍓] In `
|
|
4358
|
-
* TODO: [🍓] In `
|
|
4359
|
-
* TODO: [🍓] In `
|
|
4360
|
-
* TODO: [🍓] In `
|
|
4357
|
+
* TODO: [🍓] In `TextFormatParser` implement simple `isValid`
|
|
4358
|
+
* TODO: [🍓] In `TextFormatParser` implement partial `canBeValid`
|
|
4359
|
+
* TODO: [🍓] In `TextFormatParser` implement `heal
|
|
4360
|
+
* TODO: [🍓] In `TextFormatParser` implement `subvalueParsers`
|
|
4361
4361
|
* TODO: [🏢] Allow to expect something inside each item of list and other formats
|
|
4362
4362
|
*/
|
|
4363
4363
|
|
|
@@ -4390,7 +4390,7 @@ function isValidXmlString(value) {
|
|
|
4390
4390
|
*
|
|
4391
4391
|
* @private still in development [🏢]
|
|
4392
4392
|
*/
|
|
4393
|
-
const
|
|
4393
|
+
const XmlFormatParser = {
|
|
4394
4394
|
formatName: 'XML',
|
|
4395
4395
|
mimeType: 'application/xml',
|
|
4396
4396
|
isValid(value, settings, schema) {
|
|
@@ -4402,17 +4402,17 @@ const XmlFormatDefinition = {
|
|
|
4402
4402
|
heal(value, settings, schema) {
|
|
4403
4403
|
throw new Error('Not implemented');
|
|
4404
4404
|
},
|
|
4405
|
-
|
|
4405
|
+
subvalueParsers: [],
|
|
4406
4406
|
};
|
|
4407
4407
|
/**
|
|
4408
4408
|
* TODO: [🧠] Maybe propper instance of object
|
|
4409
4409
|
* TODO: [0] Make string_serialized_xml
|
|
4410
4410
|
* TODO: [1] Make type for XML Settings and Schema
|
|
4411
4411
|
* TODO: [🧠] What to use for validating XMLs - XSD,...
|
|
4412
|
-
* TODO: [🍓] In `
|
|
4413
|
-
* TODO: [🍓] In `
|
|
4414
|
-
* TODO: [🍓] In `
|
|
4415
|
-
* TODO: [🍓] In `
|
|
4412
|
+
* TODO: [🍓] In `XmlFormatParser` implement simple `isValid`
|
|
4413
|
+
* TODO: [🍓] In `XmlFormatParser` implement partial `canBeValid`
|
|
4414
|
+
* TODO: [🍓] In `XmlFormatParser` implement `heal
|
|
4415
|
+
* TODO: [🍓] In `XmlFormatParser` implement `subvalueParsers`
|
|
4416
4416
|
* TODO: [🏢] Allow to expect something inside XML and other formats
|
|
4417
4417
|
*/
|
|
4418
4418
|
|
|
@@ -4421,12 +4421,7 @@ const XmlFormatDefinition = {
|
|
|
4421
4421
|
*
|
|
4422
4422
|
* @private internal index of `...` <- TODO [🏢]
|
|
4423
4423
|
*/
|
|
4424
|
-
const FORMAT_DEFINITIONS = [
|
|
4425
|
-
JsonFormatDefinition,
|
|
4426
|
-
XmlFormatDefinition,
|
|
4427
|
-
TextFormatDefinition,
|
|
4428
|
-
CsvFormatDefinition,
|
|
4429
|
-
];
|
|
4424
|
+
const FORMAT_DEFINITIONS = [JsonFormatParser, XmlFormatParser, TextFormatParser, CsvFormatParser];
|
|
4430
4425
|
/**
|
|
4431
4426
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
4432
4427
|
*/
|
|
@@ -4696,7 +4691,7 @@ const CountUtils = {
|
|
|
4696
4691
|
PAGES: countPages,
|
|
4697
4692
|
};
|
|
4698
4693
|
/**
|
|
4699
|
-
* TODO: [🧠][🤠] This should be probbably as part of `
|
|
4694
|
+
* TODO: [🧠][🤠] This should be probbably as part of `TextFormatParser`
|
|
4700
4695
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
4701
4696
|
*/
|
|
4702
4697
|
|
|
@@ -4724,7 +4719,7 @@ function checkExpectations(expectations, value) {
|
|
|
4724
4719
|
}
|
|
4725
4720
|
/**
|
|
4726
4721
|
* TODO: [💝] Unite object for expecting amount and format
|
|
4727
|
-
* TODO: [🧠][🤠] This should be part of `
|
|
4722
|
+
* TODO: [🧠][🤠] This should be part of `TextFormatParser`
|
|
4728
4723
|
* Note: [💝] and [🤠] are interconnected together
|
|
4729
4724
|
*/
|
|
4730
4725
|
|
|
@@ -4952,7 +4947,7 @@ async function executeAttempts(options) {
|
|
|
4952
4947
|
if (task.format) {
|
|
4953
4948
|
if (task.format === 'JSON') {
|
|
4954
4949
|
if (!isValidJsonString($ongoingTaskResult.$resultString || '')) {
|
|
4955
|
-
// TODO: [🏢] Do more universally via `
|
|
4950
|
+
// TODO: [🏢] Do more universally via `FormatParser`
|
|
4956
4951
|
try {
|
|
4957
4952
|
$ongoingTaskResult.$resultString = extractJsonBlock($ongoingTaskResult.$resultString || '');
|
|
4958
4953
|
}
|
|
@@ -5090,16 +5085,16 @@ async function executeFormatSubvalues(options) {
|
|
|
5090
5085
|
${block(pipelineIdentification)}
|
|
5091
5086
|
`));
|
|
5092
5087
|
}
|
|
5093
|
-
const
|
|
5094
|
-
if (
|
|
5088
|
+
const subvalueParser = formatDefinition.subvalueParsers.find((subvalueParser) => [subvalueParser.subvalueName, ...(subvalueParser.aliases || [])].includes(task.foreach.subformatName));
|
|
5089
|
+
if (subvalueParser === undefined) {
|
|
5095
5090
|
throw new UnexpectedError(
|
|
5096
5091
|
// <- TODO: [🧠][🧐] Should be formats fixed per promptbook version or behave as plugins (=> change UnexpectedError)
|
|
5097
5092
|
spaceTrim((block) => `
|
|
5098
5093
|
Unsupported subformat name "${task.foreach.subformatName}" for format "${task.foreach.formatName}"
|
|
5099
5094
|
|
|
5100
5095
|
Available subformat names for format "${formatDefinition.formatName}":
|
|
5101
|
-
${block(formatDefinition.
|
|
5102
|
-
.map((
|
|
5096
|
+
${block(formatDefinition.subvalueParsers
|
|
5097
|
+
.map((subvalueParser) => subvalueParser.subvalueName)
|
|
5103
5098
|
.map((subvalueName) => `- ${subvalueName}`)
|
|
5104
5099
|
.join('\n'))}
|
|
5105
5100
|
|
|
@@ -5113,7 +5108,7 @@ async function executeFormatSubvalues(options) {
|
|
|
5113
5108
|
formatSettings = csvSettings;
|
|
5114
5109
|
// <- TODO: [🤹♂️] More universal, make simmilar pattern for other formats for example \n vs \r\n in text
|
|
5115
5110
|
}
|
|
5116
|
-
const resultString = await
|
|
5111
|
+
const resultString = await subvalueParser.mapValues(parameterValue, task.foreach.outputSubparameterName, formatSettings, async (subparameters, index) => {
|
|
5117
5112
|
let mappedParameters;
|
|
5118
5113
|
// TODO: [🤹♂️][🪂] Limit to N concurrent executions
|
|
5119
5114
|
// TODO: When done [🐚] Report progress also for each subvalue here
|
|
@@ -5175,6 +5170,27 @@ async function getExamplesForTask(task) {
|
|
|
5175
5170
|
return RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [♨] Implement */;
|
|
5176
5171
|
}
|
|
5177
5172
|
|
|
5173
|
+
/**
|
|
5174
|
+
* Computes the cosine similarity between two embedding vectors
|
|
5175
|
+
*
|
|
5176
|
+
* Note: This is helping function for RAG (retrieval-augmented generation)
|
|
5177
|
+
*
|
|
5178
|
+
* @param embeddingVector1
|
|
5179
|
+
* @param embeddingVector2
|
|
5180
|
+
* @returns Cosine similarity between the two vectors
|
|
5181
|
+
*
|
|
5182
|
+
* @public exported from `@promptbook/core`
|
|
5183
|
+
*/
|
|
5184
|
+
function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
|
|
5185
|
+
if (embeddingVector1.length !== embeddingVector2.length) {
|
|
5186
|
+
throw new TypeError('Embedding vectors must have the same length');
|
|
5187
|
+
}
|
|
5188
|
+
const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
|
|
5189
|
+
const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
|
|
5190
|
+
const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
|
|
5191
|
+
return 1 - dotProduct / (magnitude1 * magnitude2);
|
|
5192
|
+
}
|
|
5193
|
+
|
|
5178
5194
|
/**
|
|
5179
5195
|
* @@@
|
|
5180
5196
|
*
|
|
@@ -5201,7 +5217,7 @@ async function getKnowledgeForTask(options) {
|
|
|
5201
5217
|
},
|
|
5202
5218
|
content: task.content,
|
|
5203
5219
|
parameters: {
|
|
5204
|
-
/*
|
|
5220
|
+
/* !!!! */
|
|
5205
5221
|
},
|
|
5206
5222
|
};
|
|
5207
5223
|
const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
|
|
@@ -5236,16 +5252,6 @@ async function getKnowledgeForTask(options) {
|
|
|
5236
5252
|
return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
|
|
5237
5253
|
// <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
|
|
5238
5254
|
}
|
|
5239
|
-
// TODO: !!!!!! Annotate + to new file
|
|
5240
|
-
function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
|
|
5241
|
-
if (embeddingVector1.length !== embeddingVector2.length) {
|
|
5242
|
-
throw new TypeError('Embedding vectors must have the same length');
|
|
5243
|
-
}
|
|
5244
|
-
const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
|
|
5245
|
-
const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
|
|
5246
|
-
const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
|
|
5247
|
-
return 1 - dotProduct / (magnitude1 * magnitude2);
|
|
5248
|
-
}
|
|
5249
5255
|
/**
|
|
5250
5256
|
* TODO: !!!! Verify if this is working
|
|
5251
5257
|
* TODO: [♨] Implement Better - use keyword search
|
|
@@ -5748,6 +5754,22 @@ function createPipelineExecutor(options) {
|
|
|
5748
5754
|
cacheDirname,
|
|
5749
5755
|
intermediateFilesStrategy,
|
|
5750
5756
|
isAutoInstalled,
|
|
5757
|
+
}).catch((error) => {
|
|
5758
|
+
assertsError(error);
|
|
5759
|
+
return exportJson({
|
|
5760
|
+
name: 'pipelineExecutorResult',
|
|
5761
|
+
message: `Unuccessful PipelineExecutorResult, last catch`,
|
|
5762
|
+
order: [],
|
|
5763
|
+
value: {
|
|
5764
|
+
isSuccessful: false,
|
|
5765
|
+
errors: [serializeError(error)],
|
|
5766
|
+
warnings: [],
|
|
5767
|
+
usage: UNCERTAIN_USAGE,
|
|
5768
|
+
executionReport: null,
|
|
5769
|
+
outputParameters: {},
|
|
5770
|
+
preparedPipeline,
|
|
5771
|
+
},
|
|
5772
|
+
});
|
|
5751
5773
|
});
|
|
5752
5774
|
};
|
|
5753
5775
|
const pipelineExecutor = (inputParameters) => createTask({
|