@promptbook/core 0.44.0-8 โ†’ 0.44.0

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 CHANGED
@@ -1,4 +1,4 @@
1
- import spaceTrim from 'spacetrim';
1
+ import { spaceTrim } from 'spacetrim';
2
2
  import 'prettier';
3
3
  import 'prettier/parser-html';
4
4
  import 'moment';
@@ -647,6 +647,10 @@ function isPassingExpectations(expectations, value) {
647
647
  * The maximum number of iterations for a loops
648
648
  */
649
649
  var LOOP_LIMIT = 1000;
650
+ /**
651
+ * The maximum number of iterations for a loops which adds characters one by one
652
+ */
653
+ var CHARACTER_LOOP_LIMIT = 100000;
650
654
 
651
655
  /**
652
656
  * This error occurs during the parameter replacement in the template
@@ -1157,7 +1161,7 @@ function parsingMarkdownStructureToMarkdownStructure(parsingMarkdownStructure) {
1157
1161
  /**
1158
1162
  * The version of the Promptbook library
1159
1163
  */
1160
- var PROMPTBOOK_VERSION = '0.44.0-7';
1164
+ var PROMPTBOOK_VERSION = '0.44.0-23';
1161
1165
 
1162
1166
  /**
1163
1167
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -1982,6 +1986,7 @@ function createPromptbookExecutor(options) {
1982
1986
  return __awaiter(this, void 0, void 0, function () {
1983
1987
  var name, title, priority, prompt, chatThread, completionResult, result, resultString, expectError, scriptExecutionErrors, maxAttempts, jokers, attempt, isJokerAttempt, joker, _a, _b, _c, _d, scriptTools, error_2, e_2_1, _e, _f, functionName, postprocessingError, _g, _h, scriptTools, error_3, e_3_1, e_4_1, error_4;
1984
1988
  var e_2, _j, e_4, _k, e_3, _l, _m;
1989
+ var _this = this;
1985
1990
  return __generator(this, function (_o) {
1986
1991
  switch (_o.label) {
1987
1992
  case 0:
@@ -2051,6 +2056,67 @@ function createPromptbookExecutor(options) {
2051
2056
  content: replaceParameters(currentTemplate.content, parametersToPass) /* <- [2] */,
2052
2057
  modelRequirements: currentTemplate.modelRequirements,
2053
2058
  expectations: currentTemplate.expectations,
2059
+ postprocessing: (currentTemplate.postprocessing || []).map(function (functionName) { return function (result) { return __awaiter(_this, void 0, void 0, function () {
2060
+ var errors, _a, _b, scriptTools, error_5, e_5_1;
2061
+ var e_5, _c;
2062
+ return __generator(this, function (_d) {
2063
+ switch (_d.label) {
2064
+ case 0:
2065
+ errors = [];
2066
+ _d.label = 1;
2067
+ case 1:
2068
+ _d.trys.push([1, 8, 9, 10]);
2069
+ _a = __values(tools.script), _b = _a.next();
2070
+ _d.label = 2;
2071
+ case 2:
2072
+ if (!!_b.done) return [3 /*break*/, 7];
2073
+ scriptTools = _b.value;
2074
+ _d.label = 3;
2075
+ case 3:
2076
+ _d.trys.push([3, 5, , 6]);
2077
+ return [4 /*yield*/, scriptTools.execute({
2078
+ scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2079
+ script: "".concat(functionName, "(result)"),
2080
+ parameters: {
2081
+ result: result || '',
2082
+ // Note: No ...parametersToPass, because working with result only
2083
+ },
2084
+ })];
2085
+ case 4: return [2 /*return*/, _d.sent()];
2086
+ case 5:
2087
+ error_5 = _d.sent();
2088
+ if (!(error_5 instanceof Error)) {
2089
+ throw error_5;
2090
+ }
2091
+ errors.push(error_5);
2092
+ return [3 /*break*/, 6];
2093
+ case 6:
2094
+ _b = _a.next();
2095
+ return [3 /*break*/, 2];
2096
+ case 7: return [3 /*break*/, 10];
2097
+ case 8:
2098
+ e_5_1 = _d.sent();
2099
+ e_5 = { error: e_5_1 };
2100
+ return [3 /*break*/, 10];
2101
+ case 9:
2102
+ try {
2103
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2104
+ }
2105
+ finally { if (e_5) throw e_5.error; }
2106
+ return [7 /*endfinally*/];
2107
+ case 10:
2108
+ if (errors.length === 0) {
2109
+ throw new PromptbookExecutionError('Postprocessing in NaturalExecutionTools failed because no ScriptExecutionTools were provided');
2110
+ }
2111
+ else if (errors.length === 1) {
2112
+ throw errors[0];
2113
+ }
2114
+ else {
2115
+ throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Postprocessing in NaturalExecutionTools failed ".concat(errors.length, "x\n\n ").concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
2116
+ }
2117
+ }
2118
+ });
2119
+ }); }; }),
2054
2120
  };
2055
2121
  _b = currentTemplate.modelRequirements.modelVariant;
2056
2122
  switch (_b) {
@@ -2123,7 +2189,7 @@ function createPromptbookExecutor(options) {
2123
2189
  finally { if (e_2) throw e_2.error; }
2124
2190
  return [7 /*endfinally*/];
2125
2191
  case 23:
2126
- if (resultString) {
2192
+ if (resultString !== null) {
2127
2193
  return [3 /*break*/, 27];
2128
2194
  }
2129
2195
  if (scriptExecutionErrors.length === 1) {
@@ -2176,7 +2242,10 @@ function createPromptbookExecutor(options) {
2176
2242
  return [4 /*yield*/, scriptTools.execute({
2177
2243
  scriptLanguage: "javascript" /* <- TODO: Try it in each languages; In future allow postprocessing with arbitrary combination of languages to combine */,
2178
2244
  script: "".concat(functionName, "(resultString)"),
2179
- parameters: __assign(__assign({}, parametersToPass), { resultString: resultString || '' }),
2245
+ parameters: {
2246
+ resultString: resultString || '',
2247
+ // Note: No ...parametersToPass, because working with result only
2248
+ },
2180
2249
  })];
2181
2250
  case 33:
2182
2251
  resultString = _o.sent();
@@ -2267,7 +2336,7 @@ function createPromptbookExecutor(options) {
2267
2336
  return [7 /*endfinally*/];
2268
2337
  case 47:
2269
2338
  if (expectError !== null && attempt === maxAttempts - 1) {
2270
- throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\n\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n "); }));
2339
+ throw new PromptbookExecutionError(spaceTrim(function (block) { return "\n Natural execution failed ".concat(maxExecutionAttempts, "x\n\n ---\n Last error ").concat((expectError === null || expectError === void 0 ? void 0 : expectError.name) || '', ":\n ").concat(block((expectError === null || expectError === void 0 ? void 0 : expectError.message) || ''), "\n\n Last result:\n ").concat(resultString, "\n ---\n "); }));
2271
2340
  }
2272
2341
  _o.label = 48;
2273
2342
  case 48:
@@ -2499,29 +2568,73 @@ var MockedEchoNaturalExecutionTools = /** @class */ (function () {
2499
2568
  /**
2500
2569
  * Gets the expectations and creates a fake text that meets the expectations
2501
2570
  *
2571
+ * Note: You can provide postprocessing functions to modify the text before checking the expectations
2572
+ * The result will be the text BEFORE the postprocessing
2573
+ *
2502
2574
  * @private internal util for MockedFackedNaturalExecutionTools
2503
2575
  */
2504
- function $fakeTextToExpectations(expectations) {
2505
- var lorem = new LoremIpsum({
2506
- wordsPerSentence: { min: 5, max: 15 },
2507
- sentencesPerParagraph: { min: 5, max: 15 },
2576
+ function $fakeTextToExpectations(expectations, postprocessing) {
2577
+ return __awaiter(this, void 0, void 0, function () {
2578
+ var lorem, loremText, text, loopLimit, textToCheck, _a, _b, func, e_1_1;
2579
+ var e_1, _c;
2580
+ return __generator(this, function (_d) {
2581
+ switch (_d.label) {
2582
+ case 0:
2583
+ lorem = new LoremIpsum({
2584
+ wordsPerSentence: { min: 5, max: 15 },
2585
+ sentencesPerParagraph: { min: 5, max: 15 },
2586
+ });
2587
+ loremText = '';
2588
+ text = '';
2589
+ loopLimit = CHARACTER_LOOP_LIMIT;
2590
+ _d.label = 1;
2591
+ case 1:
2592
+ if (!(loopLimit-- > 0)) return [3 /*break*/, 11];
2593
+ textToCheck = text;
2594
+ _d.label = 2;
2595
+ case 2:
2596
+ _d.trys.push([2, 7, 8, 9]);
2597
+ _a = (e_1 = void 0, __values(postprocessing || [])), _b = _a.next();
2598
+ _d.label = 3;
2599
+ case 3:
2600
+ if (!!_b.done) return [3 /*break*/, 6];
2601
+ func = _b.value;
2602
+ return [4 /*yield*/, func(textToCheck)];
2603
+ case 4:
2604
+ textToCheck = _d.sent();
2605
+ _d.label = 5;
2606
+ case 5:
2607
+ _b = _a.next();
2608
+ return [3 /*break*/, 3];
2609
+ case 6: return [3 /*break*/, 9];
2610
+ case 7:
2611
+ e_1_1 = _d.sent();
2612
+ e_1 = { error: e_1_1 };
2613
+ return [3 /*break*/, 9];
2614
+ case 8:
2615
+ try {
2616
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2617
+ }
2618
+ finally { if (e_1) throw e_1.error; }
2619
+ return [7 /*endfinally*/];
2620
+ case 9:
2621
+ if (isPassingExpectations(expectations, textToCheck)) {
2622
+ return [2 /*return*/, text]; // <- Note: Returning the text because the postprocessing
2623
+ }
2624
+ if (loremText === '') {
2625
+ loremText = lorem.generateParagraphs(1) + '\n\n';
2626
+ }
2627
+ text += loremText.substring(0, 1);
2628
+ loremText = loremText.substring(1);
2629
+ _d.label = 10;
2630
+ case 10: return [3 /*break*/, 1];
2631
+ case 11: throw new Error(spaceTrim(function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
2632
+ }
2633
+ });
2508
2634
  });
2509
- var loremText = '';
2510
- var text = '';
2511
- for (var loopLimit = LOOP_LIMIT; loopLimit-- > 0;) {
2512
- if (isPassingExpectations(expectations, text)) {
2513
- return text;
2514
- }
2515
- if (loremText === '') {
2516
- loremText = lorem.generateParagraphs(1) + '\n\n';
2517
- }
2518
- text += loremText.substring(0, 1);
2519
- loremText = loremText.substring(1);
2520
- }
2521
- throw new Error(spaceTrim(function (block) { return "\n Can not generate fake text to met the expectations\n\n Loop limit reached\n The expectations:\n ".concat(block(JSON.stringify(expectations, null, 4)), "\n\n The draft text:\n ").concat(block(text), "\n\n "); }));
2522
2635
  }
2523
2636
  /**
2524
- * TODO: Implement better
2637
+ * TODO: Implement better - create FakeLLM from this
2525
2638
  * TODO: [๐Ÿ’] Unite object for expecting amount and format - use here also a format
2526
2639
  */
2527
2640
 
@@ -2537,29 +2650,40 @@ var MockedFackedNaturalExecutionTools = /** @class */ (function () {
2537
2650
  */
2538
2651
  MockedFackedNaturalExecutionTools.prototype.gptChat = function (prompt) {
2539
2652
  return __awaiter(this, void 0, void 0, function () {
2653
+ var content, result;
2540
2654
  return __generator(this, function (_a) {
2541
- if (this.options.isVerbose) {
2542
- console.info('๐Ÿ’ฌ Mocked faked call');
2655
+ switch (_a.label) {
2656
+ case 0:
2657
+ if (this.options.isVerbose) {
2658
+ console.info('๐Ÿ’ฌ Mocked faked prompt', prompt);
2659
+ }
2660
+ return [4 /*yield*/, $fakeTextToExpectations(prompt.expectations || {
2661
+ sentences: { min: 1, max: 1 },
2662
+ }, prompt.postprocessing)];
2663
+ case 1:
2664
+ content = _a.sent();
2665
+ result = {
2666
+ content: content,
2667
+ model: 'mocked-facked',
2668
+ timing: {
2669
+ start: getCurrentIsoDate(),
2670
+ complete: getCurrentIsoDate(),
2671
+ },
2672
+ usage: {
2673
+ price: 0,
2674
+ inputTokens: 0,
2675
+ outputTokens: 0,
2676
+ },
2677
+ rawResponse: {
2678
+ note: 'This is mocked echo',
2679
+ },
2680
+ // <- [๐Ÿคนโ€โ™‚๏ธ]
2681
+ };
2682
+ if (this.options.isVerbose) {
2683
+ console.info('๐Ÿ’ฌ Mocked faked result', result);
2684
+ }
2685
+ return [2 /*return*/, result];
2543
2686
  }
2544
- return [2 /*return*/, {
2545
- content: $fakeTextToExpectations(prompt.expectations || {
2546
- sentences: { min: 1, max: 1 },
2547
- }),
2548
- model: 'mocked-facked',
2549
- timing: {
2550
- start: getCurrentIsoDate(),
2551
- complete: getCurrentIsoDate(),
2552
- },
2553
- usage: {
2554
- price: 0,
2555
- inputTokens: 0,
2556
- outputTokens: 0,
2557
- },
2558
- rawResponse: {
2559
- note: 'This is mocked echo',
2560
- },
2561
- // <- [๐Ÿคนโ€โ™‚๏ธ]
2562
- }];
2563
2687
  });
2564
2688
  });
2565
2689
  };