@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/umd/index.umd.js CHANGED
@@ -48,7 +48,7 @@
48
48
  * @generated
49
49
  * @see https://github.com/webgptorg/promptbook
50
50
  */
51
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-5';
51
+ const PROMPTBOOK_ENGINE_VERSION = '0.92.0-6';
52
52
  /**
53
53
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
54
54
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -1848,6 +1848,45 @@
1848
1848
  }
1849
1849
  }
1850
1850
 
1851
+ /**
1852
+ * Converts a JavaScript Object Notation (JSON) string into an object.
1853
+ *
1854
+ * Note: This is wrapper around `JSON.parse()` with better error and type handling
1855
+ *
1856
+ * @public exported from `@promptbook/utils`
1857
+ */
1858
+ function jsonParse(value) {
1859
+ if (value === undefined) {
1860
+ throw new Error(`Can not parse JSON from undefined value.`);
1861
+ }
1862
+ else if (typeof value !== 'string') {
1863
+ console.error('Can not parse JSON from non-string value.', { text: value });
1864
+ throw new Error(spaceTrim__default["default"](`
1865
+ Can not parse JSON from non-string value.
1866
+
1867
+ The value type: ${typeof value}
1868
+ See more in console.
1869
+ `));
1870
+ }
1871
+ try {
1872
+ return JSON.parse(value);
1873
+ }
1874
+ catch (error) {
1875
+ if (!(error instanceof Error)) {
1876
+ throw error;
1877
+ }
1878
+ throw new Error(spaceTrim__default["default"]((block) => `
1879
+ ${block(error.message)}
1880
+
1881
+ The JSON text:
1882
+ ${block(value)}
1883
+ `));
1884
+ }
1885
+ }
1886
+ /**
1887
+ * TODO: !!!! Use in Promptbook.studio
1888
+ */
1889
+
1851
1890
  /**
1852
1891
  * Recursively converts JSON strings to JSON objects
1853
1892
 
@@ -1866,7 +1905,7 @@
1866
1905
  const newObject = { ...object };
1867
1906
  for (const [key, value] of Object.entries(object)) {
1868
1907
  if (typeof value === 'string' && isValidJsonString(value)) {
1869
- newObject[key] = JSON.parse(value);
1908
+ newObject[key] = jsonParse(value);
1870
1909
  }
1871
1910
  else {
1872
1911
  newObject[key] = jsonStringsToJsons(value);
@@ -2949,7 +2988,7 @@
2949
2988
  }).asPromise();
2950
2989
  const { outputParameters } = result;
2951
2990
  const { modelsRequirements: modelsRequirementsJson } = outputParameters;
2952
- const modelsRequirementsUnchecked = JSON.parse(modelsRequirementsJson);
2991
+ const modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
2953
2992
  if (isVerbose) {
2954
2993
  console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
2955
2994
  }
@@ -3791,7 +3830,7 @@
3791
3830
  > },
3792
3831
  */
3793
3832
  async asJson() {
3794
- return JSON.parse(await tools.fs.readFile(filename, 'utf-8'));
3833
+ return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
3795
3834
  },
3796
3835
  async asText() {
3797
3836
  return await tools.fs.readFile(filename, 'utf-8');
@@ -5495,13 +5534,79 @@
5495
5534
  /**
5496
5535
  * @@@
5497
5536
  *
5537
+ * Here is the place where RAG (retrieval-augmented generation) happens
5538
+ *
5498
5539
  * @private internal utility of `createPipelineExecutor`
5499
5540
  */
5500
5541
  async function getKnowledgeForTask(options) {
5501
- const { preparedPipeline, task } = options;
5502
- return preparedPipeline.knowledgePieces.map(({ content }) => `- ${content}`).join('\n');
5542
+ const { tools, preparedPipeline, task } = options;
5543
+ const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
5544
+ const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
5545
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
5546
+ if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
5547
+ return 'No knowledge pieces found';
5548
+ }
5549
+ // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
5550
+ const _llms = arrayableToArray(tools.llm);
5551
+ const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
5552
+ const taskEmbeddingPrompt = {
5553
+ title: 'Knowledge Search',
5554
+ modelRequirements: {
5555
+ modelVariant: 'EMBEDDING',
5556
+ modelName: firstKnowlegeIndex.modelName,
5557
+ },
5558
+ content: task.content,
5559
+ parameters: {
5560
+ /* !!!!!!!! */
5561
+ },
5562
+ };
5563
+ const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
5564
+ const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
5565
+ const { index } = knowledgePiece;
5566
+ const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
5567
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model
5568
+ if (knowledgePieceIndex === undefined) {
5569
+ return {
5570
+ content: knowledgePiece.content,
5571
+ relevance: 0,
5572
+ };
5573
+ }
5574
+ const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
5575
+ return {
5576
+ content: knowledgePiece.content,
5577
+ relevance,
5578
+ };
5579
+ });
5580
+ const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
5581
+ const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
5582
+ console.log('!!! Embedding', {
5583
+ task,
5584
+ taskEmbeddingPrompt,
5585
+ taskEmbeddingResult,
5586
+ firstKnowlegePiece,
5587
+ firstKnowlegeIndex,
5588
+ knowledgePiecesWithRelevance,
5589
+ knowledgePiecesSorted,
5590
+ knowledgePiecesLimited,
5591
+ });
5592
+ return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5503
5593
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5504
5594
  }
5595
+ // TODO: !!!!!! Annotate + to new file
5596
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5597
+ if (embeddingVector1.length !== embeddingVector2.length) {
5598
+ throw new TypeError('Embedding vectors must have the same length');
5599
+ }
5600
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5601
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5602
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5603
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5604
+ }
5605
+ /**
5606
+ * TODO: !!!! Verify if this is working
5607
+ * TODO: [♨] Implement Better - use keyword search
5608
+ * TODO: [♨] Examples of values
5609
+ */
5505
5610
 
5506
5611
  /**
5507
5612
  * @@@
@@ -5509,9 +5614,9 @@
5509
5614
  * @private internal utility of `createPipelineExecutor`
5510
5615
  */
5511
5616
  async function getReservedParametersForTask(options) {
5512
- const { preparedPipeline, task, pipelineIdentification } = options;
5617
+ const { tools, preparedPipeline, task, pipelineIdentification } = options;
5513
5618
  const context = await getContextForTask(); // <- [🏍]
5514
- const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
5619
+ const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
5515
5620
  const examples = await getExamplesForTask();
5516
5621
  const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
5517
5622
  const modelName = RESERVED_PARAMETER_MISSING_VALUE;
@@ -5573,6 +5678,7 @@
5573
5678
  }
5574
5679
  const definedParameters = Object.freeze({
5575
5680
  ...(await getReservedParametersForTask({
5681
+ tools,
5576
5682
  preparedPipeline,
5577
5683
  task: currentTask,
5578
5684
  pipelineIdentification,
@@ -6863,7 +6969,7 @@
6863
6969
  const openapiJson = {
6864
6970
  openapi: '3.0.0',
6865
6971
  info: {
6866
- title: 'Promptbook Remote Server API (!!!! From TS)',
6972
+ title: 'Promptbook Remote Server API (!!!! From YML)',
6867
6973
  version: '1.0.0',
6868
6974
  description: 'API documentation for the Promptbook Remote Server',
6869
6975
  },
@@ -6875,6 +6981,13 @@
6875
6981
  responses: {
6876
6982
  '200': {
6877
6983
  description: 'Server details in markdown format.',
6984
+ content: {
6985
+ 'text/markdown': {
6986
+ schema: {
6987
+ type: 'string',
6988
+ },
6989
+ },
6990
+ },
6878
6991
  },
6879
6992
  },
6880
6993
  },
@@ -6905,13 +7018,22 @@
6905
7018
  },
6906
7019
  },
6907
7020
  responses: {
6908
- '200': {
7021
+ '201': {
6909
7022
  description: 'Successful login',
6910
7023
  content: {
6911
7024
  'application/json': {
6912
7025
  schema: {
6913
7026
  type: 'object',
6914
7027
  properties: {
7028
+ isSuccess: {
7029
+ type: 'boolean',
7030
+ },
7031
+ message: {
7032
+ type: 'string',
7033
+ },
7034
+ error: {
7035
+ type: 'object',
7036
+ },
6915
7037
  identification: {
6916
7038
  type: 'object',
6917
7039
  },
@@ -6920,6 +7042,43 @@
6920
7042
  },
6921
7043
  },
6922
7044
  },
7045
+ '400': {
7046
+ description: 'Bad request or login failed',
7047
+ content: {
7048
+ 'application/json': {
7049
+ schema: {
7050
+ type: 'object',
7051
+ properties: {
7052
+ error: {
7053
+ type: 'object',
7054
+ },
7055
+ },
7056
+ },
7057
+ },
7058
+ },
7059
+ },
7060
+ '401': {
7061
+ description: 'Authentication error',
7062
+ content: {
7063
+ 'application/json': {
7064
+ schema: {
7065
+ type: 'object',
7066
+ properties: {
7067
+ isSuccess: {
7068
+ type: 'boolean',
7069
+ enum: [false],
7070
+ },
7071
+ message: {
7072
+ type: 'string',
7073
+ },
7074
+ error: {
7075
+ type: 'object',
7076
+ },
7077
+ },
7078
+ },
7079
+ },
7080
+ },
7081
+ },
6923
7082
  },
6924
7083
  },
6925
7084
  },
@@ -6941,6 +7100,16 @@
6941
7100
  },
6942
7101
  },
6943
7102
  },
7103
+ '500': {
7104
+ description: 'No collection available',
7105
+ content: {
7106
+ 'text/plain': {
7107
+ schema: {
7108
+ type: 'string',
7109
+ },
7110
+ },
7111
+ },
7112
+ },
6944
7113
  },
6945
7114
  },
6946
7115
  },
@@ -6972,6 +7141,28 @@
6972
7141
  },
6973
7142
  '404': {
6974
7143
  description: 'Book not found.',
7144
+ content: {
7145
+ 'application/json': {
7146
+ schema: {
7147
+ type: 'object',
7148
+ properties: {
7149
+ error: {
7150
+ type: 'object',
7151
+ },
7152
+ },
7153
+ },
7154
+ },
7155
+ },
7156
+ },
7157
+ '500': {
7158
+ description: 'No collection available',
7159
+ content: {
7160
+ 'text/plain': {
7161
+ schema: {
7162
+ type: 'string',
7163
+ },
7164
+ },
7165
+ },
6975
7166
  },
6976
7167
  },
6977
7168
  },
@@ -6989,11 +7180,174 @@
6989
7180
  type: 'array',
6990
7181
  items: {
6991
7182
  type: 'object',
7183
+ properties: {
7184
+ nonce: {
7185
+ type: 'string',
7186
+ },
7187
+ taskId: {
7188
+ type: 'string',
7189
+ },
7190
+ taskType: {
7191
+ type: 'string',
7192
+ },
7193
+ status: {
7194
+ type: 'string',
7195
+ },
7196
+ createdAt: {
7197
+ type: 'string',
7198
+ format: 'date-time',
7199
+ },
7200
+ updatedAt: {
7201
+ type: 'string',
7202
+ format: 'date-time',
7203
+ },
7204
+ },
7205
+ },
7206
+ },
7207
+ },
7208
+ },
7209
+ },
7210
+ },
7211
+ },
7212
+ },
7213
+ '/executions/last': {
7214
+ get: {
7215
+ summary: 'Get the last execution',
7216
+ description: 'Returns details of the last execution task.',
7217
+ responses: {
7218
+ '200': {
7219
+ description: 'The last execution task with full details.',
7220
+ content: {
7221
+ 'application/json': {
7222
+ schema: {
7223
+ type: 'object',
7224
+ properties: {
7225
+ nonce: {
7226
+ type: 'string',
7227
+ },
7228
+ taskId: {
7229
+ type: 'string',
7230
+ },
7231
+ taskType: {
7232
+ type: 'string',
7233
+ },
7234
+ status: {
7235
+ type: 'string',
7236
+ },
7237
+ errors: {
7238
+ type: 'array',
7239
+ items: {
7240
+ type: 'object',
7241
+ },
7242
+ },
7243
+ warnings: {
7244
+ type: 'array',
7245
+ items: {
7246
+ type: 'object',
7247
+ },
7248
+ },
7249
+ createdAt: {
7250
+ type: 'string',
7251
+ format: 'date-time',
7252
+ },
7253
+ updatedAt: {
7254
+ type: 'string',
7255
+ format: 'date-time',
7256
+ },
7257
+ currentValue: {
7258
+ type: 'object',
7259
+ },
6992
7260
  },
6993
7261
  },
6994
7262
  },
6995
7263
  },
6996
7264
  },
7265
+ '404': {
7266
+ description: 'No execution tasks found.',
7267
+ content: {
7268
+ 'text/plain': {
7269
+ schema: {
7270
+ type: 'string',
7271
+ },
7272
+ },
7273
+ },
7274
+ },
7275
+ },
7276
+ },
7277
+ },
7278
+ '/executions/{taskId}': {
7279
+ get: {
7280
+ summary: 'Get specific execution',
7281
+ description: 'Returns details of a specific execution task.',
7282
+ parameters: [
7283
+ {
7284
+ in: 'path',
7285
+ name: 'taskId',
7286
+ required: true,
7287
+ schema: {
7288
+ type: 'string',
7289
+ },
7290
+ description: 'The ID of the execution task to retrieve.',
7291
+ },
7292
+ ],
7293
+ responses: {
7294
+ '200': {
7295
+ description: 'The execution task with full details.',
7296
+ content: {
7297
+ 'application/json': {
7298
+ schema: {
7299
+ type: 'object',
7300
+ properties: {
7301
+ nonce: {
7302
+ type: 'string',
7303
+ },
7304
+ taskId: {
7305
+ type: 'string',
7306
+ },
7307
+ taskType: {
7308
+ type: 'string',
7309
+ },
7310
+ status: {
7311
+ type: 'string',
7312
+ },
7313
+ errors: {
7314
+ type: 'array',
7315
+ items: {
7316
+ type: 'object',
7317
+ },
7318
+ },
7319
+ warnings: {
7320
+ type: 'array',
7321
+ items: {
7322
+ type: 'object',
7323
+ },
7324
+ },
7325
+ createdAt: {
7326
+ type: 'string',
7327
+ format: 'date-time',
7328
+ },
7329
+ updatedAt: {
7330
+ type: 'string',
7331
+ format: 'date-time',
7332
+ },
7333
+ currentValue: {
7334
+ type: 'object',
7335
+ },
7336
+ },
7337
+ },
7338
+ },
7339
+ },
7340
+ },
7341
+ '404': {
7342
+ description: 'Execution task not found.',
7343
+ content: {
7344
+ 'text/plain': {
7345
+ schema: {
7346
+ type: 'string',
7347
+ },
7348
+ },
7349
+ },
7350
+ },
6997
7351
  },
6998
7352
  },
6999
7353
  },
@@ -7010,12 +7364,19 @@
7010
7364
  properties: {
7011
7365
  pipelineUrl: {
7012
7366
  type: 'string',
7367
+ description: 'URL of the pipeline to execute',
7368
+ },
7369
+ book: {
7370
+ type: 'string',
7371
+ description: 'Alternative field for pipelineUrl',
7013
7372
  },
7014
7373
  inputParameters: {
7015
7374
  type: 'object',
7375
+ description: 'Parameters for pipeline execution',
7016
7376
  },
7017
7377
  identification: {
7018
7378
  type: 'object',
7379
+ description: 'User identification data',
7019
7380
  },
7020
7381
  },
7021
7382
  },
@@ -7035,13 +7396,164 @@
7035
7396
  },
7036
7397
  '400': {
7037
7398
  description: 'Invalid input.',
7399
+ content: {
7400
+ 'application/json': {
7401
+ schema: {
7402
+ type: 'object',
7403
+ properties: {
7404
+ error: {
7405
+ type: 'object',
7406
+ },
7407
+ },
7408
+ },
7409
+ },
7410
+ },
7411
+ },
7412
+ '404': {
7413
+ description: 'Pipeline not found.',
7414
+ content: {
7415
+ 'text/plain': {
7416
+ schema: {
7417
+ type: 'string',
7418
+ },
7419
+ },
7420
+ },
7421
+ },
7422
+ },
7423
+ },
7424
+ },
7425
+ '/api-docs': {
7426
+ get: {
7427
+ summary: 'API documentation UI',
7428
+ description: 'Swagger UI for API documentation',
7429
+ responses: {
7430
+ '200': {
7431
+ description: 'HTML Swagger UI',
7432
+ },
7433
+ },
7434
+ },
7435
+ },
7436
+ '/swagger': {
7437
+ get: {
7438
+ summary: 'API documentation UI (alternative path)',
7439
+ description: 'Swagger UI for API documentation',
7440
+ responses: {
7441
+ '200': {
7442
+ description: 'HTML Swagger UI',
7443
+ },
7444
+ },
7445
+ },
7446
+ },
7447
+ '/openapi': {
7448
+ get: {
7449
+ summary: 'OpenAPI specification',
7450
+ description: 'Returns the OpenAPI JSON specification',
7451
+ responses: {
7452
+ '200': {
7453
+ description: 'OpenAPI specification',
7454
+ content: {
7455
+ 'application/json': {
7456
+ schema: {
7457
+ type: 'object',
7458
+ },
7459
+ },
7460
+ },
7038
7461
  },
7039
7462
  },
7040
7463
  },
7041
7464
  },
7042
7465
  },
7043
- components: {},
7044
- tags: [],
7466
+ components: {
7467
+ schemas: {
7468
+ Error: {
7469
+ type: 'object',
7470
+ properties: {
7471
+ error: {
7472
+ type: 'object',
7473
+ },
7474
+ },
7475
+ },
7476
+ ExecutionTaskSummary: {
7477
+ type: 'object',
7478
+ properties: {
7479
+ nonce: {
7480
+ type: 'string',
7481
+ },
7482
+ taskId: {
7483
+ type: 'string',
7484
+ },
7485
+ taskType: {
7486
+ type: 'string',
7487
+ },
7488
+ status: {
7489
+ type: 'string',
7490
+ },
7491
+ createdAt: {
7492
+ type: 'string',
7493
+ format: 'date-time',
7494
+ },
7495
+ updatedAt: {
7496
+ type: 'string',
7497
+ format: 'date-time',
7498
+ },
7499
+ },
7500
+ },
7501
+ ExecutionTaskFull: {
7502
+ type: 'object',
7503
+ properties: {
7504
+ nonce: {
7505
+ type: 'string',
7506
+ },
7507
+ taskId: {
7508
+ type: 'string',
7509
+ },
7510
+ taskType: {
7511
+ type: 'string',
7512
+ },
7513
+ status: {
7514
+ type: 'string',
7515
+ },
7516
+ errors: {
7517
+ type: 'array',
7518
+ items: {
7519
+ type: 'object',
7520
+ },
7521
+ },
7522
+ warnings: {
7523
+ type: 'array',
7524
+ items: {
7525
+ type: 'object',
7526
+ },
7527
+ },
7528
+ createdAt: {
7529
+ type: 'string',
7530
+ format: 'date-time',
7531
+ },
7532
+ updatedAt: {
7533
+ type: 'string',
7534
+ format: 'date-time',
7535
+ },
7536
+ currentValue: {
7537
+ type: 'object',
7538
+ },
7539
+ },
7540
+ },
7541
+ },
7542
+ },
7543
+ tags: [
7544
+ {
7545
+ name: 'Books',
7546
+ description: 'Operations related to books and pipelines',
7547
+ },
7548
+ {
7549
+ name: 'Executions',
7550
+ description: 'Operations related to execution tasks',
7551
+ },
7552
+ {
7553
+ name: 'Authentication',
7554
+ description: 'Authentication operations',
7555
+ },
7556
+ ],
7045
7557
  };
7046
7558
  /**
7047
7559
  * Note: [💞] Ignore a discrepancy between file name and entity name