@promptbook/markdown-utils 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 +124 -10
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +4 -0
- package/esm/typings/src/_packages/utils.index.d.ts +2 -0
- package/esm/typings/src/cli/common/$provideLlmToolsForCli.d.ts +1 -1
- package/esm/typings/src/conversion/archive/loadArchive.d.ts +2 -2
- package/esm/typings/src/execution/createPipelineExecutor/getKnowledgeForTask.d.ts +12 -0
- package/esm/typings/src/execution/createPipelineExecutor/getReservedParametersForTask.d.ts +5 -0
- package/esm/typings/src/formats/json/utils/jsonParse.d.ts +11 -0
- package/esm/typings/src/llm-providers/_common/register/LlmToolsMetadata.d.ts +43 -0
- package/esm/typings/src/remote-server/openapi-types.d.ts +348 -6
- package/esm/typings/src/remote-server/openapi.d.ts +397 -3
- package/package.json +1 -1
- package/umd/index.umd.js +128 -14
- package/umd/index.umd.js.map +1 -1
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-
|
|
28
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.92.0-7';
|
|
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] =
|
|
1857
|
+
newObject[key] = jsonParse(value);
|
|
1819
1858
|
}
|
|
1820
1859
|
else {
|
|
1821
1860
|
newObject[key] = jsonStringsToJsons(value);
|
|
@@ -2688,18 +2727,26 @@ async function preparePersona(personaDescription, tools, options) {
|
|
|
2688
2727
|
}).asPromise();
|
|
2689
2728
|
const { outputParameters } = result;
|
|
2690
2729
|
const { modelsRequirements: modelsRequirementsJson } = outputParameters;
|
|
2691
|
-
|
|
2730
|
+
let modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
|
|
2692
2731
|
if (isVerbose) {
|
|
2693
2732
|
console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
|
|
2694
2733
|
}
|
|
2695
2734
|
if (!Array.isArray(modelsRequirementsUnchecked)) {
|
|
2696
|
-
|
|
2735
|
+
// <- TODO: Book should have syntax and system to enforce shape of JSON
|
|
2736
|
+
modelsRequirementsUnchecked = [modelsRequirementsUnchecked];
|
|
2737
|
+
/*
|
|
2738
|
+
throw new UnexpectedError(
|
|
2739
|
+
spaceTrim(
|
|
2740
|
+
(block) => `
|
|
2697
2741
|
Invalid \`modelsRequirements\`:
|
|
2698
2742
|
|
|
2699
2743
|
\`\`\`json
|
|
2700
2744
|
${block(JSON.stringify(modelsRequirementsUnchecked, null, 4))}
|
|
2701
2745
|
\`\`\`
|
|
2702
|
-
|
|
2746
|
+
`,
|
|
2747
|
+
),
|
|
2748
|
+
);
|
|
2749
|
+
*/
|
|
2703
2750
|
}
|
|
2704
2751
|
const modelsRequirements = modelsRequirementsUnchecked.map((modelRequirements) => ({
|
|
2705
2752
|
modelVariant: 'CHAT',
|
|
@@ -3530,7 +3577,7 @@ async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
|
|
|
3530
3577
|
> },
|
|
3531
3578
|
*/
|
|
3532
3579
|
async asJson() {
|
|
3533
|
-
return
|
|
3580
|
+
return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
|
|
3534
3581
|
},
|
|
3535
3582
|
async asText() {
|
|
3536
3583
|
return await tools.fs.readFile(filename, 'utf-8');
|
|
@@ -5115,13 +5162,79 @@ async function getExamplesForTask(task) {
|
|
|
5115
5162
|
/**
|
|
5116
5163
|
* @@@
|
|
5117
5164
|
*
|
|
5165
|
+
* Here is the place where RAG (retrieval-augmented generation) happens
|
|
5166
|
+
*
|
|
5118
5167
|
* @private internal utility of `createPipelineExecutor`
|
|
5119
5168
|
*/
|
|
5120
5169
|
async function getKnowledgeForTask(options) {
|
|
5121
|
-
const { preparedPipeline, task } = options;
|
|
5122
|
-
|
|
5170
|
+
const { tools, preparedPipeline, task } = options;
|
|
5171
|
+
const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
|
|
5172
|
+
const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
|
|
5173
|
+
// <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
|
|
5174
|
+
if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
|
|
5175
|
+
return 'No knowledge pieces found';
|
|
5176
|
+
}
|
|
5177
|
+
// TODO: [🚐] Make arrayable LLMs -> single LLM DRY
|
|
5178
|
+
const _llms = arrayableToArray(tools.llm);
|
|
5179
|
+
const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
|
|
5180
|
+
const taskEmbeddingPrompt = {
|
|
5181
|
+
title: 'Knowledge Search',
|
|
5182
|
+
modelRequirements: {
|
|
5183
|
+
modelVariant: 'EMBEDDING',
|
|
5184
|
+
modelName: firstKnowlegeIndex.modelName,
|
|
5185
|
+
},
|
|
5186
|
+
content: task.content,
|
|
5187
|
+
parameters: {
|
|
5188
|
+
/* !!!!!!!! */
|
|
5189
|
+
},
|
|
5190
|
+
};
|
|
5191
|
+
const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
|
|
5192
|
+
const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
|
|
5193
|
+
const { index } = knowledgePiece;
|
|
5194
|
+
const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
|
|
5195
|
+
// <- TODO: Do not use just first knowledge piece and first index to determine embedding model
|
|
5196
|
+
if (knowledgePieceIndex === undefined) {
|
|
5197
|
+
return {
|
|
5198
|
+
content: knowledgePiece.content,
|
|
5199
|
+
relevance: 0,
|
|
5200
|
+
};
|
|
5201
|
+
}
|
|
5202
|
+
const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
|
|
5203
|
+
return {
|
|
5204
|
+
content: knowledgePiece.content,
|
|
5205
|
+
relevance,
|
|
5206
|
+
};
|
|
5207
|
+
});
|
|
5208
|
+
const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
|
|
5209
|
+
const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
|
|
5210
|
+
console.log('!!! Embedding', {
|
|
5211
|
+
task,
|
|
5212
|
+
taskEmbeddingPrompt,
|
|
5213
|
+
taskEmbeddingResult,
|
|
5214
|
+
firstKnowlegePiece,
|
|
5215
|
+
firstKnowlegeIndex,
|
|
5216
|
+
knowledgePiecesWithRelevance,
|
|
5217
|
+
knowledgePiecesSorted,
|
|
5218
|
+
knowledgePiecesLimited,
|
|
5219
|
+
});
|
|
5220
|
+
return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
|
|
5123
5221
|
// <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
|
|
5124
5222
|
}
|
|
5223
|
+
// TODO: !!!!!! Annotate + to new file
|
|
5224
|
+
function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
|
|
5225
|
+
if (embeddingVector1.length !== embeddingVector2.length) {
|
|
5226
|
+
throw new TypeError('Embedding vectors must have the same length');
|
|
5227
|
+
}
|
|
5228
|
+
const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
|
|
5229
|
+
const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
|
|
5230
|
+
const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
|
|
5231
|
+
return 1 - dotProduct / (magnitude1 * magnitude2);
|
|
5232
|
+
}
|
|
5233
|
+
/**
|
|
5234
|
+
* TODO: !!!! Verify if this is working
|
|
5235
|
+
* TODO: [♨] Implement Better - use keyword search
|
|
5236
|
+
* TODO: [♨] Examples of values
|
|
5237
|
+
*/
|
|
5125
5238
|
|
|
5126
5239
|
/**
|
|
5127
5240
|
* @@@
|
|
@@ -5129,9 +5242,9 @@ async function getKnowledgeForTask(options) {
|
|
|
5129
5242
|
* @private internal utility of `createPipelineExecutor`
|
|
5130
5243
|
*/
|
|
5131
5244
|
async function getReservedParametersForTask(options) {
|
|
5132
|
-
const { preparedPipeline, task, pipelineIdentification } = options;
|
|
5245
|
+
const { tools, preparedPipeline, task, pipelineIdentification } = options;
|
|
5133
5246
|
const context = await getContextForTask(); // <- [🏍]
|
|
5134
|
-
const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
|
|
5247
|
+
const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
|
|
5135
5248
|
const examples = await getExamplesForTask();
|
|
5136
5249
|
const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
|
|
5137
5250
|
const modelName = RESERVED_PARAMETER_MISSING_VALUE;
|
|
@@ -5193,6 +5306,7 @@ async function executeTask(options) {
|
|
|
5193
5306
|
}
|
|
5194
5307
|
const definedParameters = Object.freeze({
|
|
5195
5308
|
...(await getReservedParametersForTask({
|
|
5309
|
+
tools,
|
|
5196
5310
|
preparedPipeline,
|
|
5197
5311
|
task: currentTask,
|
|
5198
5312
|
pipelineIdentification,
|