@promptbook/utils
Advanced tools
Comparing version 0.24.0-0 to 0.24.0-1.0
@@ -0,10 +1,111 @@ | ||
import spaceTrim from 'spacetrim'; | ||
import { format } from 'prettier'; | ||
import parserHtml from 'prettier/parser-html'; | ||
import { removeDiacritics, capitalize } from 'n12'; | ||
import spaceTrim from 'spacetrim'; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __values(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
} | ||
function __read(o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
} | ||
/** | ||
* Prettify the html code | ||
* | ||
* @param html raw html code | ||
* @returns formatted html code | ||
*/ | ||
function prettifyMarkdown(html) { | ||
try { | ||
return format(html, { | ||
parser: 'markdown', | ||
plugins: [parserHtml], | ||
// TODO: DRY - make some import or auto-copy of .prettierrc | ||
endOfLine: 'lf', | ||
tabWidth: 4, | ||
singleQuote: true, | ||
trailingComma: 'all', | ||
arrowParens: 'always', | ||
printWidth: 120, | ||
htmlWhitespaceSensitivity: 'ignore', | ||
jsxBracketSameLine: false, | ||
bracketSpacing: true, | ||
}); | ||
} | ||
catch (error) { | ||
console.error('There was an error with prettifying the markdown, using the original as the fallback', { | ||
error: error, | ||
html: html, | ||
}); | ||
return html; | ||
} | ||
} | ||
/** | ||
* Converts execution report from JSON to string format | ||
*/ | ||
function executionReportJsonToString(executionReportJson) { | ||
var executionReportString = '!!!!!!!!!!!!!'; | ||
executionReportString += executionReportJson.length; | ||
var e_1, _a; | ||
// TODO: !!!!! Better (filter out voids) | ||
var executionReportString = spaceTrim(function (block) { return "\n # ".concat(executionReportJson.title || 'Execution report', "\n\n ").concat(block(executionReportJson.description || ''), "\n\n - PTBK URL `").concat(executionReportJson.ptbkUrl || '', "`\n - PTBK VERSION `").concat(executionReportJson.ptbkUsedVersion, "` (requested `").concat(executionReportJson.ptbkRequestedVersion || '', "`)\n\n "); }); | ||
var _loop_1 = function (promptExecution) { | ||
executionReportString += | ||
'\n\n\n\n' + | ||
spaceTrim(function (block) { | ||
var _a, _b; | ||
return "\n ## ".concat(promptExecution.prompt.title, "\n\n ### Prompt\n\n ```\n ").concat(block(promptExecution.prompt.content), "\n ```\n\n ### Result\n\n ```\n ").concat(block(((_a = promptExecution.result) === null || _a === void 0 ? void 0 : _a.content) || ''), "\n ```\n\n ### Model requirements\n\n ```\n ").concat(block(JSON.stringify(promptExecution.prompt.modelRequirements, null, 4)), "\n ```\n\n ### Error\n\n ```\n ").concat(block(((_b = promptExecution.error) === null || _b === void 0 ? void 0 : _b.message) || ''), "\n ```\n\n "); | ||
}); | ||
}; | ||
try { | ||
for (var _b = __values(executionReportJson.promptExecutions), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var promptExecution = _c.value; | ||
_loop_1(promptExecution); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
executionReportString = prettifyMarkdown(executionReportString); | ||
return executionReportString; | ||
@@ -140,46 +241,2 @@ } | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
function __values(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
} | ||
function __read(o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
} | ||
/** | ||
@@ -186,0 +243,0 @@ * Extracts all code blocks from markdown. |
@@ -1,2 +0,2 @@ | ||
import { string_name } from '.././types/typeAliases'; | ||
import { string_name, string_version } from '.././types/typeAliases'; | ||
import { PromptTemplateJson } from '../types/PromptTemplatePipelineJson/PromptTemplateJson'; | ||
@@ -19,2 +19,5 @@ import { PromptTemplateParameterJson } from '../types/PromptTemplatePipelineJson/PromptTemplateParameterJson'; | ||
readonly ptbkUrl: URL | null; | ||
readonly title: string | null; | ||
readonly ptbkVersion: string_version | null; | ||
readonly description: string | null; | ||
readonly parameters: Record<string_name, PromptTemplateParameterJson>; | ||
@@ -21,0 +24,0 @@ readonly promptTemplates: Array<PromptTemplateJson>; |
import { string_name } from '.././types/typeAliases'; | ||
import { CreatePtpExecutorSettings } from '../execution/createPtpExecutor'; | ||
import { ExecutionTools } from '../execution/ExecutionTools'; | ||
@@ -8,2 +9,6 @@ import { PtpExecutor } from '../execution/PtpExecutor'; | ||
import { PromptTemplatePipeline } from './PromptTemplatePipeline'; | ||
type PromptTemplatePipelineLibraryOptions = { | ||
readonly library: Record<string_name, PromptTemplatePipeline>; | ||
readonly settings: CreatePtpExecutorSettings; | ||
}; | ||
/** | ||
@@ -19,3 +24,3 @@ * Library of prompt template pipelines that groups together prompt template pipelines for an application. This is a very thin wrapper around the Array / Set of prompt template pipelines. | ||
export declare class PromptTemplatePipelineLibrary { | ||
readonly promptTemplatePipelines: Record<string_name, PromptTemplatePipeline>; | ||
readonly options: PromptTemplatePipelineLibraryOptions; | ||
/** | ||
@@ -30,3 +35,3 @@ * Constructs PromptTemplatePipeline from any sources | ||
*/ | ||
static fromSources(ptbkSources: Record<string_name, PromptTemplatePipelineJson | PromptTemplatePipelineString>): PromptTemplatePipelineLibrary; | ||
static fromSources(ptbkSources: Record<string_name, PromptTemplatePipelineJson | PromptTemplatePipelineString>, settings: CreatePtpExecutorSettings): PromptTemplatePipelineLibrary; | ||
private constructor(); | ||
@@ -46,2 +51,3 @@ /** | ||
} | ||
export {}; | ||
/** | ||
@@ -48,0 +54,0 @@ * TODO: !!! This should be renamed to Promptbook |
import { PromptTemplatePipeline } from '../classes/PromptTemplatePipeline'; | ||
import { ExecutionTools } from './ExecutionTools'; | ||
import { PtpExecutor } from './PtpExecutor'; | ||
export interface CreatePtpExecutorSettings { | ||
/** | ||
* When executor does not satisfy expectations it will be retried this amount of times | ||
* | ||
* !!!!!!! Make default in version 24.1.0 | ||
*/ | ||
readonly maxNaturalExecutionAttempts: number; | ||
} | ||
interface CreatePtpExecutorOptions { | ||
readonly ptp: PromptTemplatePipeline; | ||
readonly tools: ExecutionTools; | ||
readonly settings: CreatePtpExecutorSettings; | ||
} | ||
@@ -8,0 +17,0 @@ /** |
@@ -1,4 +0,5 @@ | ||
import { Promisable } from 'type-fest'; | ||
import { string_name } from '.././types/typeAliases'; | ||
import { TaskProgress } from '../types/TaskProgress'; | ||
import type { Promisable } from 'type-fest'; | ||
import type { string_name } from '.././types/typeAliases'; | ||
import type { TaskProgress } from '../types/TaskProgress'; | ||
import type { ExecutionReportJson } from '../types/execution-report/ExecutionReportJson'; | ||
/** | ||
@@ -15,3 +16,22 @@ * Executor is a simple async function that takes input parameters and returns result parameters _(along with all intermediate parameters and input parameters = it extends input object)_. | ||
export interface PtpExecutor { | ||
(inputParameters: Record<string_name, string>, onProgress: (taskProgress: TaskProgress) => Promisable<void>): Promise<Record<string_name, string>>; | ||
(inputParameters: Record<string_name, string>, onProgress: (taskProgress: TaskProgress) => Promisable<void>): Promise<{ | ||
/** | ||
* Whether the execution was successful | ||
*/ | ||
isSuccessful: boolean; | ||
/** | ||
* Errors that occured during the execution | ||
*/ | ||
errors: Array<Error>; | ||
/** | ||
* The report of the execution | ||
*/ | ||
executionReport: ExecutionReportJson; | ||
/** | ||
* Result parameters of the execution | ||
* | ||
* Note: If the execution was not successful, there are only some of the result parameters | ||
*/ | ||
outputParameters: Record<string_name, string>; | ||
}>; | ||
} | ||
@@ -18,0 +38,0 @@ /** |
import type { PromptResult } from '../../execution/PromptResult'; | ||
import { Prompt } from '../Prompt'; | ||
import type { Prompt } from '../Prompt'; | ||
import type { string_ptbk_url, string_version } from '../typeAliases'; | ||
/** | ||
@@ -13,8 +14,44 @@ * ExecutionReport is result of executing one promptbook | ||
*/ | ||
export type ExecutionReportJson = Array<{ | ||
prompt: Omit<Prompt, 'ptbkUrl' | 'parameters'>; | ||
result: PromptResult; | ||
}>; | ||
/** | ||
* TODO: [🧠] What is the best shape of ExecutionReportJson | ||
*/ | ||
export type ExecutionReportJson = { | ||
/** | ||
* Unique identifier of the ptp from ptp which was executed | ||
*/ | ||
readonly ptbkUrl?: string_ptbk_url; | ||
/** | ||
* Title of from ptp which was executed | ||
*/ | ||
readonly title?: string; | ||
/** | ||
* Version from ptp which was executed | ||
*/ | ||
readonly ptbkUsedVersion: string_version; | ||
/** | ||
* Version from ptp which was requested by promptbook | ||
*/ | ||
readonly ptbkRequestedVersion?: string_version; | ||
/** | ||
* Description of the ptp which was executed | ||
*/ | ||
readonly description?: string; | ||
/** | ||
* Sequence of prompt templates in order which were executed | ||
*/ | ||
readonly promptExecutions: Array<{ | ||
/** | ||
* The prompt wich was executed | ||
*/ | ||
prompt: Omit<Prompt, 'ptbkUrl' | 'parameters'>; | ||
/** | ||
* Result of the prompt execution (if not failed during LLM execution) | ||
*/ | ||
result?: PromptResult; | ||
/** | ||
* The error which occured during LLM execution or during postprocessing or expectation checking | ||
* | ||
* Note: It makes sense to have both error and result defined, for example when the result not pass expectations | ||
*/ | ||
error?: { | ||
message: string; | ||
}; | ||
}>; | ||
}; |
@@ -1,3 +0,3 @@ | ||
import { string_name, string_prompt, string_ptbk_url_with_hashtemplate } from '.././types/typeAliases'; | ||
import { ModelRequirements } from './ModelRequirements'; | ||
import type { string_name, string_prompt, string_ptbk_url_with_hashtemplate, string_title } from '.././types/typeAliases'; | ||
import type { ModelRequirements } from './ModelRequirements'; | ||
/** | ||
@@ -10,2 +10,8 @@ * Prompt in a text along with model requirements, but without any execution or templating logic. | ||
/** | ||
* The title of the prompt | ||
* | ||
* Note: This has no effect on the model, it is just for the reporting | ||
*/ | ||
readonly title: string_title; | ||
/** | ||
* The text of the prompt | ||
@@ -12,0 +18,0 @@ * |
{ | ||
"name": "@promptbook/utils", | ||
"version": "0.24.0-0", | ||
"version": "0.24.0-1.0", | ||
"description": "Library to supercharge your use of large language models", | ||
@@ -38,6 +38,7 @@ "private": false, | ||
"n12": "1.8.18", | ||
"spacetrim": "0.9.21" | ||
"spacetrim": "0.9.21", | ||
"prettier": "2.8.1" | ||
}, | ||
"peerDependencies": { | ||
"@promptbook/core": "0.24.0-0" | ||
"@promptbook/core": "0.24.0-1.0" | ||
}, | ||
@@ -44,0 +45,0 @@ "main": "./umd/index.umd.js", |
@@ -1,2 +0,2 @@ | ||
import { string_name } from '.././types/typeAliases'; | ||
import { string_name, string_version } from '.././types/typeAliases'; | ||
import { PromptTemplateJson } from '../types/PromptTemplatePipelineJson/PromptTemplateJson'; | ||
@@ -19,2 +19,5 @@ import { PromptTemplateParameterJson } from '../types/PromptTemplatePipelineJson/PromptTemplateParameterJson'; | ||
readonly ptbkUrl: URL | null; | ||
readonly title: string | null; | ||
readonly ptbkVersion: string_version | null; | ||
readonly description: string | null; | ||
readonly parameters: Record<string_name, PromptTemplateParameterJson>; | ||
@@ -21,0 +24,0 @@ readonly promptTemplates: Array<PromptTemplateJson>; |
import { string_name } from '.././types/typeAliases'; | ||
import { CreatePtpExecutorSettings } from '../execution/createPtpExecutor'; | ||
import { ExecutionTools } from '../execution/ExecutionTools'; | ||
@@ -8,2 +9,6 @@ import { PtpExecutor } from '../execution/PtpExecutor'; | ||
import { PromptTemplatePipeline } from './PromptTemplatePipeline'; | ||
type PromptTemplatePipelineLibraryOptions = { | ||
readonly library: Record<string_name, PromptTemplatePipeline>; | ||
readonly settings: CreatePtpExecutorSettings; | ||
}; | ||
/** | ||
@@ -19,3 +24,3 @@ * Library of prompt template pipelines that groups together prompt template pipelines for an application. This is a very thin wrapper around the Array / Set of prompt template pipelines. | ||
export declare class PromptTemplatePipelineLibrary { | ||
readonly promptTemplatePipelines: Record<string_name, PromptTemplatePipeline>; | ||
readonly options: PromptTemplatePipelineLibraryOptions; | ||
/** | ||
@@ -30,3 +35,3 @@ * Constructs PromptTemplatePipeline from any sources | ||
*/ | ||
static fromSources(ptbkSources: Record<string_name, PromptTemplatePipelineJson | PromptTemplatePipelineString>): PromptTemplatePipelineLibrary; | ||
static fromSources(ptbkSources: Record<string_name, PromptTemplatePipelineJson | PromptTemplatePipelineString>, settings: CreatePtpExecutorSettings): PromptTemplatePipelineLibrary; | ||
private constructor(); | ||
@@ -46,2 +51,3 @@ /** | ||
} | ||
export {}; | ||
/** | ||
@@ -48,0 +54,0 @@ * TODO: !!! This should be renamed to Promptbook |
import { PromptTemplatePipeline } from '../classes/PromptTemplatePipeline'; | ||
import { ExecutionTools } from './ExecutionTools'; | ||
import { PtpExecutor } from './PtpExecutor'; | ||
export interface CreatePtpExecutorSettings { | ||
/** | ||
* When executor does not satisfy expectations it will be retried this amount of times | ||
* | ||
* !!!!!!! Make default in version 24.1.0 | ||
*/ | ||
readonly maxNaturalExecutionAttempts: number; | ||
} | ||
interface CreatePtpExecutorOptions { | ||
readonly ptp: PromptTemplatePipeline; | ||
readonly tools: ExecutionTools; | ||
readonly settings: CreatePtpExecutorSettings; | ||
} | ||
@@ -8,0 +17,0 @@ /** |
@@ -1,4 +0,5 @@ | ||
import { Promisable } from 'type-fest'; | ||
import { string_name } from '.././types/typeAliases'; | ||
import { TaskProgress } from '../types/TaskProgress'; | ||
import type { Promisable } from 'type-fest'; | ||
import type { string_name } from '.././types/typeAliases'; | ||
import type { TaskProgress } from '../types/TaskProgress'; | ||
import type { ExecutionReportJson } from '../types/execution-report/ExecutionReportJson'; | ||
/** | ||
@@ -15,3 +16,22 @@ * Executor is a simple async function that takes input parameters and returns result parameters _(along with all intermediate parameters and input parameters = it extends input object)_. | ||
export interface PtpExecutor { | ||
(inputParameters: Record<string_name, string>, onProgress: (taskProgress: TaskProgress) => Promisable<void>): Promise<Record<string_name, string>>; | ||
(inputParameters: Record<string_name, string>, onProgress: (taskProgress: TaskProgress) => Promisable<void>): Promise<{ | ||
/** | ||
* Whether the execution was successful | ||
*/ | ||
isSuccessful: boolean; | ||
/** | ||
* Errors that occured during the execution | ||
*/ | ||
errors: Array<Error>; | ||
/** | ||
* The report of the execution | ||
*/ | ||
executionReport: ExecutionReportJson; | ||
/** | ||
* Result parameters of the execution | ||
* | ||
* Note: If the execution was not successful, there are only some of the result parameters | ||
*/ | ||
outputParameters: Record<string_name, string>; | ||
}>; | ||
} | ||
@@ -18,0 +38,0 @@ /** |
import type { PromptResult } from '../../execution/PromptResult'; | ||
import { Prompt } from '../Prompt'; | ||
import type { Prompt } from '../Prompt'; | ||
import type { string_ptbk_url, string_version } from '../typeAliases'; | ||
/** | ||
@@ -13,8 +14,44 @@ * ExecutionReport is result of executing one promptbook | ||
*/ | ||
export type ExecutionReportJson = Array<{ | ||
prompt: Omit<Prompt, 'ptbkUrl' | 'parameters'>; | ||
result: PromptResult; | ||
}>; | ||
/** | ||
* TODO: [🧠] What is the best shape of ExecutionReportJson | ||
*/ | ||
export type ExecutionReportJson = { | ||
/** | ||
* Unique identifier of the ptp from ptp which was executed | ||
*/ | ||
readonly ptbkUrl?: string_ptbk_url; | ||
/** | ||
* Title of from ptp which was executed | ||
*/ | ||
readonly title?: string; | ||
/** | ||
* Version from ptp which was executed | ||
*/ | ||
readonly ptbkUsedVersion: string_version; | ||
/** | ||
* Version from ptp which was requested by promptbook | ||
*/ | ||
readonly ptbkRequestedVersion?: string_version; | ||
/** | ||
* Description of the ptp which was executed | ||
*/ | ||
readonly description?: string; | ||
/** | ||
* Sequence of prompt templates in order which were executed | ||
*/ | ||
readonly promptExecutions: Array<{ | ||
/** | ||
* The prompt wich was executed | ||
*/ | ||
prompt: Omit<Prompt, 'ptbkUrl' | 'parameters'>; | ||
/** | ||
* Result of the prompt execution (if not failed during LLM execution) | ||
*/ | ||
result?: PromptResult; | ||
/** | ||
* The error which occured during LLM execution or during postprocessing or expectation checking | ||
* | ||
* Note: It makes sense to have both error and result defined, for example when the result not pass expectations | ||
*/ | ||
error?: { | ||
message: string; | ||
}; | ||
}>; | ||
}; |
@@ -1,3 +0,3 @@ | ||
import { string_name, string_prompt, string_ptbk_url_with_hashtemplate } from '.././types/typeAliases'; | ||
import { ModelRequirements } from './ModelRequirements'; | ||
import type { string_name, string_prompt, string_ptbk_url_with_hashtemplate, string_title } from '.././types/typeAliases'; | ||
import type { ModelRequirements } from './ModelRequirements'; | ||
/** | ||
@@ -10,2 +10,8 @@ * Prompt in a text along with model requirements, but without any execution or templating logic. | ||
/** | ||
* The title of the prompt | ||
* | ||
* Note: This has no effect on the model, it is just for the reporting | ||
*/ | ||
readonly title: string_title; | ||
/** | ||
* The text of the prompt | ||
@@ -12,0 +18,0 @@ * |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
459026
258
6281
4
+ Addedprettier@2.8.1
+ Added@promptbook/core@0.24.0-1.0(transitive)
+ Addedprettier@2.8.1(transitive)
- Removed@promptbook/core@0.24.0-0(transitive)