@promptbook/core 0.54.1 → 0.55.0-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,5 +1,6 @@
1
- import type { number_positive_or_zero } from '../types/typeAliases';
2
- import type { number_tokens } from '../types/typeAliases';
1
+ import type { KebabCase } from 'type-fest';
2
+ import type { ExpectationUnit } from '../types/PromptbookJson/PromptTemplateJson';
3
+ import type { number_positive } from '../types/typeAliases';
3
4
  import type { number_usd } from '../types/typeAliases';
4
5
  import type { string_date_iso8601 } from '../types/typeAliases';
5
6
  import type { string_model_name } from '../types/typeAliases';
@@ -48,27 +49,51 @@ export type PromptCommonResult = {
48
49
  /**
49
50
  * Usage of the prompt execution
50
51
  */
51
- readonly usage: {
52
- /**
53
- * Cost of the execution in USD
54
- *
55
- * If the cost is unknown, the value is `'UNKNOWN'`
56
- */
57
- price: (number_positive_or_zero & number_usd) | 'UNKNOWN';
58
- /**
59
- * Number of tokens used in the input aka. `prompt_tokens`
60
- */
61
- inputTokens: number_tokens | 'UNKNOWN';
62
- /**
63
- * Number of tokens used in the output aka. `completion_tokens`
64
- */
65
- outputTokens: number_tokens | 'UNKNOWN';
66
- };
52
+ readonly usage: PromptResultUsage;
67
53
  /**
68
54
  * Raw response from the model
69
55
  */
70
56
  readonly rawResponse: object;
71
57
  };
58
+ /**
59
+ * Usage statistics for one or many prompt results
60
+ */
61
+ export type PromptResultUsage = {
62
+ /**
63
+ * Cost of the execution in USD
64
+ *
65
+ * Note: If the cost is unknown, the value 0 and isUncertain is true
66
+ */
67
+ price: UncertainNumber;
68
+ /**
69
+ * Number of whatever used in the input aka. `prompt_tokens`
70
+ */
71
+ input: PromptResultUsageCounts;
72
+ /**
73
+ * Number of tokens used in the output aka. `completion_tokens`
74
+ */
75
+ output: PromptResultUsageCounts;
76
+ };
77
+ /**
78
+ * Record of all possible measurable units
79
+ */
80
+ export type PromptResultUsageCounts = Record<`${KebabCase<'TOKENS' | ExpectationUnit>}Count`, UncertainNumber>;
81
+ /**
82
+ * Number which can be uncertain
83
+ *
84
+ * Note: If the value is completelly unknown, the value 0 and isUncertain is true
85
+ * Note: Not using NaN or null because it looses the value which is better to be uncertain then not to be at all
86
+ */
87
+ export type UncertainNumber = {
88
+ /**
89
+ * The numeric value
90
+ */
91
+ value: number_usd & (number_positive | 0);
92
+ /**
93
+ * Is the value uncertain
94
+ */
95
+ isUncertain?: true;
96
+ };
72
97
  /**
73
98
  * TODO: [🧠] Maybe timing more accurate then seconds?
74
99
  * TODO: [🧠] Should here be link to the prompt?
@@ -0,0 +1,7 @@
1
+ import type { PromptResultUsage } from './PromptResult';
2
+ /**
3
+ * Function addPromptResultUsage will add multiple usages into one
4
+ *
5
+ * Note: If you provide 0 values, it returns void usage
6
+ */
7
+ export declare function addPromptResultUsage(...usageItems: Array<PromptResultUsage>): PromptResultUsage;
@@ -0,0 +1,10 @@
1
+ import type { PromptResultUsageCounts } from './PromptResult';
2
+ /**
3
+ * Helper of usage compute
4
+ *
5
+ * @param content the content of prompt or response
6
+ * @returns part of PromptResultUsageCounts
7
+ *
8
+ * @private internal util of LlmExecutionTools
9
+ */
10
+ export declare function computeUsageCounts(content: string): Omit<PromptResultUsageCounts, 'tokensCount'>;
@@ -1,8 +1,12 @@
1
1
  import type OpenAI from 'openai';
2
+ import type { Prompt } from '../../../../types/Prompt';
2
3
  import type { PromptResult } from '../../../PromptResult';
4
+ import type { PromptResultUsage } from '../../../PromptResult';
3
5
  /**
4
6
  * Computes the usage of the OpenAI API based on the response from OpenAI
5
7
  *
6
8
  * @throws {PromptbookExecutionError} If the usage is not defined in the response from OpenAI
9
+ * @private internal util of `OpenAiExecutionTools`
7
10
  */
8
- export declare function computeOpenaiUsage(rawResponse: Pick<OpenAI.Chat.Completions.ChatCompletion | OpenAI.Completions.Completion, 'model' | 'usage'>): PromptResult['usage'];
11
+ export declare function computeOpenaiUsage(promptContent: Prompt['content'], // <- Note: Intentionally using [] to access type properties to bring jsdoc from Prompt/PromptResult to consumer
12
+ resultContent: PromptResult['content'], rawResponse: Pick<OpenAI.Chat.Completions.ChatCompletion | OpenAI.Completions.Completion, 'model' | 'usage'>): PromptResultUsage;
@@ -3,11 +3,11 @@ import type { ExecutionType } from '../ExecutionTypes';
3
3
  import type { ModelRequirements } from '../ModelRequirements';
4
4
  import type { ScriptLanguage } from '../ScriptLanguage';
5
5
  import type { number_integer } from '../typeAliases';
6
- import type { string_markdown_text } from '../typeAliases';
7
- import type { number_positive_or_zero } from '../typeAliases';
6
+ import type { number_positive } from '../typeAliases';
8
7
  import type { string_javascript } from '../typeAliases';
9
8
  import type { string_javascript_name } from '../typeAliases';
10
9
  import type { string_markdown } from '../typeAliases';
10
+ import type { string_markdown_text } from '../typeAliases';
11
11
  import type { string_name } from '../typeAliases';
12
12
  import type { string_prompt } from '../typeAliases';
13
13
  import type { string_template } from '../typeAliases';
@@ -48,7 +48,7 @@ export type ExpectationUnit = typeof EXPECTATION_UNITS[number];
48
48
  /**
49
49
  * Amount of text measurement
50
50
  */
51
- export type ExpectationAmount = number_integer & number_positive_or_zero;
51
+ export type ExpectationAmount = number_integer & (number_positive | 0);
52
52
  /**
53
53
  * Template for simple concatenation of strings
54
54
  */
@@ -393,11 +393,9 @@ export type number_usd = number;
393
393
  /**
394
394
  * Semantic helper for number of tokens
395
395
  */
396
- export type number_tokens = number_integer & number_positive_or_zero;
396
+ export type number_tokens = number_integer & (number_positive | 0);
397
397
  export type number_positive = number;
398
398
  export type number_negative = number;
399
- export type number_positive_or_zero = number;
400
- export type number_negative_or_zero = number;
401
399
  export type number_integer = number;
402
400
  /**
403
401
  * Semantic helper;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/core",
3
- "version": "0.54.1",
3
+ "version": "0.55.0-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.54.0';
457
+ var PROMPTBOOK_VERSION = '0.54.1';
458
458
 
459
459
  /**
460
460
  * Parses the template and returns the list of all parameter names
@@ -2284,6 +2284,97 @@
2284
2284
  return TemplateError;
2285
2285
  }(Error));
2286
2286
 
2287
+ /**
2288
+ * Function addPromptResultUsage will add multiple usages into one
2289
+ *
2290
+ * Note: If you provide 0 values, it returns void usage
2291
+ */
2292
+ function addPromptResultUsage() {
2293
+ var usageItems = [];
2294
+ for (var _i = 0; _i < arguments.length; _i++) {
2295
+ usageItems[_i] = arguments[_i];
2296
+ }
2297
+ var initialStructure = {
2298
+ price: { value: 0 },
2299
+ input: {
2300
+ tokensCount: { value: 0 },
2301
+ charactersCount: { value: 0 },
2302
+ wordsCount: { value: 0 },
2303
+ sentencesCount: { value: 0 },
2304
+ linesCount: { value: 0 },
2305
+ paragraphsCount: { value: 0 },
2306
+ pagesCount: { value: 0 },
2307
+ },
2308
+ output: {
2309
+ tokensCount: { value: 0 },
2310
+ charactersCount: { value: 0 },
2311
+ wordsCount: { value: 0 },
2312
+ sentencesCount: { value: 0 },
2313
+ linesCount: { value: 0 },
2314
+ paragraphsCount: { value: 0 },
2315
+ pagesCount: { value: 0 },
2316
+ },
2317
+ };
2318
+ return usageItems.reduce(function (acc, item) {
2319
+ var e_1, _a, e_2, _b;
2320
+ var _c;
2321
+ acc.price.value += ((_c = item.price) === null || _c === void 0 ? void 0 : _c.value) || 0;
2322
+ try {
2323
+ for (var _d = __values(Object.keys(acc.input)), _e = _d.next(); !_e.done; _e = _d.next()) {
2324
+ var key = _e.value;
2325
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2326
+ //@ts-ignore
2327
+ if (item.input[key]) {
2328
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2329
+ //@ts-ignore
2330
+ acc.input[key].value += item.input[key].value || 0;
2331
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2332
+ //@ts-ignore
2333
+ if (item.input[key].isUncertain) {
2334
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2335
+ //@ts-ignore
2336
+ acc.input[key].isUncertain = true;
2337
+ }
2338
+ }
2339
+ }
2340
+ }
2341
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2342
+ finally {
2343
+ try {
2344
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
2345
+ }
2346
+ finally { if (e_1) throw e_1.error; }
2347
+ }
2348
+ try {
2349
+ for (var _f = __values(Object.keys(acc.output)), _g = _f.next(); !_g.done; _g = _f.next()) {
2350
+ var key = _g.value;
2351
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2352
+ //@ts-ignore
2353
+ if (item.output[key]) {
2354
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2355
+ //@ts-ignore
2356
+ acc.output[key].value += item.output[key].value || 0;
2357
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2358
+ //@ts-ignore
2359
+ if (item.output[key].isUncertain) {
2360
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2361
+ //@ts-ignore
2362
+ acc.output[key].isUncertain = true;
2363
+ }
2364
+ }
2365
+ }
2366
+ }
2367
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
2368
+ finally {
2369
+ try {
2370
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
2371
+ }
2372
+ finally { if (e_2) throw e_2.error; }
2373
+ }
2374
+ return acc;
2375
+ }, initialStructure);
2376
+ }
2377
+
2287
2378
  /**
2288
2379
  * Asserts that the execution of a promptnook is successful
2289
2380
  *
@@ -4010,13 +4101,13 @@
4010
4101
  return ({
4011
4102
  title: promptExecution.prompt.title,
4012
4103
  from: 0,
4013
- to: ((_b = (_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.usage) === null || _b === void 0 ? void 0 : _b.price) * (1 + taxRate),
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),
4014
4105
  });
4015
4106
  });
4016
4107
  var duration = moment__default["default"].duration(completedAt.diff(startedAt));
4017
4108
  var llmDuration = moment__default["default"].duration(countWorkingDuration(timingItems) * 1000);
4018
4109
  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'; });
4019
- var cost = executionsWithKnownCost.reduce(function (cost, promptExecution) { return cost + (promptExecution.result.usage.price || 0); }, 0);
4110
+ var cost = executionsWithKnownCost.reduce(function (cost, promptExecution) { return cost + ((promptExecution.result.usage.price.value /* uncertainNumber */) || 0); }, 0);
4020
4111
  headerList.push("STARTED AT ".concat(moment__default["default"](startedAt).format("YYYY-MM-DD HH:mm:ss")));
4021
4112
  headerList.push("COMPLETED AT ".concat(moment__default["default"](completedAt).format("YYYY-MM-DD HH:mm:ss")));
4022
4113
  headerList.push("TOTAL DURATION ".concat(duration.humanize(MOMENT_ARG_THRESHOLDS)));
@@ -4145,6 +4236,7 @@
4145
4236
  exports.SimplePromptbookLibrary = SimplePromptbookLibrary;
4146
4237
  exports.TemplateError = TemplateError;
4147
4238
  exports.UnexpectedError = UnexpectedError;
4239
+ exports.addPromptResultUsage = addPromptResultUsage;
4148
4240
  exports.assertsExecutionSuccessful = assertsExecutionSuccessful;
4149
4241
  exports.checkExpectations = checkExpectations;
4150
4242
  exports.createPromptbookExecutor = createPromptbookExecutor;