@promptbook/utils 0.50.0 → 0.52.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.
Files changed (46) hide show
  1. package/README.md +6 -2
  2. package/esm/index.es.js +189 -72
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/_packages/execute-javascript.index.d.ts +44 -1
  5. package/esm/typings/_packages/openai.index.d.ts +2 -1
  6. package/esm/typings/_packages/types.index.d.ts +3 -2
  7. package/esm/typings/_packages/utils.index.d.ts +9 -2
  8. package/esm/typings/conversion/utils/extractParametersFromPromptTemplate.d.ts +13 -0
  9. package/esm/typings/conversion/utils/extractParametersFromPromptTemplate.test.d.ts +1 -0
  10. package/esm/typings/conversion/utils/extractVariables.d.ts +4 -3
  11. package/esm/typings/execution/plugins/llm-execution-tools/openai/computeOpenaiUsage.d.ts +0 -3
  12. package/esm/typings/execution/plugins/llm-execution-tools/openai/computeUsage.d.ts +13 -0
  13. package/esm/typings/execution/plugins/llm-execution-tools/openai/computeUsage.test.d.ts +1 -0
  14. package/esm/typings/execution/plugins/llm-execution-tools/openai/models.d.ts +25 -0
  15. package/esm/typings/types/ModelRequirements.d.ts +1 -0
  16. package/esm/typings/types/PromptbookJson/PromptTemplateJson.d.ts +2 -1
  17. package/esm/typings/utils/extractParameters.d.ts +1 -3
  18. package/esm/typings/utils/sets/difference.d.ts +4 -0
  19. package/esm/typings/utils/sets/difference.test.d.ts +1 -0
  20. package/esm/typings/utils/sets/intersection.d.ts +4 -0
  21. package/esm/typings/utils/sets/intersection.test.d.ts +1 -0
  22. package/esm/typings/utils/sets/union.d.ts +4 -0
  23. package/esm/typings/utils/sets/union.test.d.ts +1 -0
  24. package/package.json +2 -2
  25. package/umd/index.umd.js +194 -71
  26. package/umd/index.umd.js.map +1 -1
  27. package/umd/typings/_packages/execute-javascript.index.d.ts +44 -1
  28. package/umd/typings/_packages/openai.index.d.ts +2 -1
  29. package/umd/typings/_packages/types.index.d.ts +3 -2
  30. package/umd/typings/_packages/utils.index.d.ts +9 -2
  31. package/umd/typings/conversion/utils/extractParametersFromPromptTemplate.d.ts +13 -0
  32. package/umd/typings/conversion/utils/extractParametersFromPromptTemplate.test.d.ts +1 -0
  33. package/umd/typings/conversion/utils/extractVariables.d.ts +4 -3
  34. package/umd/typings/execution/plugins/llm-execution-tools/openai/computeOpenaiUsage.d.ts +0 -3
  35. package/umd/typings/execution/plugins/llm-execution-tools/openai/computeUsage.d.ts +13 -0
  36. package/umd/typings/execution/plugins/llm-execution-tools/openai/computeUsage.test.d.ts +1 -0
  37. package/umd/typings/execution/plugins/llm-execution-tools/openai/models.d.ts +25 -0
  38. package/umd/typings/types/ModelRequirements.d.ts +1 -0
  39. package/umd/typings/types/PromptbookJson/PromptTemplateJson.d.ts +2 -1
  40. package/umd/typings/utils/extractParameters.d.ts +1 -3
  41. package/umd/typings/utils/sets/difference.d.ts +4 -0
  42. package/umd/typings/utils/sets/difference.test.d.ts +1 -0
  43. package/umd/typings/utils/sets/intersection.d.ts +4 -0
  44. package/umd/typings/utils/sets/intersection.test.d.ts +1 -0
  45. package/umd/typings/utils/sets/union.d.ts +4 -0
  46. package/umd/typings/utils/sets/union.test.d.ts +1 -0
package/README.md CHANGED
@@ -451,6 +451,8 @@ Or you can install them separately:
451
451
  - _(Not finished)_ **[@promptbook/wizzard](https://www.npmjs.com/package/@promptbook/wizzard)** - Wizard for creating+running promptbooks in single line
452
452
  - **[@promptbook/execute-javascript](https://www.npmjs.com/package/@promptbook/execute-javascript)** - Execution tools for javascript inside promptbooks
453
453
  - **[@promptbook/openai](https://www.npmjs.com/package/@promptbook/openai)** - Execution tools for OpenAI API, wrapper around OpenAI SDK
454
+ - **[@promptbook/anthropic-claude](https://www.npmjs.com/package/@promptbook/anthropic-claude)** - Execution tools for Anthropic Claude API, wrapper around Anthropic Claude SDK
455
+ - **[@promptbook/azure-openai](https://www.npmjs.com/package/@promptbook/azure-openai)** - Execution tools for Azure OpenAI API
454
456
  - **[@promptbook/langtail](https://www.npmjs.com/package/@promptbook/langtail)** - Execution tools for Langtail API, wrapper around Langtail SDK
455
457
  - **[@promptbook/mock](https://www.npmjs.com/package/@promptbook/mock)** - Mocked execution tools for testing the library and saving the tokens
456
458
  - **[@promptbook/remote-client](https://www.npmjs.com/package/@promptbook/remote-client)** - Remote client for remote execution of promptbooks
@@ -458,6 +460,8 @@ Or you can install them separately:
458
460
  - **[@promptbook/types](https://www.npmjs.com/package/@promptbook/types)** - Just typescript types used in the library
459
461
  - **[@promptbook/cli](https://www.npmjs.com/package/@promptbook/cli)** - Command line interface utilities for promptbooks
460
462
 
463
+
464
+
461
465
  ## 📚 Dictionary
462
466
 
463
467
  The following glossary is used to clarify certain basic concepts:
@@ -600,8 +604,8 @@ Internally it calls OpenAI, Azure, GPU, proxy, cache, logging,...
600
604
  `LlmExecutionTools` an abstract interface that is implemented by concrete execution tools:
601
605
 
602
606
  - `OpenAiExecutionTools`
603
- - _(Not implemented yet)_ `AnthropicClaudeExecutionTools`
604
- - _(Not implemented yet)_ `AzureOpenAiExecutionTools`
607
+ - _(Not implemented yet !!!!! )_ `AnthropicClaudeExecutionTools`
608
+ - _(Not implemented yet !!!!! )_ `AzureOpenAiExecutionTools`
605
609
  - _(Not implemented yet)_ `BardExecutionTools`
606
610
  - _(Not implemented yet)_ `LamaExecutionTools`
607
611
  - _(Not implemented yet)_ `GpuExecutionTools`
package/esm/index.es.js CHANGED
@@ -104,37 +104,6 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
104
104
  */
105
105
  var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
106
106
 
107
- /**
108
- * Parses the template and returns the list of all parameter names
109
- *
110
- * @param template the template with parameters in {curly} braces
111
- * @returns the list of parameter names
112
- *
113
- * @private within the library
114
- */
115
- function extractParameters(template) {
116
- var e_1, _a;
117
- var matches = template.matchAll(/{\w+}/g);
118
- var parameterNames = [];
119
- try {
120
- for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
121
- var match = matches_1_1.value;
122
- var parameterName = match[0].slice(1, -1);
123
- if (!parameterNames.includes(parameterName)) {
124
- parameterNames.push(parameterName);
125
- }
126
- }
127
- }
128
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
129
- finally {
130
- try {
131
- if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
132
- }
133
- finally { if (e_1) throw e_1.error; }
134
- }
135
- return parameterNames;
136
- }
137
-
138
107
  /**
139
108
  * Computes the deepness of the markdown structure.
140
109
  *
@@ -400,10 +369,75 @@ function removeContentComments(content) {
400
369
  return spaceTrim(content.replace(/<!--(.*?)-->/gs, ''));
401
370
  }
402
371
 
372
+ /**
373
+ * Creates a new set with all elements that are present in either set
374
+ */
375
+ function union() {
376
+ var e_1, _a, e_2, _b;
377
+ var sets = [];
378
+ for (var _i = 0; _i < arguments.length; _i++) {
379
+ sets[_i] = arguments[_i];
380
+ }
381
+ var union = new Set();
382
+ try {
383
+ for (var sets_1 = __values(sets), sets_1_1 = sets_1.next(); !sets_1_1.done; sets_1_1 = sets_1.next()) {
384
+ var set = sets_1_1.value;
385
+ try {
386
+ for (var _c = (e_2 = void 0, __values(Array.from(set))), _d = _c.next(); !_d.done; _d = _c.next()) {
387
+ var item = _d.value;
388
+ union.add(item);
389
+ }
390
+ }
391
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
392
+ finally {
393
+ try {
394
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
395
+ }
396
+ finally { if (e_2) throw e_2.error; }
397
+ }
398
+ }
399
+ }
400
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
401
+ finally {
402
+ try {
403
+ if (sets_1_1 && !sets_1_1.done && (_a = sets_1.return)) _a.call(sets_1);
404
+ }
405
+ finally { if (e_1) throw e_1.error; }
406
+ }
407
+ return union;
408
+ }
409
+
403
410
  /**
404
411
  * The version of the Promptbook library
405
412
  */
406
- var PROMPTBOOK_VERSION = '0.50.0-10';
413
+ var PROMPTBOOK_VERSION = '0.51.0';
414
+
415
+ /**
416
+ * Parses the template and returns the list of all parameter names
417
+ *
418
+ * @param template the template with parameters in {curly} braces
419
+ * @returns the list of parameter names
420
+ */
421
+ function extractParameters(template) {
422
+ var e_1, _a;
423
+ var matches = template.matchAll(/{\w+}/g);
424
+ var parameterNames = new Set();
425
+ try {
426
+ for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
427
+ var match = matches_1_1.value;
428
+ var parameterName = match[0].slice(1, -1);
429
+ parameterNames.add(parameterName);
430
+ }
431
+ }
432
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
433
+ finally {
434
+ try {
435
+ if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
436
+ }
437
+ finally { if (e_1) throw e_1.error; }
438
+ }
439
+ return parameterNames;
440
+ }
407
441
 
408
442
  /**
409
443
  * Parses the given script and returns the list of all used variables that are not defined in the script
@@ -411,11 +445,9 @@ var PROMPTBOOK_VERSION = '0.50.0-10';
411
445
  * @param script from which to extract the variables
412
446
  * @returns the list of variable names
413
447
  * @throws {PromptbookSyntaxError} if the script is invalid
414
- *
415
- * @private within the promptbookStringToJson
416
448
  */
417
449
  function extractVariables(script) {
418
- var variables = [];
450
+ var variables = new Set();
419
451
  script = "(()=>{".concat(script, "})()");
420
452
  try {
421
453
  for (var i = 0; i < 100 /* <- TODO: This limit to configuration */; i++)
@@ -439,7 +471,7 @@ function extractVariables(script) {
439
471
  script = "const ".concat(undefinedName, " = ()=>'';") + script;
440
472
  }
441
473
  else {
442
- variables.push(undefinedName);
474
+ variables.add(undefinedName);
443
475
  script = "const ".concat(undefinedName, " = '';") + script;
444
476
  }
445
477
  }
@@ -452,6 +484,53 @@ function extractVariables(script) {
452
484
  }
453
485
  return variables;
454
486
  }
487
+ /**
488
+ * TODO: [🔣] Support for multiple languages - python, java,...
489
+ */
490
+
491
+ /**
492
+ * Parses the prompt template and returns the set of all used parameters
493
+ *
494
+ * @param promptTemplate the template with used parameters
495
+ * @returns the set of parameter names
496
+ * @throws {PromptbookSyntaxError} if the script is invalid
497
+ */
498
+ function extractParametersFromPromptTemplate(promptTemplate) {
499
+ var e_1, _a, e_2, _b;
500
+ var parameterNames = new Set();
501
+ try {
502
+ for (var _c = __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(promptTemplate.title)), false), __read(extractParameters(promptTemplate.description || '')), false), __read(extractParameters(promptTemplate.content)), false)), _d = _c.next(); !_d.done; _d = _c.next()) {
503
+ var parameterName = _d.value;
504
+ parameterNames.add(parameterName);
505
+ }
506
+ }
507
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
508
+ finally {
509
+ try {
510
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
511
+ }
512
+ finally { if (e_1) throw e_1.error; }
513
+ }
514
+ if (promptTemplate.executionType === 'SCRIPT') {
515
+ try {
516
+ for (var _e = __values(extractVariables(promptTemplate.content)), _f = _e.next(); !_f.done; _f = _e.next()) {
517
+ var parameterName = _f.value;
518
+ parameterNames.add(parameterName);
519
+ }
520
+ }
521
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
522
+ finally {
523
+ try {
524
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
525
+ }
526
+ finally { if (e_2) throw e_2.error; }
527
+ }
528
+ }
529
+ return parameterNames;
530
+ }
531
+ /**
532
+ * TODO: [🔣] If script require contentLanguage
533
+ */
455
534
 
456
535
  /**
457
536
  * Execution type describes the way how the block is executed
@@ -469,10 +548,11 @@ var ExecutionTypes = [
469
548
  /**
470
549
  * Units of text measurement
471
550
  */
472
- var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'PARAGRAPHS', 'LINES', 'PAGES'];
551
+ var EXPECTATION_UNITS = ['CHARACTERS', 'WORDS', 'SENTENCES', 'LINES', 'PARAGRAPHS', 'PAGES'];
473
552
  /**
474
553
  * TODO: [💝] Unite object for expecting amount and format - remove expectFormat
475
554
  * TODO: use one helper type> (string_prompt | string_javascript | string_markdown) & string_template
555
+ * TODO: [👙][🧠] Just selecting gpt3 or gpt4 level of model
476
556
  */
477
557
 
478
558
  /**
@@ -928,11 +1008,11 @@ function promptbookStringToJson(promptbookString) {
928
1008
  finally { if (e_1) throw e_1.error; }
929
1009
  }
930
1010
  var _loop_1 = function (section) {
931
- var e_3, _e, e_4, _f, e_5, _g;
1011
+ var e_3, _e;
932
1012
  // TODO: Parse prompt template description (the content out of the codeblock and lists)
933
1013
  var templateModelRequirements = __assign({}, defaultModelRequirements);
934
1014
  var listItems_3 = extractAllListItemsFromMarkdown(section.content);
935
- var dependentParameterNames = [];
1015
+ var dependentParameterNames = new Set();
936
1016
  var executionType = 'PROMPT_TEMPLATE';
937
1017
  var jokers = [];
938
1018
  var postprocessing = [];
@@ -946,7 +1026,7 @@ function promptbookStringToJson(promptbookString) {
946
1026
  switch (command.type) {
947
1027
  case 'JOKER':
948
1028
  jokers.push(command.parameterName);
949
- dependentParameterNames.push(command.parameterName);
1029
+ dependentParameterNames.add(command.parameterName);
950
1030
  break;
951
1031
  case 'EXECUTE':
952
1032
  if (isExecutionTypeChanged) {
@@ -1000,7 +1080,7 @@ function promptbookStringToJson(promptbookString) {
1000
1080
  }
1001
1081
  finally { if (e_3) throw e_3.error; }
1002
1082
  }
1003
- var _h = extractOneBlockFromMarkdown(section.content), language = _h.language, content = _h.content;
1083
+ var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
1004
1084
  if (executionType === 'SCRIPT') {
1005
1085
  if (!language) {
1006
1086
  throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
@@ -1039,40 +1119,12 @@ function promptbookStringToJson(promptbookString) {
1039
1119
  if (Object.keys(postprocessing).length === 0) {
1040
1120
  postprocessing = undefined;
1041
1121
  }
1042
- try {
1043
- for (var _j = (e_4 = void 0, __values(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameters(section.title)), false), __read(extractParameters(description_1 || '')), false), __read(extractParameters(content)), false))), _k = _j.next(); !_k.done; _k = _j.next()) {
1044
- var parameterName = _k.value;
1045
- dependentParameterNames.push(parameterName);
1046
- }
1047
- }
1048
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
1049
- finally {
1050
- try {
1051
- if (_k && !_k.done && (_f = _j.return)) _f.call(_j);
1052
- }
1053
- finally { if (e_4) throw e_4.error; }
1054
- }
1055
- if (executionType === 'SCRIPT') {
1056
- try {
1057
- for (var _l = (e_5 = void 0, __values(extractVariables(content))), _m = _l.next(); !_m.done; _m = _l.next()) {
1058
- var parameterName = _m.value;
1059
- dependentParameterNames.push(parameterName);
1060
- }
1061
- }
1062
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
1063
- finally {
1064
- try {
1065
- if (_m && !_m.done && (_g = _l.return)) _g.call(_l);
1066
- }
1067
- finally { if (e_5) throw e_5.error; }
1068
- }
1069
- }
1070
- dependentParameterNames = __spreadArray([], __read(new Set(dependentParameterNames)), false);
1122
+ dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
1071
1123
  promptbookJson.promptTemplates.push({
1072
1124
  name: titleToName(section.title),
1073
1125
  title: section.title,
1074
1126
  description: description_1,
1075
- dependentParameterNames: dependentParameterNames,
1127
+ dependentParameterNames: Array.from(dependentParameterNames),
1076
1128
  executionType: executionType,
1077
1129
  jokers: jokers,
1078
1130
  postprocessing: postprocessing,
@@ -2510,6 +2562,71 @@ function removeQuotes(text) {
2510
2562
  return text;
2511
2563
  }
2512
2564
 
2565
+ /**
2566
+ * Create difference set of two sets.
2567
+ */
2568
+ function difference(a, b, isEqual) {
2569
+ var e_1, _a;
2570
+ if (isEqual === void 0) { isEqual = function (a, b) { return a === b; }; }
2571
+ var diff = new Set();
2572
+ var _loop_1 = function (itemA) {
2573
+ if (!Array.from(b).some(function (itemB) { return isEqual(itemA, itemB); })) {
2574
+ diff.add(itemA);
2575
+ }
2576
+ };
2577
+ try {
2578
+ for (var _b = __values(Array.from(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
2579
+ var itemA = _c.value;
2580
+ _loop_1(itemA);
2581
+ }
2582
+ }
2583
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2584
+ finally {
2585
+ try {
2586
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2587
+ }
2588
+ finally { if (e_1) throw e_1.error; }
2589
+ }
2590
+ return diff;
2591
+ }
2592
+
2593
+ /**
2594
+ * Creates a new set with all elements that are present in all sets
2595
+ */
2596
+ function intersection() {
2597
+ var e_1, _a;
2598
+ var sets = [];
2599
+ for (var _i = 0; _i < arguments.length; _i++) {
2600
+ sets[_i] = arguments[_i];
2601
+ }
2602
+ var intersection = new Set();
2603
+ if (sets[0]) {
2604
+ try {
2605
+ for (var _b = __values(Array.from(sets[0])), _c = _b.next(); !_c.done; _c = _b.next()) {
2606
+ var item = _c.value;
2607
+ var isPresentInAllSets = true;
2608
+ for (var i = 1; i < sets.length; i++) {
2609
+ if (sets[i] !== undefined && !sets[i].has(item)) {
2610
+ isPresentInAllSets = false;
2611
+ break;
2612
+ }
2613
+ }
2614
+ if (isPresentInAllSets) {
2615
+ intersection.add(item);
2616
+ }
2617
+ }
2618
+ }
2619
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2620
+ finally {
2621
+ try {
2622
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2623
+ }
2624
+ finally { if (e_1) throw e_1.error; }
2625
+ }
2626
+ }
2627
+ return intersection;
2628
+ }
2629
+
2513
2630
  /**
2514
2631
  * Function trimCodeBlock will trim starting and ending code block from the string if it is present.
2515
2632
  *
@@ -2627,5 +2744,5 @@ var normalizeTo = {
2627
2744
  * Note: [🕙] It does not make sence to have simple lower / UPPER case normalization
2628
2745
  */
2629
2746
 
2630
- export { CountUtils, DIACRITIC_VARIANTS_LETTERS, ExecutionReportStringOptionsDefaults, assertsExecutionSuccessful, capitalize, checkExpectations, countCharacters, countLines, countPages, countParagraphs, countSentences, countWords, decapitalize, executionReportJsonToString, extractAllBlocksFromMarkdown, extractAllListItemsFromMarkdown, extractBlock, extractOneBlockFromMarkdown, isPassingExpectations, isValidJsonString, isValidKeyword, nameToUriPart, nameToUriParts, normalizeTo, normalizeToKebabCase, normalizeTo_PascalCase, normalizeTo_SCREAMING_CASE, normalizeTo_camelCase, normalizeTo_snake_case, normalizeWhitespaces, parseKeywords, parseKeywordsFromString, parseNumber, prettifyPromptbookString, removeContentComments, removeDiacritics, removeEmojis, removeMarkdownFormatting, removeQuotes, renameParameter, renderPromptbookMermaid, replaceParameters, searchKeywords, splitIntoSentences, titleToName, trimCodeBlock, trimEndOfCodeBlock, unwrapResult };
2747
+ export { CountUtils, DIACRITIC_VARIANTS_LETTERS, ExecutionReportStringOptionsDefaults, assertsExecutionSuccessful, capitalize, checkExpectations, countCharacters, countLines, countPages, countParagraphs, countSentences, countWords, decapitalize, difference, executionReportJsonToString, extractAllBlocksFromMarkdown, extractAllListItemsFromMarkdown, extractBlock, extractOneBlockFromMarkdown, extractParameters, extractParametersFromPromptTemplate, extractVariables, intersection, isPassingExpectations, isValidJsonString, isValidKeyword, nameToUriPart, nameToUriParts, normalizeTo, normalizeToKebabCase, normalizeTo_PascalCase, normalizeTo_SCREAMING_CASE, normalizeTo_camelCase, normalizeTo_snake_case, normalizeWhitespaces, parseKeywords, parseKeywordsFromString, parseNumber, prettifyPromptbookString, removeContentComments, removeDiacritics, removeEmojis, removeMarkdownFormatting, removeQuotes, renameParameter, renderPromptbookMermaid, replaceParameters, searchKeywords, splitIntoSentences, titleToName, trimCodeBlock, trimEndOfCodeBlock, union, unwrapResult };
2631
2748
  //# sourceMappingURL=index.es.js.map