@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/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-7';
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,18 +2973,26 @@ 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
+ let modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
2938
2977
  if (isVerbose) {
2939
2978
  console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
2940
2979
  }
2941
2980
  if (!Array.isArray(modelsRequirementsUnchecked)) {
2942
- throw new UnexpectedError(spaceTrim((block) => `
2981
+ // <- TODO: Book should have syntax and system to enforce shape of JSON
2982
+ modelsRequirementsUnchecked = [modelsRequirementsUnchecked];
2983
+ /*
2984
+ throw new UnexpectedError(
2985
+ spaceTrim(
2986
+ (block) => `
2943
2987
  Invalid \`modelsRequirements\`:
2944
2988
 
2945
2989
  \`\`\`json
2946
2990
  ${block(JSON.stringify(modelsRequirementsUnchecked, null, 4))}
2947
2991
  \`\`\`
2948
- `));
2992
+ `,
2993
+ ),
2994
+ );
2995
+ */
2949
2996
  }
2950
2997
  const modelsRequirements = modelsRequirementsUnchecked.map((modelRequirements) => ({
2951
2998
  modelVariant: 'CHAT',
@@ -3776,7 +3823,7 @@ async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
3776
3823
  > },
3777
3824
  */
3778
3825
  async asJson() {
3779
- return JSON.parse(await tools.fs.readFile(filename, 'utf-8'));
3826
+ return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
3780
3827
  },
3781
3828
  async asText() {
3782
3829
  return await tools.fs.readFile(filename, 'utf-8');
@@ -5480,13 +5527,79 @@ async function getExamplesForTask(task) {
5480
5527
  /**
5481
5528
  * @@@
5482
5529
  *
5530
+ * Here is the place where RAG (retrieval-augmented generation) happens
5531
+ *
5483
5532
  * @private internal utility of `createPipelineExecutor`
5484
5533
  */
5485
5534
  async function getKnowledgeForTask(options) {
5486
- const { preparedPipeline, task } = options;
5487
- return preparedPipeline.knowledgePieces.map(({ content }) => `- ${content}`).join('\n');
5535
+ const { tools, preparedPipeline, task } = options;
5536
+ const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
5537
+ const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
5538
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
5539
+ if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
5540
+ return 'No knowledge pieces found';
5541
+ }
5542
+ // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
5543
+ const _llms = arrayableToArray(tools.llm);
5544
+ const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
5545
+ const taskEmbeddingPrompt = {
5546
+ title: 'Knowledge Search',
5547
+ modelRequirements: {
5548
+ modelVariant: 'EMBEDDING',
5549
+ modelName: firstKnowlegeIndex.modelName,
5550
+ },
5551
+ content: task.content,
5552
+ parameters: {
5553
+ /* !!!!!!!! */
5554
+ },
5555
+ };
5556
+ const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
5557
+ const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
5558
+ const { index } = knowledgePiece;
5559
+ const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
5560
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model
5561
+ if (knowledgePieceIndex === undefined) {
5562
+ return {
5563
+ content: knowledgePiece.content,
5564
+ relevance: 0,
5565
+ };
5566
+ }
5567
+ const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
5568
+ return {
5569
+ content: knowledgePiece.content,
5570
+ relevance,
5571
+ };
5572
+ });
5573
+ const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
5574
+ const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
5575
+ console.log('!!! Embedding', {
5576
+ task,
5577
+ taskEmbeddingPrompt,
5578
+ taskEmbeddingResult,
5579
+ firstKnowlegePiece,
5580
+ firstKnowlegeIndex,
5581
+ knowledgePiecesWithRelevance,
5582
+ knowledgePiecesSorted,
5583
+ knowledgePiecesLimited,
5584
+ });
5585
+ return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5488
5586
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5489
5587
  }
5588
+ // TODO: !!!!!! Annotate + to new file
5589
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5590
+ if (embeddingVector1.length !== embeddingVector2.length) {
5591
+ throw new TypeError('Embedding vectors must have the same length');
5592
+ }
5593
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5594
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5595
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5596
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5597
+ }
5598
+ /**
5599
+ * TODO: !!!! Verify if this is working
5600
+ * TODO: [♨] Implement Better - use keyword search
5601
+ * TODO: [♨] Examples of values
5602
+ */
5490
5603
 
5491
5604
  /**
5492
5605
  * @@@
@@ -5494,9 +5607,9 @@ async function getKnowledgeForTask(options) {
5494
5607
  * @private internal utility of `createPipelineExecutor`
5495
5608
  */
5496
5609
  async function getReservedParametersForTask(options) {
5497
- const { preparedPipeline, task, pipelineIdentification } = options;
5610
+ const { tools, preparedPipeline, task, pipelineIdentification } = options;
5498
5611
  const context = await getContextForTask(); // <- [🏍]
5499
- const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
5612
+ const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
5500
5613
  const examples = await getExamplesForTask();
5501
5614
  const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
5502
5615
  const modelName = RESERVED_PARAMETER_MISSING_VALUE;
@@ -5558,6 +5671,7 @@ async function executeTask(options) {
5558
5671
  }
5559
5672
  const definedParameters = Object.freeze({
5560
5673
  ...(await getReservedParametersForTask({
5674
+ tools,
5561
5675
  preparedPipeline,
5562
5676
  task: currentTask,
5563
5677
  pipelineIdentification,
@@ -6848,7 +6962,7 @@ async function $provideScriptingForNode(options) {
6848
6962
  const openapiJson = {
6849
6963
  openapi: '3.0.0',
6850
6964
  info: {
6851
- title: 'Promptbook Remote Server API (!!!! From TS)',
6965
+ title: 'Promptbook Remote Server API (!!!! From YML)',
6852
6966
  version: '1.0.0',
6853
6967
  description: 'API documentation for the Promptbook Remote Server',
6854
6968
  },
@@ -6860,6 +6974,13 @@ const openapiJson = {
6860
6974
  responses: {
6861
6975
  '200': {
6862
6976
  description: 'Server details in markdown format.',
6977
+ content: {
6978
+ 'text/markdown': {
6979
+ schema: {
6980
+ type: 'string',
6981
+ },
6982
+ },
6983
+ },
6863
6984
  },
6864
6985
  },
6865
6986
  },
@@ -6890,13 +7011,22 @@ const openapiJson = {
6890
7011
  },
6891
7012
  },
6892
7013
  responses: {
6893
- '200': {
7014
+ '201': {
6894
7015
  description: 'Successful login',
6895
7016
  content: {
6896
7017
  'application/json': {
6897
7018
  schema: {
6898
7019
  type: 'object',
6899
7020
  properties: {
7021
+ isSuccess: {
7022
+ type: 'boolean',
7023
+ },
7024
+ message: {
7025
+ type: 'string',
7026
+ },
7027
+ error: {
7028
+ type: 'object',
7029
+ },
6900
7030
  identification: {
6901
7031
  type: 'object',
6902
7032
  },
@@ -6905,6 +7035,43 @@ const openapiJson = {
6905
7035
  },
6906
7036
  },
6907
7037
  },
7038
+ '400': {
7039
+ description: 'Bad request or login failed',
7040
+ content: {
7041
+ 'application/json': {
7042
+ schema: {
7043
+ type: 'object',
7044
+ properties: {
7045
+ error: {
7046
+ type: 'object',
7047
+ },
7048
+ },
7049
+ },
7050
+ },
7051
+ },
7052
+ },
7053
+ '401': {
7054
+ description: 'Authentication error',
7055
+ content: {
7056
+ 'application/json': {
7057
+ schema: {
7058
+ type: 'object',
7059
+ properties: {
7060
+ isSuccess: {
7061
+ type: 'boolean',
7062
+ enum: [false],
7063
+ },
7064
+ message: {
7065
+ type: 'string',
7066
+ },
7067
+ error: {
7068
+ type: 'object',
7069
+ },
7070
+ },
7071
+ },
7072
+ },
7073
+ },
7074
+ },
6908
7075
  },
6909
7076
  },
6910
7077
  },
@@ -6926,6 +7093,16 @@ const openapiJson = {
6926
7093
  },
6927
7094
  },
6928
7095
  },
7096
+ '500': {
7097
+ description: 'No collection available',
7098
+ content: {
7099
+ 'text/plain': {
7100
+ schema: {
7101
+ type: 'string',
7102
+ },
7103
+ },
7104
+ },
7105
+ },
6929
7106
  },
6930
7107
  },
6931
7108
  },
@@ -6957,6 +7134,28 @@ const openapiJson = {
6957
7134
  },
6958
7135
  '404': {
6959
7136
  description: 'Book not found.',
7137
+ content: {
7138
+ 'application/json': {
7139
+ schema: {
7140
+ type: 'object',
7141
+ properties: {
7142
+ error: {
7143
+ type: 'object',
7144
+ },
7145
+ },
7146
+ },
7147
+ },
7148
+ },
7149
+ },
7150
+ '500': {
7151
+ description: 'No collection available',
7152
+ content: {
7153
+ 'text/plain': {
7154
+ schema: {
7155
+ type: 'string',
7156
+ },
7157
+ },
7158
+ },
6960
7159
  },
6961
7160
  },
6962
7161
  },
@@ -6974,6 +7173,28 @@ const openapiJson = {
6974
7173
  type: 'array',
6975
7174
  items: {
6976
7175
  type: 'object',
7176
+ properties: {
7177
+ nonce: {
7178
+ type: 'string',
7179
+ },
7180
+ taskId: {
7181
+ type: 'string',
7182
+ },
7183
+ taskType: {
7184
+ type: 'string',
7185
+ },
7186
+ status: {
7187
+ type: 'string',
7188
+ },
7189
+ createdAt: {
7190
+ type: 'string',
7191
+ format: 'date-time',
7192
+ },
7193
+ updatedAt: {
7194
+ type: 'string',
7195
+ format: 'date-time',
7196
+ },
7197
+ },
6977
7198
  },
6978
7199
  },
6979
7200
  },
@@ -6982,6 +7203,147 @@ const openapiJson = {
6982
7203
  },
6983
7204
  },
6984
7205
  },
7206
+ '/executions/last': {
7207
+ get: {
7208
+ summary: 'Get the last execution',
7209
+ description: 'Returns details of the last execution task.',
7210
+ responses: {
7211
+ '200': {
7212
+ description: 'The last execution task with full details.',
7213
+ content: {
7214
+ 'application/json': {
7215
+ schema: {
7216
+ type: 'object',
7217
+ properties: {
7218
+ nonce: {
7219
+ type: 'string',
7220
+ },
7221
+ taskId: {
7222
+ type: 'string',
7223
+ },
7224
+ taskType: {
7225
+ type: 'string',
7226
+ },
7227
+ status: {
7228
+ type: 'string',
7229
+ },
7230
+ errors: {
7231
+ type: 'array',
7232
+ items: {
7233
+ type: 'object',
7234
+ },
7235
+ },
7236
+ warnings: {
7237
+ type: 'array',
7238
+ items: {
7239
+ type: 'object',
7240
+ },
7241
+ },
7242
+ createdAt: {
7243
+ type: 'string',
7244
+ format: 'date-time',
7245
+ },
7246
+ updatedAt: {
7247
+ type: 'string',
7248
+ format: 'date-time',
7249
+ },
7250
+ currentValue: {
7251
+ type: 'object',
7252
+ },
7253
+ },
7254
+ },
7255
+ },
7256
+ },
7257
+ },
7258
+ '404': {
7259
+ description: 'No execution tasks found.',
7260
+ content: {
7261
+ 'text/plain': {
7262
+ schema: {
7263
+ type: 'string',
7264
+ },
7265
+ },
7266
+ },
7267
+ },
7268
+ },
7269
+ },
7270
+ },
7271
+ '/executions/{taskId}': {
7272
+ get: {
7273
+ summary: 'Get specific execution',
7274
+ description: 'Returns details of a specific execution task.',
7275
+ parameters: [
7276
+ {
7277
+ in: 'path',
7278
+ name: 'taskId',
7279
+ required: true,
7280
+ schema: {
7281
+ type: 'string',
7282
+ },
7283
+ description: 'The ID of the execution task to retrieve.',
7284
+ },
7285
+ ],
7286
+ responses: {
7287
+ '200': {
7288
+ description: 'The execution task with full details.',
7289
+ content: {
7290
+ 'application/json': {
7291
+ schema: {
7292
+ type: 'object',
7293
+ properties: {
7294
+ nonce: {
7295
+ type: 'string',
7296
+ },
7297
+ taskId: {
7298
+ type: 'string',
7299
+ },
7300
+ taskType: {
7301
+ type: 'string',
7302
+ },
7303
+ status: {
7304
+ type: 'string',
7305
+ },
7306
+ errors: {
7307
+ type: 'array',
7308
+ items: {
7309
+ type: 'object',
7310
+ },
7311
+ },
7312
+ warnings: {
7313
+ type: 'array',
7314
+ items: {
7315
+ type: 'object',
7316
+ },
7317
+ },
7318
+ createdAt: {
7319
+ type: 'string',
7320
+ format: 'date-time',
7321
+ },
7322
+ updatedAt: {
7323
+ type: 'string',
7324
+ format: 'date-time',
7325
+ },
7326
+ currentValue: {
7327
+ type: 'object',
7328
+ },
7329
+ },
7330
+ },
7331
+ },
7332
+ },
7333
+ },
7334
+ '404': {
7335
+ description: 'Execution task not found.',
7336
+ content: {
7337
+ 'text/plain': {
7338
+ schema: {
7339
+ type: 'string',
7340
+ },
7341
+ },
7342
+ },
7343
+ },
7344
+ },
7345
+ },
7346
+ },
6985
7347
  '/executions/new': {
6986
7348
  post: {
6987
7349
  summary: 'Start a new execution',
@@ -6995,12 +7357,19 @@ const openapiJson = {
6995
7357
  properties: {
6996
7358
  pipelineUrl: {
6997
7359
  type: 'string',
7360
+ description: 'URL of the pipeline to execute',
7361
+ },
7362
+ book: {
7363
+ type: 'string',
7364
+ description: 'Alternative field for pipelineUrl',
6998
7365
  },
6999
7366
  inputParameters: {
7000
7367
  type: 'object',
7368
+ description: 'Parameters for pipeline execution',
7001
7369
  },
7002
7370
  identification: {
7003
7371
  type: 'object',
7372
+ description: 'User identification data',
7004
7373
  },
7005
7374
  },
7006
7375
  },
@@ -7020,13 +7389,164 @@ const openapiJson = {
7020
7389
  },
7021
7390
  '400': {
7022
7391
  description: 'Invalid input.',
7392
+ content: {
7393
+ 'application/json': {
7394
+ schema: {
7395
+ type: 'object',
7396
+ properties: {
7397
+ error: {
7398
+ type: 'object',
7399
+ },
7400
+ },
7401
+ },
7402
+ },
7403
+ },
7404
+ },
7405
+ '404': {
7406
+ description: 'Pipeline not found.',
7407
+ content: {
7408
+ 'text/plain': {
7409
+ schema: {
7410
+ type: 'string',
7411
+ },
7412
+ },
7413
+ },
7414
+ },
7415
+ },
7416
+ },
7417
+ },
7418
+ '/api-docs': {
7419
+ get: {
7420
+ summary: 'API documentation UI',
7421
+ description: 'Swagger UI for API documentation',
7422
+ responses: {
7423
+ '200': {
7424
+ description: 'HTML Swagger UI',
7425
+ },
7426
+ },
7427
+ },
7428
+ },
7429
+ '/swagger': {
7430
+ get: {
7431
+ summary: 'API documentation UI (alternative path)',
7432
+ description: 'Swagger UI for API documentation',
7433
+ responses: {
7434
+ '200': {
7435
+ description: 'HTML Swagger UI',
7436
+ },
7437
+ },
7438
+ },
7439
+ },
7440
+ '/openapi': {
7441
+ get: {
7442
+ summary: 'OpenAPI specification',
7443
+ description: 'Returns the OpenAPI JSON specification',
7444
+ responses: {
7445
+ '200': {
7446
+ description: 'OpenAPI specification',
7447
+ content: {
7448
+ 'application/json': {
7449
+ schema: {
7450
+ type: 'object',
7451
+ },
7452
+ },
7453
+ },
7454
+ },
7455
+ },
7456
+ },
7457
+ },
7458
+ },
7459
+ components: {
7460
+ schemas: {
7461
+ Error: {
7462
+ type: 'object',
7463
+ properties: {
7464
+ error: {
7465
+ type: 'object',
7466
+ },
7467
+ },
7468
+ },
7469
+ ExecutionTaskSummary: {
7470
+ type: 'object',
7471
+ properties: {
7472
+ nonce: {
7473
+ type: 'string',
7474
+ },
7475
+ taskId: {
7476
+ type: 'string',
7477
+ },
7478
+ taskType: {
7479
+ type: 'string',
7480
+ },
7481
+ status: {
7482
+ type: 'string',
7483
+ },
7484
+ createdAt: {
7485
+ type: 'string',
7486
+ format: 'date-time',
7487
+ },
7488
+ updatedAt: {
7489
+ type: 'string',
7490
+ format: 'date-time',
7491
+ },
7492
+ },
7493
+ },
7494
+ ExecutionTaskFull: {
7495
+ type: 'object',
7496
+ properties: {
7497
+ nonce: {
7498
+ type: 'string',
7499
+ },
7500
+ taskId: {
7501
+ type: 'string',
7502
+ },
7503
+ taskType: {
7504
+ type: 'string',
7505
+ },
7506
+ status: {
7507
+ type: 'string',
7508
+ },
7509
+ errors: {
7510
+ type: 'array',
7511
+ items: {
7512
+ type: 'object',
7513
+ },
7514
+ },
7515
+ warnings: {
7516
+ type: 'array',
7517
+ items: {
7518
+ type: 'object',
7519
+ },
7520
+ },
7521
+ createdAt: {
7522
+ type: 'string',
7523
+ format: 'date-time',
7524
+ },
7525
+ updatedAt: {
7526
+ type: 'string',
7527
+ format: 'date-time',
7528
+ },
7529
+ currentValue: {
7530
+ type: 'object',
7023
7531
  },
7024
7532
  },
7025
7533
  },
7026
7534
  },
7027
7535
  },
7028
- components: {},
7029
- tags: [],
7536
+ tags: [
7537
+ {
7538
+ name: 'Books',
7539
+ description: 'Operations related to books and pipelines',
7540
+ },
7541
+ {
7542
+ name: 'Executions',
7543
+ description: 'Operations related to execution tasks',
7544
+ },
7545
+ {
7546
+ name: 'Authentication',
7547
+ description: 'Authentication operations',
7548
+ },
7549
+ ],
7030
7550
  };
7031
7551
  /**
7032
7552
  * Note: [💞] Ignore a discrepancy between file name and entity name