@promptbook/remote-server 0.92.0-5 → 0.92.0-6

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
@@ -33,7 +33,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
33
33
  * @generated
34
34
  * @see https://github.com/webgptorg/promptbook
35
35
  */
36
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-5';
36
+ const PROMPTBOOK_ENGINE_VERSION = '0.92.0-6';
37
37
  /**
38
38
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
39
39
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -1833,6 +1833,45 @@ function isValidJsonString(value /* <- [👨‍⚖️] */) {
1833
1833
  }
1834
1834
  }
1835
1835
 
1836
+ /**
1837
+ * Converts a JavaScript Object Notation (JSON) string into an object.
1838
+ *
1839
+ * Note: This is wrapper around `JSON.parse()` with better error and type handling
1840
+ *
1841
+ * @public exported from `@promptbook/utils`
1842
+ */
1843
+ function jsonParse(value) {
1844
+ if (value === undefined) {
1845
+ throw new Error(`Can not parse JSON from undefined value.`);
1846
+ }
1847
+ else if (typeof value !== 'string') {
1848
+ console.error('Can not parse JSON from non-string value.', { text: value });
1849
+ throw new Error(spaceTrim(`
1850
+ Can not parse JSON from non-string value.
1851
+
1852
+ The value type: ${typeof value}
1853
+ See more in console.
1854
+ `));
1855
+ }
1856
+ try {
1857
+ return JSON.parse(value);
1858
+ }
1859
+ catch (error) {
1860
+ if (!(error instanceof Error)) {
1861
+ throw error;
1862
+ }
1863
+ throw new Error(spaceTrim((block) => `
1864
+ ${block(error.message)}
1865
+
1866
+ The JSON text:
1867
+ ${block(value)}
1868
+ `));
1869
+ }
1870
+ }
1871
+ /**
1872
+ * TODO: !!!! Use in Promptbook.studio
1873
+ */
1874
+
1836
1875
  /**
1837
1876
  * Recursively converts JSON strings to JSON objects
1838
1877
 
@@ -1851,7 +1890,7 @@ function jsonStringsToJsons(object) {
1851
1890
  const newObject = { ...object };
1852
1891
  for (const [key, value] of Object.entries(object)) {
1853
1892
  if (typeof value === 'string' && isValidJsonString(value)) {
1854
- newObject[key] = JSON.parse(value);
1893
+ newObject[key] = jsonParse(value);
1855
1894
  }
1856
1895
  else {
1857
1896
  newObject[key] = jsonStringsToJsons(value);
@@ -2934,7 +2973,7 @@ async function preparePersona(personaDescription, tools, options) {
2934
2973
  }).asPromise();
2935
2974
  const { outputParameters } = result;
2936
2975
  const { modelsRequirements: modelsRequirementsJson } = outputParameters;
2937
- const modelsRequirementsUnchecked = JSON.parse(modelsRequirementsJson);
2976
+ const modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
2938
2977
  if (isVerbose) {
2939
2978
  console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
2940
2979
  }
@@ -3776,7 +3815,7 @@ async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
3776
3815
  > },
3777
3816
  */
3778
3817
  async asJson() {
3779
- return JSON.parse(await tools.fs.readFile(filename, 'utf-8'));
3818
+ return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
3780
3819
  },
3781
3820
  async asText() {
3782
3821
  return await tools.fs.readFile(filename, 'utf-8');
@@ -5480,13 +5519,79 @@ async function getExamplesForTask(task) {
5480
5519
  /**
5481
5520
  * @@@
5482
5521
  *
5522
+ * Here is the place where RAG (retrieval-augmented generation) happens
5523
+ *
5483
5524
  * @private internal utility of `createPipelineExecutor`
5484
5525
  */
5485
5526
  async function getKnowledgeForTask(options) {
5486
- const { preparedPipeline, task } = options;
5487
- return preparedPipeline.knowledgePieces.map(({ content }) => `- ${content}`).join('\n');
5527
+ const { tools, preparedPipeline, task } = options;
5528
+ const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
5529
+ const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
5530
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
5531
+ if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
5532
+ return 'No knowledge pieces found';
5533
+ }
5534
+ // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
5535
+ const _llms = arrayableToArray(tools.llm);
5536
+ const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
5537
+ const taskEmbeddingPrompt = {
5538
+ title: 'Knowledge Search',
5539
+ modelRequirements: {
5540
+ modelVariant: 'EMBEDDING',
5541
+ modelName: firstKnowlegeIndex.modelName,
5542
+ },
5543
+ content: task.content,
5544
+ parameters: {
5545
+ /* !!!!!!!! */
5546
+ },
5547
+ };
5548
+ const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
5549
+ const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
5550
+ const { index } = knowledgePiece;
5551
+ const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
5552
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model
5553
+ if (knowledgePieceIndex === undefined) {
5554
+ return {
5555
+ content: knowledgePiece.content,
5556
+ relevance: 0,
5557
+ };
5558
+ }
5559
+ const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
5560
+ return {
5561
+ content: knowledgePiece.content,
5562
+ relevance,
5563
+ };
5564
+ });
5565
+ const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
5566
+ const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
5567
+ console.log('!!! Embedding', {
5568
+ task,
5569
+ taskEmbeddingPrompt,
5570
+ taskEmbeddingResult,
5571
+ firstKnowlegePiece,
5572
+ firstKnowlegeIndex,
5573
+ knowledgePiecesWithRelevance,
5574
+ knowledgePiecesSorted,
5575
+ knowledgePiecesLimited,
5576
+ });
5577
+ return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5488
5578
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5489
5579
  }
5580
+ // TODO: !!!!!! Annotate + to new file
5581
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5582
+ if (embeddingVector1.length !== embeddingVector2.length) {
5583
+ throw new TypeError('Embedding vectors must have the same length');
5584
+ }
5585
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5586
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5587
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5588
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5589
+ }
5590
+ /**
5591
+ * TODO: !!!! Verify if this is working
5592
+ * TODO: [♨] Implement Better - use keyword search
5593
+ * TODO: [♨] Examples of values
5594
+ */
5490
5595
 
5491
5596
  /**
5492
5597
  * @@@
@@ -5494,9 +5599,9 @@ async function getKnowledgeForTask(options) {
5494
5599
  * @private internal utility of `createPipelineExecutor`
5495
5600
  */
5496
5601
  async function getReservedParametersForTask(options) {
5497
- const { preparedPipeline, task, pipelineIdentification } = options;
5602
+ const { tools, preparedPipeline, task, pipelineIdentification } = options;
5498
5603
  const context = await getContextForTask(); // <- [🏍]
5499
- const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
5604
+ const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
5500
5605
  const examples = await getExamplesForTask();
5501
5606
  const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
5502
5607
  const modelName = RESERVED_PARAMETER_MISSING_VALUE;
@@ -5558,6 +5663,7 @@ async function executeTask(options) {
5558
5663
  }
5559
5664
  const definedParameters = Object.freeze({
5560
5665
  ...(await getReservedParametersForTask({
5666
+ tools,
5561
5667
  preparedPipeline,
5562
5668
  task: currentTask,
5563
5669
  pipelineIdentification,
@@ -6848,7 +6954,7 @@ async function $provideScriptingForNode(options) {
6848
6954
  const openapiJson = {
6849
6955
  openapi: '3.0.0',
6850
6956
  info: {
6851
- title: 'Promptbook Remote Server API (!!!! From TS)',
6957
+ title: 'Promptbook Remote Server API (!!!! From YML)',
6852
6958
  version: '1.0.0',
6853
6959
  description: 'API documentation for the Promptbook Remote Server',
6854
6960
  },
@@ -6860,6 +6966,13 @@ const openapiJson = {
6860
6966
  responses: {
6861
6967
  '200': {
6862
6968
  description: 'Server details in markdown format.',
6969
+ content: {
6970
+ 'text/markdown': {
6971
+ schema: {
6972
+ type: 'string',
6973
+ },
6974
+ },
6975
+ },
6863
6976
  },
6864
6977
  },
6865
6978
  },
@@ -6890,13 +7003,22 @@ const openapiJson = {
6890
7003
  },
6891
7004
  },
6892
7005
  responses: {
6893
- '200': {
7006
+ '201': {
6894
7007
  description: 'Successful login',
6895
7008
  content: {
6896
7009
  'application/json': {
6897
7010
  schema: {
6898
7011
  type: 'object',
6899
7012
  properties: {
7013
+ isSuccess: {
7014
+ type: 'boolean',
7015
+ },
7016
+ message: {
7017
+ type: 'string',
7018
+ },
7019
+ error: {
7020
+ type: 'object',
7021
+ },
6900
7022
  identification: {
6901
7023
  type: 'object',
6902
7024
  },
@@ -6905,6 +7027,43 @@ const openapiJson = {
6905
7027
  },
6906
7028
  },
6907
7029
  },
7030
+ '400': {
7031
+ description: 'Bad request or login failed',
7032
+ content: {
7033
+ 'application/json': {
7034
+ schema: {
7035
+ type: 'object',
7036
+ properties: {
7037
+ error: {
7038
+ type: 'object',
7039
+ },
7040
+ },
7041
+ },
7042
+ },
7043
+ },
7044
+ },
7045
+ '401': {
7046
+ description: 'Authentication error',
7047
+ content: {
7048
+ 'application/json': {
7049
+ schema: {
7050
+ type: 'object',
7051
+ properties: {
7052
+ isSuccess: {
7053
+ type: 'boolean',
7054
+ enum: [false],
7055
+ },
7056
+ message: {
7057
+ type: 'string',
7058
+ },
7059
+ error: {
7060
+ type: 'object',
7061
+ },
7062
+ },
7063
+ },
7064
+ },
7065
+ },
7066
+ },
6908
7067
  },
6909
7068
  },
6910
7069
  },
@@ -6926,6 +7085,16 @@ const openapiJson = {
6926
7085
  },
6927
7086
  },
6928
7087
  },
7088
+ '500': {
7089
+ description: 'No collection available',
7090
+ content: {
7091
+ 'text/plain': {
7092
+ schema: {
7093
+ type: 'string',
7094
+ },
7095
+ },
7096
+ },
7097
+ },
6929
7098
  },
6930
7099
  },
6931
7100
  },
@@ -6957,6 +7126,28 @@ const openapiJson = {
6957
7126
  },
6958
7127
  '404': {
6959
7128
  description: 'Book not found.',
7129
+ content: {
7130
+ 'application/json': {
7131
+ schema: {
7132
+ type: 'object',
7133
+ properties: {
7134
+ error: {
7135
+ type: 'object',
7136
+ },
7137
+ },
7138
+ },
7139
+ },
7140
+ },
7141
+ },
7142
+ '500': {
7143
+ description: 'No collection available',
7144
+ content: {
7145
+ 'text/plain': {
7146
+ schema: {
7147
+ type: 'string',
7148
+ },
7149
+ },
7150
+ },
6960
7151
  },
6961
7152
  },
6962
7153
  },
@@ -6974,11 +7165,174 @@ const openapiJson = {
6974
7165
  type: 'array',
6975
7166
  items: {
6976
7167
  type: 'object',
7168
+ properties: {
7169
+ nonce: {
7170
+ type: 'string',
7171
+ },
7172
+ taskId: {
7173
+ type: 'string',
7174
+ },
7175
+ taskType: {
7176
+ type: 'string',
7177
+ },
7178
+ status: {
7179
+ type: 'string',
7180
+ },
7181
+ createdAt: {
7182
+ type: 'string',
7183
+ format: 'date-time',
7184
+ },
7185
+ updatedAt: {
7186
+ type: 'string',
7187
+ format: 'date-time',
7188
+ },
7189
+ },
7190
+ },
7191
+ },
7192
+ },
7193
+ },
7194
+ },
7195
+ },
7196
+ },
7197
+ },
7198
+ '/executions/last': {
7199
+ get: {
7200
+ summary: 'Get the last execution',
7201
+ description: 'Returns details of the last execution task.',
7202
+ responses: {
7203
+ '200': {
7204
+ description: 'The last execution task with full details.',
7205
+ content: {
7206
+ 'application/json': {
7207
+ schema: {
7208
+ type: 'object',
7209
+ properties: {
7210
+ nonce: {
7211
+ type: 'string',
7212
+ },
7213
+ taskId: {
7214
+ type: 'string',
7215
+ },
7216
+ taskType: {
7217
+ type: 'string',
7218
+ },
7219
+ status: {
7220
+ type: 'string',
7221
+ },
7222
+ errors: {
7223
+ type: 'array',
7224
+ items: {
7225
+ type: 'object',
7226
+ },
7227
+ },
7228
+ warnings: {
7229
+ type: 'array',
7230
+ items: {
7231
+ type: 'object',
7232
+ },
7233
+ },
7234
+ createdAt: {
7235
+ type: 'string',
7236
+ format: 'date-time',
7237
+ },
7238
+ updatedAt: {
7239
+ type: 'string',
7240
+ format: 'date-time',
7241
+ },
7242
+ currentValue: {
7243
+ type: 'object',
7244
+ },
6977
7245
  },
6978
7246
  },
6979
7247
  },
6980
7248
  },
6981
7249
  },
7250
+ '404': {
7251
+ description: 'No execution tasks found.',
7252
+ content: {
7253
+ 'text/plain': {
7254
+ schema: {
7255
+ type: 'string',
7256
+ },
7257
+ },
7258
+ },
7259
+ },
7260
+ },
7261
+ },
7262
+ },
7263
+ '/executions/{taskId}': {
7264
+ get: {
7265
+ summary: 'Get specific execution',
7266
+ description: 'Returns details of a specific execution task.',
7267
+ parameters: [
7268
+ {
7269
+ in: 'path',
7270
+ name: 'taskId',
7271
+ required: true,
7272
+ schema: {
7273
+ type: 'string',
7274
+ },
7275
+ description: 'The ID of the execution task to retrieve.',
7276
+ },
7277
+ ],
7278
+ responses: {
7279
+ '200': {
7280
+ description: 'The execution task with full details.',
7281
+ content: {
7282
+ 'application/json': {
7283
+ schema: {
7284
+ type: 'object',
7285
+ properties: {
7286
+ nonce: {
7287
+ type: 'string',
7288
+ },
7289
+ taskId: {
7290
+ type: 'string',
7291
+ },
7292
+ taskType: {
7293
+ type: 'string',
7294
+ },
7295
+ status: {
7296
+ type: 'string',
7297
+ },
7298
+ errors: {
7299
+ type: 'array',
7300
+ items: {
7301
+ type: 'object',
7302
+ },
7303
+ },
7304
+ warnings: {
7305
+ type: 'array',
7306
+ items: {
7307
+ type: 'object',
7308
+ },
7309
+ },
7310
+ createdAt: {
7311
+ type: 'string',
7312
+ format: 'date-time',
7313
+ },
7314
+ updatedAt: {
7315
+ type: 'string',
7316
+ format: 'date-time',
7317
+ },
7318
+ currentValue: {
7319
+ type: 'object',
7320
+ },
7321
+ },
7322
+ },
7323
+ },
7324
+ },
7325
+ },
7326
+ '404': {
7327
+ description: 'Execution task not found.',
7328
+ content: {
7329
+ 'text/plain': {
7330
+ schema: {
7331
+ type: 'string',
7332
+ },
7333
+ },
7334
+ },
7335
+ },
6982
7336
  },
6983
7337
  },
6984
7338
  },
@@ -6995,12 +7349,19 @@ const openapiJson = {
6995
7349
  properties: {
6996
7350
  pipelineUrl: {
6997
7351
  type: 'string',
7352
+ description: 'URL of the pipeline to execute',
7353
+ },
7354
+ book: {
7355
+ type: 'string',
7356
+ description: 'Alternative field for pipelineUrl',
6998
7357
  },
6999
7358
  inputParameters: {
7000
7359
  type: 'object',
7360
+ description: 'Parameters for pipeline execution',
7001
7361
  },
7002
7362
  identification: {
7003
7363
  type: 'object',
7364
+ description: 'User identification data',
7004
7365
  },
7005
7366
  },
7006
7367
  },
@@ -7020,13 +7381,164 @@ const openapiJson = {
7020
7381
  },
7021
7382
  '400': {
7022
7383
  description: 'Invalid input.',
7384
+ content: {
7385
+ 'application/json': {
7386
+ schema: {
7387
+ type: 'object',
7388
+ properties: {
7389
+ error: {
7390
+ type: 'object',
7391
+ },
7392
+ },
7393
+ },
7394
+ },
7395
+ },
7396
+ },
7397
+ '404': {
7398
+ description: 'Pipeline not found.',
7399
+ content: {
7400
+ 'text/plain': {
7401
+ schema: {
7402
+ type: 'string',
7403
+ },
7404
+ },
7405
+ },
7406
+ },
7407
+ },
7408
+ },
7409
+ },
7410
+ '/api-docs': {
7411
+ get: {
7412
+ summary: 'API documentation UI',
7413
+ description: 'Swagger UI for API documentation',
7414
+ responses: {
7415
+ '200': {
7416
+ description: 'HTML Swagger UI',
7417
+ },
7418
+ },
7419
+ },
7420
+ },
7421
+ '/swagger': {
7422
+ get: {
7423
+ summary: 'API documentation UI (alternative path)',
7424
+ description: 'Swagger UI for API documentation',
7425
+ responses: {
7426
+ '200': {
7427
+ description: 'HTML Swagger UI',
7428
+ },
7429
+ },
7430
+ },
7431
+ },
7432
+ '/openapi': {
7433
+ get: {
7434
+ summary: 'OpenAPI specification',
7435
+ description: 'Returns the OpenAPI JSON specification',
7436
+ responses: {
7437
+ '200': {
7438
+ description: 'OpenAPI specification',
7439
+ content: {
7440
+ 'application/json': {
7441
+ schema: {
7442
+ type: 'object',
7443
+ },
7444
+ },
7445
+ },
7023
7446
  },
7024
7447
  },
7025
7448
  },
7026
7449
  },
7027
7450
  },
7028
- components: {},
7029
- tags: [],
7451
+ components: {
7452
+ schemas: {
7453
+ Error: {
7454
+ type: 'object',
7455
+ properties: {
7456
+ error: {
7457
+ type: 'object',
7458
+ },
7459
+ },
7460
+ },
7461
+ ExecutionTaskSummary: {
7462
+ type: 'object',
7463
+ properties: {
7464
+ nonce: {
7465
+ type: 'string',
7466
+ },
7467
+ taskId: {
7468
+ type: 'string',
7469
+ },
7470
+ taskType: {
7471
+ type: 'string',
7472
+ },
7473
+ status: {
7474
+ type: 'string',
7475
+ },
7476
+ createdAt: {
7477
+ type: 'string',
7478
+ format: 'date-time',
7479
+ },
7480
+ updatedAt: {
7481
+ type: 'string',
7482
+ format: 'date-time',
7483
+ },
7484
+ },
7485
+ },
7486
+ ExecutionTaskFull: {
7487
+ type: 'object',
7488
+ properties: {
7489
+ nonce: {
7490
+ type: 'string',
7491
+ },
7492
+ taskId: {
7493
+ type: 'string',
7494
+ },
7495
+ taskType: {
7496
+ type: 'string',
7497
+ },
7498
+ status: {
7499
+ type: 'string',
7500
+ },
7501
+ errors: {
7502
+ type: 'array',
7503
+ items: {
7504
+ type: 'object',
7505
+ },
7506
+ },
7507
+ warnings: {
7508
+ type: 'array',
7509
+ items: {
7510
+ type: 'object',
7511
+ },
7512
+ },
7513
+ createdAt: {
7514
+ type: 'string',
7515
+ format: 'date-time',
7516
+ },
7517
+ updatedAt: {
7518
+ type: 'string',
7519
+ format: 'date-time',
7520
+ },
7521
+ currentValue: {
7522
+ type: 'object',
7523
+ },
7524
+ },
7525
+ },
7526
+ },
7527
+ },
7528
+ tags: [
7529
+ {
7530
+ name: 'Books',
7531
+ description: 'Operations related to books and pipelines',
7532
+ },
7533
+ {
7534
+ name: 'Executions',
7535
+ description: 'Operations related to execution tasks',
7536
+ },
7537
+ {
7538
+ name: 'Authentication',
7539
+ description: 'Authentication operations',
7540
+ },
7541
+ ],
7030
7542
  };
7031
7543
  /**
7032
7544
  * Note: [💞] Ignore a discrepancy between file name and entity name