@promptbook/pdf 0.92.0-4 → 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
@@ -25,7 +25,7 @@
25
25
  * @generated
26
26
  * @see https://github.com/webgptorg/promptbook
27
27
  */
28
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-4';
28
+ const PROMPTBOOK_ENGINE_VERSION = '0.92.0-6';
29
29
  /**
30
30
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
31
31
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2136,6 +2136,45 @@
2136
2136
  * - [♨] Are tasks prepared
2137
2137
  */
2138
2138
 
2139
+ /**
2140
+ * Converts a JavaScript Object Notation (JSON) string into an object.
2141
+ *
2142
+ * Note: This is wrapper around `JSON.parse()` with better error and type handling
2143
+ *
2144
+ * @public exported from `@promptbook/utils`
2145
+ */
2146
+ function jsonParse(value) {
2147
+ if (value === undefined) {
2148
+ throw new Error(`Can not parse JSON from undefined value.`);
2149
+ }
2150
+ else if (typeof value !== 'string') {
2151
+ console.error('Can not parse JSON from non-string value.', { text: value });
2152
+ throw new Error(spaceTrim__default["default"](`
2153
+ Can not parse JSON from non-string value.
2154
+
2155
+ The value type: ${typeof value}
2156
+ See more in console.
2157
+ `));
2158
+ }
2159
+ try {
2160
+ return JSON.parse(value);
2161
+ }
2162
+ catch (error) {
2163
+ if (!(error instanceof Error)) {
2164
+ throw error;
2165
+ }
2166
+ throw new Error(spaceTrim__default["default"]((block) => `
2167
+ ${block(error.message)}
2168
+
2169
+ The JSON text:
2170
+ ${block(value)}
2171
+ `));
2172
+ }
2173
+ }
2174
+ /**
2175
+ * TODO: !!!! Use in Promptbook.studio
2176
+ */
2177
+
2139
2178
  /**
2140
2179
  * Recursively converts JSON strings to JSON objects
2141
2180
 
@@ -2154,7 +2193,7 @@
2154
2193
  const newObject = { ...object };
2155
2194
  for (const [key, value] of Object.entries(object)) {
2156
2195
  if (typeof value === 'string' && isValidJsonString(value)) {
2157
- newObject[key] = JSON.parse(value);
2196
+ newObject[key] = jsonParse(value);
2158
2197
  }
2159
2198
  else {
2160
2199
  newObject[key] = jsonStringsToJsons(value);
@@ -3001,7 +3040,7 @@
3001
3040
  }).asPromise();
3002
3041
  const { outputParameters } = result;
3003
3042
  const { modelsRequirements: modelsRequirementsJson } = outputParameters;
3004
- const modelsRequirementsUnchecked = JSON.parse(modelsRequirementsJson);
3043
+ const modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
3005
3044
  if (isVerbose) {
3006
3045
  console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
3007
3046
  }
@@ -3457,7 +3496,7 @@
3457
3496
  > },
3458
3497
  */
3459
3498
  async asJson() {
3460
- return JSON.parse(await tools.fs.readFile(filename, 'utf-8'));
3499
+ return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
3461
3500
  },
3462
3501
  async asText() {
3463
3502
  return await tools.fs.readFile(filename, 'utf-8');
@@ -5144,13 +5183,79 @@
5144
5183
  /**
5145
5184
  * @@@
5146
5185
  *
5186
+ * Here is the place where RAG (retrieval-augmented generation) happens
5187
+ *
5147
5188
  * @private internal utility of `createPipelineExecutor`
5148
5189
  */
5149
5190
  async function getKnowledgeForTask(options) {
5150
- const { preparedPipeline, task } = options;
5151
- return preparedPipeline.knowledgePieces.map(({ content }) => `- ${content}`).join('\n');
5191
+ const { tools, preparedPipeline, task } = options;
5192
+ const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
5193
+ const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
5194
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
5195
+ if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
5196
+ return 'No knowledge pieces found';
5197
+ }
5198
+ // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
5199
+ const _llms = arrayableToArray(tools.llm);
5200
+ const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
5201
+ const taskEmbeddingPrompt = {
5202
+ title: 'Knowledge Search',
5203
+ modelRequirements: {
5204
+ modelVariant: 'EMBEDDING',
5205
+ modelName: firstKnowlegeIndex.modelName,
5206
+ },
5207
+ content: task.content,
5208
+ parameters: {
5209
+ /* !!!!!!!! */
5210
+ },
5211
+ };
5212
+ const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
5213
+ const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
5214
+ const { index } = knowledgePiece;
5215
+ const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
5216
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model
5217
+ if (knowledgePieceIndex === undefined) {
5218
+ return {
5219
+ content: knowledgePiece.content,
5220
+ relevance: 0,
5221
+ };
5222
+ }
5223
+ const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
5224
+ return {
5225
+ content: knowledgePiece.content,
5226
+ relevance,
5227
+ };
5228
+ });
5229
+ const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
5230
+ const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
5231
+ console.log('!!! Embedding', {
5232
+ task,
5233
+ taskEmbeddingPrompt,
5234
+ taskEmbeddingResult,
5235
+ firstKnowlegePiece,
5236
+ firstKnowlegeIndex,
5237
+ knowledgePiecesWithRelevance,
5238
+ knowledgePiecesSorted,
5239
+ knowledgePiecesLimited,
5240
+ });
5241
+ return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5152
5242
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5153
5243
  }
5244
+ // TODO: !!!!!! Annotate + to new file
5245
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5246
+ if (embeddingVector1.length !== embeddingVector2.length) {
5247
+ throw new TypeError('Embedding vectors must have the same length');
5248
+ }
5249
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5250
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5251
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5252
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5253
+ }
5254
+ /**
5255
+ * TODO: !!!! Verify if this is working
5256
+ * TODO: [♨] Implement Better - use keyword search
5257
+ * TODO: [♨] Examples of values
5258
+ */
5154
5259
 
5155
5260
  /**
5156
5261
  * @@@
@@ -5158,9 +5263,9 @@
5158
5263
  * @private internal utility of `createPipelineExecutor`
5159
5264
  */
5160
5265
  async function getReservedParametersForTask(options) {
5161
- const { preparedPipeline, task, pipelineIdentification } = options;
5266
+ const { tools, preparedPipeline, task, pipelineIdentification } = options;
5162
5267
  const context = await getContextForTask(); // <- [🏍]
5163
- const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
5268
+ const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
5164
5269
  const examples = await getExamplesForTask();
5165
5270
  const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
5166
5271
  const modelName = RESERVED_PARAMETER_MISSING_VALUE;
@@ -5222,6 +5327,7 @@
5222
5327
  }
5223
5328
  const definedParameters = Object.freeze({
5224
5329
  ...(await getReservedParametersForTask({
5330
+ tools,
5225
5331
  preparedPipeline,
5226
5332
  task: currentTask,
5227
5333
  pipelineIdentification,