@promptbook/core 0.59.0-32 → 0.59.0-34

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/esm/index.es.js CHANGED
@@ -185,21 +185,7 @@ function prettifyMarkdown(content) {
185
185
  }
186
186
  }
187
187
 
188
- /**
189
- * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
190
- */
191
- var PromptbookSyntaxError = /** @class */ (function (_super) {
192
- __extends(PromptbookSyntaxError, _super);
193
- function PromptbookSyntaxError(message) {
194
- var _this = _super.call(this, message) || this;
195
- _this.name = 'PromptbookSyntaxError';
196
- Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
197
- return _this;
198
- }
199
- return PromptbookSyntaxError;
200
- }(Error));
201
-
202
- var promptbookLibrary = [{title:"Prepare Keywords",promptbookUrl:"https://promptbook.studio/promptbook/prepare-keywords.ptbk.md",promptbookVersion:"0.59.0-31",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],executionType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"keywords"}],knowledge:[]},{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-31",parameters:[{name:"content",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledge",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],executionType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"knowledge"}],knowledge:[]}];
188
+ var promptbookLibrary = [{title:"Prepare Keywords",promptbookUrl:"https://promptbook.studio/promptbook/prepare-keywords.ptbk.md",promptbookVersion:"0.59.0-33",parameters:[{name:"content",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],executionType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"keywords"}],knowledge:[]},{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-33",parameters:[{name:"content",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledge",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{name:"knowledge",title:"Knowledge",dependentParameterNames:["content"],executionType:"PROMPT_TEMPLATE",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {content}",resultingParameterName:"knowledge"}],knowledge:[]}];
203
189
 
204
190
  /**
205
191
  * This error indicates errors during the execution of the promptbook
@@ -259,6 +245,20 @@ var PromptbookLogicError = /** @class */ (function (_super) {
259
245
  return PromptbookLogicError;
260
246
  }(Error));
261
247
 
248
+ /**
249
+ * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
250
+ */
251
+ var PromptbookSyntaxError = /** @class */ (function (_super) {
252
+ __extends(PromptbookSyntaxError, _super);
253
+ function PromptbookSyntaxError(message) {
254
+ var _this = _super.call(this, message) || this;
255
+ _this.name = 'PromptbookSyntaxError';
256
+ Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
257
+ return _this;
258
+ }
259
+ return PromptbookSyntaxError;
260
+ }(Error));
261
+
262
262
  /**
263
263
  * This error type indicates that the error should not happen and its last check before crashing with some other error
264
264
  */
@@ -523,7 +523,7 @@ function isValidJsonString(value /* <-[👨‍⚖️] */) {
523
523
  /**
524
524
  * The version of the Promptbook library
525
525
  */
526
- var PROMPTBOOK_VERSION = '0.59.0-31';
526
+ var PROMPTBOOK_VERSION = '0.59.0-33';
527
527
 
528
528
  /**
529
529
  * Function `addUsage` will add multiple usages into one
@@ -2096,11 +2096,6 @@ function prepareKnowledgeFromMarkdown(options) {
2096
2096
  });
2097
2097
  }
2098
2098
 
2099
- /**
2100
- * Supported script languages
2101
- */
2102
- var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
2103
-
2104
2099
  /**
2105
2100
  * Returns the same value that is passed as argument.
2106
2101
  * No side effects.
@@ -2118,6 +2113,11 @@ function just(value) {
2118
2113
  return value;
2119
2114
  }
2120
2115
 
2116
+ /**
2117
+ * Supported script languages
2118
+ */
2119
+ var SUPPORTED_SCRIPT_LANGUAGES = ['javascript', 'typescript', 'python'];
2120
+
2121
2121
  /**
2122
2122
  * Computes the deepness of the markdown structure.
2123
2123
  *
@@ -2945,9 +2945,279 @@ function titleToName(value) {
2945
2945
  return value;
2946
2946
  }
2947
2947
 
2948
+ /**
2949
+ * Compile promptbook from string (markdown) format to JSON format synchronously
2950
+ *
2951
+ * Note: There are two similar functions:
2952
+ * - `promptbookStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
2953
+ * - `promptbookStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
2954
+ *
2955
+ * @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
2956
+ * @param options - Options and tools for the compilation
2957
+ * @returns {Promptbook} compiled in JSON format (.ptbk.json)
2958
+ * @throws {PromptbookSyntaxError} if the promptbook string is not valid
2959
+ *
2960
+ * Note: This function does not validate logic of the pipeline only the syntax
2961
+ * Note: This function acts as compilation process
2962
+ */
2963
+ function promptbookStringToJsonSync(promptbookString) {
2964
+ var e_1, _a, e_2, _b;
2965
+ var promptbookJson = {
2966
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2967
+ title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
2968
+ promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
2969
+ promptbookVersion: PROMPTBOOK_VERSION,
2970
+ description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
2971
+ parameters: [],
2972
+ promptTemplates: [],
2973
+ knowledge: [],
2974
+ };
2975
+ // =============================================================
2976
+ // Note: 1️⃣ Normalization of the PROMPTBOOK string
2977
+ promptbookString = removeContentComments(promptbookString);
2978
+ promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
2979
+ promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
2980
+ // =============================================================
2981
+ ///Note: 2️⃣ Function for adding parameters
2982
+ var addParam = function (parameterCommand) {
2983
+ var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
2984
+ var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
2985
+ if (existingParameter &&
2986
+ existingParameter.description &&
2987
+ existingParameter.description !== parameterDescription &&
2988
+ parameterDescription) {
2989
+ throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
2990
+ }
2991
+ if (existingParameter) {
2992
+ if (parameterDescription) {
2993
+ existingParameter.description = parameterDescription;
2994
+ }
2995
+ }
2996
+ else {
2997
+ promptbookJson.parameters.push({
2998
+ name: parameterName,
2999
+ description: parameterDescription || undefined,
3000
+ isInput: isInput,
3001
+ isOutput: isOutput,
3002
+ });
3003
+ }
3004
+ };
3005
+ // =============================================================
3006
+ // Note: 3️⃣ Parse the dynamic part - the template pipeline
3007
+ var markdownStructure = markdownToMarkdownStructure(promptbookString);
3008
+ var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
3009
+ if (markdownStructureDeepness !== 2) {
3010
+ throw new PromptbookSyntaxError(spaceTrim("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
3011
+ }
3012
+ promptbookJson.title = markdownStructure.title;
3013
+ // TODO: [1] DRY description
3014
+ var description = markdownStructure.content;
3015
+ // Note: Remove codeblocks
3016
+ description = description.split(/^```.*^```/gms).join('');
3017
+ //Note: Remove lists and return statement
3018
+ description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3019
+ description = spaceTrim(description);
3020
+ if (description === '') {
3021
+ description = undefined;
3022
+ }
3023
+ promptbookJson.description = description;
3024
+ var defaultModelRequirements = {};
3025
+ var listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
3026
+ try {
3027
+ for (var listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
3028
+ var listItem = listItems_1_1.value;
3029
+ var command = parseCommand(listItem);
3030
+ switch (command.type) {
3031
+ case 'PROMPTBOOK_URL':
3032
+ promptbookJson.promptbookUrl = command.promptbookUrl.href;
3033
+ break;
3034
+ case 'PROMPTBOOK_VERSION':
3035
+ promptbookJson.promptbookVersion = command.promptbookVersion;
3036
+ break;
3037
+ case 'MODEL':
3038
+ defaultModelRequirements[command.key] = command.value;
3039
+ break;
3040
+ case 'PARAMETER':
3041
+ addParam(command);
3042
+ break;
3043
+ default:
3044
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
3045
+ }
3046
+ }
3047
+ }
3048
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
3049
+ finally {
3050
+ try {
3051
+ if (listItems_1_1 && !listItems_1_1.done && (_a = listItems_1.return)) _a.call(listItems_1);
3052
+ }
3053
+ finally { if (e_1) throw e_1.error; }
3054
+ }
3055
+ var _loop_1 = function (section) {
3056
+ var e_3, _e;
3057
+ // TODO: Parse prompt template description (the content out of the codeblock and lists)
3058
+ var templateModelRequirements = __assign({}, defaultModelRequirements);
3059
+ var listItems_3 = extractAllListItemsFromMarkdown(section.content);
3060
+ var dependentParameterNames = new Set();
3061
+ var executionType = 'PROMPT_TEMPLATE';
3062
+ var jokers = [];
3063
+ var postprocessing = [];
3064
+ var expectAmount = {};
3065
+ var expectFormat = undefined;
3066
+ var isExecutionTypeChanged = false;
3067
+ try {
3068
+ for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
3069
+ var listItem = listItems_2_1.value;
3070
+ var command = parseCommand(listItem);
3071
+ switch (command.type) {
3072
+ case 'JOKER':
3073
+ jokers.push(command.parameterName);
3074
+ dependentParameterNames.add(command.parameterName);
3075
+ break;
3076
+ case 'EXECUTE':
3077
+ if (isExecutionTypeChanged) {
3078
+ throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
3079
+ }
3080
+ executionType = command.executionType;
3081
+ isExecutionTypeChanged = true;
3082
+ break;
3083
+ case 'MODEL':
3084
+ templateModelRequirements[command.key] = command.value;
3085
+ break;
3086
+ case 'PARAMETER':
3087
+ // Note: This is just for detecting resulitng parameter name
3088
+ addParam(command);
3089
+ break;
3090
+ case 'POSTPROCESS':
3091
+ postprocessing.push(command.functionName);
3092
+ break;
3093
+ case 'EXPECT_AMOUNT':
3094
+ // eslint-disable-next-line no-case-declarations
3095
+ var unit = command.unit.toLowerCase();
3096
+ expectAmount[unit] = expectAmount[unit] || {};
3097
+ if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
3098
+ if (expectAmount[unit].min !== undefined) {
3099
+ throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
3100
+ }
3101
+ expectAmount[unit].min = command.amount;
3102
+ } /* not else */
3103
+ if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
3104
+ if (expectAmount[unit].max !== undefined) {
3105
+ throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
3106
+ }
3107
+ expectAmount[unit].max = command.amount;
3108
+ }
3109
+ break;
3110
+ case 'EXPECT_FORMAT':
3111
+ if (expectFormat !== undefined && command.format !== expectFormat) {
3112
+ throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
3113
+ }
3114
+ expectFormat = command.format;
3115
+ break;
3116
+ default:
3117
+ throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
3118
+ }
3119
+ }
3120
+ }
3121
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
3122
+ finally {
3123
+ try {
3124
+ if (listItems_2_1 && !listItems_2_1.done && (_e = listItems_2.return)) _e.call(listItems_2);
3125
+ }
3126
+ finally { if (e_3) throw e_3.error; }
3127
+ }
3128
+ var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
3129
+ if (executionType === 'SCRIPT') {
3130
+ if (!language) {
3131
+ throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
3132
+ }
3133
+ else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
3134
+ throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
3135
+ }
3136
+ }
3137
+ var lastLine = section.content.split('\n').pop();
3138
+ var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
3139
+ if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
3140
+ throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
3141
+ // TODO: Show code of invalid sections each time + DRY
3142
+ section.content
3143
+ .split('\n')
3144
+ .map(function (line) { return "> ".concat(line); })
3145
+ .join('\n')), "\n "); }));
3146
+ }
3147
+ var resultingParameterName = match.groups.resultingParamName;
3148
+ // TODO: [1] DRY description
3149
+ var description_1 = section.content;
3150
+ // Note: Remove codeblocks
3151
+ description_1 = description_1.split(/^```.*^```/gms).join('');
3152
+ //Note: Remove lists and return statement
3153
+ description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3154
+ description_1 = spaceTrim(description_1);
3155
+ if (description_1 === '') {
3156
+ description_1 = undefined;
3157
+ }
3158
+ if (Object.keys(jokers).length === 0) {
3159
+ jokers = undefined;
3160
+ }
3161
+ if (Object.keys(expectAmount).length === 0) {
3162
+ expectAmount = undefined;
3163
+ }
3164
+ if (Object.keys(postprocessing).length === 0) {
3165
+ postprocessing = undefined;
3166
+ }
3167
+ dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
3168
+ if (templateModelRequirements.modelVariant === undefined) {
3169
+ templateModelRequirements.modelVariant = 'CHAT';
3170
+ }
3171
+ var template = {
3172
+ name: titleToName(section.title),
3173
+ title: section.title,
3174
+ description: description_1,
3175
+ dependentParameterNames: Array.from(dependentParameterNames),
3176
+ executionType: executionType,
3177
+ jokers: jokers,
3178
+ postprocessing: postprocessing,
3179
+ expectations: expectAmount,
3180
+ expectFormat: expectFormat,
3181
+ modelRequirements: templateModelRequirements,
3182
+ contentLanguage: executionType === 'SCRIPT' ? language : undefined,
3183
+ content: content,
3184
+ resultingParameterName: resultingParameterName,
3185
+ };
3186
+ if (executionType !== 'PROMPT_TEMPLATE') {
3187
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3188
+ delete template.modelRequirements;
3189
+ }
3190
+ promptbookJson.promptTemplates.push(template);
3191
+ };
3192
+ try {
3193
+ for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
3194
+ var section = _d.value;
3195
+ _loop_1(section);
3196
+ }
3197
+ }
3198
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
3199
+ finally {
3200
+ try {
3201
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
3202
+ }
3203
+ finally { if (e_2) throw e_2.error; }
3204
+ }
3205
+ // =============================================================
3206
+ return promptbookJson;
3207
+ }
3208
+ /**
3209
+ * TODO: Report here line/column of error
3210
+ * TODO: Use spaceTrim more effectively
3211
+ * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
3212
+ */
3213
+
2948
3214
  /**
2949
3215
  * Compile promptbook from string (markdown) format to JSON format
2950
3216
  *
3217
+ * Note: There are two similar functions:
3218
+ * - `promptbookStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
3219
+ * - `promptbookStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
3220
+ *
2951
3221
  * @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
2952
3222
  * @param options - Options and tools for the compilation
2953
3223
  * @returns {Promptbook} compiled in JSON format (.ptbk.json)
@@ -2959,267 +3229,28 @@ function titleToName(value) {
2959
3229
  function promptbookStringToJson(promptbookString, options) {
2960
3230
  if (options === void 0) { options = {}; }
2961
3231
  return __awaiter(this, void 0, void 0, function () {
2962
- var llmTools, promptbookJson, knowledge, addParam, markdownStructure, markdownStructureDeepness, description, defaultModelRequirements, listItems, listItems_1, listItems_1_1, listItem, command, _loop_1, _a, _b, section;
2963
- var e_1, _c, e_2, _d;
2964
- return __generator(this, function (_e) {
2965
- switch (_e.label) {
3232
+ var llmTools, knowledge, promptbookJson;
3233
+ return __generator(this, function (_a) {
3234
+ switch (_a.label) {
2966
3235
  case 0:
2967
3236
  llmTools = options.llmTools;
2968
- promptbookJson = {
2969
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
2970
- title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
2971
- promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
2972
- promptbookVersion: PROMPTBOOK_VERSION,
2973
- description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
2974
- parameters: [],
2975
- promptTemplates: [],
2976
- knowledge: [],
2977
- };
2978
3237
  if (!llmTools) return [3 /*break*/, 2];
2979
3238
  return [4 /*yield*/, prepareKnowledgeFromMarkdown({
2980
3239
  content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
2981
3240
  llmTools: llmTools,
2982
3241
  })];
2983
3242
  case 1:
2984
- knowledge = _e.sent();
3243
+ knowledge = _a.sent();
2985
3244
  console.info('!!!! knowledge', knowledge);
2986
- _e.label = 2;
3245
+ _a.label = 2;
2987
3246
  case 2:
2988
- // =============================================================
2989
- // Note: 1️⃣ Normalization of the PROMPTBOOK string
2990
- promptbookString = removeContentComments(promptbookString);
2991
- promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
2992
- promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
2993
- addParam = function (parameterCommand) {
2994
- var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
2995
- var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
2996
- if (existingParameter &&
2997
- existingParameter.description &&
2998
- existingParameter.description !== parameterDescription &&
2999
- parameterDescription) {
3000
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Parameter {".concat(parameterName, "} is defined multiple times with different description.\n\n First definition:\n ").concat(block(existingParameter.description || '[undefined]'), "\n\n Second definition:\n ").concat(block(parameterDescription || '[undefined]'), "\n "); }));
3001
- }
3002
- if (existingParameter) {
3003
- if (parameterDescription) {
3004
- existingParameter.description = parameterDescription;
3005
- }
3006
- }
3007
- else {
3008
- promptbookJson.parameters.push({
3009
- name: parameterName,
3010
- description: parameterDescription || undefined,
3011
- isInput: isInput,
3012
- isOutput: isOutput,
3013
- });
3014
- }
3015
- };
3016
- markdownStructure = markdownToMarkdownStructure(promptbookString);
3017
- markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
3018
- if (markdownStructureDeepness !== 2) {
3019
- throw new PromptbookSyntaxError(spaceTrim("\n Invalid markdown structure.\n The markdown must have exactly 2 levels of headings (one top-level section and one section for each template).\n Now it has ".concat(markdownStructureDeepness, " levels of headings.\n ")));
3020
- }
3021
- promptbookJson.title = markdownStructure.title;
3022
- description = markdownStructure.content;
3023
- // Note: Remove codeblocks
3024
- description = description.split(/^```.*^```/gms).join('');
3025
- //Note: Remove lists and return statement
3026
- description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3027
- description = spaceTrim(description);
3028
- if (description === '') {
3029
- description = undefined;
3030
- }
3031
- promptbookJson.description = description;
3032
- defaultModelRequirements = {};
3033
- listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
3034
- try {
3035
- for (listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
3036
- listItem = listItems_1_1.value;
3037
- command = parseCommand(listItem);
3038
- switch (command.type) {
3039
- case 'PROMPTBOOK_URL':
3040
- promptbookJson.promptbookUrl = command.promptbookUrl.href;
3041
- break;
3042
- case 'PROMPTBOOK_VERSION':
3043
- promptbookJson.promptbookVersion = command.promptbookVersion;
3044
- break;
3045
- case 'MODEL':
3046
- defaultModelRequirements[command.key] = command.value;
3047
- break;
3048
- case 'PARAMETER':
3049
- addParam(command);
3050
- break;
3051
- default:
3052
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
3053
- }
3054
- }
3055
- }
3056
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
3057
- finally {
3058
- try {
3059
- if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
3060
- }
3061
- finally { if (e_1) throw e_1.error; }
3062
- }
3063
- _loop_1 = function (section) {
3064
- var e_3, _f;
3065
- // TODO: Parse prompt template description (the content out of the codeblock and lists)
3066
- var templateModelRequirements = __assign({}, defaultModelRequirements);
3067
- var listItems_3 = extractAllListItemsFromMarkdown(section.content);
3068
- var dependentParameterNames = new Set();
3069
- var executionType = 'PROMPT_TEMPLATE';
3070
- var jokers = [];
3071
- var postprocessing = [];
3072
- var expectAmount = {};
3073
- var expectFormat = undefined;
3074
- var isExecutionTypeChanged = false;
3075
- try {
3076
- for (var listItems_2 = (e_3 = void 0, __values(listItems_3)), listItems_2_1 = listItems_2.next(); !listItems_2_1.done; listItems_2_1 = listItems_2.next()) {
3077
- var listItem = listItems_2_1.value;
3078
- var command = parseCommand(listItem);
3079
- switch (command.type) {
3080
- case 'JOKER':
3081
- jokers.push(command.parameterName);
3082
- dependentParameterNames.add(command.parameterName);
3083
- break;
3084
- case 'EXECUTE':
3085
- if (isExecutionTypeChanged) {
3086
- throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
3087
- }
3088
- executionType = command.executionType;
3089
- isExecutionTypeChanged = true;
3090
- break;
3091
- case 'MODEL':
3092
- templateModelRequirements[command.key] = command.value;
3093
- break;
3094
- case 'PARAMETER':
3095
- // Note: This is just for detecting resulitng parameter name
3096
- addParam(command);
3097
- break;
3098
- case 'POSTPROCESS':
3099
- postprocessing.push(command.functionName);
3100
- break;
3101
- case 'EXPECT_AMOUNT':
3102
- // eslint-disable-next-line no-case-declarations
3103
- var unit = command.unit.toLowerCase();
3104
- expectAmount[unit] = expectAmount[unit] || {};
3105
- if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
3106
- if (expectAmount[unit].min !== undefined) {
3107
- throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
3108
- }
3109
- expectAmount[unit].min = command.amount;
3110
- } /* not else */
3111
- if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
3112
- if (expectAmount[unit].max !== undefined) {
3113
- throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
3114
- }
3115
- expectAmount[unit].max = command.amount;
3116
- }
3117
- break;
3118
- case 'EXPECT_FORMAT':
3119
- if (expectFormat !== undefined && command.format !== expectFormat) {
3120
- throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
3121
- }
3122
- expectFormat = command.format;
3123
- break;
3124
- default:
3125
- throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
3126
- }
3127
- }
3128
- }
3129
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
3130
- finally {
3131
- try {
3132
- if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
3133
- }
3134
- finally { if (e_3) throw e_3.error; }
3135
- }
3136
- var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
3137
- if (executionType === 'SCRIPT') {
3138
- if (!language) {
3139
- throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
3140
- }
3141
- else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
3142
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Script language ".concat(language, " is not supported.\n\n Supported languages are:\n ").concat(block(SUPPORTED_SCRIPT_LANGUAGES.join(', ')), "\n\n "); }));
3143
- }
3144
- }
3145
- var lastLine = section.content.split('\n').pop();
3146
- var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
3147
- if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
3148
- throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
3149
- // TODO: Show code of invalid sections each time + DRY
3150
- section.content
3151
- .split('\n')
3152
- .map(function (line) { return "> ".concat(line); })
3153
- .join('\n')), "\n "); }));
3154
- }
3155
- var resultingParameterName = match.groups.resultingParamName;
3156
- // TODO: [1] DRY description
3157
- var description_1 = section.content;
3158
- // Note: Remove codeblocks
3159
- description_1 = description_1.split(/^```.*^```/gms).join('');
3160
- //Note: Remove lists and return statement
3161
- description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
3162
- description_1 = spaceTrim(description_1);
3163
- if (description_1 === '') {
3164
- description_1 = undefined;
3165
- }
3166
- if (Object.keys(jokers).length === 0) {
3167
- jokers = undefined;
3168
- }
3169
- if (Object.keys(expectAmount).length === 0) {
3170
- expectAmount = undefined;
3171
- }
3172
- if (Object.keys(postprocessing).length === 0) {
3173
- postprocessing = undefined;
3174
- }
3175
- dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
3176
- if (templateModelRequirements.modelVariant === undefined) {
3177
- templateModelRequirements.modelVariant = 'CHAT';
3178
- }
3179
- var template = {
3180
- name: titleToName(section.title),
3181
- title: section.title,
3182
- description: description_1,
3183
- dependentParameterNames: Array.from(dependentParameterNames),
3184
- executionType: executionType,
3185
- jokers: jokers,
3186
- postprocessing: postprocessing,
3187
- expectations: expectAmount,
3188
- expectFormat: expectFormat,
3189
- modelRequirements: templateModelRequirements,
3190
- contentLanguage: executionType === 'SCRIPT' ? language : undefined,
3191
- content: content,
3192
- resultingParameterName: resultingParameterName,
3193
- };
3194
- if (executionType !== 'PROMPT_TEMPLATE') {
3195
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3196
- delete template.modelRequirements;
3197
- }
3198
- promptbookJson.promptTemplates.push(template);
3199
- };
3200
- try {
3201
- for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
3202
- section = _b.value;
3203
- _loop_1(section);
3204
- }
3205
- }
3206
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
3207
- finally {
3208
- try {
3209
- if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
3210
- }
3211
- finally { if (e_2) throw e_2.error; }
3212
- }
3213
- // =============================================================
3247
+ promptbookJson = promptbookStringToJsonSync(promptbookString);
3214
3248
  return [2 /*return*/, promptbookJson];
3215
3249
  }
3216
3250
  });
3217
3251
  });
3218
3252
  }
3219
3253
  /**
3220
- * TODO: Report here line/column of error
3221
- * TODO: Use spaceTrim more effectively
3222
- * TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
3223
3254
  * TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
3224
3255
  */
3225
3256
 
@@ -4130,5 +4161,5 @@ function executionReportJsonToString(executionReportJson, options) {
4130
4161
  * TODO: [🧠] Allow to filter out some parts of the report by options
4131
4162
  */
4132
4163
 
4133
- export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, ExpectError, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, PromptbookExecutionError, PromptbookLibraryError, PromptbookLogicError, PromptbookNotFoundError, PromptbookReferenceError, PromptbookSyntaxError, SimplePromptInterfaceTools, TemplateError, UnexpectedError, addUsage, assertsExecutionSuccessful, checkExpectations, createLibraryFromJson, createLibraryFromPromise, createLibraryFromUrl, createPromptbookExecutor, createSublibrary, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, libraryToJson, prepareKnowledgeFromMarkdown, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, usageToWorktime, validatePromptbook };
4164
+ export { CallbackInterfaceTools, ExecutionReportStringOptionsDefaults, ExecutionTypes, ExpectError, MultipleLlmExecutionTools, PROMPTBOOK_VERSION, PromptbookExecutionError, PromptbookLibraryError, PromptbookLogicError, PromptbookNotFoundError, PromptbookReferenceError, PromptbookSyntaxError, SimplePromptInterfaceTools, TemplateError, UnexpectedError, addUsage, assertsExecutionSuccessful, checkExpectations, createLibraryFromJson, createLibraryFromPromise, createLibraryFromUrl, createPromptbookExecutor, createSublibrary, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, libraryToJson, prepareKnowledgeFromMarkdown, prettifyPromptbookString, promptbookJsonToString, promptbookStringToJson, promptbookStringToJsonSync, usageToWorktime, validatePromptbook };
4134
4165
  //# sourceMappingURL=index.es.js.map