@promptbook/core 0.55.0-1 → 0.55.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.
@@ -1,4 +1,4 @@
1
- import type { PromptResultUsageCounts } from './PromptResult';
1
+ import type { PromptResultUsageCounts } from '../PromptResult';
2
2
  /**
3
3
  * Helper of usage compute
4
4
  *
@@ -0,0 +1,9 @@
1
+ import type { UncertainNumber } from '../PromptResult';
2
+ /**
3
+ * Make UncertainNumber
4
+ *
5
+ * @param value
6
+ *
7
+ * @private utility for initializating UncertainNumber
8
+ */
9
+ export declare function uncertainNumber(value?: number | typeof NaN | undefined | null): UncertainNumber;
@@ -0,0 +1,10 @@
1
+ import type { PromptResultUsage } from '../PromptResult';
2
+ import type { UncertainNumber } from '../PromptResult';
3
+ /**
4
+ * Function usageToWorktime will take usage and estimate saved worktime in hours of reading / writing
5
+ *
6
+ * Note: This is an estimate based of theese sources:
7
+ * - https://jecas.cz/doba-cteni
8
+ * - https://www.originalnitonery.cz/blog/psani-vsemi-deseti-se-muzete-naucit-i-sami-doma
9
+ */
10
+ export declare function usageToWorktime(usage: PromptResultUsage): UncertainNumber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/core",
3
- "version": "0.55.0-1",
3
+ "version": "0.55.0",
4
4
  "description": "Library to supercharge your use of large language models",
5
5
  "private": false,
6
6
  "sideEffects": false,
package/umd/index.umd.js CHANGED
@@ -454,7 +454,7 @@
454
454
  /**
455
455
  * The version of the Promptbook library
456
456
  */
457
- var PROMPTBOOK_VERSION = '0.55.0-0';
457
+ var PROMPTBOOK_VERSION = '0.55.0-2';
458
458
 
459
459
  /**
460
460
  * Parses the template and returns the list of all parameter names
@@ -2285,11 +2285,55 @@
2285
2285
  }(Error));
2286
2286
 
2287
2287
  /**
2288
- * Function addPromptResultUsage will add multiple usages into one
2288
+ * Asserts that the execution of a promptnook is successful
2289
+ *
2290
+ * @param executionResult - The partial result of the promptnook execution
2291
+ * @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
2292
+ */
2293
+ function assertsExecutionSuccessful(executionResult) {
2294
+ var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
2295
+ if (isSuccessful === true) {
2296
+ return;
2297
+ }
2298
+ if (errors.length === 0) {
2299
+ throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
2300
+ }
2301
+ else if (errors.length === 1) {
2302
+ throw errors[0];
2303
+ }
2304
+ else {
2305
+ throw new PromptbookExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
2306
+ }
2307
+ }
2308
+ /**
2309
+ * TODO: [🧠] Can this return type be better typed than void
2310
+ */
2311
+
2312
+ /**
2313
+ * Function isValidJsonString will tell you if the string is valid JSON or not
2314
+ */
2315
+ function isValidJsonString(value) {
2316
+ try {
2317
+ JSON.parse(value);
2318
+ return true;
2319
+ }
2320
+ catch (error) {
2321
+ if (!(error instanceof Error)) {
2322
+ throw error;
2323
+ }
2324
+ if (error.message.includes('Unexpected token')) {
2325
+ return false;
2326
+ }
2327
+ return false;
2328
+ }
2329
+ }
2330
+
2331
+ /**
2332
+ * Function `addUsage` will add multiple usages into one
2289
2333
  *
2290
2334
  * Note: If you provide 0 values, it returns void usage
2291
2335
  */
2292
- function addPromptResultUsage() {
2336
+ function addUsage() {
2293
2337
  var usageItems = [];
2294
2338
  for (var _i = 0; _i < arguments.length; _i++) {
2295
2339
  usageItems[_i] = arguments[_i];
@@ -2375,50 +2419,6 @@
2375
2419
  }, initialStructure);
2376
2420
  }
2377
2421
 
2378
- /**
2379
- * Asserts that the execution of a promptnook is successful
2380
- *
2381
- * @param executionResult - The partial result of the promptnook execution
2382
- * @throws {PromptbookExecutionError} If the execution is not successful or if multiple errors occurred
2383
- */
2384
- function assertsExecutionSuccessful(executionResult) {
2385
- var isSuccessful = executionResult.isSuccessful, errors = executionResult.errors;
2386
- if (isSuccessful === true) {
2387
- return;
2388
- }
2389
- if (errors.length === 0) {
2390
- throw new PromptbookExecutionError("Promptnook Execution failed because of unknown reason");
2391
- }
2392
- else if (errors.length === 1) {
2393
- throw errors[0];
2394
- }
2395
- else {
2396
- throw new PromptbookExecutionError(spaceTrim.spaceTrim(function (block) { return "\n Multiple errors occurred during promptnook execution\n\n ".concat(block(errors.map(function (error) { return '- ' + error.message; }).join('\n')), "\n "); }));
2397
- }
2398
- }
2399
- /**
2400
- * TODO: [🧠] Can this return type be better typed than void
2401
- */
2402
-
2403
- /**
2404
- * Function isValidJsonString will tell you if the string is valid JSON or not
2405
- */
2406
- function isValidJsonString(value) {
2407
- try {
2408
- JSON.parse(value);
2409
- return true;
2410
- }
2411
- catch (error) {
2412
- if (!(error instanceof Error)) {
2413
- throw error;
2414
- }
2415
- if (error.message.includes('Unexpected token')) {
2416
- return false;
2417
- }
2418
- return false;
2419
- }
2420
- }
2421
-
2422
2422
  /**
2423
2423
  * Counts number of characters in the text
2424
2424
  */
@@ -3008,7 +3008,7 @@
3008
3008
  });
3009
3009
  });
3010
3010
  }
3011
- var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, _a, _b, parameter;
3011
+ var parametersToPass, executionReport, resovedParameters_1, unresovedTemplates, resolving_1, loopLimit, _loop_1, error_1, usage_1, _a, _b, parameter, usage;
3012
3012
  var e_1, _c;
3013
3013
  return __generator(this, function (_d) {
3014
3014
  switch (_d.label) {
@@ -3087,9 +3087,14 @@
3087
3087
  if (!(error_1 instanceof Error)) {
3088
3088
  throw error_1;
3089
3089
  }
3090
+ usage_1 = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
3091
+ var result = _a.result;
3092
+ return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3093
+ })), false));
3090
3094
  return [2 /*return*/, {
3091
3095
  isSuccessful: false,
3092
3096
  errors: [error_1],
3097
+ usage: usage_1,
3093
3098
  executionReport: executionReport,
3094
3099
  outputParameters: parametersToPass,
3095
3100
  }];
@@ -3111,9 +3116,14 @@
3111
3116
  }
3112
3117
  finally { if (e_1) throw e_1.error; }
3113
3118
  }
3119
+ usage = addUsage.apply(void 0, __spreadArray([], __read(executionReport.promptExecutions.map(function (_a) {
3120
+ var result = _a.result;
3121
+ return (result === null || result === void 0 ? void 0 : result.usage) || addUsage();
3122
+ })), false));
3114
3123
  return [2 /*return*/, {
3115
3124
  isSuccessful: true,
3116
3125
  errors: [],
3126
+ usage: usage,
3117
3127
  executionReport: executionReport,
3118
3128
  outputParameters: parametersToPass,
3119
3129
  }];
@@ -3321,6 +3331,24 @@
3321
3331
  return SimplePromptInterfaceTools;
3322
3332
  }());
3323
3333
 
3334
+ /**
3335
+ * Function usageToWorktime will take usage and estimate saved worktime in hours of reading / writing
3336
+ *
3337
+ * Note: This is an estimate based of theese sources:
3338
+ * - https://jecas.cz/doba-cteni
3339
+ * - https://www.originalnitonery.cz/blog/psani-vsemi-deseti-se-muzete-naucit-i-sami-doma
3340
+ */
3341
+ function usageToWorktime(usage) {
3342
+ var value = usage.input.wordsCount.value / (200 /* words per minute */ * 60) +
3343
+ usage.output.wordsCount.value / (40 /* words per minute */ * 60);
3344
+ var isUncertain = usage.input.wordsCount.isUncertain || usage.output.wordsCount.isUncertain;
3345
+ var uncertainNumber = { value: value };
3346
+ if (isUncertain === true) {
3347
+ uncertainNumber.isUncertain = true;
3348
+ }
3349
+ return uncertainNumber;
3350
+ }
3351
+
3324
3352
  /**
3325
3353
  * Detects if the code is running in a browser environment in main thread (Not in a web worker)
3326
3354
  */
@@ -4101,13 +4129,16 @@
4101
4129
  return ({
4102
4130
  title: promptExecution.prompt.title,
4103
4131
  from: 0,
4104
- to: (((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price.value) || 0) /* uncertainNumber */ * (1 + taxRate),
4132
+ to: (((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price.value) || 0) /* <- TODO: look at uncertain numbers */ *
4133
+ (1 + taxRate),
4105
4134
  });
4106
4135
  });
4107
4136
  var duration = moment__default["default"].duration(completedAt.diff(startedAt));
4108
4137
  var llmDuration = moment__default["default"].duration(countWorkingDuration(timingItems) * 1000);
4109
4138
  var executionsWithKnownCost = executionReportJson.promptExecutions.filter(function (promptExecution) { var _a, _b; return (((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price) || 'UNKNOWN') !== 'UNKNOWN'; });
4110
- var cost = executionsWithKnownCost.reduce(function (cost, promptExecution) { return cost + ((promptExecution.result.usage.price.value /* uncertainNumber */) || 0); }, 0);
4139
+ var cost = executionsWithKnownCost.reduce(function (cost, promptExecution) {
4140
+ return cost + (promptExecution.result.usage.price.value /* <- Look at uncertain number */ || 0);
4141
+ }, 0);
4111
4142
  headerList.push("STARTED AT ".concat(moment__default["default"](startedAt).format("YYYY-MM-DD HH:mm:ss")));
4112
4143
  headerList.push("COMPLETED AT ".concat(moment__default["default"](completedAt).format("YYYY-MM-DD HH:mm:ss")));
4113
4144
  headerList.push("TOTAL DURATION ".concat(duration.humanize(MOMENT_ARG_THRESHOLDS)));
@@ -4236,7 +4267,7 @@
4236
4267
  exports.SimplePromptbookLibrary = SimplePromptbookLibrary;
4237
4268
  exports.TemplateError = TemplateError;
4238
4269
  exports.UnexpectedError = UnexpectedError;
4239
- exports.addPromptResultUsage = addPromptResultUsage;
4270
+ exports.addUsage = addUsage;
4240
4271
  exports.assertsExecutionSuccessful = assertsExecutionSuccessful;
4241
4272
  exports.checkExpectations = checkExpectations;
4242
4273
  exports.createPromptbookExecutor = createPromptbookExecutor;
@@ -4250,6 +4281,7 @@
4250
4281
  exports.prettifyPromptbookString = prettifyPromptbookString;
4251
4282
  exports.promptbookJsonToString = promptbookJsonToString;
4252
4283
  exports.promptbookStringToJson = promptbookStringToJson;
4284
+ exports.usageToWorktime = usageToWorktime;
4253
4285
  exports.validatePromptbookJson = validatePromptbookJson;
4254
4286
 
4255
4287
  Object.defineProperty(exports, '__esModule', { value: true });