@promptbook/openai 0.78.0-0 → 0.78.2

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/umd/index.umd.js CHANGED
@@ -22,7 +22,7 @@
22
22
  *
23
23
  * @see https://github.com/webgptorg/promptbook
24
24
  */
25
- var PROMPTBOOK_ENGINE_VERSION = '0.77.1';
25
+ var PROMPTBOOK_ENGINE_VERSION = '0.78.1';
26
26
  /**
27
27
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
28
28
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -299,6 +299,103 @@
299
299
  return new Date().toISOString();
300
300
  }
301
301
 
302
+ /**
303
+ * Name for the Promptbook
304
+ *
305
+ * TODO: [🗽] Unite branding and make single place for it
306
+ *
307
+ * @public exported from `@promptbook/core`
308
+ */
309
+ var NAME = "Promptbook";
310
+ /**
311
+ * Email of the responsible person
312
+ *
313
+ * @public exported from `@promptbook/core`
314
+ */
315
+ var ADMIN_EMAIL = 'me@pavolhejny.com';
316
+ /**
317
+ * Name of the responsible person for the Promptbook on GitHub
318
+ *
319
+ * @public exported from `@promptbook/core`
320
+ */
321
+ var ADMIN_GITHUB_NAME = 'hejny';
322
+ // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
323
+ /**
324
+ * The maximum number of iterations for a loops
325
+ *
326
+ * @private within the repository - too low-level in comparison with other `MAX_...`
327
+ */
328
+ var LOOP_LIMIT = 1000;
329
+ /**
330
+ * Nonce which is used for replacing things in strings
331
+ *
332
+ * @private within the repository
333
+ */
334
+ var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
335
+ /**
336
+ * @@@
337
+ *
338
+ * @private within the repository
339
+ */
340
+ var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
341
+ /**
342
+ * @@@
343
+ *
344
+ * @private within the repository
345
+ */
346
+ var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
347
+ // <- TODO: [🧜‍♂️]
348
+ /**
349
+ * @@@
350
+ *
351
+ * @public exported from `@promptbook/core`
352
+ */
353
+ Object.freeze({
354
+ delimiter: ',',
355
+ quoteChar: '"',
356
+ newline: '\n',
357
+ skipEmptyLines: true,
358
+ });
359
+ /**
360
+ * TODO: Extract `constants.ts` from `config.ts`
361
+ * Note: [💞] Ignore a discrepancy between file name and entity name
362
+ * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
363
+ */
364
+
365
+ /**
366
+ * This error type indicates that some limit was reached
367
+ *
368
+ * @public exported from `@promptbook/core`
369
+ */
370
+ var LimitReachedError = /** @class */ (function (_super) {
371
+ __extends(LimitReachedError, _super);
372
+ function LimitReachedError(message) {
373
+ var _this = _super.call(this, message) || this;
374
+ _this.name = 'LimitReachedError';
375
+ Object.setPrototypeOf(_this, LimitReachedError.prototype);
376
+ return _this;
377
+ }
378
+ return LimitReachedError;
379
+ }(Error));
380
+
381
+ /**
382
+ * Make error report URL for the given error
383
+ *
384
+ * @private !!!!!!
385
+ */
386
+ function getErrorReportUrl(error) {
387
+ var report = {
388
+ title: "\uD83D\uDC1C Error report from ".concat(NAME),
389
+ body: spaceTrim__default["default"](function (block) { return "\n\n\n `".concat(error.name || 'Error', "` has occurred in the [").concat(NAME, "], please look into it @").concat(ADMIN_GITHUB_NAME, ".\n\n ```\n ").concat(block(error.message || '(no error message)'), "\n ```\n\n\n ## More info:\n\n - **Promptbook engine version:** ").concat(PROMPTBOOK_ENGINE_VERSION, "\n - **Book language version:** ").concat(BOOK_LANGUAGE_VERSION, "\n - **Time:** ").concat(new Date().toISOString(), "\n\n <details>\n <summary>Stack trace:</summary>\n\n ## Stack trace:\n\n ```stacktrace\n ").concat(block(error.stack || '(empty)'), "\n ```\n </details>\n\n "); }),
390
+ };
391
+ var reportUrl = new URL("https://github.com/webgptorg/promptbook/issues/new");
392
+ reportUrl.searchParams.set('labels', 'bug');
393
+ reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
394
+ reportUrl.searchParams.set('title', report.title);
395
+ reportUrl.searchParams.set('body', report.body);
396
+ return reportUrl;
397
+ }
398
+
302
399
  /**
303
400
  * This error type indicates that the error should not happen and its last check before crashing with some other error
304
401
  *
@@ -307,7 +404,7 @@
307
404
  var UnexpectedError = /** @class */ (function (_super) {
308
405
  __extends(UnexpectedError, _super);
309
406
  function UnexpectedError(message) {
310
- var _this = _super.call(this, spaceTrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the pipeline collection\n\n Please report issue:\n https://github.com/webgptorg/promptbook/issues\n\n Or contact us on me@pavolhejny.com\n\n "); })) || this;
407
+ var _this = _super.call(this, spaceTrim.spaceTrim(function (block) { return "\n ".concat(block(message), "\n\n Note: This error should not happen.\n It's probbably a bug in the pipeline collection\n\n Please report issue:\n ").concat(block(getErrorReportUrl(new Error(message)).href), "\n\n Or contact us on ").concat(ADMIN_EMAIL, "\n\n "); })) || this;
311
408
  _this.name = 'UnexpectedError';
312
409
  Object.setPrototypeOf(_this, UnexpectedError.prototype);
313
410
  return _this;
@@ -315,6 +412,85 @@
315
412
  return UnexpectedError;
316
413
  }(Error));
317
414
 
415
+ /**
416
+ * Replaces parameters in template with values from parameters object
417
+ *
418
+ * @param template the template with parameters in {curly} braces
419
+ * @param parameters the object with parameters
420
+ * @returns the template with replaced parameters
421
+ * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
422
+ * @public exported from `@promptbook/utils`
423
+ */
424
+ function replaceParameters(template, parameters) {
425
+ var e_1, _a;
426
+ try {
427
+ for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
428
+ var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
429
+ if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
430
+ throw new UnexpectedError("Parameter `{".concat(parameterName, "}` has missing value"));
431
+ }
432
+ else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
433
+ // TODO: [🍵]
434
+ throw new UnexpectedError("Parameter `{".concat(parameterName, "}` is restricted to use"));
435
+ }
436
+ }
437
+ }
438
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
439
+ finally {
440
+ try {
441
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
442
+ }
443
+ finally { if (e_1) throw e_1.error; }
444
+ }
445
+ var replacedTemplates = template;
446
+ var match;
447
+ var loopLimit = LOOP_LIMIT;
448
+ var _loop_1 = function () {
449
+ if (loopLimit-- < 0) {
450
+ throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
451
+ }
452
+ var precol = match.groups.precol;
453
+ var parameterName = match.groups.parameterName;
454
+ if (parameterName === '') {
455
+ return "continue";
456
+ }
457
+ if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
458
+ throw new PipelineExecutionError('Parameter is already opened or not closed');
459
+ }
460
+ if (parameters[parameterName] === undefined) {
461
+ throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
462
+ }
463
+ var parameterValue = parameters[parameterName];
464
+ if (parameterValue === undefined) {
465
+ throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
466
+ }
467
+ parameterValue = parameterValue.toString();
468
+ if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
469
+ parameterValue = parameterValue
470
+ .split('\n')
471
+ .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
472
+ .join('\n');
473
+ }
474
+ replacedTemplates =
475
+ replacedTemplates.substring(0, match.index + precol.length) +
476
+ parameterValue +
477
+ replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
478
+ };
479
+ while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
480
+ .exec(replacedTemplates))) {
481
+ _loop_1();
482
+ }
483
+ // [💫] Check if there are parameters that are not closed properly
484
+ if (/{\w+$/.test(replacedTemplates)) {
485
+ throw new PipelineExecutionError('Parameter is not closed');
486
+ }
487
+ // [💫] Check if there are parameters that are not opened properly
488
+ if (/^\w+}/.test(replacedTemplates)) {
489
+ throw new PipelineExecutionError('Parameter is not opened');
490
+ }
491
+ return replacedTemplates;
492
+ }
493
+
318
494
  /**
319
495
  * Checks if the value is [🚉] serializable as JSON
320
496
  * If not, throws an UnexpectedError with a rich error message and tracking
@@ -458,160 +634,6 @@
458
634
  * TODO: [🧠] Is there a way how to meaningfully test this utility
459
635
  */
460
636
 
461
- // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
462
- /**
463
- * The maximum number of iterations for a loops
464
- *
465
- * @private within the repository - too low-level in comparison with other `MAX_...`
466
- */
467
- var LOOP_LIMIT = 1000;
468
- /**
469
- * Nonce which is used for replacing things in strings
470
- *
471
- * @private within the repository
472
- */
473
- var REPLACING_NONCE = 'u$k42k%!V2zo34w7Fu#@QUHYPW';
474
- /**
475
- * The names of the parameters that are reserved for special purposes
476
- *
477
- * @public exported from `@promptbook/core`
478
- */
479
- $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
480
- 'content',
481
- 'context',
482
- 'knowledge',
483
- 'examples',
484
- 'modelName',
485
- 'currentDate',
486
- // <- TODO: list here all command names
487
- // <- TODO: Add more like 'date', 'modelName',...
488
- // <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
489
- ]);
490
- /**
491
- * @@@
492
- *
493
- * @private within the repository
494
- */
495
- var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
496
- /**
497
- * @@@
498
- *
499
- * @private within the repository
500
- */
501
- var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
502
- // <- TODO: [🧜‍♂️]
503
- /**
504
- * @@@
505
- *
506
- * @public exported from `@promptbook/core`
507
- */
508
- Object.freeze({
509
- delimiter: ',',
510
- quoteChar: '"',
511
- newline: '\n',
512
- skipEmptyLines: true,
513
- });
514
- /**
515
- * TODO: Extract `constants.ts` from `config.ts`
516
- * Note: [💞] Ignore a discrepancy between file name and entity name
517
- * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
518
- */
519
-
520
- /**
521
- * This error type indicates that some limit was reached
522
- *
523
- * @public exported from `@promptbook/core`
524
- */
525
- var LimitReachedError = /** @class */ (function (_super) {
526
- __extends(LimitReachedError, _super);
527
- function LimitReachedError(message) {
528
- var _this = _super.call(this, message) || this;
529
- _this.name = 'LimitReachedError';
530
- Object.setPrototypeOf(_this, LimitReachedError.prototype);
531
- return _this;
532
- }
533
- return LimitReachedError;
534
- }(Error));
535
-
536
- /**
537
- * Replaces parameters in template with values from parameters object
538
- *
539
- * @param template the template with parameters in {curly} braces
540
- * @param parameters the object with parameters
541
- * @returns the template with replaced parameters
542
- * @throws {PipelineExecutionError} if parameter is not defined, not closed, or not opened
543
- * @public exported from `@promptbook/utils`
544
- */
545
- function replaceParameters(template, parameters) {
546
- var e_1, _a;
547
- try {
548
- for (var _b = __values(Object.entries(parameters)), _c = _b.next(); !_c.done; _c = _b.next()) {
549
- var _d = __read(_c.value, 2), parameterName = _d[0], parameterValue = _d[1];
550
- if (parameterValue === RESERVED_PARAMETER_MISSING_VALUE) {
551
- throw new UnexpectedError("Parameter `{".concat(parameterName, "}` has missing value"));
552
- }
553
- else if (parameterValue === RESERVED_PARAMETER_RESTRICTED) {
554
- // TODO: [🍵]
555
- throw new UnexpectedError("Parameter `{".concat(parameterName, "}` is restricted to use"));
556
- }
557
- }
558
- }
559
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
560
- finally {
561
- try {
562
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
563
- }
564
- finally { if (e_1) throw e_1.error; }
565
- }
566
- var replacedTemplates = template;
567
- var match;
568
- var loopLimit = LOOP_LIMIT;
569
- var _loop_1 = function () {
570
- if (loopLimit-- < 0) {
571
- throw new LimitReachedError('Loop limit reached during parameters replacement in `replaceParameters`');
572
- }
573
- var precol = match.groups.precol;
574
- var parameterName = match.groups.parameterName;
575
- if (parameterName === '') {
576
- return "continue";
577
- }
578
- if (parameterName.indexOf('{') !== -1 || parameterName.indexOf('}') !== -1) {
579
- throw new PipelineExecutionError('Parameter is already opened or not closed');
580
- }
581
- if (parameters[parameterName] === undefined) {
582
- throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
583
- }
584
- var parameterValue = parameters[parameterName];
585
- if (parameterValue === undefined) {
586
- throw new PipelineExecutionError("Parameter `{".concat(parameterName, "}` is not defined"));
587
- }
588
- parameterValue = parameterValue.toString();
589
- if (parameterValue.includes('\n') && /^\s*\W{0,3}\s*$/.test(precol)) {
590
- parameterValue = parameterValue
591
- .split('\n')
592
- .map(function (line, index) { return (index === 0 ? line : "".concat(precol).concat(line)); })
593
- .join('\n');
594
- }
595
- replacedTemplates =
596
- replacedTemplates.substring(0, match.index + precol.length) +
597
- parameterValue +
598
- replacedTemplates.substring(match.index + precol.length + parameterName.length + 2);
599
- };
600
- while ((match = /^(?<precol>.*){(?<parameterName>\w+)}(.*)/m /* <- Not global */
601
- .exec(replacedTemplates))) {
602
- _loop_1();
603
- }
604
- // [💫] Check if there are parameters that are not closed properly
605
- if (/{\w+$/.test(replacedTemplates)) {
606
- throw new PipelineExecutionError('Parameter is not closed');
607
- }
608
- // [💫] Check if there are parameters that are not opened properly
609
- if (/^\w+}/.test(replacedTemplates)) {
610
- throw new PipelineExecutionError('Parameter is not opened');
611
- }
612
- return replacedTemplates;
613
- }
614
-
615
637
  /**
616
638
  * Counts number of characters in the text
617
639
  *
@@ -1353,7 +1375,7 @@
1353
1375
  modelVariant: 'CHAT',
1354
1376
  modelTitle: 'o1-preview-2024-09-12',
1355
1377
  modelName: 'o1-preview-2024-09-12',
1356
- // <- TODO: [main] !!! Some better system to organize theese date suffixes and versions
1378
+ // <- TODO: [💩] Some better system to organize theese date suffixes and versions
1357
1379
  pricing: {
1358
1380
  prompt: computeUsage("$15.00 / 1M tokens"),
1359
1381
  output: computeUsage("$60.00 / 1M tokens"),
@@ -1403,7 +1425,7 @@
1403
1425
  * @see https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4
1404
1426
  * @see https://openai.com/api/pricing/
1405
1427
  * @see /other/playground/playground.ts
1406
- * TODO: [🍓] Make better
1428
+ * TODO: [🍓][💩] Make better
1407
1429
  * TODO: Change model titles to human eg: "gpt-4-turbo-2024-04-09" -> "GPT-4 Turbo (2024-04-09)"
1408
1430
  * TODO: [🚸] Not all models are compatible with JSON mode, add this information here and use it
1409
1431
  * Note: [💞] Ignore a discrepancy between file name and entity name