@promptbook/legacy-documents 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 +2 -2
- package/umd/index.umd.js +128 -14
- package/umd/index.umd.js.map +1 -1
package/esm/index.es.js
CHANGED
|
@@ -28,7 +28,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
28
28
|
* @generated
|
|
29
29
|
* @see https://github.com/webgptorg/promptbook
|
|
30
30
|
*/
|
|
31
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.92.0-
|
|
31
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.92.0-7';
|
|
32
32
|
/**
|
|
33
33
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
34
34
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -2298,6 +2298,45 @@ function isPipelinePrepared(pipeline) {
|
|
|
2298
2298
|
* - [♨] Are tasks prepared
|
|
2299
2299
|
*/
|
|
2300
2300
|
|
|
2301
|
+
/**
|
|
2302
|
+
* Converts a JavaScript Object Notation (JSON) string into an object.
|
|
2303
|
+
*
|
|
2304
|
+
* Note: This is wrapper around `JSON.parse()` with better error and type handling
|
|
2305
|
+
*
|
|
2306
|
+
* @public exported from `@promptbook/utils`
|
|
2307
|
+
*/
|
|
2308
|
+
function jsonParse(value) {
|
|
2309
|
+
if (value === undefined) {
|
|
2310
|
+
throw new Error(`Can not parse JSON from undefined value.`);
|
|
2311
|
+
}
|
|
2312
|
+
else if (typeof value !== 'string') {
|
|
2313
|
+
console.error('Can not parse JSON from non-string value.', { text: value });
|
|
2314
|
+
throw new Error(spaceTrim$1(`
|
|
2315
|
+
Can not parse JSON from non-string value.
|
|
2316
|
+
|
|
2317
|
+
The value type: ${typeof value}
|
|
2318
|
+
See more in console.
|
|
2319
|
+
`));
|
|
2320
|
+
}
|
|
2321
|
+
try {
|
|
2322
|
+
return JSON.parse(value);
|
|
2323
|
+
}
|
|
2324
|
+
catch (error) {
|
|
2325
|
+
if (!(error instanceof Error)) {
|
|
2326
|
+
throw error;
|
|
2327
|
+
}
|
|
2328
|
+
throw new Error(spaceTrim$1((block) => `
|
|
2329
|
+
${block(error.message)}
|
|
2330
|
+
|
|
2331
|
+
The JSON text:
|
|
2332
|
+
${block(value)}
|
|
2333
|
+
`));
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
/**
|
|
2337
|
+
* TODO: !!!! Use in Promptbook.studio
|
|
2338
|
+
*/
|
|
2339
|
+
|
|
2301
2340
|
/**
|
|
2302
2341
|
* Recursively converts JSON strings to JSON objects
|
|
2303
2342
|
|
|
@@ -2316,7 +2355,7 @@ function jsonStringsToJsons(object) {
|
|
|
2316
2355
|
const newObject = { ...object };
|
|
2317
2356
|
for (const [key, value] of Object.entries(object)) {
|
|
2318
2357
|
if (typeof value === 'string' && isValidJsonString(value)) {
|
|
2319
|
-
newObject[key] =
|
|
2358
|
+
newObject[key] = jsonParse(value);
|
|
2320
2359
|
}
|
|
2321
2360
|
else {
|
|
2322
2361
|
newObject[key] = jsonStringsToJsons(value);
|
|
@@ -3163,18 +3202,26 @@ async function preparePersona(personaDescription, tools, options) {
|
|
|
3163
3202
|
}).asPromise();
|
|
3164
3203
|
const { outputParameters } = result;
|
|
3165
3204
|
const { modelsRequirements: modelsRequirementsJson } = outputParameters;
|
|
3166
|
-
|
|
3205
|
+
let modelsRequirementsUnchecked = jsonParse(modelsRequirementsJson);
|
|
3167
3206
|
if (isVerbose) {
|
|
3168
3207
|
console.info(`PERSONA ${personaDescription}`, modelsRequirementsUnchecked);
|
|
3169
3208
|
}
|
|
3170
3209
|
if (!Array.isArray(modelsRequirementsUnchecked)) {
|
|
3171
|
-
|
|
3210
|
+
// <- TODO: Book should have syntax and system to enforce shape of JSON
|
|
3211
|
+
modelsRequirementsUnchecked = [modelsRequirementsUnchecked];
|
|
3212
|
+
/*
|
|
3213
|
+
throw new UnexpectedError(
|
|
3214
|
+
spaceTrim(
|
|
3215
|
+
(block) => `
|
|
3172
3216
|
Invalid \`modelsRequirements\`:
|
|
3173
3217
|
|
|
3174
3218
|
\`\`\`json
|
|
3175
3219
|
${block(JSON.stringify(modelsRequirementsUnchecked, null, 4))}
|
|
3176
3220
|
\`\`\`
|
|
3177
|
-
|
|
3221
|
+
`,
|
|
3222
|
+
),
|
|
3223
|
+
);
|
|
3224
|
+
*/
|
|
3178
3225
|
}
|
|
3179
3226
|
const modelsRequirements = modelsRequirementsUnchecked.map((modelRequirements) => ({
|
|
3180
3227
|
modelVariant: 'CHAT',
|
|
@@ -3609,7 +3656,7 @@ async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
|
|
|
3609
3656
|
> },
|
|
3610
3657
|
*/
|
|
3611
3658
|
async asJson() {
|
|
3612
|
-
return
|
|
3659
|
+
return jsonParse(await tools.fs.readFile(filename, 'utf-8'));
|
|
3613
3660
|
},
|
|
3614
3661
|
async asText() {
|
|
3615
3662
|
return await tools.fs.readFile(filename, 'utf-8');
|
|
@@ -5296,13 +5343,79 @@ async function getExamplesForTask(task) {
|
|
|
5296
5343
|
/**
|
|
5297
5344
|
* @@@
|
|
5298
5345
|
*
|
|
5346
|
+
* Here is the place where RAG (retrieval-augmented generation) happens
|
|
5347
|
+
*
|
|
5299
5348
|
* @private internal utility of `createPipelineExecutor`
|
|
5300
5349
|
*/
|
|
5301
5350
|
async function getKnowledgeForTask(options) {
|
|
5302
|
-
const { preparedPipeline, task } = options;
|
|
5303
|
-
|
|
5351
|
+
const { tools, preparedPipeline, task } = options;
|
|
5352
|
+
const firstKnowlegePiece = preparedPipeline.knowledgePieces[0];
|
|
5353
|
+
const firstKnowlegeIndex = firstKnowlegePiece === null || firstKnowlegePiece === void 0 ? void 0 : firstKnowlegePiece.index[0];
|
|
5354
|
+
// <- TODO: Do not use just first knowledge piece and first index to determine embedding model, use also keyword search
|
|
5355
|
+
if (firstKnowlegePiece === undefined || firstKnowlegeIndex === undefined) {
|
|
5356
|
+
return 'No knowledge pieces found';
|
|
5357
|
+
}
|
|
5358
|
+
// TODO: [🚐] Make arrayable LLMs -> single LLM DRY
|
|
5359
|
+
const _llms = arrayableToArray(tools.llm);
|
|
5360
|
+
const llmTools = _llms.length === 1 ? _llms[0] : joinLlmExecutionTools(..._llms);
|
|
5361
|
+
const taskEmbeddingPrompt = {
|
|
5362
|
+
title: 'Knowledge Search',
|
|
5363
|
+
modelRequirements: {
|
|
5364
|
+
modelVariant: 'EMBEDDING',
|
|
5365
|
+
modelName: firstKnowlegeIndex.modelName,
|
|
5366
|
+
},
|
|
5367
|
+
content: task.content,
|
|
5368
|
+
parameters: {
|
|
5369
|
+
/* !!!!!!!! */
|
|
5370
|
+
},
|
|
5371
|
+
};
|
|
5372
|
+
const taskEmbeddingResult = await llmTools.callEmbeddingModel(taskEmbeddingPrompt);
|
|
5373
|
+
const knowledgePiecesWithRelevance = preparedPipeline.knowledgePieces.map((knowledgePiece) => {
|
|
5374
|
+
const { index } = knowledgePiece;
|
|
5375
|
+
const knowledgePieceIndex = index.find((i) => i.modelName === firstKnowlegeIndex.modelName);
|
|
5376
|
+
// <- TODO: Do not use just first knowledge piece and first index to determine embedding model
|
|
5377
|
+
if (knowledgePieceIndex === undefined) {
|
|
5378
|
+
return {
|
|
5379
|
+
content: knowledgePiece.content,
|
|
5380
|
+
relevance: 0,
|
|
5381
|
+
};
|
|
5382
|
+
}
|
|
5383
|
+
const relevance = computeCosineSimilarity(knowledgePieceIndex.position, taskEmbeddingResult.content);
|
|
5384
|
+
return {
|
|
5385
|
+
content: knowledgePiece.content,
|
|
5386
|
+
relevance,
|
|
5387
|
+
};
|
|
5388
|
+
});
|
|
5389
|
+
const knowledgePiecesSorted = knowledgePiecesWithRelevance.sort((a, b) => a.relevance - b.relevance);
|
|
5390
|
+
const knowledgePiecesLimited = knowledgePiecesSorted.slice(0, 5);
|
|
5391
|
+
console.log('!!! Embedding', {
|
|
5392
|
+
task,
|
|
5393
|
+
taskEmbeddingPrompt,
|
|
5394
|
+
taskEmbeddingResult,
|
|
5395
|
+
firstKnowlegePiece,
|
|
5396
|
+
firstKnowlegeIndex,
|
|
5397
|
+
knowledgePiecesWithRelevance,
|
|
5398
|
+
knowledgePiecesSorted,
|
|
5399
|
+
knowledgePiecesLimited,
|
|
5400
|
+
});
|
|
5401
|
+
return knowledgePiecesLimited.map(({ content }) => `- ${content}`).join('\n');
|
|
5304
5402
|
// <- TODO: [🧠] Some smart aggregation of knowledge pieces, single-line vs multi-line vs mixed
|
|
5305
5403
|
}
|
|
5404
|
+
// TODO: !!!!!! Annotate + to new file
|
|
5405
|
+
function computeCosineSimilarity(embeddingVector1, embeddingVector2) {
|
|
5406
|
+
if (embeddingVector1.length !== embeddingVector2.length) {
|
|
5407
|
+
throw new TypeError('Embedding vectors must have the same length');
|
|
5408
|
+
}
|
|
5409
|
+
const dotProduct = embeddingVector1.reduce((sum, value, index) => sum + value * embeddingVector2[index], 0);
|
|
5410
|
+
const magnitude1 = Math.sqrt(embeddingVector1.reduce((sum, value) => sum + value * value, 0));
|
|
5411
|
+
const magnitude2 = Math.sqrt(embeddingVector2.reduce((sum, value) => sum + value * value, 0));
|
|
5412
|
+
return 1 - dotProduct / (magnitude1 * magnitude2);
|
|
5413
|
+
}
|
|
5414
|
+
/**
|
|
5415
|
+
* TODO: !!!! Verify if this is working
|
|
5416
|
+
* TODO: [♨] Implement Better - use keyword search
|
|
5417
|
+
* TODO: [♨] Examples of values
|
|
5418
|
+
*/
|
|
5306
5419
|
|
|
5307
5420
|
/**
|
|
5308
5421
|
* @@@
|
|
@@ -5310,9 +5423,9 @@ async function getKnowledgeForTask(options) {
|
|
|
5310
5423
|
* @private internal utility of `createPipelineExecutor`
|
|
5311
5424
|
*/
|
|
5312
5425
|
async function getReservedParametersForTask(options) {
|
|
5313
|
-
const { preparedPipeline, task, pipelineIdentification } = options;
|
|
5426
|
+
const { tools, preparedPipeline, task, pipelineIdentification } = options;
|
|
5314
5427
|
const context = await getContextForTask(); // <- [🏍]
|
|
5315
|
-
const knowledge = await getKnowledgeForTask({ preparedPipeline, task });
|
|
5428
|
+
const knowledge = await getKnowledgeForTask({ tools, preparedPipeline, task });
|
|
5316
5429
|
const examples = await getExamplesForTask();
|
|
5317
5430
|
const currentDate = new Date().toISOString(); // <- TODO: [🧠][💩] Better
|
|
5318
5431
|
const modelName = RESERVED_PARAMETER_MISSING_VALUE;
|
|
@@ -5374,6 +5487,7 @@ async function executeTask(options) {
|
|
|
5374
5487
|
}
|
|
5375
5488
|
const definedParameters = Object.freeze({
|
|
5376
5489
|
...(await getReservedParametersForTask({
|
|
5490
|
+
tools,
|
|
5377
5491
|
preparedPipeline,
|
|
5378
5492
|
task: currentTask,
|
|
5379
5493
|
pipelineIdentification,
|