@promptbook/pdf 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
@@ -26,7 +26,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
26
26
  * @generated
27
27
  * @see https://github.com/webgptorg/promptbook
28
28
  */
29
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-5';
29
+ const PROMPTBOOK_ENGINE_VERSION = '0.92.0-7';
30
30
  /**
31
31
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
32
32
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2137,6 +2137,45 @@ function isPipelinePrepared(pipeline) {
2137
2137
  * - [♨] Are tasks prepared
2138
2138
  */
2139
2139
 
2140
+ /**
2141
+ * Converts a JavaScript Object Notation (JSON) string into an object.
2142
+ *
2143
+ * Note: This is wrapper around `JSON.parse()` with better error and type handling
2144
+ *
2145
+ * @public exported from `@promptbook/utils`
2146
+ */
2147
+ function jsonParse(value) {
2148
+ if (value === undefined) {
2149
+ throw new Error(`Can not parse JSON from undefined value.`);
2150
+ }
2151
+ else if (typeof value !== 'string') {
2152
+ console.error('Can not parse JSON from non-string value.', { text: value });
2153
+ throw new Error(spaceTrim(`
2154
+ Can not parse JSON from non-string value.
2155
+
2156
+ The value type: ${typeof value}
2157
+ See more in console.
2158
+ `));
2159
+ }
2160
+ try {
2161
+ return JSON.parse(value);
2162
+ }
2163
+ catch (error) {
2164
+ if (!(error instanceof Error)) {
2165
+ throw error;
2166
+ }
2167
+ throw new Error(spaceTrim((block) => `
2168
+ ${block(error.message)}
2169
+
2170
+ The JSON text:
2171
+ ${block(value)}
2172
+ `));
2173
+ }
2174
+ }
2175
+ /**
2176
+ * TODO: !!!! Use in Promptbook.studio
2177
+ */
2178
+
2140
2179
  /**
2141
2180
  * Recursively converts JSON strings to JSON objects
2142
2181
 
@@ -2155,7 +2194,7 @@ function jsonStringsToJsons(object) {
2155
2194
  const newObject = { ...object };
2156
2195
  for (const [key, value] of Object.entries(object)) {
2157
2196
  if (typeof value === 'string' && isValidJsonString(value)) {
2158
- newObject[key] = JSON.parse(value);
2197
+ newObject[key] = jsonParse(value);
2159
2198
  }
2160
2199
  else {
2161
2200
  newObject[key] = jsonStringsToJsons(value);
@@ -3002,18 +3041,26 @@ async function preparePersona(personaDescription, tools, options) {
3002
3041
  }).asPromise();
3003
3042
  const { outputParameters } = result;
3004
3043
  const { modelsRequirements: modelsRequirementsJson } = outputParameters;
3005
- const modelsRequirementsUnchecked = JSON.parse(modelsRequirementsJson);
3044
+ let modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
3006
3045
  if (isVerbose) {
3007
3046
  console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
3008
3047
  }
3009
3048
  if (!Array.isArray(modelsRequirementsUnchecked)) {
3010
- throw new UnexpectedError(spaceTrim((block) => `
3049
+ // <- TODO: Book should have syntax and system to enforce shape of JSON
3050
+ modelsRequirementsUnchecked = [modelsRequirementsUnchecked];
3051
+ /*
3052
+ throw new UnexpectedError(
3053
+ spaceTrim(
3054
+ (block) => `
3011
3055
  Invalid \`modelsRequirements\`:
3012
3056
 
3013
3057
  \`\`\`json
3014
3058
  ${block(JSON.stringify(modelsRequirementsUnchecked, null, 4))}
3015
3059
  \`\`\`
3016
- `));
3060
+ `,
3061
+ ),
3062
+ );
3063
+ */
3017
3064
  }
3018
3065
  const modelsRequirements = modelsRequirementsUnchecked.map((modelRequirements) => ({
3019
3066
  modelVariant: 'CHAT',
@@ -3458,7 +3505,7 @@ async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
3458
3505
  > },
3459
3506
  */
3460
3507
  async asJson() {
3461
- return JSON.parse(await tools.fs.readFile(filename, 'utf-8'));
3508
+ return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
3462
3509
  },
3463
3510
  async asText() {
3464
3511
  return await tools.fs.readFile(filename, 'utf-8');
@@ -5145,13 +5192,79 @@ async function getExamplesForTask(task) {
5145
5192
  /**
5146
5193
  * @@@
5147
5194
  *
5195
+ * Here is the place where RAG (retrieval-augmented generation) happens
5196
+ *
5148
5197
  * @private internal utility of `createPipelineExecutor`
5149
5198
  */
5150
5199
  async function getKnowledgeForTask(options) {
5151
- const { preparedPipeline, task } = options;
5152
- return preparedPipeline.knowledgePieces.map(({ content }) => `- ${content}`).join('\n');
5200
+ const { tools, preparedPipeline, task } = options;
5201
+ const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
5202
+ const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
5203
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
5204
+ if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
5205
+ return 'No knowledge pieces found';
5206
+ }
5207
+ // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
5208
+ const _llms = arrayableToArray(tools.llm);
5209
+ const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
5210
+ const taskEmbeddingPrompt = {
5211
+ title: 'Knowledge Search',
5212
+ modelRequirements: {
5213
+ modelVariant: 'EMBEDDING',
5214
+ modelName: firstKnowlegeIndex.modelName,
5215
+ },
5216
+ content: task.content,
5217
+ parameters: {
5218
+ /* !!!!!!!! */
5219
+ },
5220
+ };
5221
+ const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
5222
+ const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
5223
+ const { index } = knowledgePiece;
5224
+ const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
5225
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model
5226
+ if (knowledgePieceIndex === undefined) {
5227
+ return {
5228
+ content: knowledgePiece.content,
5229
+ relevance: 0,
5230
+ };
5231
+ }
5232
+ const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
5233
+ return {
5234
+ content: knowledgePiece.content,
5235
+ relevance,
5236
+ };
5237
+ });
5238
+ const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
5239
+ const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
5240
+ console.log('!!! Embedding', {
5241
+ task,
5242
+ taskEmbeddingPrompt,
5243
+ taskEmbeddingResult,
5244
+ firstKnowlegePiece,
5245
+ firstKnowlegeIndex,
5246
+ knowledgePiecesWithRelevance,
5247
+ knowledgePiecesSorted,
5248
+ knowledgePiecesLimited,
5249
+ });
5250
+ return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5153
5251
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5154
5252
  }
5253
+ // TODO: !!!!!! Annotate + to new file
5254
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5255
+ if (embeddingVector1.length !== embeddingVector2.length) {
5256
+ throw new TypeError('Embedding vectors must have the same length');
5257
+ }
5258
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5259
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5260
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5261
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5262
+ }
5263
+ /**
5264
+ * TODO: !!!! Verify if this is working
5265
+ * TODO: [♨] Implement Better - use keyword search
5266
+ * TODO: [♨] Examples of values
5267
+ */
5155
5268
 
5156
5269
  /**
5157
5270
  * @@@
@@ -5159,9 +5272,9 @@ async function getKnowledgeForTask(options) {
5159
5272
  * @private internal utility of `createPipelineExecutor`
5160
5273
  */
5161
5274
  async function getReservedParametersForTask(options) {
5162
- const { preparedPipeline, task, pipelineIdentification } = options;
5275
+ const { tools, preparedPipeline, task, pipelineIdentification } = options;
5163
5276
  const context = await getContextForTask(); // <- [🏍]
5164
- const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
5277
+ const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
5165
5278
  const examples = await getExamplesForTask();
5166
5279
  const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
5167
5280
  const modelName = RESERVED_PARAMETER_MISSING_VALUE;
@@ -5223,6 +5336,7 @@ async function executeTask(options) {
5223
5336
  }
5224
5337
  const definedParameters = Object.freeze({
5225
5338
  ...(await getReservedParametersForTask({
5339
+ tools,
5226
5340
  preparedPipeline,
5227
5341
  task: currentTask,
5228
5342
  pipelineIdentification,