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