@promptbook/markdown-utils 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/esm/index.es.js CHANGED
@@ -25,7 +25,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
25
25
  * @generated
26
26
  * @see https://github.com/webgptorg/promptbook
27
27
  */
28
- const PROMPTBOOK_ENGINE_VERSION = '0.92.0-5';
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
@@ -1797,6 +1797,45 @@ function isPipelinePrepared(pipeline) {
1797
1797
  * - [♨] Are tasks prepared
1798
1798
  */
1799
1799
 
1800
+ /**
1801
+ * Converts a JavaScript Object Notation (JSON) string into an object.
1802
+ *
1803
+ * Note: This is wrapper around `JSON.parse()` with better error and type handling
1804
+ *
1805
+ * @public exported from `@promptbook/utils`
1806
+ */
1807
+ function jsonParse(value) {
1808
+ if (value === undefined) {
1809
+ throw new Error(`Can not parse JSON from undefined value.`);
1810
+ }
1811
+ else if (typeof value !== 'string') {
1812
+ console.error('Can not parse JSON from non-string value.', { text: value });
1813
+ throw new Error(spaceTrim(`
1814
+ Can not parse JSON from non-string value.
1815
+
1816
+ The value type: ${typeof value}
1817
+ See more in console.
1818
+ `));
1819
+ }
1820
+ try {
1821
+ return JSON.parse(value);
1822
+ }
1823
+ catch (error) {
1824
+ if (!(error instanceof Error)) {
1825
+ throw error;
1826
+ }
1827
+ throw new Error(spaceTrim((block) => `
1828
+ ${block(error.message)}
1829
+
1830
+ The JSON text:
1831
+ ${block(value)}
1832
+ `));
1833
+ }
1834
+ }
1835
+ /**
1836
+ * TODO: !!!! Use in Promptbook.studio
1837
+ */
1838
+
1800
1839
  /**
1801
1840
  * Recursively converts JSON strings to JSON objects
1802
1841
 
@@ -1815,7 +1854,7 @@ function jsonStringsToJsons(object) {
1815
1854
  const newObject = { ...object };
1816
1855
  for (const [key, value] of Object.entries(object)) {
1817
1856
  if (typeof value === 'string' && isValidJsonString(value)) {
1818
- newObject[key] = JSON.parse(value);
1857
+ newObject[key] = jsonParse(value);
1819
1858
  }
1820
1859
  else {
1821
1860
  newObject[key] = jsonStringsToJsons(value);
@@ -2688,7 +2727,7 @@ async function preparePersona(personaDescription, tools, options) {
2688
2727
  }).asPromise();
2689
2728
  const { outputParameters } = result;
2690
2729
  const { modelsRequirements: modelsRequirementsJson } = outputParameters;
2691
- const modelsRequirementsUnchecked = JSON.parse(modelsRequirementsJson);
2730
+ const modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
2692
2731
  if (isVerbose) {
2693
2732
  console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
2694
2733
  }
@@ -3530,7 +3569,7 @@ async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
3530
3569
  > },
3531
3570
  */
3532
3571
  async asJson() {
3533
- return JSON.parse(await tools.fs.readFile(filename, 'utf-8'));
3572
+ return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
3534
3573
  },
3535
3574
  async asText() {
3536
3575
  return await tools.fs.readFile(filename, 'utf-8');
@@ -5115,13 +5154,79 @@ async function getExamplesForTask(task) {
5115
5154
  /**
5116
5155
  * @@@
5117
5156
  *
5157
+ * Here is the place where RAG (retrieval-augmented generation) happens
5158
+ *
5118
5159
  * @private internal utility of `createPipelineExecutor`
5119
5160
  */
5120
5161
  async function getKnowledgeForTask(options) {
5121
- const { preparedPipeline, task } = options;
5122
- return preparedPipeline.knowledgePieces.map(({ content }) => `- ${content}`).join('\n');
5162
+ const { tools, preparedPipeline, task } = options;
5163
+ const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
5164
+ const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
5165
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
5166
+ if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
5167
+ return 'No knowledge pieces found';
5168
+ }
5169
+ // TODO: [🚐] Make arrayable LLMs -> single LLM DRY
5170
+ const _llms = arrayableToArray(tools.llm);
5171
+ const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
5172
+ const taskEmbeddingPrompt = {
5173
+ title: 'Knowledge Search',
5174
+ modelRequirements: {
5175
+ modelVariant: 'EMBEDDING',
5176
+ modelName: firstKnowlegeIndex.modelName,
5177
+ },
5178
+ content: task.content,
5179
+ parameters: {
5180
+ /* !!!!!!!! */
5181
+ },
5182
+ };
5183
+ const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
5184
+ const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
5185
+ const { index } = knowledgePiece;
5186
+ const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
5187
+ // <- TODO: Do not use just first knowledge piece and first index to determine embedding model
5188
+ if (knowledgePieceIndex === undefined) {
5189
+ return {
5190
+ content: knowledgePiece.content,
5191
+ relevance: 0,
5192
+ };
5193
+ }
5194
+ const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
5195
+ return {
5196
+ content: knowledgePiece.content,
5197
+ relevance,
5198
+ };
5199
+ });
5200
+ const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
5201
+ const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
5202
+ console.log('!!! Embedding', {
5203
+ task,
5204
+ taskEmbeddingPrompt,
5205
+ taskEmbeddingResult,
5206
+ firstKnowlegePiece,
5207
+ firstKnowlegeIndex,
5208
+ knowledgePiecesWithRelevance,
5209
+ knowledgePiecesSorted,
5210
+ knowledgePiecesLimited,
5211
+ });
5212
+ return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
5123
5213
  // <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
5124
5214
  }
5215
+ // TODO: !!!!!! Annotate + to new file
5216
+ function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
5217
+ if (embeddingVector1.length !== embeddingVector2.length) {
5218
+ throw new TypeError('Embedding vectors must have the same length');
5219
+ }
5220
+ const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
5221
+ const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
5222
+ const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
5223
+ return 1 - dotProduct / (magnitude1 * magnitude2);
5224
+ }
5225
+ /**
5226
+ * TODO: !!!! Verify if this is working
5227
+ * TODO: [♨] Implement Better - use keyword search
5228
+ * TODO: [♨] Examples of values
5229
+ */
5125
5230
 
5126
5231
  /**
5127
5232
  * @@@
@@ -5129,9 +5234,9 @@ async function getKnowledgeForTask(options) {
5129
5234
  * @private internal utility of `createPipelineExecutor`
5130
5235
  */
5131
5236
  async function getReservedParametersForTask(options) {
5132
- const { preparedPipeline, task, pipelineIdentification } = options;
5237
+ const { tools, preparedPipeline, task, pipelineIdentification } = options;
5133
5238
  const context = await getContextForTask(); // <- [🏍]
5134
- const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
5239
+ const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
5135
5240
  const examples = await getExamplesForTask();
5136
5241
  const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
5137
5242
  const modelName = RESERVED_PARAMETER_MISSING_VALUE;
@@ -5193,6 +5298,7 @@ async function executeTask(options) {
5193
5298
  }
5194
5299
  const definedParameters = Object.freeze({
5195
5300
  ...(await getReservedParametersForTask({
5301
+ tools,
5196
5302
  preparedPipeline,
5197
5303
  task: currentTask,
5198
5304
  pipelineIdentification,