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