@promptbook/utils 0.71.0-0 → 0.72.0-0
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/README.md +6 -5
- package/esm/index.es.js +114 -153
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +5 -11
- package/esm/typings/src/_packages/openai.index.d.ts +4 -0
- package/esm/typings/src/_packages/types.index.d.ts +2 -12
- package/esm/typings/src/_packages/utils.index.d.ts +4 -4
- package/esm/typings/src/commands/FOREACH/ForeachCommand.d.ts +2 -3
- package/esm/typings/src/commands/FOREACH/foreachCommandParser.d.ts +1 -1
- package/esm/typings/src/commands/_common/types/CommandParser.d.ts +1 -16
- package/esm/typings/src/config.d.ts +1 -14
- package/esm/typings/src/conversion/prettify/renderPipelineMermaidOptions.d.ts +0 -3
- package/esm/typings/src/conversion/utils/extractParameterNamesFromTemplate.d.ts +1 -2
- package/esm/typings/src/conversion/validation/validatePipeline.d.ts +0 -5
- package/esm/typings/src/execution/PipelineExecutorResult.d.ts +8 -9
- package/esm/typings/src/execution/createPipelineExecutor.d.ts +72 -0
- package/esm/typings/src/execution/translation/automatic-translate/automatic-translators/LindatAutomaticTranslator.d.ts +1 -1
- package/esm/typings/src/execution/utils/addUsage.d.ts +0 -56
- package/esm/typings/src/execution/utils/checkExpectations.d.ts +0 -2
- package/esm/typings/src/execution/utils/usage-constants.d.ts +127 -0
- package/esm/typings/src/execution/utils/usageToHuman.d.ts +4 -3
- package/esm/typings/src/formats/_common/FormatDefinition.d.ts +15 -14
- package/esm/typings/src/formats/csv/{CsvFormatDefinition.d.ts → ListFormatDefinition.d.ts} +3 -6
- package/esm/typings/src/formats/index.d.ts +1 -1
- package/esm/typings/src/formats/json/JsonFormatDefinition.d.ts +3 -4
- package/esm/typings/src/formats/list/ListFormatDefinition.d.ts +16 -0
- package/esm/typings/src/formats/xml/XmlFormatDefinition.d.ts +3 -4
- package/esm/typings/src/knowledge/dialogs/callback/CallbackInterfaceTools.d.ts +1 -1
- package/esm/typings/src/knowledge/dialogs/simple-prompt/SimplePromptInterfaceTools.d.ts +1 -1
- package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +3 -2
- package/esm/typings/src/llm-providers/azure-openai/AzureOpenAiExecutionTools.d.ts +3 -2
- package/esm/typings/src/llm-providers/mocked/MockedEchoLlmExecutionTools.d.ts +1 -1
- package/esm/typings/src/llm-providers/mocked/MockedFackedLlmExecutionTools.d.ts +1 -1
- package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +37 -0
- package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionToolsOptions.d.ts +14 -0
- package/esm/typings/src/llm-providers/openai/OpenAiExecutionTools.d.ts +3 -2
- package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +1 -1
- package/esm/typings/src/scripting/javascript/JavascriptEvalExecutionTools.d.ts +1 -1
- package/esm/typings/src/scripting/python/PythonExecutionTools.d.ts +1 -1
- package/esm/typings/src/scripting/typescript/TypescriptExecutionTools.d.ts +1 -1
- package/esm/typings/src/storage/files-storage/FilesStorage.d.ts +1 -1
- package/esm/typings/src/types/PipelineJson/KnowledgeSourceJson.d.ts +2 -9
- package/esm/typings/src/types/PipelineJson/ParameterJson.d.ts +0 -1
- package/esm/typings/src/types/PipelineJson/TemplateJsonCommon.d.ts +0 -5
- package/esm/typings/src/types/execution-report/ExecutionReportJson.d.ts +0 -3
- package/esm/typings/src/utils/expectation-counters/index.d.ts +0 -3
- package/esm/typings/src/utils/{parameters/extractParameterNames.d.ts → extractParameterNames.d.ts} +2 -2
- package/esm/typings/src/utils/organization/{empty_object.d.ts → f.d.ts} +1 -5
- package/esm/typings/src/utils/{parameters/replaceParameters.d.ts → replaceParameters.d.ts} +2 -2
- package/package.json +12 -16
- package/umd/index.umd.js +114 -153
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/commands/FOREACH/ForeachJson.d.ts +0 -23
- package/esm/typings/src/execution/createPipelineExecutor/$OngoingTemplateResult.d.ts +0 -45
- package/esm/typings/src/execution/createPipelineExecutor/00-CreatePipelineExecutorOptions.d.ts +0 -20
- package/esm/typings/src/execution/createPipelineExecutor/00-CreatePipelineExecutorSettings.d.ts +0 -40
- package/esm/typings/src/execution/createPipelineExecutor/00-createPipelineExecutor.d.ts +0 -10
- package/esm/typings/src/execution/createPipelineExecutor/10-executePipeline.d.ts +0 -55
- package/esm/typings/src/execution/createPipelineExecutor/20-executeTemplate.d.ts +0 -62
- package/esm/typings/src/execution/createPipelineExecutor/30-executeFormatCells.d.ts +0 -19
- package/esm/typings/src/execution/createPipelineExecutor/40-executeAttempts.d.ts +0 -74
- package/esm/typings/src/execution/createPipelineExecutor/filterJustOutputParameters.d.ts +0 -34
- package/esm/typings/src/execution/createPipelineExecutor/getContextForTemplate.d.ts +0 -10
- package/esm/typings/src/execution/createPipelineExecutor/getKnowledgeForTemplate.d.ts +0 -27
- package/esm/typings/src/execution/createPipelineExecutor/getReservedParametersForTemplate.d.ts +0 -30
- package/esm/typings/src/execution/createPipelineExecutor/getSamplesForTemplate.d.ts +0 -10
- package/esm/typings/src/formats/_common/FormatSubvalueDefinition.d.ts +0 -30
- package/esm/typings/src/formats/csv/CsvSettings.d.ts +0 -13
- package/esm/typings/src/formats/text/TextFormatDefinition.d.ts +0 -19
- package/esm/typings/src/utils/organization/just_empty_object.d.ts +0 -12
- package/esm/typings/src/utils/parameters/mapAvailableToExpectedParameters.d.ts +0 -27
- package/esm/typings/src/utils/validators/parameterName/validateParameterName.d.ts +0 -10
- package/esm/typings/src/utils/validators/parameterName/validateParameterName.test.d.ts +0 -1
- /package/esm/typings/src/{utils/parameters/extractParameterNames.test.d.ts → execution/utils/usageToHuman.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{parameters/mapAvailableToExpectedParameters.test.d.ts → extractParameterNames.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{parameters/replaceParameters.test.d.ts → replaceParameters.test.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -16,10 +16,6 @@ Supercharge your use of large language models
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
|
|
19
|
-
## ✨ New Features
|
|
20
|
-
|
|
21
|
-
- ✨ **Support of [OpenAI o1 model](https://openai.com/o1/)**
|
|
22
|
-
|
|
23
19
|
## 📦 Package `@promptbook/utils`
|
|
24
20
|
|
|
25
21
|
- Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
|
|
@@ -32,7 +28,7 @@ To install this package, run:
|
|
|
32
28
|
npm i ptbk
|
|
33
29
|
|
|
34
30
|
# Install just this package to save space
|
|
35
|
-
npm
|
|
31
|
+
npm i @promptbook/utils
|
|
36
32
|
```
|
|
37
33
|
|
|
38
34
|
Utility functions used in the library but also useful for individual use in preprocessing and postprocessing LLM inputs and outputs
|
|
@@ -163,6 +159,10 @@ See also the documentation for all the functions in the `@promptbook/utils` pack
|
|
|
163
159
|
|
|
164
160
|
Rest of the documentation is common for **entire promptbook ecosystem**:
|
|
165
161
|
|
|
162
|
+
# ✨ New Features
|
|
163
|
+
|
|
164
|
+
- ✨ **Support [OpenAI o1 model](https://openai.com/o1/)**
|
|
165
|
+
|
|
166
166
|
## 🤍 The Promptbook Whitepaper
|
|
167
167
|
|
|
168
168
|
|
|
@@ -209,6 +209,7 @@ File `write-website-content.ptbk.md`:
|
|
|
209
209
|
> Instructions for creating web page content.
|
|
210
210
|
>
|
|
211
211
|
> - PIPELINE URL https://promptbook.studio/webgpt/write-website-content.ptbk.md
|
|
212
|
+
> - PROMPTBOOK VERSION 0.0.1
|
|
212
213
|
> - INPUT PARAM `{rawTitle}` Automatically suggested a site name or empty text
|
|
213
214
|
> - INPUT PARAM `{rawAssigment}` Automatically generated site entry from image recognition
|
|
214
215
|
> - OUTPUT PARAM `{websiteContent}` Web content
|
package/esm/index.es.js
CHANGED
|
@@ -4,7 +4,7 @@ import spaceTrim$1, { spaceTrim } from 'spacetrim';
|
|
|
4
4
|
/**
|
|
5
5
|
* The version of the Promptbook library
|
|
6
6
|
*/
|
|
7
|
-
var PROMPTBOOK_VERSION = '0.
|
|
7
|
+
var PROMPTBOOK_VERSION = '0.68.5';
|
|
8
8
|
// TODO:[main] !!!! List here all the versions and annotate + put into script
|
|
9
9
|
|
|
10
10
|
/*! *****************************************************************************
|
|
@@ -625,9 +625,6 @@ function renderPromptbookMermaid(pipelineJson, options) {
|
|
|
625
625
|
return promptbookMermaid;
|
|
626
626
|
}
|
|
627
627
|
/**
|
|
628
|
-
* TODO: !!!!! FOREACH in mermaid graph
|
|
629
|
-
* TODO: !!!!! Knowledge in mermaid graph
|
|
630
|
-
* TODO: !!!!! Personas in mermaid graph
|
|
631
628
|
* TODO: Maybe use some Mermaid package instead of string templating
|
|
632
629
|
* TODO: [🕌] When more than 2 functionalities, split into separate functions
|
|
633
630
|
*/
|
|
@@ -737,72 +734,52 @@ function extractVariables(script) {
|
|
|
737
734
|
* @public exported from `@promptbook/utils`
|
|
738
735
|
*/
|
|
739
736
|
function extractParameterNamesFromTemplate(template) {
|
|
740
|
-
var e_1, _a, e_2, _b, e_3, _c
|
|
741
|
-
var title = template.title, description = template.description, templateType = template.templateType, content = template.content, preparedContent = template.preparedContent, jokerParameterNames = template.jokerParameterNames
|
|
737
|
+
var e_1, _a, e_2, _b, e_3, _c;
|
|
738
|
+
var title = template.title, description = template.description, templateType = template.templateType, content = template.content, preparedContent = template.preparedContent, jokerParameterNames = template.jokerParameterNames;
|
|
742
739
|
var parameterNames = new Set();
|
|
743
740
|
try {
|
|
744
|
-
for (var
|
|
745
|
-
var parameterName =
|
|
741
|
+
for (var _d = __values(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameterNames(title)), false), __read(extractParameterNames(description || '')), false), __read(extractParameterNames(content)), false), __read(extractParameterNames(preparedContent || '')), false)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
742
|
+
var parameterName = _e.value;
|
|
746
743
|
parameterNames.add(parameterName);
|
|
747
744
|
}
|
|
748
745
|
}
|
|
749
746
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
750
747
|
finally {
|
|
751
748
|
try {
|
|
752
|
-
if (
|
|
749
|
+
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
|
|
753
750
|
}
|
|
754
751
|
finally { if (e_1) throw e_1.error; }
|
|
755
752
|
}
|
|
756
753
|
if (templateType === 'SCRIPT_TEMPLATE') {
|
|
757
754
|
try {
|
|
758
|
-
for (var
|
|
759
|
-
var parameterName =
|
|
755
|
+
for (var _f = __values(extractVariables(content)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
756
|
+
var parameterName = _g.value;
|
|
760
757
|
parameterNames.add(parameterName);
|
|
761
758
|
}
|
|
762
759
|
}
|
|
763
760
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
764
761
|
finally {
|
|
765
762
|
try {
|
|
766
|
-
if (
|
|
763
|
+
if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
|
|
767
764
|
}
|
|
768
765
|
finally { if (e_2) throw e_2.error; }
|
|
769
766
|
}
|
|
770
767
|
}
|
|
771
768
|
try {
|
|
772
|
-
for (var
|
|
773
|
-
var jokerName =
|
|
769
|
+
for (var _h = __values(jokerParameterNames || []), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
770
|
+
var jokerName = _j.value;
|
|
774
771
|
parameterNames.add(jokerName);
|
|
775
772
|
}
|
|
776
773
|
}
|
|
777
774
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
778
775
|
finally {
|
|
779
776
|
try {
|
|
780
|
-
if (
|
|
777
|
+
if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
|
|
781
778
|
}
|
|
782
779
|
finally { if (e_3) throw e_3.error; }
|
|
783
780
|
}
|
|
784
781
|
parameterNames.delete('content');
|
|
785
782
|
// <- Note {websiteContent} is used in `preparedContent`
|
|
786
|
-
// Note: [🍭] Fixing dependent subparameterName from FOREACH command
|
|
787
|
-
if (foreach !== undefined) {
|
|
788
|
-
try {
|
|
789
|
-
for (var _l = __values(foreach.subparameterNames), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
790
|
-
var subparameterName = _m.value;
|
|
791
|
-
if (parameterNames.has(subparameterName)) {
|
|
792
|
-
parameterNames.delete(subparameterName);
|
|
793
|
-
parameterNames.add(foreach.parameterName);
|
|
794
|
-
// <- TODO: [🚎] Warn/logic error when `subparameterName` not used
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
799
|
-
finally {
|
|
800
|
-
try {
|
|
801
|
-
if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
|
802
|
-
}
|
|
803
|
-
finally { if (e_4) throw e_4.error; }
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
783
|
return parameterNames;
|
|
807
784
|
}
|
|
808
785
|
/**
|
|
@@ -1287,9 +1264,6 @@ var CountUtils = {
|
|
|
1287
1264
|
LINES: countLines,
|
|
1288
1265
|
PAGES: countPages,
|
|
1289
1266
|
};
|
|
1290
|
-
/**
|
|
1291
|
-
* TODO: [🧠][🤠] This should be probbably as part of `TextFormatDefinition`
|
|
1292
|
-
*/
|
|
1293
1267
|
|
|
1294
1268
|
/**
|
|
1295
1269
|
* Makes first letter of a string uppercase
|
|
@@ -1544,6 +1518,108 @@ function searchKeywords(haystack, needle) {
|
|
|
1544
1518
|
return true;
|
|
1545
1519
|
}
|
|
1546
1520
|
|
|
1521
|
+
/**
|
|
1522
|
+
* Function parseNumber will parse number from string
|
|
1523
|
+
*
|
|
1524
|
+
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
1525
|
+
* Note: it also works only with decimal numbers
|
|
1526
|
+
*
|
|
1527
|
+
* @returns parsed number
|
|
1528
|
+
* @throws {ParseError} if the value is not a number
|
|
1529
|
+
*
|
|
1530
|
+
* @public exported from `@promptbook/utils`
|
|
1531
|
+
*/
|
|
1532
|
+
function parseNumber(value) {
|
|
1533
|
+
var originalValue = value;
|
|
1534
|
+
if (typeof value === 'number') {
|
|
1535
|
+
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
1536
|
+
}
|
|
1537
|
+
if (typeof value !== 'string') {
|
|
1538
|
+
return 0;
|
|
1539
|
+
}
|
|
1540
|
+
value = value.trim();
|
|
1541
|
+
if (value.startsWith('+')) {
|
|
1542
|
+
return parseNumber(value.substring(1));
|
|
1543
|
+
}
|
|
1544
|
+
if (value.startsWith('-')) {
|
|
1545
|
+
var number = parseNumber(value.substring(1));
|
|
1546
|
+
if (number === 0) {
|
|
1547
|
+
return 0; // <- Note: To prevent -0
|
|
1548
|
+
}
|
|
1549
|
+
return -number;
|
|
1550
|
+
}
|
|
1551
|
+
value = value.replace(/,/g, '.');
|
|
1552
|
+
value = value.toUpperCase();
|
|
1553
|
+
if (value === '') {
|
|
1554
|
+
return 0;
|
|
1555
|
+
}
|
|
1556
|
+
if (value === '♾' || value.startsWith('INF')) {
|
|
1557
|
+
return Infinity;
|
|
1558
|
+
}
|
|
1559
|
+
if (value.includes('/')) {
|
|
1560
|
+
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
1561
|
+
var numerator = parseNumber(numerator_);
|
|
1562
|
+
var denominator = parseNumber(denominator_);
|
|
1563
|
+
if (denominator === 0) {
|
|
1564
|
+
throw new ParseError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
1565
|
+
}
|
|
1566
|
+
return numerator / denominator;
|
|
1567
|
+
}
|
|
1568
|
+
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
1569
|
+
return 0;
|
|
1570
|
+
}
|
|
1571
|
+
if (value.includes('E')) {
|
|
1572
|
+
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
1573
|
+
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
1574
|
+
}
|
|
1575
|
+
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
1576
|
+
throw new ParseError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
1577
|
+
}
|
|
1578
|
+
var num = parseFloat(value);
|
|
1579
|
+
if (isNaN(num)) {
|
|
1580
|
+
throw new ParseError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
1581
|
+
}
|
|
1582
|
+
return num;
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
1586
|
+
* TODO: [🧠][🌻] Maybe export through `@promptbook/markdown-utils` not `@promptbook/utils`
|
|
1587
|
+
*/
|
|
1588
|
+
|
|
1589
|
+
/**
|
|
1590
|
+
* Generates random seed
|
|
1591
|
+
*
|
|
1592
|
+
* Note: `$` is used to indicate that this function is not a pure function - it is not deterministic
|
|
1593
|
+
* Warning: This function is not cryptographically secure (it uses Math.random internally)
|
|
1594
|
+
* @public exported from `@promptbook/utils`
|
|
1595
|
+
*/
|
|
1596
|
+
function $randomSeed() {
|
|
1597
|
+
return Math.random();
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
/**
|
|
1601
|
+
* Removes quotes from a string
|
|
1602
|
+
*
|
|
1603
|
+
* Tip: This is very usefull for post-processing of the result of the LLM model
|
|
1604
|
+
* Note: This function removes only the same quotes from the beginning and the end of the string
|
|
1605
|
+
* Note: There are two simmilar functions:
|
|
1606
|
+
* - `removeQuotes` which removes only bounding quotes
|
|
1607
|
+
* - `unwrapResult` which removes whole introduce sentence
|
|
1608
|
+
*
|
|
1609
|
+
* @param text optionally quoted text
|
|
1610
|
+
* @returns text without quotes
|
|
1611
|
+
* @public exported from `@promptbook/utils`
|
|
1612
|
+
*/
|
|
1613
|
+
function removeQuotes(text) {
|
|
1614
|
+
if (text.startsWith('"') && text.endsWith('"')) {
|
|
1615
|
+
return text.slice(1, -1);
|
|
1616
|
+
}
|
|
1617
|
+
if (text.startsWith('\'') && text.endsWith('\'')) {
|
|
1618
|
+
return text.slice(1, -1);
|
|
1619
|
+
}
|
|
1620
|
+
return text;
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1547
1623
|
/**
|
|
1548
1624
|
* @@@
|
|
1549
1625
|
*
|
|
@@ -1746,7 +1822,6 @@ $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
|
|
|
1746
1822
|
'samples',
|
|
1747
1823
|
'modelName',
|
|
1748
1824
|
'currentDate',
|
|
1749
|
-
// <- TODO: !!!!! list here all command names
|
|
1750
1825
|
// <- TODO: Add more like 'date', 'modelName',...
|
|
1751
1826
|
// <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
|
|
1752
1827
|
]);
|
|
@@ -1762,18 +1837,6 @@ var RESERVED_PARAMETER_MISSING_VALUE = 'MISSING-' + REPLACING_NONCE;
|
|
|
1762
1837
|
* @private within the repository
|
|
1763
1838
|
*/
|
|
1764
1839
|
var RESERVED_PARAMETER_RESTRICTED = 'RESTRICTED-' + REPLACING_NONCE;
|
|
1765
|
-
// <- TODO: [🧜♂️]
|
|
1766
|
-
/**
|
|
1767
|
-
* @@@
|
|
1768
|
-
*
|
|
1769
|
-
* @public exported from `@promptbook/core`
|
|
1770
|
-
*/
|
|
1771
|
-
Object.freeze({
|
|
1772
|
-
delimiter: ',',
|
|
1773
|
-
quoteChar: '"',
|
|
1774
|
-
newline: '\n',
|
|
1775
|
-
skipEmptyLines: true,
|
|
1776
|
-
});
|
|
1777
1840
|
/**
|
|
1778
1841
|
* TODO: [🧠][🧜♂️] Maybe join remoteUrl and path into single value
|
|
1779
1842
|
*/
|
|
@@ -1857,108 +1920,6 @@ function replaceParameters(template, parameters) {
|
|
|
1857
1920
|
return replacedTemplate;
|
|
1858
1921
|
}
|
|
1859
1922
|
|
|
1860
|
-
/**
|
|
1861
|
-
* Function parseNumber will parse number from string
|
|
1862
|
-
*
|
|
1863
|
-
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
1864
|
-
* Note: it also works only with decimal numbers
|
|
1865
|
-
*
|
|
1866
|
-
* @returns parsed number
|
|
1867
|
-
* @throws {ParseError} if the value is not a number
|
|
1868
|
-
*
|
|
1869
|
-
* @public exported from `@promptbook/utils`
|
|
1870
|
-
*/
|
|
1871
|
-
function parseNumber(value) {
|
|
1872
|
-
var originalValue = value;
|
|
1873
|
-
if (typeof value === 'number') {
|
|
1874
|
-
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
1875
|
-
}
|
|
1876
|
-
if (typeof value !== 'string') {
|
|
1877
|
-
return 0;
|
|
1878
|
-
}
|
|
1879
|
-
value = value.trim();
|
|
1880
|
-
if (value.startsWith('+')) {
|
|
1881
|
-
return parseNumber(value.substring(1));
|
|
1882
|
-
}
|
|
1883
|
-
if (value.startsWith('-')) {
|
|
1884
|
-
var number = parseNumber(value.substring(1));
|
|
1885
|
-
if (number === 0) {
|
|
1886
|
-
return 0; // <- Note: To prevent -0
|
|
1887
|
-
}
|
|
1888
|
-
return -number;
|
|
1889
|
-
}
|
|
1890
|
-
value = value.replace(/,/g, '.');
|
|
1891
|
-
value = value.toUpperCase();
|
|
1892
|
-
if (value === '') {
|
|
1893
|
-
return 0;
|
|
1894
|
-
}
|
|
1895
|
-
if (value === '♾' || value.startsWith('INF')) {
|
|
1896
|
-
return Infinity;
|
|
1897
|
-
}
|
|
1898
|
-
if (value.includes('/')) {
|
|
1899
|
-
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
1900
|
-
var numerator = parseNumber(numerator_);
|
|
1901
|
-
var denominator = parseNumber(denominator_);
|
|
1902
|
-
if (denominator === 0) {
|
|
1903
|
-
throw new ParseError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
1904
|
-
}
|
|
1905
|
-
return numerator / denominator;
|
|
1906
|
-
}
|
|
1907
|
-
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
1908
|
-
return 0;
|
|
1909
|
-
}
|
|
1910
|
-
if (value.includes('E')) {
|
|
1911
|
-
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
1912
|
-
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
1913
|
-
}
|
|
1914
|
-
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
1915
|
-
throw new ParseError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
1916
|
-
}
|
|
1917
|
-
var num = parseFloat(value);
|
|
1918
|
-
if (isNaN(num)) {
|
|
1919
|
-
throw new ParseError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
1920
|
-
}
|
|
1921
|
-
return num;
|
|
1922
|
-
}
|
|
1923
|
-
/**
|
|
1924
|
-
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
1925
|
-
* TODO: [🧠][🌻] Maybe export through `@promptbook/markdown-utils` not `@promptbook/utils`
|
|
1926
|
-
*/
|
|
1927
|
-
|
|
1928
|
-
/**
|
|
1929
|
-
* Generates random seed
|
|
1930
|
-
*
|
|
1931
|
-
* Note: `$` is used to indicate that this function is not a pure function - it is not deterministic
|
|
1932
|
-
* Warning: This function is not cryptographically secure (it uses Math.random internally)
|
|
1933
|
-
* @public exported from `@promptbook/utils`
|
|
1934
|
-
*/
|
|
1935
|
-
function $randomSeed() {
|
|
1936
|
-
return Math.random();
|
|
1937
|
-
}
|
|
1938
|
-
|
|
1939
|
-
/**
|
|
1940
|
-
* Removes quotes from a string
|
|
1941
|
-
*
|
|
1942
|
-
* Tip: This is very usefull for post-processing of the result of the LLM model
|
|
1943
|
-
* Note: This function removes only the same quotes from the beginning and the end of the string
|
|
1944
|
-
* Note: There are two simmilar functions:
|
|
1945
|
-
* - `removeQuotes` which removes only bounding quotes
|
|
1946
|
-
* - `unwrapResult` which removes whole introduce sentence
|
|
1947
|
-
*
|
|
1948
|
-
* @param text optionally quoted text
|
|
1949
|
-
* @returns text without quotes
|
|
1950
|
-
* @public exported from `@promptbook/utils`
|
|
1951
|
-
*/
|
|
1952
|
-
function removeQuotes(text) {
|
|
1953
|
-
if (text.startsWith('"') && text.endsWith('"')) {
|
|
1954
|
-
return text.slice(1, -1);
|
|
1955
|
-
}
|
|
1956
|
-
if (text.startsWith('\'') && text.endsWith('\'')) {
|
|
1957
|
-
return text.slice(1, -1);
|
|
1958
|
-
}
|
|
1959
|
-
return text;
|
|
1960
|
-
}
|
|
1961
|
-
|
|
1962
1923
|
/**
|
|
1963
1924
|
* @@@
|
|
1964
1925
|
*
|