@promptbook/utils 0.69.0-1 → 0.69.0-11
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 +17 -58
- package/esm/index.es.js +141 -114
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/core.index.d.ts +5 -1
- package/esm/typings/src/_packages/types.index.d.ts +8 -0
- package/esm/typings/src/_packages/utils.index.d.ts +4 -4
- package/esm/typings/src/commands/FOREACH/ForeachCommand.d.ts +2 -6
- package/esm/typings/src/commands/FOREACH/ForeachJson.d.ts +26 -0
- package/esm/typings/src/commands/FOREACH/foreachCommand.test.d.ts +3 -0
- package/esm/typings/src/commands/FOREACH/foreachCommandParser.d.ts +2 -1
- package/esm/typings/src/commands/_common/types/CommandParser.d.ts +4 -4
- package/esm/typings/src/config.d.ts +7 -1
- package/esm/typings/src/conversion/prettify/renderPipelineMermaidOptions.d.ts +3 -0
- package/esm/typings/src/conversion/utils/extractParameterNamesFromTemplate.d.ts +2 -1
- package/esm/typings/src/conversion/validation/validatePipeline.d.ts +5 -0
- package/esm/typings/src/execution/PipelineExecutorResult.d.ts +5 -3
- package/esm/typings/src/execution/createPipelineExecutor/$OngoingTemplateResult.d.ts +21 -0
- package/esm/typings/src/execution/createPipelineExecutor/00-CreatePipelineExecutorOptions.d.ts +20 -0
- package/esm/typings/src/execution/createPipelineExecutor/00-CreatePipelineExecutorSettings.d.ts +33 -0
- package/esm/typings/src/execution/createPipelineExecutor/00-createPipelineExecutor.d.ts +10 -0
- package/esm/typings/src/execution/createPipelineExecutor/10-executePipeline.d.ts +55 -0
- package/esm/typings/src/execution/createPipelineExecutor/20-executeTemplate.d.ts +62 -0
- package/esm/typings/src/execution/createPipelineExecutor/30-executeFormatCells.d.ts +21 -0
- package/esm/typings/src/execution/createPipelineExecutor/40-executeAttempts.d.ts +74 -0
- package/esm/typings/src/execution/createPipelineExecutor/filterJustOutputParameters.d.ts +34 -0
- package/esm/typings/src/execution/createPipelineExecutor/getContextForTemplate.d.ts +10 -0
- package/esm/typings/src/execution/createPipelineExecutor/getKnowledgeForTemplate.d.ts +27 -0
- package/esm/typings/src/execution/createPipelineExecutor/getReservedParametersForTemplate.d.ts +30 -0
- package/esm/typings/src/execution/createPipelineExecutor/getSamplesForTemplate.d.ts +10 -0
- package/esm/typings/src/execution/utils/checkExpectations.d.ts +2 -0
- package/esm/typings/src/execution/utils/usageToHuman.d.ts +3 -4
- package/esm/typings/src/formats/_common/FormatDefinition.d.ts +9 -12
- package/esm/typings/src/formats/_common/FormatSubvalueDefinition.d.ts +30 -0
- package/esm/typings/src/formats/csv/{ListFormatDefinition.d.ts → CsvFormatDefinition.d.ts} +3 -2
- package/esm/typings/src/formats/index.d.ts +1 -1
- package/esm/typings/src/formats/json/JsonFormatDefinition.d.ts +1 -1
- package/esm/typings/src/formats/text/TextFormatDefinition.d.ts +17 -0
- package/esm/typings/src/formats/xml/XmlFormatDefinition.d.ts +1 -1
- package/esm/typings/src/types/PipelineJson/ParameterJson.d.ts +1 -0
- package/esm/typings/src/types/PipelineJson/TemplateJsonCommon.d.ts +5 -0
- package/esm/typings/src/types/execution-report/ExecutionReportJson.d.ts +3 -0
- package/esm/typings/src/utils/expectation-counters/index.d.ts +3 -0
- package/esm/typings/src/utils/{extractParameterNames.d.ts → parameters/extractParameterNames.d.ts} +2 -2
- package/esm/typings/src/utils/parameters/mapAvailableToExpectedParameters.d.ts +27 -0
- package/esm/typings/src/utils/{replaceParameters.d.ts → parameters/replaceParameters.d.ts} +2 -2
- package/esm/typings/src/utils/validators/parameterName/validateParameterName.d.ts +10 -0
- package/package.json +1 -1
- package/umd/index.umd.js +141 -114
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/execution/createPipelineExecutor.d.ts +0 -72
- package/esm/typings/src/formats/list/ListFormatDefinition.d.ts +0 -16
- /package/esm/typings/src/utils/{extractParameterNames.test.d.ts → parameters/extractParameterNames.test.d.ts} +0 -0
- /package/esm/typings/src/{collection/constructors/createCollectionFromDirectory.test.d.ts → utils/parameters/mapAvailableToExpectedParameters.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{replaceParameters.test.d.ts → parameters/replaceParameters.test.d.ts} +0 -0
- /package/esm/typings/src/{execution/utils/usageToHuman.test.d.ts → utils/validators/parameterName/validateParameterName.test.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -161,13 +161,15 @@ Rest of the documentation is common for **entire promptbook ecosystem**:
|
|
|
161
161
|
|
|
162
162
|
## 🤍 The Promptbook Whitepaper
|
|
163
163
|
|
|
164
|
+
|
|
165
|
+
|
|
164
166
|
If you have a simple, single prompt for ChatGPT, GPT-4, Anthropic Claude, Google Gemini, Llama 2, or whatever, it doesn't matter how you integrate it. Whether it's calling a REST API directly, using the SDK, hardcoding the prompt into the source code, or importing a text file, the process remains the same.
|
|
165
167
|
|
|
166
168
|
But often you will struggle with the limitations of LLMs, such as hallucinations, off-topic responses, poor quality output, language drift, word repetition repetition repetition repetition or misuse, lack of context, or just plain w𝒆𝐢rd responses. When this happens, you generally have three options:
|
|
167
169
|
|
|
168
170
|
1. **Fine-tune** the model to your specifications or even train your own.
|
|
169
171
|
2. **Prompt-engineer** the prompt to the best shape you can achieve.
|
|
170
|
-
3.
|
|
172
|
+
3. Orchestrate **multiple prompts** in a [pipeline](https://github.com/webgptorg/promptbook/discussions/64) to get the best result.
|
|
171
173
|
|
|
172
174
|
In all of these situations, but especially in 3., the Promptbook library can make your life easier.
|
|
173
175
|
|
|
@@ -179,7 +181,9 @@ In all of these situations, but especially in 3., the Promptbook library can mak
|
|
|
179
181
|
- Promptbook has built in versioning. You can test multiple **A/B versions** of pipelines and see which one works best.
|
|
180
182
|
- Promptbook is designed to do [**RAG** (Retrieval-Augmented Generation)](https://github.com/webgptorg/promptbook/discussions/41) and other advanced techniques. You can use **knowledge** to improve the quality of the output.
|
|
181
183
|
|
|
182
|
-
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
## 🧔 Pipeline _(for prompt-engeneers)_
|
|
183
187
|
|
|
184
188
|
**P**romp**t** **b**oo**k** markdown file (or `.ptbk.md` file) is document that describes a **pipeline** - a series of prompts that are chained together to form somewhat reciepe for transforming natural language input.
|
|
185
189
|
|
|
@@ -201,7 +205,6 @@ File `write-website-content.ptbk.md`:
|
|
|
201
205
|
> Instructions for creating web page content.
|
|
202
206
|
>
|
|
203
207
|
> - PIPELINE URL https://promptbook.studio/webgpt/write-website-content.ptbk.md
|
|
204
|
-
> - PROMPTBOOK VERSION 0.0.1
|
|
205
208
|
> - INPUT PARAM `{rawTitle}` Automatically suggested a site name or empty text
|
|
206
209
|
> - INPUT PARAM `{rawAssigment}` Automatically generated site entry from image recognition
|
|
207
210
|
> - OUTPUT PARAM `{websiteContent}` Web content
|
|
@@ -496,6 +499,8 @@ The following glossary is used to clarify certain concepts:
|
|
|
496
499
|
- When you want to **version** your prompts and **test multiple versions**
|
|
497
500
|
- When you want to **log** the execution of prompts and backtrace the issues
|
|
498
501
|
|
|
502
|
+
[See more](https://github.com/webgptorg/promptbook/discussions/111)
|
|
503
|
+
|
|
499
504
|
### ➖ When not to use
|
|
500
505
|
|
|
501
506
|
- When you have already implemented single simple prompt and it works fine for your job
|
|
@@ -505,6 +510,8 @@ The following glossary is used to clarify certain concepts:
|
|
|
505
510
|
- When your main focus is on something other than text - like images, audio, video, spreadsheets _(other media types may be added in the future, [see discussion](https://github.com/webgptorg/promptbook/discussions/103))_
|
|
506
511
|
- When you need to use recursion _([see the discussion](https://github.com/webgptorg/promptbook/discussions/38))_
|
|
507
512
|
|
|
513
|
+
[See more](https://github.com/webgptorg/promptbook/discussions/112)
|
|
514
|
+
|
|
508
515
|
## 🐜 Known issues
|
|
509
516
|
|
|
510
517
|
- [🤸♂️ Iterations not working yet](https://github.com/webgptorg/promptbook/discussions/55)
|
|
@@ -517,63 +524,15 @@ The following glossary is used to clarify certain concepts:
|
|
|
517
524
|
|
|
518
525
|
## ❔ FAQ
|
|
519
526
|
|
|
520
|
-
|
|
521
|
-
|
|
522
527
|
If you have a question [start a discussion](https://github.com/webgptorg/promptbook/discussions/), [open an issue](https://github.com/webgptorg/promptbook/issues) or [write me an email](https://www.pavolhejny.com/contact).
|
|
523
528
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
We are considering creating a bridge/converter between these two libraries.
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
### Promptbooks vs. OpenAI`s GPTs
|
|
537
|
-
|
|
538
|
-
GPTs are chat assistants that can be assigned to specific tasks and materials. But they are still chat assistants. Promptbooks are a way to orchestrate many more predefined tasks to have much tighter control over the process. Promptbooks are not a good technology for creating human-like chatbots, GPTs are not a good technology for creating outputs with specific requirements.
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
### Where should I store my promptbooks?
|
|
555
|
-
|
|
556
|
-
If you use raw SDKs, you just put prompts in the sourcecode, mixed in with typescript, javascript, python or whatever programming language you use.
|
|
557
|
-
|
|
558
|
-
If you use promptbooks, you can store them in several places, each with its own advantages and disadvantages:
|
|
559
|
-
|
|
560
|
-
1. As **source code**, typically git-committed. In this case you can use the versioning system and the promptbooks will be tightly coupled with the version of the application. You still get the power of promptbooks, as you separate the concerns of the prompt-engineer and the programmer.
|
|
561
|
-
|
|
562
|
-
2. As data in a **database** In this case, promptbooks are like posts / articles on the blog. They can be modified independently of the application. You don't need to redeploy the application to change the promptbooks. You can have multiple versions of promptbooks for each user. You can have a web interface for non-programmers to create and modify promptbooks. But you lose the versioning system and you still have to consider the interface between the promptbooks and the application _(= input and output parameters)_.
|
|
563
|
-
|
|
564
|
-
3. In a **configuration** in environment variables. This is a good way to store promptbooks if you have an application with multiple deployments and you want to have different but simple promptbooks for each deployment and you don't need to change them often.
|
|
565
|
-
|
|
566
|
-
### What should I do when I need same promptbook in multiple human languages?
|
|
567
|
-
|
|
568
|
-
A single promptbook can be written for several _(human)_ languages at once. However, we recommend that you have separate promptbooks for each language.
|
|
569
|
-
|
|
570
|
-
In large language models, you will get better results if you have prompts in the same language as the user input.
|
|
571
|
-
|
|
572
|
-
The best way to manage this is to have suffixed promptbooks like `write-website-content.en.ptbk.md` and `write-website-content.cs.ptbk.md` for each supported language.
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
529
|
+
- [❔ Why not just use the OpenAI SDK / Anthropic Claude SDK / ...?](https://github.com/webgptorg/promptbook/discussions/114)
|
|
530
|
+
- [❔ How is it different from the OpenAI`s GPTs?](https://github.com/webgptorg/promptbook/discussions/118)
|
|
531
|
+
- [❔ How is it different from the Langchain?](https://github.com/webgptorg/promptbook/discussions/115)
|
|
532
|
+
- [❔ How is it different from the DSPy?](https://github.com/webgptorg/promptbook/discussions/117)
|
|
533
|
+
- [❔ How is it different from _anything_?](https://github.com/webgptorg/promptbook/discussions?discussions_q=is%3Aopen+label%3A%22Promptbook+vs%22)
|
|
534
|
+
- [❔ Is Promptbook using RAG _(Retrieval-Augmented Generation)_?](https://github.com/webgptorg/promptbook/discussions/123)
|
|
535
|
+
- [❔ Is Promptbook using function calling?](https://github.com/webgptorg/promptbook/discussions/124)
|
|
577
536
|
|
|
578
537
|
## ⌚ Changelog
|
|
579
538
|
|
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.69.0-
|
|
7
|
+
var PROMPTBOOK_VERSION = '0.69.0-10';
|
|
8
8
|
// TODO: !!!! List here all the versions and annotate + put into script
|
|
9
9
|
|
|
10
10
|
/*! *****************************************************************************
|
|
@@ -625,6 +625,9 @@ 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
|
|
628
631
|
* TODO: Maybe use some Mermaid package instead of string templating
|
|
629
632
|
* TODO: [🕌] When more than 2 functionalities, split into separate functions
|
|
630
633
|
*/
|
|
@@ -734,52 +737,72 @@ function extractVariables(script) {
|
|
|
734
737
|
* @public exported from `@promptbook/utils`
|
|
735
738
|
*/
|
|
736
739
|
function extractParameterNamesFromTemplate(template) {
|
|
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;
|
|
740
|
+
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
|
|
741
|
+
var title = template.title, description = template.description, templateType = template.templateType, content = template.content, preparedContent = template.preparedContent, jokerParameterNames = template.jokerParameterNames, foreach = template.foreach;
|
|
739
742
|
var parameterNames = new Set();
|
|
740
743
|
try {
|
|
741
|
-
for (var
|
|
742
|
-
var parameterName =
|
|
744
|
+
for (var _e = __values(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(extractParameterNames(title)), false), __read(extractParameterNames(description || '')), false), __read(extractParameterNames(content)), false), __read(extractParameterNames(preparedContent || '')), false)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
745
|
+
var parameterName = _f.value;
|
|
743
746
|
parameterNames.add(parameterName);
|
|
744
747
|
}
|
|
745
748
|
}
|
|
746
749
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
747
750
|
finally {
|
|
748
751
|
try {
|
|
749
|
-
if (
|
|
752
|
+
if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
|
750
753
|
}
|
|
751
754
|
finally { if (e_1) throw e_1.error; }
|
|
752
755
|
}
|
|
753
756
|
if (templateType === 'SCRIPT_TEMPLATE') {
|
|
754
757
|
try {
|
|
755
|
-
for (var
|
|
756
|
-
var parameterName =
|
|
758
|
+
for (var _g = __values(extractVariables(content)), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
759
|
+
var parameterName = _h.value;
|
|
757
760
|
parameterNames.add(parameterName);
|
|
758
761
|
}
|
|
759
762
|
}
|
|
760
763
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
761
764
|
finally {
|
|
762
765
|
try {
|
|
763
|
-
if (
|
|
766
|
+
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
764
767
|
}
|
|
765
768
|
finally { if (e_2) throw e_2.error; }
|
|
766
769
|
}
|
|
767
770
|
}
|
|
768
771
|
try {
|
|
769
|
-
for (var
|
|
770
|
-
var jokerName =
|
|
772
|
+
for (var _j = __values(jokerParameterNames || []), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
773
|
+
var jokerName = _k.value;
|
|
771
774
|
parameterNames.add(jokerName);
|
|
772
775
|
}
|
|
773
776
|
}
|
|
774
777
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
775
778
|
finally {
|
|
776
779
|
try {
|
|
777
|
-
if (
|
|
780
|
+
if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
|
778
781
|
}
|
|
779
782
|
finally { if (e_3) throw e_3.error; }
|
|
780
783
|
}
|
|
781
784
|
parameterNames.delete('content');
|
|
782
785
|
// <- 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
|
+
}
|
|
783
806
|
return parameterNames;
|
|
784
807
|
}
|
|
785
808
|
/**
|
|
@@ -1264,6 +1287,9 @@ var CountUtils = {
|
|
|
1264
1287
|
LINES: countLines,
|
|
1265
1288
|
PAGES: countPages,
|
|
1266
1289
|
};
|
|
1290
|
+
/**
|
|
1291
|
+
* TODO: [🧠][🤠] This should be probbably as part of `TextFormatDefinition`
|
|
1292
|
+
*/
|
|
1267
1293
|
|
|
1268
1294
|
/**
|
|
1269
1295
|
* Makes first letter of a string uppercase
|
|
@@ -1518,108 +1544,6 @@ function searchKeywords(haystack, needle) {
|
|
|
1518
1544
|
return true;
|
|
1519
1545
|
}
|
|
1520
1546
|
|
|
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
|
-
|
|
1623
1547
|
/**
|
|
1624
1548
|
* @@@
|
|
1625
1549
|
*
|
|
@@ -1822,6 +1746,7 @@ $asDeeplyFrozenSerializableJson('RESERVED_PARAMETER_NAMES', [
|
|
|
1822
1746
|
'samples',
|
|
1823
1747
|
'modelName',
|
|
1824
1748
|
'currentDate',
|
|
1749
|
+
// <- TODO: !!!!! list here all command names
|
|
1825
1750
|
// <- TODO: Add more like 'date', 'modelName',...
|
|
1826
1751
|
// <- TODO: Add [emoji] + instructions ACRY when adding new reserved parameter
|
|
1827
1752
|
]);
|
|
@@ -1920,6 +1845,108 @@ function replaceParameters(template, parameters) {
|
|
|
1920
1845
|
return replacedTemplate;
|
|
1921
1846
|
}
|
|
1922
1847
|
|
|
1848
|
+
/**
|
|
1849
|
+
* Function parseNumber will parse number from string
|
|
1850
|
+
*
|
|
1851
|
+
* Unlike Number.parseInt, Number.parseFloat it will never ever result in NaN
|
|
1852
|
+
* Note: it also works only with decimal numbers
|
|
1853
|
+
*
|
|
1854
|
+
* @returns parsed number
|
|
1855
|
+
* @throws {ParseError} if the value is not a number
|
|
1856
|
+
*
|
|
1857
|
+
* @public exported from `@promptbook/utils`
|
|
1858
|
+
*/
|
|
1859
|
+
function parseNumber(value) {
|
|
1860
|
+
var originalValue = value;
|
|
1861
|
+
if (typeof value === 'number') {
|
|
1862
|
+
value = value.toString(); // <- TODO: Maybe more efficient way to do this
|
|
1863
|
+
}
|
|
1864
|
+
if (typeof value !== 'string') {
|
|
1865
|
+
return 0;
|
|
1866
|
+
}
|
|
1867
|
+
value = value.trim();
|
|
1868
|
+
if (value.startsWith('+')) {
|
|
1869
|
+
return parseNumber(value.substring(1));
|
|
1870
|
+
}
|
|
1871
|
+
if (value.startsWith('-')) {
|
|
1872
|
+
var number = parseNumber(value.substring(1));
|
|
1873
|
+
if (number === 0) {
|
|
1874
|
+
return 0; // <- Note: To prevent -0
|
|
1875
|
+
}
|
|
1876
|
+
return -number;
|
|
1877
|
+
}
|
|
1878
|
+
value = value.replace(/,/g, '.');
|
|
1879
|
+
value = value.toUpperCase();
|
|
1880
|
+
if (value === '') {
|
|
1881
|
+
return 0;
|
|
1882
|
+
}
|
|
1883
|
+
if (value === '♾' || value.startsWith('INF')) {
|
|
1884
|
+
return Infinity;
|
|
1885
|
+
}
|
|
1886
|
+
if (value.includes('/')) {
|
|
1887
|
+
var _a = __read(value.split('/'), 2), numerator_ = _a[0], denominator_ = _a[1];
|
|
1888
|
+
var numerator = parseNumber(numerator_);
|
|
1889
|
+
var denominator = parseNumber(denominator_);
|
|
1890
|
+
if (denominator === 0) {
|
|
1891
|
+
throw new ParseError("Unable to parse number from \"".concat(originalValue, "\" because denominator is zero"));
|
|
1892
|
+
}
|
|
1893
|
+
return numerator / denominator;
|
|
1894
|
+
}
|
|
1895
|
+
if (/^(NAN|NULL|NONE|UNDEFINED|ZERO|NO.*)$/.test(value)) {
|
|
1896
|
+
return 0;
|
|
1897
|
+
}
|
|
1898
|
+
if (value.includes('E')) {
|
|
1899
|
+
var _b = __read(value.split('E'), 2), significand = _b[0], exponent = _b[1];
|
|
1900
|
+
return parseNumber(significand) * Math.pow(10, parseNumber(exponent));
|
|
1901
|
+
}
|
|
1902
|
+
if (!/^[0-9.]+$/.test(value) || value.split('.').length > 2) {
|
|
1903
|
+
throw new ParseError("Unable to parse number from \"".concat(originalValue, "\""));
|
|
1904
|
+
}
|
|
1905
|
+
var num = parseFloat(value);
|
|
1906
|
+
if (isNaN(num)) {
|
|
1907
|
+
throw new ParseError("Unexpected NaN when parsing number from \"".concat(originalValue, "\""));
|
|
1908
|
+
}
|
|
1909
|
+
return num;
|
|
1910
|
+
}
|
|
1911
|
+
/**
|
|
1912
|
+
* TODO: Maybe use sth. like safe-eval in fraction/calculation case @see https://www.npmjs.com/package/safe-eval
|
|
1913
|
+
* TODO: [🧠][🌻] Maybe export through `@promptbook/markdown-utils` not `@promptbook/utils`
|
|
1914
|
+
*/
|
|
1915
|
+
|
|
1916
|
+
/**
|
|
1917
|
+
* Generates random seed
|
|
1918
|
+
*
|
|
1919
|
+
* Note: `$` is used to indicate that this function is not a pure function - it is not deterministic
|
|
1920
|
+
* Warning: This function is not cryptographically secure (it uses Math.random internally)
|
|
1921
|
+
* @public exported from `@promptbook/utils`
|
|
1922
|
+
*/
|
|
1923
|
+
function $randomSeed() {
|
|
1924
|
+
return Math.random();
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
/**
|
|
1928
|
+
* Removes quotes from a string
|
|
1929
|
+
*
|
|
1930
|
+
* Tip: This is very usefull for post-processing of the result of the LLM model
|
|
1931
|
+
* Note: This function removes only the same quotes from the beginning and the end of the string
|
|
1932
|
+
* Note: There are two simmilar functions:
|
|
1933
|
+
* - `removeQuotes` which removes only bounding quotes
|
|
1934
|
+
* - `unwrapResult` which removes whole introduce sentence
|
|
1935
|
+
*
|
|
1936
|
+
* @param text optionally quoted text
|
|
1937
|
+
* @returns text without quotes
|
|
1938
|
+
* @public exported from `@promptbook/utils`
|
|
1939
|
+
*/
|
|
1940
|
+
function removeQuotes(text) {
|
|
1941
|
+
if (text.startsWith('"') && text.endsWith('"')) {
|
|
1942
|
+
return text.slice(1, -1);
|
|
1943
|
+
}
|
|
1944
|
+
if (text.startsWith('\'') && text.endsWith('\'')) {
|
|
1945
|
+
return text.slice(1, -1);
|
|
1946
|
+
}
|
|
1947
|
+
return text;
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1923
1950
|
/**
|
|
1924
1951
|
* @@@
|
|
1925
1952
|
*
|