@promptbook/node 0.59.0-24 → 0.59.0-26

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
@@ -1,4 +1,5 @@
1
- import { readFile, readdir } from 'fs/promises';
1
+ import colors from 'colors';
2
+ import { access, constants, readFile, readdir } from 'fs/promises';
2
3
  import { join } from 'path';
3
4
  import spaceTrim$1, { spaceTrim } from 'spacetrim';
4
5
  import { format } from 'prettier';
@@ -123,6 +124,15 @@ function __spreadArray(to, from, pack) {
123
124
  return to.concat(ar || Array.prototype.slice.call(from));
124
125
  }
125
126
 
127
+ /**
128
+ * The maximum number of iterations for a loops
129
+ */
130
+ var LOOP_LIMIT = 1000;
131
+ /**
132
+ * The name of the builded promptbook library made by CLI `promptbook make` and for lookup in `createLibraryFromDirectory`
133
+ */
134
+ var PROMPTBOOK_MAKED_BASE_FILENAME = "promptbook-library";
135
+
126
136
  /**
127
137
  * This error indicates that the promptbook in a markdown format cannot be parsed into a valid promptbook object
128
138
  */
@@ -137,7 +147,7 @@ var PromptbookSyntaxError = /** @class */ (function (_super) {
137
147
  return PromptbookSyntaxError;
138
148
  }(Error));
139
149
 
140
- var promptbookLibrary = [{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-23",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:[]}];
150
+ var promptbookLibrary = [{title:"Prepare Knowledge from Markdown",promptbookUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.59.0-25",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
151
 
142
152
  /**
143
153
  * This error indicates errors during the execution of the promptbook
@@ -178,11 +188,6 @@ function assertsExecutionSuccessful(executionResult) {
178
188
  * TODO: [🧠] Can this return type be better typed than void
179
189
  */
180
190
 
181
- /**
182
- * The maximum number of iterations for a loops
183
- */
184
- var LOOP_LIMIT = 1000;
185
-
186
191
  /**
187
192
  * This error indicates that the promptbook object has valid syntax but contains logical errors (like circular dependencies)
188
193
  */
@@ -250,7 +255,7 @@ function isValidUrl(url) {
250
255
  * @returns the same promptbook if it is logically valid
251
256
  * @throws {PromptbookLogicError} on logical error in the promptbook
252
257
  */
253
- function validatePromptbookJson(promptbook) {
258
+ function validatePromptbook(promptbook) {
254
259
  // TODO: [🧠] Maybe test if promptbook is a promise and make specific error case for that
255
260
  var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
256
261
  if (promptbook.promptbookUrl !== undefined) {
@@ -314,8 +319,7 @@ function validatePromptbookJson(promptbook) {
314
319
  throw new PromptbookLogicError("Parameter {".concat(template.resultingParameterName, "} is defined multiple times"));
315
320
  }
316
321
  definedParameters.add(template.resultingParameterName);
317
- if (template.executionType === 'PROMPT_TEMPLATE' &&
318
- (template.modelRequirements.modelVariant === undefined)) {
322
+ if (template.executionType === 'PROMPT_TEMPLATE' && template.modelRequirements.modelVariant === undefined) {
319
323
  throw new PromptbookLogicError(spaceTrim("\n\n You must specify MODEL VARIANT in the prompt template \"".concat(template.title, "\"\n\n For example:\n - MODEL VARIANT Chat\n - MODEL NAME `gpt-4-1106-preview`\n\n ")));
320
324
  }
321
325
  if (template.jokers && template.jokers.length > 0) {
@@ -385,7 +389,7 @@ function validatePromptbookJson(promptbook) {
385
389
  var loopLimit = LOOP_LIMIT;
386
390
  var _loop_2 = function () {
387
391
  if (loopLimit-- < 0) {
388
- throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbookJson`');
392
+ throw new UnexpectedError('Loop limit reached during detection of circular dependencies in `validatePromptbook`');
389
393
  }
390
394
  var currentlyResovedTemplates = unresovedTemplates.filter(function (template) {
391
395
  return template.dependentParameterNames.every(function (name) { return resovedParameters.includes(name); });
@@ -420,7 +424,7 @@ function validatePromptbookJson(promptbook) {
420
424
  * > * It checks:
421
425
  * > * - it has a valid structure
422
426
  * > * - ...
423
- * > ex port function validatePromptbookJson(promptbook: unknown): asserts promptbook is PromptbookJson {
427
+ * > ex port function validatePromptbook(promptbook: unknown): asserts promptbook is PromptbookJson {
424
428
  */
425
429
 
426
430
  /**
@@ -462,7 +466,7 @@ function isValidJsonString(value /* <-[👨‍⚖️] */) {
462
466
  /**
463
467
  * The version of the Promptbook library
464
468
  */
465
- var PROMPTBOOK_VERSION = '0.59.0-23';
469
+ var PROMPTBOOK_VERSION = '0.59.0-25';
466
470
 
467
471
  /**
468
472
  * Function `addUsage` will add multiple usages into one
@@ -1003,7 +1007,7 @@ function createPromptbookExecutor(options) {
1003
1007
  var _this = this;
1004
1008
  var promptbook = options.promptbook, tools = options.tools, _a = options.settings, settings = _a === void 0 ? {} : _a;
1005
1009
  var _b = settings.maxExecutionAttempts, maxExecutionAttempts = _b === void 0 ? 3 : _b;
1006
- validatePromptbookJson(promptbook);
1010
+ validatePromptbook(promptbook);
1007
1011
  var promptbookExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
1008
1012
  function executeSingleTemplate(currentTemplate) {
1009
1013
  return __awaiter(this, void 0, void 0, function () {
@@ -1434,7 +1438,7 @@ function createPromptbookExecutor(options) {
1434
1438
  return template.dependentParameterNames.every(function (name) { return resovedParameters_1.includes(name); });
1435
1439
  });
1436
1440
  if (!(!currentTemplate && resolving_1.length === 0)) return [3 /*break*/, 1];
1437
- throw new UnexpectedError(spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbookJson\n "));
1441
+ throw new UnexpectedError(spaceTrim("\n Can not resolve some parameters\n\n Note: This should be catched during validatePromptbook\n "));
1438
1442
  case 1:
1439
1443
  if (!!currentTemplate) return [3 /*break*/, 3];
1440
1444
  /* [5] */ return [4 /*yield*/, Promise.race(resolving_1)];
@@ -1821,7 +1825,7 @@ var SimplePromptbookLibrary = /** @class */ (function () {
1821
1825
  if (promptbook.promptbookUrl === undefined) {
1822
1826
  throw new PromptbookReferenceError(spaceTrim("\n Promptbook with name \"".concat(promptbook.title, "\" does not have defined URL\n\n Note: Promptbooks without URLs are called anonymous promptbooks\n They can be used as standalone promptbooks, but they cannot be referenced by other promptbooks\n And also they cannot be used in the promptbook library\n\n ")));
1823
1827
  }
1824
- validatePromptbookJson(promptbook);
1828
+ validatePromptbook(promptbook);
1825
1829
  // Note: [🦄]
1826
1830
  if (this.library.has(promptbook.promptbookUrl) &&
1827
1831
  promptbookJsonToString(promptbook) !==
@@ -1886,11 +1890,7 @@ function createLibraryFromJson() {
1886
1890
  for (var _i = 0; _i < arguments.length; _i++) {
1887
1891
  promptbooks[_i] = arguments[_i];
1888
1892
  }
1889
- return __awaiter(this, void 0, void 0, function () {
1890
- return __generator(this, function (_a) {
1891
- return [2 /*return*/, new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))()];
1892
- });
1893
- });
1893
+ return new (SimplePromptbookLibrary.bind.apply(SimplePromptbookLibrary, __spreadArray([void 0], __read(promptbooks), false)))();
1894
1894
  }
1895
1895
 
1896
1896
  /* tslint:disable */
@@ -1956,9 +1956,7 @@ function prepareKnowledgeFromMarkdown(options) {
1956
1956
  switch (_b.label) {
1957
1957
  case 0:
1958
1958
  content = options.content, llmTools = options.llmTools, _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
1959
- return [4 /*yield*/, createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookLibrary), false))];
1960
- case 1:
1961
- library = _b.sent();
1959
+ library = createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookLibrary), false));
1962
1960
  promptbook = library.getPromptbookByUrl('https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md');
1963
1961
  executor = createPromptbookExecutor({
1964
1962
  promptbook: promptbook,
@@ -1970,7 +1968,7 @@ function prepareKnowledgeFromMarkdown(options) {
1970
1968
  },
1971
1969
  });
1972
1970
  return [4 /*yield*/, executor({ content: content })];
1973
- case 2:
1971
+ case 1:
1974
1972
  result = _b.sent();
1975
1973
  assertsExecutionSuccessful(result);
1976
1974
  outputParameters = result.outputParameters;
@@ -2009,7 +2007,7 @@ function prepareKnowledgeFromMarkdown(options) {
2009
2007
  }];
2010
2008
  });
2011
2009
  }); }))];
2012
- case 3:
2010
+ case 2:
2013
2011
  knowledge = _b.sent();
2014
2012
  return [2 /*return*/, knowledge];
2015
2013
  }
@@ -3192,9 +3190,7 @@ function createLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
3192
3190
  return [4 /*yield*/, promptbookSourcesPromiseOrFactory];
3193
3191
  case 1:
3194
3192
  promptbookSources = _a.sent();
3195
- return [4 /*yield*/, createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookSources), false))];
3196
- case 2:
3197
- library = _a.sent();
3193
+ library = createLibraryFromJson.apply(void 0, __spreadArray([], __read(promptbookSources), false));
3198
3194
  return [2 /*return*/];
3199
3195
  }
3200
3196
  });
@@ -3254,7 +3250,7 @@ function createLibraryFromPromise(promptbookSourcesPromiseOrFactory) {
3254
3250
  */
3255
3251
  function createLibraryFromDirectory(path, options) {
3256
3252
  return __awaiter(this, void 0, void 0, function () {
3257
- var _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
3253
+ var makedLibraryFilePath, makedLibraryFileExists, _a, _b, isRecursive, _c, isVerbose, _d, isLazyLoaded, _e, isCrashOnError, library;
3258
3254
  var _this = this;
3259
3255
  return __generator(this, function (_f) {
3260
3256
  switch (_f.label) {
@@ -3262,6 +3258,19 @@ function createLibraryFromDirectory(path, options) {
3262
3258
  if (!isRunningInNode()) {
3263
3259
  throw new Error('Function `createLibraryFromDirectory` can only be run in Node.js environment because it reads the file system.');
3264
3260
  }
3261
+ makedLibraryFilePath = join(path, "".concat(PROMPTBOOK_MAKED_BASE_FILENAME, ".json"));
3262
+ return [4 /*yield*/, access(makedLibraryFilePath, constants.R_OK)
3263
+ .then(function () { return true; })
3264
+ .catch(function () { return false; })];
3265
+ case 1:
3266
+ makedLibraryFileExists = _f.sent();
3267
+ if (!makedLibraryFileExists) {
3268
+ console.info(colors.yellow("Tip: Prebuild your promptbook library (file with supposed prebuild ".concat(makedLibraryFilePath, " not found) with CLI util \"promptbook make\" to speed up the library creation.")));
3269
+ }
3270
+ else {
3271
+ colors.green("Using your prebuild promptbook library ".concat(makedLibraryFilePath));
3272
+ // TODO: !!!!! Implement;
3273
+ }
3265
3274
  _a = options || {}, _b = _a.isRecursive, isRecursive = _b === void 0 ? true : _b, _c = _a.isVerbose, isVerbose = _c === void 0 ? false : _c, _d = _a.isLazyLoaded, isLazyLoaded = _d === void 0 ? false : _d, _e = _a.isCrashOnError, isCrashOnError = _e === void 0 ? true : _e;
3266
3275
  library = createLibraryFromPromise(function () { return __awaiter(_this, void 0, void 0, function () {
3267
3276
  var fileNames, promptbooks, _loop_1, fileNames_1, fileNames_1_1, fileName, e_1_1;
@@ -3325,7 +3334,7 @@ function createLibraryFromDirectory(path, options) {
3325
3334
  if (!isCrashOnError) {
3326
3335
  // Note: Validate promptbook to check if it is logically correct to not crash on invalid promptbooks
3327
3336
  // But be handled in current try-catch block
3328
- validatePromptbookJson(promptbook);
3337
+ validatePromptbook(promptbook);
3329
3338
  }
3330
3339
  // Note: [🦄] Promptbook with same url uniqueness will be checked automatically in SimplePromptbookLibrary
3331
3340
  promptbooks.push(promptbook);
@@ -3377,12 +3386,12 @@ function createLibraryFromDirectory(path, options) {
3377
3386
  }
3378
3387
  });
3379
3388
  }); });
3380
- if (!(isLazyLoaded === false)) return [3 /*break*/, 2];
3389
+ if (!(isLazyLoaded === false)) return [3 /*break*/, 3];
3381
3390
  return [4 /*yield*/, library.listPromptbooks()];
3382
- case 1:
3391
+ case 2:
3383
3392
  _f.sent();
3384
- _f.label = 2;
3385
- case 2: return [2 /*return*/, library];
3393
+ _f.label = 3;
3394
+ case 3: return [2 /*return*/, library];
3386
3395
  }
3387
3396
  });
3388
3397
  });