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

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-7';
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,18 +2988,26 @@
2949
2988
  }).asPromise();
2950
2989
  const { outputParameters } = result;
2951
2990
  const { modelsRequirements: modelsRequirementsJson } = outputParameters;
2952
- const modelsRequirementsUnchecked = JSON.parse(modelsRequirementsJson);
2991
+ let modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
2953
2992
  if (isVerbose) {
2954
2993
  console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
2955
2994
  }
2956
2995
  if (!Array.isArray(modelsRequirementsUnchecked)) {
2957
- throw new UnexpectedError(spaceTrim__default["default"]((block) => `
2958
- Invalid \`modelsRequirements\`:
2996
+ // <- TODO: Book should have syntax and system to enforce shape of JSON
2997
+ modelsRequirementsUnchecked = [modelsRequirementsUnchecked];
2998
+ /*
2999
+ throw new UnexpectedError(
3000
+ spaceTrim(
3001
+ (block) => `
3002
+ Invalid \`modelsRequirements\`:
2959
3003
 
2960
- \`\`\`json
2961
- ${block(JSON.stringify(modelsRequirementsUnchecked, null, 4))}
2962
- \`\`\`
2963
- `));
3004
+ \`\`\`json
3005
+ ${block(JSON.stringify(modelsRequirementsUnchecked, null, 4))}
3006
+ \`\`\`
3007
+ `,
3008
+ ),
3009
+ );
3010
+ */
2964
3011
  }
2965
3012
  const modelsRequirements = modelsRequirementsUnchecked.map((modelRequirements) => ({
2966
3013
  modelVariant: 'CHAT',
@@ -3791,7 +3838,7 @@
3791
3838
  > },
3792
3839
  */
3793
3840
  async asJson() {
3794
- return JSON.parse(await tools.fs.readFile(filename, 'utf-8'));
3841
+ return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
3795
3842
  },
3796
3843
  async asText() {
3797
3844
  return await tools.fs.readFile(filename, 'utf-8');
@@ -5495,13 +5542,79 @@
5495
5542
  /**
5496
5543
  * @@@
5497
5544
  *
5545
+ * Here is the place where RAG (retrieval-augmented generation) happens
5546
+ *
5498
5547
  * @private internal utility of `createPipelineExecutor`
5499
5548
  */
5500
5549
  async function getKnowledgeForTask(options) {
5501
- const { preparedPipeline, task } = options;
5502
- return preparedPipeline.knowledgePieces.map(({ content }) => `- ${content}`).join('\n');
5550
+ const { tools, preparedPipeline, task } = options;
5551
+ const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
5552
+ const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
5553
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
5554
+ if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
5555
+ return 'No knowledge pieces found';
5556
+ }
5557
+ // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
5558
+ const _llms = arrayableToArray(tools.llm);
5559
+ const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
5560
+ const taskEmbeddingPrompt = {
5561
+ title: 'Knowledge Search',
5562
+ modelRequirements: {
5563
+ modelVariant: 'EMBEDDING',
5564
+ modelName: firstKnowlegeIndex.modelName,
5565
+ },
5566
+ content: task.content,
5567
+ parameters: {
5568
+ /* !!!!!!!! */
5569
+ },
5570
+ };
5571
+ const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
5572
+ const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
5573
+ const { index } = knowledgePiece;
5574
+ const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
5575
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model
5576
+ if (knowledgePieceIndex === undefined) {
5577
+ return {
5578
+ content: knowledgePiece.content,
5579
+ relevance: 0,
5580
+ };
5581
+ }
5582
+ const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
5583
+ return {
5584
+ content: knowledgePiece.content,
5585
+ relevance,
5586
+ };
5587
+ });
5588
+ const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
5589
+ const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
5590
+ console.log('!!! Embedding', {
5591
+ task,
5592
+ taskEmbeddingPrompt,
5593
+ taskEmbeddingResult,
5594
+ firstKnowlegePiece,
5595
+ firstKnowlegeIndex,
5596
+ knowledgePiecesWithRelevance,
5597
+ knowledgePiecesSorted,
5598
+ knowledgePiecesLimited,
5599
+ });
5600
+ return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5503
5601
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5504
5602
  }
5603
+ // TODO: !!!!!! Annotate + to new file
5604
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5605
+ if (embeddingVector1.length !== embeddingVector2.length) {
5606
+ throw new TypeError('Embedding vectors must have the same length');
5607
+ }
5608
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5609
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5610
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5611
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5612
+ }
5613
+ /**
5614
+ * TODO: !!!! Verify if this is working
5615
+ * TODO: [♨] Implement Better - use keyword search
5616
+ * TODO: [♨] Examples of values
5617
+ */
5505
5618
 
5506
5619
  /**
5507
5620
  * @@@
@@ -5509,9 +5622,9 @@
5509
5622
  * @private internal utility of `createPipelineExecutor`
5510
5623
  */
5511
5624
  async function getReservedParametersForTask(options) {
5512
- const { preparedPipeline, task, pipelineIdentification } = options;
5625
+ const { tools, preparedPipeline, task, pipelineIdentification } = options;
5513
5626
  const context = await getContextForTask(); // <- [🏍]
5514
- const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
5627
+ const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
5515
5628
  const examples = await getExamplesForTask();
5516
5629
  const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
5517
5630
  const modelName = RESERVED_PARAMETER_MISSING_VALUE;
@@ -5573,6 +5686,7 @@
5573
5686
  }
5574
5687
  const definedParameters = Object.freeze({
5575
5688
  ...(await getReservedParametersForTask({
5689
+ tools,
5576
5690
  preparedPipeline,
5577
5691
  task: currentTask,
5578
5692
  pipelineIdentification,
@@ -6863,7 +6977,7 @@
6863
6977
  const openapiJson = {
6864
6978
  openapi: '3.0.0',
6865
6979
  info: {
6866
- title: 'Promptbook Remote Server API (!!!! From TS)',
6980
+ title: 'Promptbook Remote Server API (!!!! From YML)',
6867
6981
  version: '1.0.0',
6868
6982
  description: 'API documentation for the Promptbook Remote Server',
6869
6983
  },
@@ -6875,6 +6989,13 @@
6875
6989
  responses: {
6876
6990
  '200': {
6877
6991
  description: 'Server details in markdown format.',
6992
+ content: {
6993
+ 'text/markdown': {
6994
+ schema: {
6995
+ type: 'string',
6996
+ },
6997
+ },
6998
+ },
6878
6999
  },
6879
7000
  },
6880
7001
  },
@@ -6905,13 +7026,22 @@
6905
7026
  },
6906
7027
  },
6907
7028
  responses: {
6908
- '200': {
7029
+ '201': {
6909
7030
  description: 'Successful login',
6910
7031
  content: {
6911
7032
  'application/json': {
6912
7033
  schema: {
6913
7034
  type: 'object',
6914
7035
  properties: {
7036
+ isSuccess: {
7037
+ type: 'boolean',
7038
+ },
7039
+ message: {
7040
+ type: 'string',
7041
+ },
7042
+ error: {
7043
+ type: 'object',
7044
+ },
6915
7045
  identification: {
6916
7046
  type: 'object',
6917
7047
  },
@@ -6920,6 +7050,43 @@
6920
7050
  },
6921
7051
  },
6922
7052
  },
7053
+ '400': {
7054
+ description: 'Bad request or login failed',
7055
+ content: {
7056
+ 'application/json': {
7057
+ schema: {
7058
+ type: 'object',
7059
+ properties: {
7060
+ error: {
7061
+ type: 'object',
7062
+ },
7063
+ },
7064
+ },
7065
+ },
7066
+ },
7067
+ },
7068
+ '401': {
7069
+ description: 'Authentication error',
7070
+ content: {
7071
+ 'application/json': {
7072
+ schema: {
7073
+ type: 'object',
7074
+ properties: {
7075
+ isSuccess: {
7076
+ type: 'boolean',
7077
+ enum: [false],
7078
+ },
7079
+ message: {
7080
+ type: 'string',
7081
+ },
7082
+ error: {
7083
+ type: 'object',
7084
+ },
7085
+ },
7086
+ },
7087
+ },
7088
+ },
7089
+ },
6923
7090
  },
6924
7091
  },
6925
7092
  },
@@ -6941,6 +7108,16 @@
6941
7108
  },
6942
7109
  },
6943
7110
  },
7111
+ '500': {
7112
+ description: 'No collection available',
7113
+ content: {
7114
+ 'text/plain': {
7115
+ schema: {
7116
+ type: 'string',
7117
+ },
7118
+ },
7119
+ },
7120
+ },
6944
7121
  },
6945
7122
  },
6946
7123
  },
@@ -6972,6 +7149,28 @@
6972
7149
  },
6973
7150
  '404': {
6974
7151
  description: 'Book not found.',
7152
+ content: {
7153
+ 'application/json': {
7154
+ schema: {
7155
+ type: 'object',
7156
+ properties: {
7157
+ error: {
7158
+ type: 'object',
7159
+ },
7160
+ },
7161
+ },
7162
+ },
7163
+ },
7164
+ },
7165
+ '500': {
7166
+ description: 'No collection available',
7167
+ content: {
7168
+ 'text/plain': {
7169
+ schema: {
7170
+ type: 'string',
7171
+ },
7172
+ },
7173
+ },
6975
7174
  },
6976
7175
  },
6977
7176
  },
@@ -6989,11 +7188,174 @@
6989
7188
  type: 'array',
6990
7189
  items: {
6991
7190
  type: 'object',
7191
+ properties: {
7192
+ nonce: {
7193
+ type: 'string',
7194
+ },
7195
+ taskId: {
7196
+ type: 'string',
7197
+ },
7198
+ taskType: {
7199
+ type: 'string',
7200
+ },
7201
+ status: {
7202
+ type: 'string',
7203
+ },
7204
+ createdAt: {
7205
+ type: 'string',
7206
+ format: 'date-time',
7207
+ },
7208
+ updatedAt: {
7209
+ type: 'string',
7210
+ format: 'date-time',
7211
+ },
7212
+ },
7213
+ },
7214
+ },
7215
+ },
7216
+ },
7217
+ },
7218
+ },
7219
+ },
7220
+ },
7221
+ '/executions/last': {
7222
+ get: {
7223
+ summary: 'Get the last execution',
7224
+ description: 'Returns details of the last execution task.',
7225
+ responses: {
7226
+ '200': {
7227
+ description: 'The last execution task with full details.',
7228
+ content: {
7229
+ 'application/json': {
7230
+ schema: {
7231
+ type: 'object',
7232
+ properties: {
7233
+ nonce: {
7234
+ type: 'string',
7235
+ },
7236
+ taskId: {
7237
+ type: 'string',
7238
+ },
7239
+ taskType: {
7240
+ type: 'string',
7241
+ },
7242
+ status: {
7243
+ type: 'string',
7244
+ },
7245
+ errors: {
7246
+ type: 'array',
7247
+ items: {
7248
+ type: 'object',
7249
+ },
7250
+ },
7251
+ warnings: {
7252
+ type: 'array',
7253
+ items: {
7254
+ type: 'object',
7255
+ },
7256
+ },
7257
+ createdAt: {
7258
+ type: 'string',
7259
+ format: 'date-time',
7260
+ },
7261
+ updatedAt: {
7262
+ type: 'string',
7263
+ format: 'date-time',
7264
+ },
7265
+ currentValue: {
7266
+ type: 'object',
7267
+ },
6992
7268
  },
6993
7269
  },
6994
7270
  },
6995
7271
  },
6996
7272
  },
7273
+ '404': {
7274
+ description: 'No execution tasks found.',
7275
+ content: {
7276
+ 'text/plain': {
7277
+ schema: {
7278
+ type: 'string',
7279
+ },
7280
+ },
7281
+ },
7282
+ },
7283
+ },
7284
+ },
7285
+ },
7286
+ '/executions/{taskId}': {
7287
+ get: {
7288
+ summary: 'Get specific execution',
7289
+ description: 'Returns details of a specific execution task.',
7290
+ parameters: [
7291
+ {
7292
+ in: 'path',
7293
+ name: 'taskId',
7294
+ required: true,
7295
+ schema: {
7296
+ type: 'string',
7297
+ },
7298
+ description: 'The ID of the execution task to retrieve.',
7299
+ },
7300
+ ],
7301
+ responses: {
7302
+ '200': {
7303
+ description: 'The execution task with full details.',
7304
+ content: {
7305
+ 'application/json': {
7306
+ schema: {
7307
+ type: 'object',
7308
+ properties: {
7309
+ nonce: {
7310
+ type: 'string',
7311
+ },
7312
+ taskId: {
7313
+ type: 'string',
7314
+ },
7315
+ taskType: {
7316
+ type: 'string',
7317
+ },
7318
+ status: {
7319
+ type: 'string',
7320
+ },
7321
+ errors: {
7322
+ type: 'array',
7323
+ items: {
7324
+ type: 'object',
7325
+ },
7326
+ },
7327
+ warnings: {
7328
+ type: 'array',
7329
+ items: {
7330
+ type: 'object',
7331
+ },
7332
+ },
7333
+ createdAt: {
7334
+ type: 'string',
7335
+ format: 'date-time',
7336
+ },
7337
+ updatedAt: {
7338
+ type: 'string',
7339
+ format: 'date-time',
7340
+ },
7341
+ currentValue: {
7342
+ type: 'object',
7343
+ },
7344
+ },
7345
+ },
7346
+ },
7347
+ },
7348
+ },
7349
+ '404': {
7350
+ description: 'Execution task not found.',
7351
+ content: {
7352
+ 'text/plain': {
7353
+ schema: {
7354
+ type: 'string',
7355
+ },
7356
+ },
7357
+ },
7358
+ },
6997
7359
  },
6998
7360
  },
6999
7361
  },
@@ -7010,12 +7372,19 @@
7010
7372
  properties: {
7011
7373
  pipelineUrl: {
7012
7374
  type: 'string',
7375
+ description: 'URL of the pipeline to execute',
7376
+ },
7377
+ book: {
7378
+ type: 'string',
7379
+ description: 'Alternative field for pipelineUrl',
7013
7380
  },
7014
7381
  inputParameters: {
7015
7382
  type: 'object',
7383
+ description: 'Parameters for pipeline execution',
7016
7384
  },
7017
7385
  identification: {
7018
7386
  type: 'object',
7387
+ description: 'User identification data',
7019
7388
  },
7020
7389
  },
7021
7390
  },
@@ -7035,13 +7404,164 @@
7035
7404
  },
7036
7405
  '400': {
7037
7406
  description: 'Invalid input.',
7407
+ content: {
7408
+ 'application/json': {
7409
+ schema: {
7410
+ type: 'object',
7411
+ properties: {
7412
+ error: {
7413
+ type: 'object',
7414
+ },
7415
+ },
7416
+ },
7417
+ },
7418
+ },
7419
+ },
7420
+ '404': {
7421
+ description: 'Pipeline not found.',
7422
+ content: {
7423
+ 'text/plain': {
7424
+ schema: {
7425
+ type: 'string',
7426
+ },
7427
+ },
7428
+ },
7429
+ },
7430
+ },
7431
+ },
7432
+ },
7433
+ '/api-docs': {
7434
+ get: {
7435
+ summary: 'API documentation UI',
7436
+ description: 'Swagger UI for API documentation',
7437
+ responses: {
7438
+ '200': {
7439
+ description: 'HTML Swagger UI',
7440
+ },
7441
+ },
7442
+ },
7443
+ },
7444
+ '/swagger': {
7445
+ get: {
7446
+ summary: 'API documentation UI (alternative path)',
7447
+ description: 'Swagger UI for API documentation',
7448
+ responses: {
7449
+ '200': {
7450
+ description: 'HTML Swagger UI',
7451
+ },
7452
+ },
7453
+ },
7454
+ },
7455
+ '/openapi': {
7456
+ get: {
7457
+ summary: 'OpenAPI specification',
7458
+ description: 'Returns the OpenAPI JSON specification',
7459
+ responses: {
7460
+ '200': {
7461
+ description: 'OpenAPI specification',
7462
+ content: {
7463
+ 'application/json': {
7464
+ schema: {
7465
+ type: 'object',
7466
+ },
7467
+ },
7468
+ },
7469
+ },
7470
+ },
7471
+ },
7472
+ },
7473
+ },
7474
+ components: {
7475
+ schemas: {
7476
+ Error: {
7477
+ type: 'object',
7478
+ properties: {
7479
+ error: {
7480
+ type: 'object',
7481
+ },
7482
+ },
7483
+ },
7484
+ ExecutionTaskSummary: {
7485
+ type: 'object',
7486
+ properties: {
7487
+ nonce: {
7488
+ type: 'string',
7489
+ },
7490
+ taskId: {
7491
+ type: 'string',
7492
+ },
7493
+ taskType: {
7494
+ type: 'string',
7495
+ },
7496
+ status: {
7497
+ type: 'string',
7498
+ },
7499
+ createdAt: {
7500
+ type: 'string',
7501
+ format: 'date-time',
7502
+ },
7503
+ updatedAt: {
7504
+ type: 'string',
7505
+ format: 'date-time',
7506
+ },
7507
+ },
7508
+ },
7509
+ ExecutionTaskFull: {
7510
+ type: 'object',
7511
+ properties: {
7512
+ nonce: {
7513
+ type: 'string',
7514
+ },
7515
+ taskId: {
7516
+ type: 'string',
7517
+ },
7518
+ taskType: {
7519
+ type: 'string',
7520
+ },
7521
+ status: {
7522
+ type: 'string',
7523
+ },
7524
+ errors: {
7525
+ type: 'array',
7526
+ items: {
7527
+ type: 'object',
7528
+ },
7529
+ },
7530
+ warnings: {
7531
+ type: 'array',
7532
+ items: {
7533
+ type: 'object',
7534
+ },
7535
+ },
7536
+ createdAt: {
7537
+ type: 'string',
7538
+ format: 'date-time',
7539
+ },
7540
+ updatedAt: {
7541
+ type: 'string',
7542
+ format: 'date-time',
7543
+ },
7544
+ currentValue: {
7545
+ type: 'object',
7038
7546
  },
7039
7547
  },
7040
7548
  },
7041
7549
  },
7042
7550
  },
7043
- components: {},
7044
- tags: [],
7551
+ tags: [
7552
+ {
7553
+ name: 'Books',
7554
+ description: 'Operations related to books and pipelines',
7555
+ },
7556
+ {
7557
+ name: 'Executions',
7558
+ description: 'Operations related to execution tasks',
7559
+ },
7560
+ {
7561
+ name: 'Authentication',
7562
+ description: 'Authentication operations',
7563
+ },
7564
+ ],
7045
7565
  };
7046
7566
  /**
7047
7567
  * Note: [💞] Ignore a discrepancy between file name and entity name