@promptbook/documents 0.92.0-10 → 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.
Files changed (24) hide show
  1. package/esm/index.es.js +221 -199
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/browser.index.d.ts +2 -0
  4. package/esm/typings/src/_packages/core.index.d.ts +6 -4
  5. package/esm/typings/src/_packages/types.index.d.ts +2 -2
  6. package/esm/typings/src/execution/PipelineExecutorResult.d.ts +3 -1
  7. package/esm/typings/src/execution/createPipelineExecutor/computeCosineSimilarity.d.ts +13 -0
  8. package/esm/typings/src/execution/utils/checkExpectations.d.ts +1 -1
  9. package/esm/typings/src/formats/_common/{FormatDefinition.d.ts → FormatParser.d.ts} +3 -3
  10. package/esm/typings/src/formats/_common/{FormatSubvalueDefinition.d.ts → FormatSubvalueParser.d.ts} +1 -1
  11. package/esm/typings/src/formats/csv/CsvFormatParser.d.ts +17 -0
  12. package/esm/typings/src/formats/index.d.ts +2 -2
  13. package/esm/typings/src/formats/json/{JsonFormatDefinition.d.ts → JsonFormatParser.d.ts} +6 -6
  14. package/esm/typings/src/formats/text/{TextFormatDefinition.d.ts → TextFormatParser.d.ts} +7 -7
  15. package/esm/typings/src/formats/xml/XmlFormatParser.d.ts +19 -0
  16. package/esm/typings/src/postprocessing/utils/extractJsonBlock.d.ts +1 -1
  17. package/esm/typings/src/storage/local-storage/getIndexedDbStorage.d.ts +10 -0
  18. package/esm/typings/src/storage/local-storage/utils/makePromptbookStorageFromIndexedDb.d.ts +7 -0
  19. package/esm/typings/src/utils/expectation-counters/index.d.ts +1 -1
  20. package/package.json +2 -2
  21. package/umd/index.umd.js +221 -199
  22. package/umd/index.umd.js.map +1 -1
  23. package/esm/typings/src/formats/csv/CsvFormatDefinition.d.ts +0 -17
  24. package/esm/typings/src/formats/xml/XmlFormatDefinition.d.ts +0 -19
package/umd/index.umd.js CHANGED
@@ -26,7 +26,7 @@
26
26
  * @generated
27
27
  * @see https://github.com/webgptorg/promptbook
28
28
  */
29
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-10';
29
+ const PROMPTBOOK_ENGINE_VERSION = '0.92.0-12';
30
30
  /**
31
31
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
32
32
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2296,75 +2296,6 @@
2296
2296
  * - [♨] Are tasks prepared
2297
2297
  */
2298
2298
 
2299
- /**
2300
- * Converts a JavaScript Object Notation (JSON) string into an object.
2301
- *
2302
- * Note: This is wrapper around `JSON.parse()` with better error and type handling
2303
- *
2304
- * @public exported from `@promptbook/utils`
2305
- */
2306
- function jsonParse(value) {
2307
- if (value === undefined) {
2308
- throw new Error(`Can not parse JSON from undefined value.`);
2309
- }
2310
- else if (typeof value !== 'string') {
2311
- console.error('Can not parse JSON from non-string value.', { text: value });
2312
- throw new Error(spaceTrim__default["default"](`
2313
- Can not parse JSON from non-string value.
2314
-
2315
- The value type: ${typeof value}
2316
- See more in console.
2317
- `));
2318
- }
2319
- try {
2320
- return JSON.parse(value);
2321
- }
2322
- catch (error) {
2323
- if (!(error instanceof Error)) {
2324
- throw error;
2325
- }
2326
- throw new Error(spaceTrim__default["default"]((block) => `
2327
- ${block(error.message)}
2328
-
2329
- The JSON text:
2330
- ${block(value)}
2331
- `));
2332
- }
2333
- }
2334
- /**
2335
- * TODO: !!!! Use in Promptbook.studio
2336
- */
2337
-
2338
- /**
2339
- * Recursively converts JSON strings to JSON objects
2340
-
2341
- * @public exported from `@promptbook/utils`
2342
- */
2343
- function jsonStringsToJsons(object) {
2344
- if (object === null) {
2345
- return object;
2346
- }
2347
- if (Array.isArray(object)) {
2348
- return object.map(jsonStringsToJsons);
2349
- }
2350
- if (typeof object !== 'object') {
2351
- return object;
2352
- }
2353
- const newObject = { ...object };
2354
- for (const [key, value] of Object.entries(object)) {
2355
- if (typeof value === 'string' && isValidJsonString(value)) {
2356
- newObject[key] = jsonParse(value);
2357
- }
2358
- else {
2359
- newObject[key] = jsonStringsToJsons(value);
2360
- }
2361
- }
2362
- return newObject;
2363
- }
2364
- /**
2365
- * TODO: Type the return type correctly
2366
- */
2367
-
2368
2299
  /**
2369
2300
  * This error indicates problems parsing the format value
2370
2301
  *
@@ -2548,6 +2479,104 @@
2548
2479
  * Note: [💞] Ignore a discrepancy between file name and entity name
2549
2480
  */
2550
2481
 
2482
+ /**
2483
+ * Serializes an error into a [🚉] JSON-serializable object
2484
+ *
2485
+ * @public exported from `@promptbook/utils`
2486
+ */
2487
+ function serializeError(error) {
2488
+ const { name, message, stack } = error;
2489
+ const { id } = error;
2490
+ if (!Object.keys(ALL_ERRORS).includes(name)) {
2491
+ console.error(spaceTrim__default["default"]((block) => `
2492
+
2493
+ Cannot serialize error with name "${name}"
2494
+
2495
+ Authors of Promptbook probably forgot to add this error into the list of errors:
2496
+ https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
2497
+
2498
+
2499
+ ${block(stack || message)}
2500
+
2501
+ `));
2502
+ }
2503
+ return {
2504
+ name: name,
2505
+ message,
2506
+ stack,
2507
+ id, // Include id in the serialized object
2508
+ };
2509
+ }
2510
+
2511
+ /**
2512
+ * Converts a JavaScript Object Notation (JSON) string into an object.
2513
+ *
2514
+ * Note: This is wrapper around `JSON.parse()` with better error and type handling
2515
+ *
2516
+ * @public exported from `@promptbook/utils`
2517
+ */
2518
+ function jsonParse(value) {
2519
+ if (value === undefined) {
2520
+ throw new Error(`Can not parse JSON from undefined value.`);
2521
+ }
2522
+ else if (typeof value !== 'string') {
2523
+ console.error('Can not parse JSON from non-string value.', { text: value });
2524
+ throw new Error(spaceTrim__default["default"](`
2525
+ Can not parse JSON from non-string value.
2526
+
2527
+ The value type: ${typeof value}
2528
+ See more in console.
2529
+ `));
2530
+ }
2531
+ try {
2532
+ return JSON.parse(value);
2533
+ }
2534
+ catch (error) {
2535
+ if (!(error instanceof Error)) {
2536
+ throw error;
2537
+ }
2538
+ throw new Error(spaceTrim__default["default"]((block) => `
2539
+ ${block(error.message)}
2540
+
2541
+ The JSON text:
2542
+ ${block(value)}
2543
+ `));
2544
+ }
2545
+ }
2546
+ /**
2547
+ * TODO: !!!! Use in Promptbook.studio
2548
+ */
2549
+
2550
+ /**
2551
+ * Recursively converts JSON strings to JSON objects
2552
+
2553
+ * @public exported from `@promptbook/utils`
2554
+ */
2555
+ function jsonStringsToJsons(object) {
2556
+ if (object === null) {
2557
+ return object;
2558
+ }
2559
+ if (Array.isArray(object)) {
2560
+ return object.map(jsonStringsToJsons);
2561
+ }
2562
+ if (typeof object !== 'object') {
2563
+ return object;
2564
+ }
2565
+ const newObject = { ...object };
2566
+ for (const [key, value] of Object.entries(object)) {
2567
+ if (typeof value === 'string' && isValidJsonString(value)) {
2568
+ newObject[key] = jsonParse(value);
2569
+ }
2570
+ else {
2571
+ newObject[key] = jsonStringsToJsons(value);
2572
+ }
2573
+ }
2574
+ return newObject;
2575
+ }
2576
+ /**
2577
+ * TODO: Type the return type correctly
2578
+ */
2579
+
2551
2580
  /**
2552
2581
  * Deserializes the error object
2553
2582
  *
@@ -2713,64 +2742,6 @@
2713
2742
  * TODO: [🐚] Split into more files and make `PrepareTask` & `RemoteTask` + split the function
2714
2743
  */
2715
2744
 
2716
- /**
2717
- * Serializes an error into a [🚉] JSON-serializable object
2718
- *
2719
- * @public exported from `@promptbook/utils`
2720
- */
2721
- function serializeError(error) {
2722
- const { name, message, stack } = error;
2723
- const { id } = error;
2724
- if (!Object.keys(ALL_ERRORS).includes(name)) {
2725
- console.error(spaceTrim__default["default"]((block) => `
2726
-
2727
- Cannot serialize error with name "${name}"
2728
-
2729
- Authors of Promptbook probably forgot to add this error into the list of errors:
2730
- https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
2731
-
2732
-
2733
- ${block(stack || message)}
2734
-
2735
- `));
2736
- }
2737
- return {
2738
- name: name,
2739
- message,
2740
- stack,
2741
- id, // Include id in the serialized object
2742
- };
2743
- }
2744
-
2745
- /**
2746
- * Async version of Array.forEach
2747
- *
2748
- * @param array - Array to iterate over
2749
- * @param options - Options for the function
2750
- * @param callbackfunction - Function to call for each item
2751
- * @public exported from `@promptbook/utils`
2752
- * @deprecated [🪂] Use queues instead
2753
- */
2754
- async function forEachAsync(array, options, callbackfunction) {
2755
- const { maxParallelCount = Infinity } = options;
2756
- let index = 0;
2757
- let runningTasks = [];
2758
- const tasks = [];
2759
- for (const item of array) {
2760
- const currentIndex = index++;
2761
- const task = callbackfunction(item, currentIndex, array);
2762
- tasks.push(task);
2763
- runningTasks.push(task);
2764
- /* not await */ Promise.resolve(task).then(() => {
2765
- runningTasks = runningTasks.filter((t) => t !== task);
2766
- });
2767
- if (maxParallelCount < runningTasks.length) {
2768
- await Promise.race(runningTasks);
2769
- }
2770
- }
2771
- await Promise.all(tasks);
2772
- }
2773
-
2774
2745
  /**
2775
2746
  * Represents the uncertain value
2776
2747
  *
@@ -2814,7 +2785,7 @@
2814
2785
  *
2815
2786
  * @public exported from `@promptbook/core`
2816
2787
  */
2817
- $deepFreeze({
2788
+ const UNCERTAIN_USAGE = $deepFreeze({
2818
2789
  price: UNCERTAIN_ZERO_VALUE,
2819
2790
  input: {
2820
2791
  tokensCount: UNCERTAIN_ZERO_VALUE,
@@ -2839,6 +2810,35 @@
2839
2810
  * Note: [💞] Ignore a discrepancy between file name and entity name
2840
2811
  */
2841
2812
 
2813
+ /**
2814
+ * Async version of Array.forEach
2815
+ *
2816
+ * @param array - Array to iterate over
2817
+ * @param options - Options for the function
2818
+ * @param callbackfunction - Function to call for each item
2819
+ * @public exported from `@promptbook/utils`
2820
+ * @deprecated [🪂] Use queues instead
2821
+ */
2822
+ async function forEachAsync(array, options, callbackfunction) {
2823
+ const { maxParallelCount = Infinity } = options;
2824
+ let index = 0;
2825
+ let runningTasks = [];
2826
+ const tasks = [];
2827
+ for (const item of array) {
2828
+ const currentIndex = index++;
2829
+ const task = callbackfunction(item, currentIndex, array);
2830
+ tasks.push(task);
2831
+ runningTasks.push(task);
2832
+ /* not await */ Promise.resolve(task).then(() => {
2833
+ runningTasks = runningTasks.filter((t) => t !== task);
2834
+ });
2835
+ if (maxParallelCount < runningTasks.length) {
2836
+ await Promise.race(runningTasks);
2837
+ }
2838
+ }
2839
+ await Promise.all(tasks);
2840
+ }
2841
+
2842
2842
  /**
2843
2843
  * Function `addUsage` will add multiple usages into one
2844
2844
  *
@@ -4236,6 +4236,24 @@
4236
4236
  // encoding: 'utf-8',
4237
4237
  });
4238
4238
 
4239
+ /**
4240
+ * Converts a CSV string into an object
4241
+ *
4242
+ * Note: This is wrapper around `papaparse.parse()` with better autohealing
4243
+ *
4244
+ * @private - for now until `@promptbook/csv` is released
4245
+ */
4246
+ function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
4247
+ settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
4248
+ // Note: Autoheal invalid '\n' characters
4249
+ if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
4250
+ console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
4251
+ value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
4252
+ }
4253
+ const csv = papaparse.parse(value, settings);
4254
+ return csv;
4255
+ }
4256
+
4239
4257
  /**
4240
4258
  * Function to check if a string is valid CSV
4241
4259
  *
@@ -4258,31 +4276,13 @@
4258
4276
  }
4259
4277
  }
4260
4278
 
4261
- /**
4262
- * Converts a CSV string into an object
4263
- *
4264
- * Note: This is wrapper around `papaparse.parse()` with better autohealing
4265
- *
4266
- * @private - for now until `@promptbook/csv` is released
4267
- */
4268
- function csvParse(value /* <- TODO: string_csv */, settings, schema /* <- TODO: Make CSV Schemas */) {
4269
- settings = { ...settings, ...MANDATORY_CSV_SETTINGS };
4270
- // Note: Autoheal invalid '\n' characters
4271
- if (settings.newline && !settings.newline.includes('\r') && value.includes('\r')) {
4272
- console.warn('CSV string contains carriage return characters, but in the CSV settings the `newline` setting does not include them. Autohealing the CSV string.');
4273
- value = value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
4274
- }
4275
- const csv = papaparse.parse(value, settings);
4276
- return csv;
4277
- }
4278
-
4279
4279
  /**
4280
4280
  * Definition for CSV spreadsheet
4281
4281
  *
4282
4282
  * @public exported from `@promptbook/core`
4283
4283
  * <- TODO: [🏢] Export from package `@promptbook/csv`
4284
4284
  */
4285
- const CsvFormatDefinition = {
4285
+ const CsvFormatParser = {
4286
4286
  formatName: 'CSV',
4287
4287
  aliases: ['SPREADSHEET', 'TABLE'],
4288
4288
  isValid(value, settings, schema) {
@@ -4294,7 +4294,7 @@
4294
4294
  heal(value, settings, schema) {
4295
4295
  throw new Error('Not implemented');
4296
4296
  },
4297
- subvalueDefinitions: [
4297
+ subvalueParsers: [
4298
4298
  {
4299
4299
  subvalueName: 'ROW',
4300
4300
  async mapValues(value, outputParameterName, settings, mapCallback) {
@@ -4355,10 +4355,10 @@
4355
4355
  ],
4356
4356
  };
4357
4357
  /**
4358
- * TODO: [🍓] In `CsvFormatDefinition` implement simple `isValid`
4359
- * TODO: [🍓] In `CsvFormatDefinition` implement partial `canBeValid`
4360
- * TODO: [🍓] In `CsvFormatDefinition` implement `heal
4361
- * TODO: [🍓] In `CsvFormatDefinition` implement `subvalueDefinitions`
4358
+ * TODO: [🍓] In `CsvFormatParser` implement simple `isValid`
4359
+ * TODO: [🍓] In `CsvFormatParser` implement partial `canBeValid`
4360
+ * TODO: [🍓] In `CsvFormatParser` implement `heal
4361
+ * TODO: [🍓] In `CsvFormatParser` implement `subvalueParsers`
4362
4362
  * TODO: [🏢] Allow to expect something inside CSV objects and other formats
4363
4363
  */
4364
4364
 
@@ -4367,7 +4367,7 @@
4367
4367
  *
4368
4368
  * @private still in development [🏢]
4369
4369
  */
4370
- const JsonFormatDefinition = {
4370
+ const JsonFormatParser = {
4371
4371
  formatName: 'JSON',
4372
4372
  mimeType: 'application/json',
4373
4373
  isValid(value, settings, schema) {
@@ -4379,28 +4379,28 @@
4379
4379
  heal(value, settings, schema) {
4380
4380
  throw new Error('Not implemented');
4381
4381
  },
4382
- subvalueDefinitions: [],
4382
+ subvalueParsers: [],
4383
4383
  };
4384
4384
  /**
4385
4385
  * TODO: [🧠] Maybe propper instance of object
4386
4386
  * TODO: [0] Make string_serialized_json
4387
4387
  * TODO: [1] Make type for JSON Settings and Schema
4388
4388
  * TODO: [🧠] What to use for validating JSONs - JSON Schema, ZoD, typescript types/interfaces,...?
4389
- * TODO: [🍓] In `JsonFormatDefinition` implement simple `isValid`
4390
- * TODO: [🍓] In `JsonFormatDefinition` implement partial `canBeValid`
4391
- * TODO: [🍓] In `JsonFormatDefinition` implement `heal
4392
- * TODO: [🍓] In `JsonFormatDefinition` implement `subvalueDefinitions`
4389
+ * TODO: [🍓] In `JsonFormatParser` implement simple `isValid`
4390
+ * TODO: [🍓] In `JsonFormatParser` implement partial `canBeValid`
4391
+ * TODO: [🍓] In `JsonFormatParser` implement `heal
4392
+ * TODO: [🍓] In `JsonFormatParser` implement `subvalueParsers`
4393
4393
  * TODO: [🏢] Allow to expect something inside JSON objects and other formats
4394
4394
  */
4395
4395
 
4396
4396
  /**
4397
4397
  * Definition for any text - this will be always valid
4398
4398
  *
4399
- * Note: This is not useful for validation, but for splitting and mapping with `subvalueDefinitions`
4399
+ * Note: This is not useful for validation, but for splitting and mapping with `subvalueParsers`
4400
4400
  *
4401
4401
  * @public exported from `@promptbook/core`
4402
4402
  */
4403
- const TextFormatDefinition = {
4403
+ const TextFormatParser = {
4404
4404
  formatName: 'TEXT',
4405
4405
  isValid(value) {
4406
4406
  return typeof value === 'string';
@@ -4409,9 +4409,9 @@
4409
4409
  return typeof partialValue === 'string';
4410
4410
  },
4411
4411
  heal() {
4412
- throw new UnexpectedError('It does not make sense to call `TextFormatDefinition.heal`');
4412
+ throw new UnexpectedError('It does not make sense to call `TextFormatParser.heal`');
4413
4413
  },
4414
- subvalueDefinitions: [
4414
+ subvalueParsers: [
4415
4415
  {
4416
4416
  subvalueName: 'LINE',
4417
4417
  async mapValues(value, outputParameterName, settings, mapCallback) {
@@ -4431,10 +4431,10 @@
4431
4431
  /**
4432
4432
  * TODO: [1] Make type for XML Text and Schema
4433
4433
  * TODO: [🧠][🤠] Here should be all words, characters, lines, paragraphs, pages available as subvalues
4434
- * TODO: [🍓] In `TextFormatDefinition` implement simple `isValid`
4435
- * TODO: [🍓] In `TextFormatDefinition` implement partial `canBeValid`
4436
- * TODO: [🍓] In `TextFormatDefinition` implement `heal
4437
- * TODO: [🍓] In `TextFormatDefinition` implement `subvalueDefinitions`
4434
+ * TODO: [🍓] In `TextFormatParser` implement simple `isValid`
4435
+ * TODO: [🍓] In `TextFormatParser` implement partial `canBeValid`
4436
+ * TODO: [🍓] In `TextFormatParser` implement `heal
4437
+ * TODO: [🍓] In `TextFormatParser` implement `subvalueParsers`
4438
4438
  * TODO: [🏢] Allow to expect something inside each item of list and other formats
4439
4439
  */
4440
4440
 
@@ -4467,7 +4467,7 @@
4467
4467
  *
4468
4468
  * @private still in development [🏢]
4469
4469
  */
4470
- const XmlFormatDefinition = {
4470
+ const XmlFormatParser = {
4471
4471
  formatName: 'XML',
4472
4472
  mimeType: 'application/xml',
4473
4473
  isValid(value, settings, schema) {
@@ -4479,17 +4479,17 @@
4479
4479
  heal(value, settings, schema) {
4480
4480
  throw new Error('Not implemented');
4481
4481
  },
4482
- subvalueDefinitions: [],
4482
+ subvalueParsers: [],
4483
4483
  };
4484
4484
  /**
4485
4485
  * TODO: [🧠] Maybe propper instance of object
4486
4486
  * TODO: [0] Make string_serialized_xml
4487
4487
  * TODO: [1] Make type for XML Settings and Schema
4488
4488
  * TODO: [🧠] What to use for validating XMLs - XSD,...
4489
- * TODO: [🍓] In `XmlFormatDefinition` implement simple `isValid`
4490
- * TODO: [🍓] In `XmlFormatDefinition` implement partial `canBeValid`
4491
- * TODO: [🍓] In `XmlFormatDefinition` implement `heal
4492
- * TODO: [🍓] In `XmlFormatDefinition` implement `subvalueDefinitions`
4489
+ * TODO: [🍓] In `XmlFormatParser` implement simple `isValid`
4490
+ * TODO: [🍓] In `XmlFormatParser` implement partial `canBeValid`
4491
+ * TODO: [🍓] In `XmlFormatParser` implement `heal
4492
+ * TODO: [🍓] In `XmlFormatParser` implement `subvalueParsers`
4493
4493
  * TODO: [🏢] Allow to expect something inside XML and other formats
4494
4494
  */
4495
4495
 
@@ -4498,12 +4498,7 @@
4498
4498
  *
4499
4499
  * @private internal index of `...` <- TODO [🏢]
4500
4500
  */
4501
- const FORMAT_DEFINITIONS = [
4502
- JsonFormatDefinition,
4503
- XmlFormatDefinition,
4504
- TextFormatDefinition,
4505
- CsvFormatDefinition,
4506
- ];
4501
+ const FORMAT_DEFINITIONS = [JsonFormatParser, XmlFormatParser, TextFormatParser, CsvFormatParser];
4507
4502
  /**
4508
4503
  * Note: [💞] Ignore a discrepancy between file name and entity name
4509
4504
  */
@@ -4673,7 +4668,7 @@
4673
4668
  }
4674
4669
  /**
4675
4670
  * TODO: Add some auto-healing logic + extract YAML, JSON5, TOML, etc.
4676
- * TODO: [🏢] Make this logic part of `JsonFormatDefinition` or `isValidJsonString`
4671
+ * TODO: [🏢] Make this logic part of `JsonFormatParser` or `isValidJsonString`
4677
4672
  */
4678
4673
 
4679
4674
  /**
@@ -4875,7 +4870,7 @@
4875
4870
  PAGES: countPages,
4876
4871
  };
4877
4872
  /**
4878
- * TODO: [🧠][🤠] This should be probbably as part of `TextFormatDefinition`
4873
+ * TODO: [🧠][🤠] This should be probbably as part of `TextFormatParser`
4879
4874
  * Note: [💞] Ignore a discrepancy between file name and entity name
4880
4875
  */
4881
4876
 
@@ -4903,7 +4898,7 @@
4903
4898
  }
4904
4899
  /**
4905
4900
  * TODO: [💝] Unite object for expecting amount and format
4906
- * TODO: [🧠][🤠] This should be part of `TextFormatDefinition`
4901
+ * TODO: [🧠][🤠] This should be part of `TextFormatParser`
4907
4902
  * Note: [💝] and [🤠] are interconnected together
4908
4903
  */
4909
4904
 
@@ -5131,7 +5126,7 @@
5131
5126
  if (task.format) {
5132
5127
  if (task.format === 'JSON') {
5133
5128
  if (!isValidJsonString($ongoingTaskResult.$resultString || '')) {
5134
- // TODO: [🏢] Do more universally via `FormatDefinition`
5129
+ // TODO: [🏢] Do more universally via `FormatParser`
5135
5130
  try {
5136
5131
  $ongoingTaskResult.$resultString = extractJsonBlock($ongoingTaskResult.$resultString || '');
5137
5132
  }
@@ -5269,16 +5264,16 @@
5269
5264
  ${block(pipelineIdentification)}
5270
5265
  `));
5271
5266
  }
5272
- const subvalueDefinition = formatDefinition.subvalueDefinitions.find((subvalueDefinition) => [subvalueDefinition.subvalueName, ...(subvalueDefinition.aliases || [])].includes(task.foreach.subformatName));
5273
- if (subvalueDefinition === undefined) {
5267
+ const subvalueParser = formatDefinition.subvalueParsers.find((subvalueParser) => [subvalueParser.subvalueName, ...(subvalueParser.aliases || [])].includes(task.foreach.subformatName));
5268
+ if (subvalueParser === undefined) {
5274
5269
  throw new UnexpectedError(
5275
5270
  // <- TODO: [🧠][🧐] Should be formats fixed per promptbook version or behave as plugins (=> change UnexpectedError)
5276
5271
  spaceTrim__default["default"]((block) => `
5277
5272
  Unsupported subformat name "${task.foreach.subformatName}" for format "${task.foreach.formatName}"
5278
5273
 
5279
5274
  Available subformat names for format "${formatDefinition.formatName}":
5280
- ${block(formatDefinition.subvalueDefinitions
5281
- .map((subvalueDefinition) => subvalueDefinition.subvalueName)
5275
+ ${block(formatDefinition.subvalueParsers
5276
+ .map((subvalueParser) => subvalueParser.subvalueName)
5282
5277
  .map((subvalueName) => `- ${subvalueName}`)
5283
5278
  .join('\n'))}
5284
5279
 
@@ -5292,7 +5287,7 @@
5292
5287
  formatSettings = csvSettings;
5293
5288
  // <- TODO: [🤹‍♂️] More universal, make simmilar pattern for other formats for example \n vs \r\n in text
5294
5289
  }
5295
- const resultString = await subvalueDefinition.mapValues(parameterValue, task.foreach.outputSubparameterName, formatSettings, async (subparameters, index) => {
5290
+ const resultString = await subvalueParser.mapValues(parameterValue, task.foreach.outputSubparameterName, formatSettings, async (subparameters, index) => {
5296
5291
  let mappedParameters;
5297
5292
  // TODO: [🤹‍♂️][🪂] Limit to N concurrent executions
5298
5293
  // TODO: When done [🐚] Report progress also for each subvalue here
@@ -5354,6 +5349,27 @@
5354
5349
  return RESERVED_PARAMETER_MISSING_VALUE /* <- TODO: [♨] Implement */;
5355
5350
  }
5356
5351
 
5352
+ /**
5353
+ * Computes the cosine similarity between two embedding vectors
5354
+ *
5355
+ * Note: This is helping function for RAG (retrieval-augmented generation)
5356
+ *
5357
+ * @param embeddingVector1
5358
+ * @param embeddingVector2
5359
+ * @returns Cosine similarity between the two vectors
5360
+ *
5361
+ * @public exported from `@promptbook/core`
5362
+ */
5363
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5364
+ if (embeddingVector1.length !== embeddingVector2.length) {
5365
+ throw new TypeError('Embedding vectors must have the same length');
5366
+ }
5367
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5368
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5369
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5370
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5371
+ }
5372
+
5357
5373
  /**
5358
5374
  * @@@
5359
5375
  *
@@ -5380,7 +5396,7 @@
5380
5396
  },
5381
5397
  content: task.content,
5382
5398
  parameters: {
5383
- /* !!!!!!!! */
5399
+ /* !!!! */
5384
5400
  },
5385
5401
  };
5386
5402
  const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
@@ -5415,16 +5431,6 @@
5415
5431
  return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5416
5432
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5417
5433
  }
5418
- // TODO: !!!!!! Annotate + to new file
5419
- function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5420
- if (embeddingVector1.length !== embeddingVector2.length) {
5421
- throw new TypeError('Embedding vectors must have the same length');
5422
- }
5423
- const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5424
- const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5425
- const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5426
- return 1 - dotProduct / (magnitude1 * magnitude2);
5427
- }
5428
5434
  /**
5429
5435
  * TODO: !!!! Verify if this is working
5430
5436
  * TODO: [♨] Implement Better - use keyword search
@@ -5927,6 +5933,22 @@
5927
5933
  cacheDirname,
5928
5934
  intermediateFilesStrategy,
5929
5935
  isAutoInstalled,
5936
+ }).catch((error) => {
5937
+ assertsError(error);
5938
+ return exportJson({
5939
+ name: 'pipelineExecutorResult',
5940
+ message: `Unuccessful PipelineExecutorResult, last catch`,
5941
+ order: [],
5942
+ value: {
5943
+ isSuccessful: false,
5944
+ errors: [serializeError(error)],
5945
+ warnings: [],
5946
+ usage: UNCERTAIN_USAGE,
5947
+ executionReport: null,
5948
+ outputParameters: {},
5949
+ preparedPipeline,
5950
+ },
5951
+ });
5930
5952
  });
5931
5953
  };
5932
5954
  const pipelineExecutor = (inputParameters) => createTask({