@promptbook/node 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 +292 -261
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +2 -1
- package/esm/typings/src/conversion/promptbookStringToJson.d.ts +4 -3
- package/esm/typings/src/conversion/promptbookStringToJsonSync.d.ts +23 -0
- package/esm/typings/src/conversion/promptbookStringToJsonSync.test.d.ts +1 -0
- package/package.json +2 -2
- package/umd/index.umd.js +292 -261
- package/umd/index.umd.js.map +1 -1
- package/umd/typings/src/_packages/core.index.d.ts +2 -1
- package/umd/typings/src/conversion/promptbookStringToJson.d.ts +4 -3
- package/umd/typings/src/conversion/promptbookStringToJsonSync.d.ts +23 -0
- package/umd/typings/src/conversion/promptbookStringToJsonSync.test.d.ts +1 -0
package/esm/index.es.js
CHANGED
|
@@ -133,21 +133,7 @@ var LOOP_LIMIT = 1000;
|
|
|
133
133
|
*/
|
|
134
134
|
var PROMPTBOOK_MAKED_BASE_FILENAME = "index";
|
|
135
135
|
|
|
136
|
-
|
|
137
|
-
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
138
|
-
*/
|
|
139
|
-
var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
140
|
-
__extends(PromptbookSyntaxError, _super);
|
|
141
|
-
function PromptbookSyntaxError(message) {
|
|
142
|
-
var _this = _super.call(this, message) || this;
|
|
143
|
-
_this.name = 'PromptbookSyntaxError';
|
|
144
|
-
Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
|
|
145
|
-
return _this;
|
|
146
|
-
}
|
|
147
|
-
return PromptbookSyntaxError;
|
|
148
|
-
}(Error));
|
|
149
|
-
|
|
150
|
-
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:[]}];
|
|
136
|
+
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:[]}];
|
|
151
137
|
|
|
152
138
|
/**
|
|
153
139
|
* This error indicates errors during the execution of the promptbook
|
|
@@ -202,6 +188,20 @@ var PromptbookLogicError = /** @class */ (function (_super) {
|
|
|
202
188
|
return PromptbookLogicError;
|
|
203
189
|
}(Error));
|
|
204
190
|
|
|
191
|
+
/**
|
|
192
|
+
* This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
|
|
193
|
+
*/
|
|
194
|
+
var PromptbookSyntaxError = /** @class */ (function (_super) {
|
|
195
|
+
__extends(PromptbookSyntaxError, _super);
|
|
196
|
+
function PromptbookSyntaxError(message) {
|
|
197
|
+
var _this = _super.call(this, message) || this;
|
|
198
|
+
_this.name = 'PromptbookSyntaxError';
|
|
199
|
+
Object.setPrototypeOf(_this, PromptbookSyntaxError.prototype);
|
|
200
|
+
return _this;
|
|
201
|
+
}
|
|
202
|
+
return PromptbookSyntaxError;
|
|
203
|
+
}(Error));
|
|
204
|
+
|
|
205
205
|
/**
|
|
206
206
|
* This error type indicates that the error should not happen and its last check before crashing with some other error
|
|
207
207
|
*/
|
|
@@ -466,7 +466,7 @@ function isValidJsonString(value /* <-[👨⚖️] */) {
|
|
|
466
466
|
/**
|
|
467
467
|
* The version of the Promptbook library
|
|
468
468
|
*/
|
|
469
|
-
var PROMPTBOOK_VERSION = '0.59.0-
|
|
469
|
+
var PROMPTBOOK_VERSION = '0.59.0-33';
|
|
470
470
|
|
|
471
471
|
/**
|
|
472
472
|
* Function `addUsage` will add multiple usages into one
|
|
@@ -2892,9 +2892,279 @@ function titleToName(value) {
|
|
|
2892
2892
|
return value;
|
|
2893
2893
|
}
|
|
2894
2894
|
|
|
2895
|
+
/**
|
|
2896
|
+
* Compile promptbook from string (markdown) format to JSON format synchronously
|
|
2897
|
+
*
|
|
2898
|
+
* Note: There are two similar functions:
|
|
2899
|
+
* - `promptbookStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
|
|
2900
|
+
* - `promptbookStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
|
|
2901
|
+
*
|
|
2902
|
+
* @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
|
|
2903
|
+
* @param options - Options and tools for the compilation
|
|
2904
|
+
* @returns {Promptbook} compiled in JSON format (.ptbk.json)
|
|
2905
|
+
* @throws {PromptbookSyntaxError} if the promptbook string is not valid
|
|
2906
|
+
*
|
|
2907
|
+
* Note: This function does not validate logic of the pipeline only the syntax
|
|
2908
|
+
* Note: This function acts as compilation process
|
|
2909
|
+
*/
|
|
2910
|
+
function promptbookStringToJsonSync(promptbookString) {
|
|
2911
|
+
var e_1, _a, e_2, _b;
|
|
2912
|
+
var promptbookJson = {
|
|
2913
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2914
|
+
title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
|
|
2915
|
+
promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
|
|
2916
|
+
promptbookVersion: PROMPTBOOK_VERSION,
|
|
2917
|
+
description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
|
|
2918
|
+
parameters: [],
|
|
2919
|
+
promptTemplates: [],
|
|
2920
|
+
knowledge: [],
|
|
2921
|
+
};
|
|
2922
|
+
// =============================================================
|
|
2923
|
+
// Note: 1️⃣ Normalization of the PROMPTBOOK string
|
|
2924
|
+
promptbookString = removeContentComments(promptbookString);
|
|
2925
|
+
promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
|
|
2926
|
+
promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
|
|
2927
|
+
// =============================================================
|
|
2928
|
+
///Note: 2️⃣ Function for adding parameters
|
|
2929
|
+
var addParam = function (parameterCommand) {
|
|
2930
|
+
var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
|
|
2931
|
+
var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
|
|
2932
|
+
if (existingParameter &&
|
|
2933
|
+
existingParameter.description &&
|
|
2934
|
+
existingParameter.description !== parameterDescription &&
|
|
2935
|
+
parameterDescription) {
|
|
2936
|
+
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 "); }));
|
|
2937
|
+
}
|
|
2938
|
+
if (existingParameter) {
|
|
2939
|
+
if (parameterDescription) {
|
|
2940
|
+
existingParameter.description = parameterDescription;
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
else {
|
|
2944
|
+
promptbookJson.parameters.push({
|
|
2945
|
+
name: parameterName,
|
|
2946
|
+
description: parameterDescription || undefined,
|
|
2947
|
+
isInput: isInput,
|
|
2948
|
+
isOutput: isOutput,
|
|
2949
|
+
});
|
|
2950
|
+
}
|
|
2951
|
+
};
|
|
2952
|
+
// =============================================================
|
|
2953
|
+
// Note: 3️⃣ Parse the dynamic part - the template pipeline
|
|
2954
|
+
var markdownStructure = markdownToMarkdownStructure(promptbookString);
|
|
2955
|
+
var markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
|
|
2956
|
+
if (markdownStructureDeepness !== 2) {
|
|
2957
|
+
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 ")));
|
|
2958
|
+
}
|
|
2959
|
+
promptbookJson.title = markdownStructure.title;
|
|
2960
|
+
// TODO: [1] DRY description
|
|
2961
|
+
var description = markdownStructure.content;
|
|
2962
|
+
// Note: Remove codeblocks
|
|
2963
|
+
description = description.split(/^```.*^```/gms).join('');
|
|
2964
|
+
//Note: Remove lists and return statement
|
|
2965
|
+
description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
2966
|
+
description = spaceTrim(description);
|
|
2967
|
+
if (description === '') {
|
|
2968
|
+
description = undefined;
|
|
2969
|
+
}
|
|
2970
|
+
promptbookJson.description = description;
|
|
2971
|
+
var defaultModelRequirements = {};
|
|
2972
|
+
var listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
|
|
2973
|
+
try {
|
|
2974
|
+
for (var listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
|
|
2975
|
+
var listItem = listItems_1_1.value;
|
|
2976
|
+
var command = parseCommand(listItem);
|
|
2977
|
+
switch (command.type) {
|
|
2978
|
+
case 'PROMPTBOOK_URL':
|
|
2979
|
+
promptbookJson.promptbookUrl = command.promptbookUrl.href;
|
|
2980
|
+
break;
|
|
2981
|
+
case 'PROMPTBOOK_VERSION':
|
|
2982
|
+
promptbookJson.promptbookVersion = command.promptbookVersion;
|
|
2983
|
+
break;
|
|
2984
|
+
case 'MODEL':
|
|
2985
|
+
defaultModelRequirements[command.key] = command.value;
|
|
2986
|
+
break;
|
|
2987
|
+
case 'PARAMETER':
|
|
2988
|
+
addParam(command);
|
|
2989
|
+
break;
|
|
2990
|
+
default:
|
|
2991
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
|
|
2992
|
+
}
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
2996
|
+
finally {
|
|
2997
|
+
try {
|
|
2998
|
+
if (listItems_1_1 && !listItems_1_1.done && (_a = listItems_1.return)) _a.call(listItems_1);
|
|
2999
|
+
}
|
|
3000
|
+
finally { if (e_1) throw e_1.error; }
|
|
3001
|
+
}
|
|
3002
|
+
var _loop_1 = function (section) {
|
|
3003
|
+
var e_3, _e;
|
|
3004
|
+
// TODO: Parse prompt template description (the content out of the codeblock and lists)
|
|
3005
|
+
var templateModelRequirements = __assign({}, defaultModelRequirements);
|
|
3006
|
+
var listItems_3 = extractAllListItemsFromMarkdown(section.content);
|
|
3007
|
+
var dependentParameterNames = new Set();
|
|
3008
|
+
var executionType = 'PROMPT_TEMPLATE';
|
|
3009
|
+
var jokers = [];
|
|
3010
|
+
var postprocessing = [];
|
|
3011
|
+
var expectAmount = {};
|
|
3012
|
+
var expectFormat = undefined;
|
|
3013
|
+
var isExecutionTypeChanged = false;
|
|
3014
|
+
try {
|
|
3015
|
+
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()) {
|
|
3016
|
+
var listItem = listItems_2_1.value;
|
|
3017
|
+
var command = parseCommand(listItem);
|
|
3018
|
+
switch (command.type) {
|
|
3019
|
+
case 'JOKER':
|
|
3020
|
+
jokers.push(command.parameterName);
|
|
3021
|
+
dependentParameterNames.add(command.parameterName);
|
|
3022
|
+
break;
|
|
3023
|
+
case 'EXECUTE':
|
|
3024
|
+
if (isExecutionTypeChanged) {
|
|
3025
|
+
throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
|
|
3026
|
+
}
|
|
3027
|
+
executionType = command.executionType;
|
|
3028
|
+
isExecutionTypeChanged = true;
|
|
3029
|
+
break;
|
|
3030
|
+
case 'MODEL':
|
|
3031
|
+
templateModelRequirements[command.key] = command.value;
|
|
3032
|
+
break;
|
|
3033
|
+
case 'PARAMETER':
|
|
3034
|
+
// Note: This is just for detecting resulitng parameter name
|
|
3035
|
+
addParam(command);
|
|
3036
|
+
break;
|
|
3037
|
+
case 'POSTPROCESS':
|
|
3038
|
+
postprocessing.push(command.functionName);
|
|
3039
|
+
break;
|
|
3040
|
+
case 'EXPECT_AMOUNT':
|
|
3041
|
+
// eslint-disable-next-line no-case-declarations
|
|
3042
|
+
var unit = command.unit.toLowerCase();
|
|
3043
|
+
expectAmount[unit] = expectAmount[unit] || {};
|
|
3044
|
+
if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
|
|
3045
|
+
if (expectAmount[unit].min !== undefined) {
|
|
3046
|
+
throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3047
|
+
}
|
|
3048
|
+
expectAmount[unit].min = command.amount;
|
|
3049
|
+
} /* not else */
|
|
3050
|
+
if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
|
|
3051
|
+
if (expectAmount[unit].max !== undefined) {
|
|
3052
|
+
throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3053
|
+
}
|
|
3054
|
+
expectAmount[unit].max = command.amount;
|
|
3055
|
+
}
|
|
3056
|
+
break;
|
|
3057
|
+
case 'EXPECT_FORMAT':
|
|
3058
|
+
if (expectFormat !== undefined && command.format !== expectFormat) {
|
|
3059
|
+
throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
|
|
3060
|
+
}
|
|
3061
|
+
expectFormat = command.format;
|
|
3062
|
+
break;
|
|
3063
|
+
default:
|
|
3064
|
+
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
3069
|
+
finally {
|
|
3070
|
+
try {
|
|
3071
|
+
if (listItems_2_1 && !listItems_2_1.done && (_e = listItems_2.return)) _e.call(listItems_2);
|
|
3072
|
+
}
|
|
3073
|
+
finally { if (e_3) throw e_3.error; }
|
|
3074
|
+
}
|
|
3075
|
+
var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
|
|
3076
|
+
if (executionType === 'SCRIPT') {
|
|
3077
|
+
if (!language) {
|
|
3078
|
+
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
3079
|
+
}
|
|
3080
|
+
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
3081
|
+
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 "); }));
|
|
3082
|
+
}
|
|
3083
|
+
}
|
|
3084
|
+
var lastLine = section.content.split('\n').pop();
|
|
3085
|
+
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
3086
|
+
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
3087
|
+
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
3088
|
+
// TODO: Show code of invalid sections each time + DRY
|
|
3089
|
+
section.content
|
|
3090
|
+
.split('\n')
|
|
3091
|
+
.map(function (line) { return "> ".concat(line); })
|
|
3092
|
+
.join('\n')), "\n "); }));
|
|
3093
|
+
}
|
|
3094
|
+
var resultingParameterName = match.groups.resultingParamName;
|
|
3095
|
+
// TODO: [1] DRY description
|
|
3096
|
+
var description_1 = section.content;
|
|
3097
|
+
// Note: Remove codeblocks
|
|
3098
|
+
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
3099
|
+
//Note: Remove lists and return statement
|
|
3100
|
+
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
3101
|
+
description_1 = spaceTrim(description_1);
|
|
3102
|
+
if (description_1 === '') {
|
|
3103
|
+
description_1 = undefined;
|
|
3104
|
+
}
|
|
3105
|
+
if (Object.keys(jokers).length === 0) {
|
|
3106
|
+
jokers = undefined;
|
|
3107
|
+
}
|
|
3108
|
+
if (Object.keys(expectAmount).length === 0) {
|
|
3109
|
+
expectAmount = undefined;
|
|
3110
|
+
}
|
|
3111
|
+
if (Object.keys(postprocessing).length === 0) {
|
|
3112
|
+
postprocessing = undefined;
|
|
3113
|
+
}
|
|
3114
|
+
dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
|
|
3115
|
+
if (templateModelRequirements.modelVariant === undefined) {
|
|
3116
|
+
templateModelRequirements.modelVariant = 'CHAT';
|
|
3117
|
+
}
|
|
3118
|
+
var template = {
|
|
3119
|
+
name: titleToName(section.title),
|
|
3120
|
+
title: section.title,
|
|
3121
|
+
description: description_1,
|
|
3122
|
+
dependentParameterNames: Array.from(dependentParameterNames),
|
|
3123
|
+
executionType: executionType,
|
|
3124
|
+
jokers: jokers,
|
|
3125
|
+
postprocessing: postprocessing,
|
|
3126
|
+
expectations: expectAmount,
|
|
3127
|
+
expectFormat: expectFormat,
|
|
3128
|
+
modelRequirements: templateModelRequirements,
|
|
3129
|
+
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
3130
|
+
content: content,
|
|
3131
|
+
resultingParameterName: resultingParameterName,
|
|
3132
|
+
};
|
|
3133
|
+
if (executionType !== 'PROMPT_TEMPLATE') {
|
|
3134
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3135
|
+
delete template.modelRequirements;
|
|
3136
|
+
}
|
|
3137
|
+
promptbookJson.promptTemplates.push(template);
|
|
3138
|
+
};
|
|
3139
|
+
try {
|
|
3140
|
+
for (var _c = __values(markdownStructure.sections), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
3141
|
+
var section = _d.value;
|
|
3142
|
+
_loop_1(section);
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
3146
|
+
finally {
|
|
3147
|
+
try {
|
|
3148
|
+
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
3149
|
+
}
|
|
3150
|
+
finally { if (e_2) throw e_2.error; }
|
|
3151
|
+
}
|
|
3152
|
+
// =============================================================
|
|
3153
|
+
return promptbookJson;
|
|
3154
|
+
}
|
|
3155
|
+
/**
|
|
3156
|
+
* TODO: Report here line/column of error
|
|
3157
|
+
* TODO: Use spaceTrim more effectively
|
|
3158
|
+
* TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
|
|
3159
|
+
*/
|
|
3160
|
+
|
|
2895
3161
|
/**
|
|
2896
3162
|
* Compile promptbook from string (markdown) format to JSON format
|
|
2897
3163
|
*
|
|
3164
|
+
* Note: There are two similar functions:
|
|
3165
|
+
* - `promptbookStringToJson` **(preferred)** - which propperly compiles the promptbook and use embedding for external knowledge
|
|
3166
|
+
* - `promptbookStringToJsonSync` - use only if you need to compile promptbook synchronously and it contains NO external knowledge
|
|
3167
|
+
*
|
|
2898
3168
|
* @param promptbookString {Promptbook} in string markdown format (.ptbk.md)
|
|
2899
3169
|
* @param options - Options and tools for the compilation
|
|
2900
3170
|
* @returns {Promptbook} compiled in JSON format (.ptbk.json)
|
|
@@ -2906,267 +3176,28 @@ function titleToName(value) {
|
|
|
2906
3176
|
function promptbookStringToJson(promptbookString, options) {
|
|
2907
3177
|
if (options === void 0) { options = {}; }
|
|
2908
3178
|
return __awaiter(this, void 0, void 0, function () {
|
|
2909
|
-
var llmTools,
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
switch (_e.label) {
|
|
3179
|
+
var llmTools, knowledge, promptbookJson;
|
|
3180
|
+
return __generator(this, function (_a) {
|
|
3181
|
+
switch (_a.label) {
|
|
2913
3182
|
case 0:
|
|
2914
3183
|
llmTools = options.llmTools;
|
|
2915
|
-
promptbookJson = {
|
|
2916
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2917
|
-
title: undefined /* <- Note: Putting here placeholder to keep `title` on top at final JSON */,
|
|
2918
|
-
promptbookUrl: undefined /* <- Note: Putting here placeholder to keep `promptbookUrl` on top at final JSON */,
|
|
2919
|
-
promptbookVersion: PROMPTBOOK_VERSION,
|
|
2920
|
-
description: undefined /* <- Note: Putting here placeholder to keep `description` on top at final JSON */,
|
|
2921
|
-
parameters: [],
|
|
2922
|
-
promptTemplates: [],
|
|
2923
|
-
knowledge: [],
|
|
2924
|
-
};
|
|
2925
3184
|
if (!llmTools) return [3 /*break*/, 2];
|
|
2926
3185
|
return [4 /*yield*/, prepareKnowledgeFromMarkdown({
|
|
2927
3186
|
content: 'Roses are red, violets are blue, programmers use Promptbook, users too',
|
|
2928
3187
|
llmTools: llmTools,
|
|
2929
3188
|
})];
|
|
2930
3189
|
case 1:
|
|
2931
|
-
knowledge =
|
|
3190
|
+
knowledge = _a.sent();
|
|
2932
3191
|
console.info('!!!! knowledge', knowledge);
|
|
2933
|
-
|
|
3192
|
+
_a.label = 2;
|
|
2934
3193
|
case 2:
|
|
2935
|
-
|
|
2936
|
-
// Note: 1️⃣ Normalization of the PROMPTBOOK string
|
|
2937
|
-
promptbookString = removeContentComments(promptbookString);
|
|
2938
|
-
promptbookString = promptbookString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
|
|
2939
|
-
promptbookString = promptbookString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
|
|
2940
|
-
addParam = function (parameterCommand) {
|
|
2941
|
-
var parameterName = parameterCommand.parameterName, parameterDescription = parameterCommand.parameterDescription, isInput = parameterCommand.isInput, isOutput = parameterCommand.isOutput;
|
|
2942
|
-
var existingParameter = promptbookJson.parameters.find(function (parameter) { return parameter.name === parameterName; });
|
|
2943
|
-
if (existingParameter &&
|
|
2944
|
-
existingParameter.description &&
|
|
2945
|
-
existingParameter.description !== parameterDescription &&
|
|
2946
|
-
parameterDescription) {
|
|
2947
|
-
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 "); }));
|
|
2948
|
-
}
|
|
2949
|
-
if (existingParameter) {
|
|
2950
|
-
if (parameterDescription) {
|
|
2951
|
-
existingParameter.description = parameterDescription;
|
|
2952
|
-
}
|
|
2953
|
-
}
|
|
2954
|
-
else {
|
|
2955
|
-
promptbookJson.parameters.push({
|
|
2956
|
-
name: parameterName,
|
|
2957
|
-
description: parameterDescription || undefined,
|
|
2958
|
-
isInput: isInput,
|
|
2959
|
-
isOutput: isOutput,
|
|
2960
|
-
});
|
|
2961
|
-
}
|
|
2962
|
-
};
|
|
2963
|
-
markdownStructure = markdownToMarkdownStructure(promptbookString);
|
|
2964
|
-
markdownStructureDeepness = countMarkdownStructureDeepness(markdownStructure);
|
|
2965
|
-
if (markdownStructureDeepness !== 2) {
|
|
2966
|
-
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 ")));
|
|
2967
|
-
}
|
|
2968
|
-
promptbookJson.title = markdownStructure.title;
|
|
2969
|
-
description = markdownStructure.content;
|
|
2970
|
-
// Note: Remove codeblocks
|
|
2971
|
-
description = description.split(/^```.*^```/gms).join('');
|
|
2972
|
-
//Note: Remove lists and return statement
|
|
2973
|
-
description = description.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
2974
|
-
description = spaceTrim(description);
|
|
2975
|
-
if (description === '') {
|
|
2976
|
-
description = undefined;
|
|
2977
|
-
}
|
|
2978
|
-
promptbookJson.description = description;
|
|
2979
|
-
defaultModelRequirements = {};
|
|
2980
|
-
listItems = extractAllListItemsFromMarkdown(markdownStructure.content);
|
|
2981
|
-
try {
|
|
2982
|
-
for (listItems_1 = __values(listItems), listItems_1_1 = listItems_1.next(); !listItems_1_1.done; listItems_1_1 = listItems_1.next()) {
|
|
2983
|
-
listItem = listItems_1_1.value;
|
|
2984
|
-
command = parseCommand(listItem);
|
|
2985
|
-
switch (command.type) {
|
|
2986
|
-
case 'PROMPTBOOK_URL':
|
|
2987
|
-
promptbookJson.promptbookUrl = command.promptbookUrl.href;
|
|
2988
|
-
break;
|
|
2989
|
-
case 'PROMPTBOOK_VERSION':
|
|
2990
|
-
promptbookJson.promptbookVersion = command.promptbookVersion;
|
|
2991
|
-
break;
|
|
2992
|
-
case 'MODEL':
|
|
2993
|
-
defaultModelRequirements[command.key] = command.value;
|
|
2994
|
-
break;
|
|
2995
|
-
case 'PARAMETER':
|
|
2996
|
-
addParam(command);
|
|
2997
|
-
break;
|
|
2998
|
-
default:
|
|
2999
|
-
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the head of the promptbook ONLY at the prompt template block"));
|
|
3000
|
-
}
|
|
3001
|
-
}
|
|
3002
|
-
}
|
|
3003
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
3004
|
-
finally {
|
|
3005
|
-
try {
|
|
3006
|
-
if (listItems_1_1 && !listItems_1_1.done && (_c = listItems_1.return)) _c.call(listItems_1);
|
|
3007
|
-
}
|
|
3008
|
-
finally { if (e_1) throw e_1.error; }
|
|
3009
|
-
}
|
|
3010
|
-
_loop_1 = function (section) {
|
|
3011
|
-
var e_3, _f;
|
|
3012
|
-
// TODO: Parse prompt template description (the content out of the codeblock and lists)
|
|
3013
|
-
var templateModelRequirements = __assign({}, defaultModelRequirements);
|
|
3014
|
-
var listItems_3 = extractAllListItemsFromMarkdown(section.content);
|
|
3015
|
-
var dependentParameterNames = new Set();
|
|
3016
|
-
var executionType = 'PROMPT_TEMPLATE';
|
|
3017
|
-
var jokers = [];
|
|
3018
|
-
var postprocessing = [];
|
|
3019
|
-
var expectAmount = {};
|
|
3020
|
-
var expectFormat = undefined;
|
|
3021
|
-
var isExecutionTypeChanged = false;
|
|
3022
|
-
try {
|
|
3023
|
-
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()) {
|
|
3024
|
-
var listItem = listItems_2_1.value;
|
|
3025
|
-
var command = parseCommand(listItem);
|
|
3026
|
-
switch (command.type) {
|
|
3027
|
-
case 'JOKER':
|
|
3028
|
-
jokers.push(command.parameterName);
|
|
3029
|
-
dependentParameterNames.add(command.parameterName);
|
|
3030
|
-
break;
|
|
3031
|
-
case 'EXECUTE':
|
|
3032
|
-
if (isExecutionTypeChanged) {
|
|
3033
|
-
throw new PromptbookSyntaxError('Execution type is already defined in the prompt template. It can be defined only once.');
|
|
3034
|
-
}
|
|
3035
|
-
executionType = command.executionType;
|
|
3036
|
-
isExecutionTypeChanged = true;
|
|
3037
|
-
break;
|
|
3038
|
-
case 'MODEL':
|
|
3039
|
-
templateModelRequirements[command.key] = command.value;
|
|
3040
|
-
break;
|
|
3041
|
-
case 'PARAMETER':
|
|
3042
|
-
// Note: This is just for detecting resulitng parameter name
|
|
3043
|
-
addParam(command);
|
|
3044
|
-
break;
|
|
3045
|
-
case 'POSTPROCESS':
|
|
3046
|
-
postprocessing.push(command.functionName);
|
|
3047
|
-
break;
|
|
3048
|
-
case 'EXPECT_AMOUNT':
|
|
3049
|
-
// eslint-disable-next-line no-case-declarations
|
|
3050
|
-
var unit = command.unit.toLowerCase();
|
|
3051
|
-
expectAmount[unit] = expectAmount[unit] || {};
|
|
3052
|
-
if (command.sign === 'MINIMUM' || command.sign === 'EXACTLY') {
|
|
3053
|
-
if (expectAmount[unit].min !== undefined) {
|
|
3054
|
-
throw new PromptbookSyntaxError("Already defined minumum ".concat(expectAmount[unit].min, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3055
|
-
}
|
|
3056
|
-
expectAmount[unit].min = command.amount;
|
|
3057
|
-
} /* not else */
|
|
3058
|
-
if (command.sign === 'MAXIMUM' || command.sign === 'EXACTLY') {
|
|
3059
|
-
if (expectAmount[unit].max !== undefined) {
|
|
3060
|
-
throw new PromptbookSyntaxError("Already defined maximum ".concat(expectAmount[unit].max, " ").concat(command.unit.toLowerCase(), ", now trying to redefine it to ").concat(command.amount));
|
|
3061
|
-
}
|
|
3062
|
-
expectAmount[unit].max = command.amount;
|
|
3063
|
-
}
|
|
3064
|
-
break;
|
|
3065
|
-
case 'EXPECT_FORMAT':
|
|
3066
|
-
if (expectFormat !== undefined && command.format !== expectFormat) {
|
|
3067
|
-
throw new PromptbookSyntaxError("Expect format is already defined to \"".concat(expectFormat, "\". Now you try to redefine it by \"").concat(command.format, "\"."));
|
|
3068
|
-
}
|
|
3069
|
-
expectFormat = command.format;
|
|
3070
|
-
break;
|
|
3071
|
-
default:
|
|
3072
|
-
throw new PromptbookSyntaxError("Command ".concat(command.type, " is not allowed in the block of the prompt template ONLY at the head of the promptbook"));
|
|
3073
|
-
}
|
|
3074
|
-
}
|
|
3075
|
-
}
|
|
3076
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
3077
|
-
finally {
|
|
3078
|
-
try {
|
|
3079
|
-
if (listItems_2_1 && !listItems_2_1.done && (_f = listItems_2.return)) _f.call(listItems_2);
|
|
3080
|
-
}
|
|
3081
|
-
finally { if (e_3) throw e_3.error; }
|
|
3082
|
-
}
|
|
3083
|
-
var _g = extractOneBlockFromMarkdown(section.content), language = _g.language, content = _g.content;
|
|
3084
|
-
if (executionType === 'SCRIPT') {
|
|
3085
|
-
if (!language) {
|
|
3086
|
-
throw new PromptbookSyntaxError('You must specify the language of the script in the prompt template');
|
|
3087
|
-
}
|
|
3088
|
-
else if (!SUPPORTED_SCRIPT_LANGUAGES.includes(language)) {
|
|
3089
|
-
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 "); }));
|
|
3090
|
-
}
|
|
3091
|
-
}
|
|
3092
|
-
var lastLine = section.content.split('\n').pop();
|
|
3093
|
-
var match = /^->\s*\{(?<resultingParamName>[a-z0-9_]+)\}/im.exec(lastLine);
|
|
3094
|
-
if (!match || match.groups === undefined || match.groups.resultingParamName === undefined) {
|
|
3095
|
-
throw new PromptbookSyntaxError(spaceTrim(function (block) { return "\n Invalid template - each section must end with \"-> {...}\"\n\n Invalid section:\n ".concat(block(
|
|
3096
|
-
// TODO: Show code of invalid sections each time + DRY
|
|
3097
|
-
section.content
|
|
3098
|
-
.split('\n')
|
|
3099
|
-
.map(function (line) { return "> ".concat(line); })
|
|
3100
|
-
.join('\n')), "\n "); }));
|
|
3101
|
-
}
|
|
3102
|
-
var resultingParameterName = match.groups.resultingParamName;
|
|
3103
|
-
// TODO: [1] DRY description
|
|
3104
|
-
var description_1 = section.content;
|
|
3105
|
-
// Note: Remove codeblocks
|
|
3106
|
-
description_1 = description_1.split(/^```.*^```/gms).join('');
|
|
3107
|
-
//Note: Remove lists and return statement
|
|
3108
|
-
description_1 = description_1.split(/^(?:(?:-)|(?:\d\))|(?:`?->))\s+.*$/gm).join('');
|
|
3109
|
-
description_1 = spaceTrim(description_1);
|
|
3110
|
-
if (description_1 === '') {
|
|
3111
|
-
description_1 = undefined;
|
|
3112
|
-
}
|
|
3113
|
-
if (Object.keys(jokers).length === 0) {
|
|
3114
|
-
jokers = undefined;
|
|
3115
|
-
}
|
|
3116
|
-
if (Object.keys(expectAmount).length === 0) {
|
|
3117
|
-
expectAmount = undefined;
|
|
3118
|
-
}
|
|
3119
|
-
if (Object.keys(postprocessing).length === 0) {
|
|
3120
|
-
postprocessing = undefined;
|
|
3121
|
-
}
|
|
3122
|
-
dependentParameterNames = union(dependentParameterNames, extractParametersFromPromptTemplate(__assign(__assign({}, section), { description: description_1, executionType: executionType, content: content })));
|
|
3123
|
-
if (templateModelRequirements.modelVariant === undefined) {
|
|
3124
|
-
templateModelRequirements.modelVariant = 'CHAT';
|
|
3125
|
-
}
|
|
3126
|
-
var template = {
|
|
3127
|
-
name: titleToName(section.title),
|
|
3128
|
-
title: section.title,
|
|
3129
|
-
description: description_1,
|
|
3130
|
-
dependentParameterNames: Array.from(dependentParameterNames),
|
|
3131
|
-
executionType: executionType,
|
|
3132
|
-
jokers: jokers,
|
|
3133
|
-
postprocessing: postprocessing,
|
|
3134
|
-
expectations: expectAmount,
|
|
3135
|
-
expectFormat: expectFormat,
|
|
3136
|
-
modelRequirements: templateModelRequirements,
|
|
3137
|
-
contentLanguage: executionType === 'SCRIPT' ? language : undefined,
|
|
3138
|
-
content: content,
|
|
3139
|
-
resultingParameterName: resultingParameterName,
|
|
3140
|
-
};
|
|
3141
|
-
if (executionType !== 'PROMPT_TEMPLATE') {
|
|
3142
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3143
|
-
delete template.modelRequirements;
|
|
3144
|
-
}
|
|
3145
|
-
promptbookJson.promptTemplates.push(template);
|
|
3146
|
-
};
|
|
3147
|
-
try {
|
|
3148
|
-
for (_a = __values(markdownStructure.sections), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
3149
|
-
section = _b.value;
|
|
3150
|
-
_loop_1(section);
|
|
3151
|
-
}
|
|
3152
|
-
}
|
|
3153
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
3154
|
-
finally {
|
|
3155
|
-
try {
|
|
3156
|
-
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
3157
|
-
}
|
|
3158
|
-
finally { if (e_2) throw e_2.error; }
|
|
3159
|
-
}
|
|
3160
|
-
// =============================================================
|
|
3194
|
+
promptbookJson = promptbookStringToJsonSync(promptbookString);
|
|
3161
3195
|
return [2 /*return*/, promptbookJson];
|
|
3162
3196
|
}
|
|
3163
3197
|
});
|
|
3164
3198
|
});
|
|
3165
3199
|
}
|
|
3166
3200
|
/**
|
|
3167
|
-
* TODO: Report here line/column of error
|
|
3168
|
-
* TODO: Use spaceTrim more effectively
|
|
3169
|
-
* TODO: [🧠] Parameter flags - isInput, isOutput, isInternal
|
|
3170
3201
|
* TODO: [🏏] Leverage the batch API and build queues @see https://platform.openai.com/docs/guides/batch
|
|
3171
3202
|
*/
|
|
3172
3203
|
|