@far-world-labs/verblets 0.1.1 → 0.1.2
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/.cursor/launch.json +30 -0
- package/.cursor/settings.json +20 -0
- package/.github/workflows/branch-protection.yml +22 -0
- package/.github/workflows/ci.yml +117 -0
- package/.prettierrc +6 -0
- package/.release-it.json +4 -1
- package/.vscode/launch.json +31 -0
- package/AGENTS.md +220 -0
- package/DEVELOPING.md +105 -0
- package/README.md +671 -0
- package/eslint.config.js +80 -0
- package/package.json +28 -16
- package/scripts/generate-test/index.js +29 -3
- package/scripts/runner/index.js +26 -0
- package/scripts/simple-editor/index.js +29 -18
- package/scripts/summarize-files/index.js +28 -4
- package/src/chains/README.md +30 -0
- package/src/chains/anonymize/README.md +21 -0
- package/src/chains/anonymize/index.examples.js +75 -0
- package/src/chains/anonymize/index.js +121 -0
- package/src/chains/anonymize/index.spec.js +78 -0
- package/src/chains/bulk-central-tendency/index.examples.js +138 -0
- package/src/chains/bulk-central-tendency/index.js +91 -0
- package/src/chains/bulk-filter/README.md +21 -0
- package/src/chains/bulk-filter/index.examples.js +22 -0
- package/src/chains/bulk-filter/index.js +58 -0
- package/src/chains/bulk-filter/index.spec.js +38 -0
- package/src/chains/bulk-find/README.md +16 -0
- package/src/chains/bulk-find/index.examples.js +20 -0
- package/src/chains/bulk-find/index.js +30 -0
- package/src/chains/bulk-find/index.spec.js +26 -0
- package/src/chains/bulk-group/README.md +23 -0
- package/src/chains/bulk-group/index.examples.js +18 -0
- package/src/chains/bulk-group/index.js +34 -0
- package/src/chains/bulk-group/index.spec.js +41 -0
- package/src/chains/bulk-map/README.md +43 -0
- package/src/chains/bulk-map/index.examples.js +17 -0
- package/src/chains/bulk-map/index.js +86 -0
- package/src/chains/bulk-map/index.spec.js +44 -0
- package/src/chains/bulk-reduce/README.md +12 -0
- package/src/chains/bulk-reduce/index.examples.js +15 -0
- package/src/chains/bulk-reduce/index.js +13 -0
- package/src/chains/bulk-reduce/index.spec.js +25 -0
- package/src/chains/bulk-score/README.md +16 -0
- package/src/chains/bulk-score/bulk-score-result.json +18 -0
- package/src/chains/bulk-score/index.examples.js +22 -0
- package/src/chains/bulk-score/index.js +133 -0
- package/src/chains/bulk-score/index.spec.js +30 -0
- package/src/chains/category-samples/README.md +61 -0
- package/src/chains/category-samples/index.examples.js +103 -0
- package/src/chains/category-samples/index.js +134 -0
- package/src/chains/collect-terms/README.md +12 -0
- package/src/chains/collect-terms/index.examples.js +16 -0
- package/src/chains/collect-terms/index.js +44 -0
- package/src/chains/collect-terms/index.spec.js +25 -0
- package/src/chains/date/README.md +12 -0
- package/src/chains/date/index.examples.js +47 -0
- package/src/chains/date/index.js +74 -0
- package/src/chains/date/index.spec.js +62 -0
- package/src/chains/disambiguate/README.md +22 -0
- package/src/chains/disambiguate/disambiguate-meanings-result.json +16 -0
- package/src/chains/disambiguate/index.examples.js +18 -0
- package/src/chains/disambiguate/index.js +92 -0
- package/src/chains/disambiguate/index.spec.js +25 -0
- package/src/chains/dismantle/README.md +67 -0
- package/src/chains/dismantle/dismantle.examples.js +27 -0
- package/src/chains/dismantle/index.js +6 -17
- package/src/chains/dismantle/index.spec.js +1 -2
- package/src/chains/expect/README.md +171 -0
- package/src/chains/expect/index.examples.js +146 -0
- package/src/chains/expect/index.js +173 -0
- package/src/chains/expect/index.spec.js +324 -0
- package/src/chains/filter-ambiguous/README.md +11 -0
- package/src/chains/filter-ambiguous/index.examples.js +20 -0
- package/src/chains/filter-ambiguous/index.js +49 -0
- package/src/chains/filter-ambiguous/index.spec.js +31 -0
- package/src/chains/glossary/README.md +19 -0
- package/src/chains/glossary/index.examples.js +386 -0
- package/src/chains/glossary/index.js +75 -0
- package/src/chains/glossary/index.spec.js +19 -0
- package/src/chains/intersections/README.md +152 -0
- package/src/chains/intersections/index.examples.js +279 -0
- package/src/chains/intersections/index.js +366 -0
- package/src/chains/intersections/intersection-result.json +38 -0
- package/src/chains/list/index.examples.js +12 -16
- package/src/chains/list/index.js +106 -53
- package/src/chains/list/index.spec.js +3 -9
- package/src/chains/list/list-result.json +16 -0
- package/src/chains/llm-logger/README.md +208 -0
- package/src/chains/llm-logger/index.js +205 -0
- package/src/chains/llm-logger/index.spec.js +330 -0
- package/src/chains/questions/index.examples.js +2 -1
- package/src/chains/questions/index.js +14 -15
- package/src/chains/scan-js/index.js +6 -9
- package/src/chains/set-interval/README.md +81 -0
- package/src/chains/set-interval/index.examples.js +36 -0
- package/src/chains/set-interval/index.js +131 -0
- package/src/chains/set-interval/index.spec.js +70 -0
- package/src/chains/socratic/README.md +17 -0
- package/src/chains/socratic/index.js +64 -0
- package/src/chains/socratic/index.spec.js +24 -0
- package/src/chains/sort/index.examples.js +3 -7
- package/src/chains/sort/index.js +65 -15
- package/src/chains/sort/index.spec.js +5 -8
- package/src/chains/sort/sort-result.json +16 -0
- package/src/chains/summary-map/README.md +9 -1
- package/src/chains/summary-map/index.examples.js +9 -2
- package/src/chains/summary-map/index.js +43 -25
- package/src/chains/summary-map/index.spec.js +78 -3
- package/src/chains/test/index.js +9 -13
- package/src/chains/test-advice/index.js +4 -5
- package/src/chains/themes/README.md +20 -0
- package/src/chains/themes/index.examples.js +17 -0
- package/src/chains/themes/index.js +28 -0
- package/src/chains/themes/index.spec.js +19 -0
- package/src/chains/veiled-variants/index.examples.js +18 -0
- package/src/chains/veiled-variants/index.js +107 -0
- package/src/chains/veiled-variants/index.spec.js +40 -0
- package/src/constants/common.js +0 -2
- package/src/constants/models.js +172 -0
- package/src/index.js +178 -18
- package/src/json-schemas/README.md +13 -0
- package/src/json-schemas/index.js +8 -14
- package/src/json-schemas/schema-dot-org-photograph.json +11 -5
- package/src/json-schemas/schema-dot-org-place.json +78 -5
- package/src/lib/README.md +26 -0
- package/src/lib/bulk-filter/README.md +22 -0
- package/src/lib/bulk-filter/index.examples.js +27 -0
- package/src/lib/bulk-filter/index.js +63 -0
- package/src/lib/bulk-filter/index.spec.js +38 -0
- package/src/lib/bulk-find/README.md +18 -0
- package/src/lib/bulk-find/index.examples.js +19 -0
- package/src/lib/bulk-find/index.js +30 -0
- package/src/lib/bulk-find/index.spec.js +41 -0
- package/src/lib/chatgpt/index.js +63 -43
- package/src/lib/combinations/index.js +30 -0
- package/src/lib/combinations/index.spec.js +23 -0
- package/src/lib/functional/index.js +28 -0
- package/src/lib/logger-service/index.js +32 -0
- package/src/lib/parse-js-parts/index.js +9 -21
- package/src/lib/parse-llm-list/README.md +39 -0
- package/src/lib/parse-llm-list/index.js +54 -0
- package/src/lib/parse-llm-list/index.spec.js +59 -0
- package/src/lib/path-aliases/index.js +1 -3
- package/src/lib/path-aliases/index.spec.js +2 -8
- package/src/lib/pave/index.js +4 -4
- package/src/lib/pave/index.spec.js +6 -3
- package/src/lib/prompt-cache/index.js +14 -10
- package/src/lib/retry/index.js +11 -8
- package/src/lib/ring-buffer/README.md +460 -0
- package/src/lib/ring-buffer/index.js +1074 -0
- package/src/lib/search-best-first/city-walk.spec.js +37 -0
- package/src/lib/search-best-first/index.js +42 -11
- package/src/lib/search-best-first/index.spec.js +35 -0
- package/src/lib/search-js-files/index.js +21 -41
- package/src/lib/search-js-files/scan-file.js +10 -21
- package/src/lib/shorten-text/index.js +2 -7
- package/src/lib/shorten-text/index.spec.js +3 -3
- package/src/lib/strip-response/index.js +2 -7
- package/src/lib/template-replace/index.js +23 -0
- package/src/lib/template-replace/index.spec.js +60 -0
- package/src/lib/to-date/index.js +11 -0
- package/src/lib/to-number/index.js +1 -1
- package/src/lib/transcribe/index.js +4 -4
- package/src/prompts/README.md +3 -1
- package/src/prompts/as-object-with-schema.js +3 -8
- package/src/prompts/as-schema-org-text.js +10 -2
- package/src/prompts/code-features.js +1 -5
- package/src/prompts/constants.js +27 -27
- package/src/prompts/generate-collection.js +1 -1
- package/src/prompts/intent.js +11 -16
- package/src/prompts/select-from-threshold.js +1 -2
- package/src/prompts/sort.js +4 -8
- package/src/prompts/style.js +4 -7
- package/src/prompts/wrap-list.js +1 -4
- package/src/services/llm-model/global-overrides.spec.js +432 -0
- package/src/services/llm-model/index.js +234 -40
- package/src/services/llm-model/model.js +2 -2
- package/src/services/llm-model/negotiate.spec.js +447 -0
- package/src/services/redis/index.js +70 -7
- package/src/test/setup.js +20 -0
- package/src/verblets/README.md +26 -0
- package/src/verblets/auto/index.examples.js +12 -9
- package/src/verblets/auto/index.js +10 -10
- package/src/verblets/auto/index.spec.js +4 -6
- package/src/verblets/bool/README.md +36 -0
- package/src/verblets/bool/index.examples.js +53 -1
- package/src/verblets/bool/index.js +6 -9
- package/src/verblets/bool/index.spec.js +1 -3
- package/src/verblets/central-tendency/README.md +166 -0
- package/src/verblets/central-tendency/central-tendency-result.json +24 -0
- package/src/verblets/central-tendency/index.examples.js +196 -0
- package/src/verblets/central-tendency/index.js +171 -0
- package/src/verblets/central-tendency/index.spec.js +148 -0
- package/src/verblets/enum/index.examples.js +1 -4
- package/src/verblets/enum/index.js +7 -4
- package/src/verblets/expect/README.md +64 -0
- package/src/verblets/expect/index.examples.js +109 -0
- package/src/verblets/expect/index.js +75 -0
- package/src/verblets/expect/index.spec.js +127 -0
- package/src/verblets/intent/index.examples.js +84 -1
- package/src/verblets/intent/index.js +56 -68
- package/src/verblets/intersection/README.md +16 -0
- package/src/verblets/intersection/index.examples.js +89 -0
- package/src/verblets/intersection/index.js +84 -0
- package/src/verblets/intersection/index.spec.js +60 -0
- package/src/verblets/intersection/intersection-result.json +16 -0
- package/src/verblets/list-expand/README.md +10 -0
- package/src/verblets/list-expand/index.examples.js +14 -0
- package/src/verblets/list-expand/index.js +104 -0
- package/src/verblets/list-expand/index.spec.js +18 -0
- package/src/verblets/list-expand/list-expand-result.json +16 -0
- package/src/verblets/list-filter/README.md +22 -0
- package/src/verblets/list-filter/index.examples.js +26 -0
- package/src/verblets/list-filter/index.js +18 -0
- package/src/verblets/list-filter/index.spec.js +19 -0
- package/src/verblets/list-find/README.md +11 -0
- package/src/verblets/list-find/index.examples.js +15 -0
- package/src/verblets/list-find/index.js +17 -0
- package/src/verblets/list-find/index.spec.js +19 -0
- package/src/verblets/list-group/README.md +16 -0
- package/src/verblets/list-group/index.examples.js +16 -0
- package/src/verblets/list-group/index.js +112 -0
- package/src/verblets/list-group/index.spec.js +35 -0
- package/src/verblets/list-group/list-group-result.json +16 -0
- package/src/verblets/list-map/README.md +11 -0
- package/src/verblets/list-map/index.examples.js +15 -0
- package/src/verblets/list-map/index.js +26 -0
- package/src/verblets/list-map/index.spec.js +17 -0
- package/src/verblets/list-reduce/README.md +10 -0
- package/src/verblets/list-reduce/index.examples.js +14 -0
- package/src/verblets/list-reduce/index.js +21 -0
- package/src/verblets/list-reduce/index.spec.js +27 -0
- package/src/verblets/list-reduce/index.spec.jsx +27 -0
- package/src/verblets/name/README.md +15 -0
- package/src/verblets/name/index.examples.js +28 -0
- package/src/verblets/name/index.js +19 -0
- package/src/verblets/name/index.spec.js +33 -0
- package/src/verblets/name-similar-to/README.md +26 -0
- package/src/verblets/name-similar-to/index.examples.js +18 -0
- package/src/verblets/name-similar-to/index.js +20 -0
- package/src/verblets/name-similar-to/index.spec.js +13 -0
- package/src/verblets/number/index.examples.js +173 -7
- package/src/verblets/number/index.js +5 -2
- package/src/verblets/number/index.spec.js +1 -3
- package/src/verblets/number-with-units/index.examples.js +5 -1
- package/src/verblets/number-with-units/index.js +74 -9
- package/src/verblets/number-with-units/number-with-units-result.json +23 -0
- package/src/verblets/schema-org/index.examples.js +2 -7
- package/src/verblets/schema-org/index.js +32 -3
- package/src/verblets/sentiment/README.md +10 -0
- package/src/verblets/sentiment/index.examples.js +20 -0
- package/src/verblets/sentiment/index.js +9 -0
- package/src/verblets/sentiment/index.spec.js +20 -0
- package/src/verblets/to-object/index.js +10 -15
- package/src/verblets/to-object/index.spec.js +1 -4
- package/.eslintrc.json +0 -42
- package/docs/README.md +0 -41
- package/docs/babel.config.js +0 -3
- package/docs/blog/2019-05-28-first-blog-post.md +0 -12
- package/docs/blog/2019-05-29-long-blog-post.md +0 -44
- package/docs/blog/2021-08-01-mdx-blog-post.mdx +0 -20
- package/docs/blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg +0 -0
- package/docs/blog/2021-08-26-welcome/index.md +0 -25
- package/docs/blog/authors.yml +0 -17
- package/docs/docs/api/bool.md +0 -74
- package/docs/docs/api/search.md +0 -51
- package/docs/docs/intro.md +0 -47
- package/docs/docs/tutorial-basics/_category_.json +0 -8
- package/docs/docs/tutorial-basics/congratulations.md +0 -23
- package/docs/docs/tutorial-basics/create-a-blog-post.md +0 -34
- package/docs/docs/tutorial-basics/create-a-document.md +0 -57
- package/docs/docs/tutorial-basics/create-a-page.md +0 -43
- package/docs/docs/tutorial-basics/deploy-your-site.md +0 -31
- package/docs/docs/tutorial-basics/markdown-features.mdx +0 -152
- package/docs/docs/tutorial-extras/_category_.json +0 -7
- package/docs/docs/tutorial-extras/img/docsVersionDropdown.png +0 -0
- package/docs/docs/tutorial-extras/img/localeDropdown.png +0 -0
- package/docs/docs/tutorial-extras/manage-docs-versions.md +0 -55
- package/docs/docs/tutorial-extras/translate-your-site.md +0 -88
- package/docs/docusaurus.config.js +0 -120
- package/docs/package.json +0 -44
- package/docs/sidebars.js +0 -31
- package/docs/src/components/HomepageFeatures/index.js +0 -61
- package/docs/src/components/HomepageFeatures/styles.module.css +0 -11
- package/docs/src/css/custom.css +0 -30
- package/docs/src/pages/index.js +0 -43
- package/docs/src/pages/index.module.css +0 -23
- package/docs/src/pages/markdown-page.md +0 -7
- package/docs/static/.nojekyll +0 -0
- package/docs/static/img/docusaurus-social-card.jpg +0 -0
- package/docs/static/img/docusaurus.png +0 -0
- package/docs/static/img/favicon.ico +0 -0
- package/docs/static/img/logo.svg +0 -1
- package/docs/static/img/undraw_docusaurus_mountain.svg +0 -171
- package/docs/static/img/undraw_docusaurus_react.svg +0 -170
- package/docs/static/img/undraw_docusaurus_tree.svg +0 -40
- package/src/constants/openai.js +0 -65
- /package/{.vite.config.examples.js → .vitest.config.examples.js} +0 -0
- /package/{.vite.config.js → .vitest.config.js} +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { longTestTimeout } from '../../constants/common.js';
|
|
3
|
+
import { dismantle } from './index.js';
|
|
4
|
+
|
|
5
|
+
const examples = [
|
|
6
|
+
{
|
|
7
|
+
name: 'Basic usage',
|
|
8
|
+
inputs: { text: 'test' },
|
|
9
|
+
want: { result: {} },
|
|
10
|
+
},
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
describe('Dismantle chain', () => {
|
|
14
|
+
examples.forEach((example) => {
|
|
15
|
+
it(
|
|
16
|
+
example.name,
|
|
17
|
+
async () => {
|
|
18
|
+
const result = await dismantle(example.inputs.text);
|
|
19
|
+
|
|
20
|
+
if (example.want.typeOfResult) {
|
|
21
|
+
expect(JSON.stringify(result.tree)).toStrictEqual(JSON.stringify(example.want.result));
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
longTestTimeout
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
/* eslint-disable no-await-in-loop */
|
|
2
|
-
|
|
3
1
|
import { v4 as uuid } from 'uuid';
|
|
4
2
|
|
|
5
3
|
import chatGPT from '../../lib/chatgpt/index.js';
|
|
6
|
-
import {
|
|
7
|
-
constants as promptConstants,
|
|
8
|
-
outputSuccinctNames,
|
|
9
|
-
} from '../../prompts/index.js';
|
|
4
|
+
import { outputSuccinctNames, constants as promptConstants } from '../../prompts/index.js';
|
|
10
5
|
import modelService from '../../services/llm-model/index.js';
|
|
11
6
|
import toObject from '../../verblets/to-object/index.js';
|
|
12
7
|
|
|
@@ -82,15 +77,11 @@ const defaultDecompose = async ({
|
|
|
82
77
|
focus,
|
|
83
78
|
rootName,
|
|
84
79
|
fixes,
|
|
85
|
-
model = modelService.
|
|
80
|
+
model = modelService.getBestPublicModel(),
|
|
86
81
|
} = {}) => {
|
|
87
82
|
const focusFormatted = focus ? `: ${focus}` : '';
|
|
88
83
|
|
|
89
|
-
const promptCreated = subComponentsPrompt(
|
|
90
|
-
`${name}${focusFormatted}`,
|
|
91
|
-
rootName,
|
|
92
|
-
fixes
|
|
93
|
-
);
|
|
84
|
+
const promptCreated = subComponentsPrompt(`${name}${focusFormatted}`, rootName, fixes);
|
|
94
85
|
const budget = model.budgetTokens(promptCreated);
|
|
95
86
|
return toObject(
|
|
96
87
|
await chatGPT(promptCreated, {
|
|
@@ -107,7 +98,7 @@ const defaultEnhance = async ({
|
|
|
107
98
|
name,
|
|
108
99
|
rootName,
|
|
109
100
|
fixes,
|
|
110
|
-
model = modelService.
|
|
101
|
+
model = modelService.getBestPublicModel(),
|
|
111
102
|
} = {}) => {
|
|
112
103
|
const promptCreated = componentOptionsPrompt(name, rootName, fixes);
|
|
113
104
|
const budget = model.budgetTokens(promptCreated);
|
|
@@ -207,6 +198,7 @@ const makeSubtree = async ({
|
|
|
207
198
|
|
|
208
199
|
const children = [];
|
|
209
200
|
for (const child of tree.children) {
|
|
201
|
+
// eslint-disable-next-line no-await-in-loop
|
|
210
202
|
const subtree = await makeSubtree({
|
|
211
203
|
tree: child,
|
|
212
204
|
rootName,
|
|
@@ -245,10 +237,7 @@ export const simplifyTree = (node) => {
|
|
|
245
237
|
};
|
|
246
238
|
|
|
247
239
|
class ChainTree {
|
|
248
|
-
constructor(
|
|
249
|
-
name,
|
|
250
|
-
{ decompose, enhance, makeId, enhanceFixes, decomposeFixes } = {}
|
|
251
|
-
) {
|
|
240
|
+
constructor(name, { decompose, enhance, makeId, enhanceFixes, decomposeFixes } = {}) {
|
|
252
241
|
this.rootName = name;
|
|
253
242
|
this.tree = {};
|
|
254
243
|
this.decompose = decompose;
|
|
@@ -25,8 +25,7 @@ describe('Dismantle chain', () => {
|
|
|
25
25
|
const result = await dismantle(example.inputs.text);
|
|
26
26
|
|
|
27
27
|
if (example.want.typeOfResult) {
|
|
28
|
-
expect(JSON.stringify(result.tree))
|
|
29
|
-
.toStrictEqual(JSON.stringify(example.want.result));
|
|
28
|
+
expect(JSON.stringify(result.tree)).toStrictEqual(JSON.stringify(example.want.result));
|
|
30
29
|
}
|
|
31
30
|
});
|
|
32
31
|
});
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# LLM Expect Chain
|
|
2
|
+
|
|
3
|
+
Advanced intelligent assertions with debugging features, environment variable modes, and structured results. This chain provides enhanced functionality beyond the basic [expect verblet](../../verblets/expect/).
|
|
4
|
+
|
|
5
|
+
## Why Use the Chain?
|
|
6
|
+
|
|
7
|
+
While the verblet provides simple pass/fail assertions, the chain offers:
|
|
8
|
+
|
|
9
|
+
- **Advanced Debugging**: Automatic code context analysis and intelligent advice
|
|
10
|
+
- **Environment Modes**: Different behaviors for development, testing, and CI
|
|
11
|
+
- **Structured Results**: Detailed information about assertions and failures
|
|
12
|
+
- **Stack Trace Integration**: Automatic detection of calling file and line
|
|
13
|
+
|
|
14
|
+
## Environment Variable Modes
|
|
15
|
+
|
|
16
|
+
Control behavior with the `LLM_EXPECT_MODE` environment variable:
|
|
17
|
+
|
|
18
|
+
### `none` (default)
|
|
19
|
+
Silent mode - returns structured results without throwing or logging
|
|
20
|
+
```bash
|
|
21
|
+
export LLM_EXPECT_MODE=none
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### `info`
|
|
25
|
+
Development mode - logs failures with context and advice to console
|
|
26
|
+
```bash
|
|
27
|
+
export LLM_EXPECT_MODE=info
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### `error`
|
|
31
|
+
CI/Testing mode - throws detailed errors on assertion failures
|
|
32
|
+
```bash
|
|
33
|
+
export LLM_EXPECT_MODE=error
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## API Reference
|
|
37
|
+
|
|
38
|
+
### Enhanced API: `expect(actual, expected?, constraint?)`
|
|
39
|
+
|
|
40
|
+
Returns a tuple `[passed, details]` with structured results:
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
import { expect } from './index.js';
|
|
44
|
+
|
|
45
|
+
const [passed, details] = await expect(
|
|
46
|
+
actualValue,
|
|
47
|
+
"Does this meet our quality standards?"
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
console.log(details);
|
|
51
|
+
/* Returns:
|
|
52
|
+
{
|
|
53
|
+
passed: false,
|
|
54
|
+
advice: "ISSUE: Content lacks specific examples...",
|
|
55
|
+
file: "/path/to/test.js",
|
|
56
|
+
line: 42
|
|
57
|
+
}
|
|
58
|
+
*/
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Simple API: `expect(actual, expected?, constraint?)`
|
|
62
|
+
|
|
63
|
+
Backward compatible with the verblet - returns boolean:
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
import expect from './index.js';
|
|
67
|
+
|
|
68
|
+
const result = await expect("hello", "hello");
|
|
69
|
+
// Returns: true
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Real-World Use Cases
|
|
73
|
+
|
|
74
|
+
### Content Quality Assurance with Debugging
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import { expect } from './index.js';
|
|
78
|
+
|
|
79
|
+
// Set development mode for detailed feedback
|
|
80
|
+
process.env.LLM_EXPECT_MODE = 'info';
|
|
81
|
+
|
|
82
|
+
const [passed, details] = await expect(
|
|
83
|
+
generatedCopy,
|
|
84
|
+
"Is this marketing copy professional, engaging, and free of grammatical errors?"
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
if (!passed) {
|
|
88
|
+
console.log(`Failed at ${details.file}:${details.line}`);
|
|
89
|
+
console.log(details.advice);
|
|
90
|
+
// ISSUE: Copy contains informal language and lacks call-to-action
|
|
91
|
+
// FIX: Replace casual phrases with professional alternatives and add clear CTA
|
|
92
|
+
// CONTEXT: Marketing copy should maintain professional tone while being engaging
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Advanced Features
|
|
97
|
+
|
|
98
|
+
### Automatic Code Context Analysis
|
|
99
|
+
|
|
100
|
+
The chain automatically:
|
|
101
|
+
- Detects the calling file and line number
|
|
102
|
+
- Reads 400 lines before and 100 lines after the assertion
|
|
103
|
+
- Provides this context to the LLM for better debugging advice
|
|
104
|
+
|
|
105
|
+
### Intelligent Advice Generation
|
|
106
|
+
|
|
107
|
+
Failed assertions generate structured advice:
|
|
108
|
+
- **ISSUE**: Brief description of why the assertion failed
|
|
109
|
+
- **FIX**: Specific actionable steps to resolve the issue
|
|
110
|
+
- **CONTEXT**: Additional context about the problem and root causes
|
|
111
|
+
|
|
112
|
+
### Environment-Aware Behavior
|
|
113
|
+
|
|
114
|
+
Different modes for different environments:
|
|
115
|
+
- **Development**: Rich console output with advice
|
|
116
|
+
- **Testing**: Detailed error throwing for CI/CD
|
|
117
|
+
- **Production**: Silent operation with structured results
|
|
118
|
+
|
|
119
|
+
## Best Practices
|
|
120
|
+
|
|
121
|
+
### **Write Specific Constraints**
|
|
122
|
+
```javascript
|
|
123
|
+
// ❌ Vague
|
|
124
|
+
await expect(text, "Is this good?");
|
|
125
|
+
|
|
126
|
+
// ✅ Specific
|
|
127
|
+
await expect(text, "Is this text grammatically correct, under 100 words, and written in a professional tone?");
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### **Use Structured Results**
|
|
131
|
+
```javascript
|
|
132
|
+
const [passed, details] = await expect(content, constraint);
|
|
133
|
+
|
|
134
|
+
// Access rich debugging information
|
|
135
|
+
console.log('File:', details.file);
|
|
136
|
+
console.log('Line:', details.line);
|
|
137
|
+
console.log('Advice:', details.advice);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### **Combine with Traditional Tests**
|
|
141
|
+
```javascript
|
|
142
|
+
// Traditional assertion for structure
|
|
143
|
+
expect(response).toHaveProperty('status');
|
|
144
|
+
expect(response.status).toBe(200);
|
|
145
|
+
|
|
146
|
+
// LLM assertion for content quality
|
|
147
|
+
const [passed] = await expect(
|
|
148
|
+
response.message,
|
|
149
|
+
"Is this error message helpful and user-friendly?"
|
|
150
|
+
);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Environment Modes
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
# Silent operation (default)
|
|
157
|
+
export LLM_EXPECT_MODE=none
|
|
158
|
+
|
|
159
|
+
# Log debugging advice on failures
|
|
160
|
+
export LLM_EXPECT_MODE=info
|
|
161
|
+
|
|
162
|
+
# Throw with detailed debugging advice
|
|
163
|
+
export LLM_EXPECT_MODE=error
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Best Practices
|
|
167
|
+
|
|
168
|
+
- **Be specific**: Use clear, detailed constraints
|
|
169
|
+
- **Test qualitatively**: Verify qualitative details with a clear yes/no answer
|
|
170
|
+
- **Use robust constraints**: Write criteria to pass under a wide range of input hallucinations. Assert cases that classical software can't. Tune the level of rigorousness to the model performing the eval.
|
|
171
|
+
- **Performance**: Remember this makes an LLM call - use judiciously
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { describe, expect, it, beforeAll, afterAll } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { expect as aiExpect } from './index.js';
|
|
4
|
+
import { longTestTimeout } from '../../constants/common.js';
|
|
5
|
+
|
|
6
|
+
const examples = [
|
|
7
|
+
{
|
|
8
|
+
inputs: {
|
|
9
|
+
actual: 'Hello world!',
|
|
10
|
+
constraint: 'Is this a greeting?',
|
|
11
|
+
},
|
|
12
|
+
want: { result: true },
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
inputs: {
|
|
16
|
+
actual: 'Goodbye cruel world',
|
|
17
|
+
constraint: 'Is this a greeting?',
|
|
18
|
+
},
|
|
19
|
+
want: { result: false },
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
inputs: {
|
|
23
|
+
actual: 'hello',
|
|
24
|
+
expected: 'hello',
|
|
25
|
+
},
|
|
26
|
+
want: { result: true },
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
inputs: {
|
|
30
|
+
actual: 'hello',
|
|
31
|
+
expected: 'goodbye',
|
|
32
|
+
},
|
|
33
|
+
want: { result: false },
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
inputs: {
|
|
37
|
+
actual: 'This is a well-written, professional email with proper grammar and clear intent.',
|
|
38
|
+
constraint: 'Is this text professional and grammatically correct?',
|
|
39
|
+
},
|
|
40
|
+
want: { result: true },
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
inputs: {
|
|
44
|
+
actual: { name: 'John Doe', age: 30, city: 'New York' },
|
|
45
|
+
constraint: 'Does this person data look realistic?',
|
|
46
|
+
},
|
|
47
|
+
want: { result: true },
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
describe('LLM Expect Chain', () => {
|
|
52
|
+
// Set environment mode to 'none' for all tests to avoid throwing
|
|
53
|
+
const originalMode = process.env.LLM_EXPECT_MODE;
|
|
54
|
+
|
|
55
|
+
beforeAll(() => {
|
|
56
|
+
process.env.LLM_EXPECT_MODE = 'none';
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
afterAll(() => {
|
|
60
|
+
if (originalMode !== undefined) {
|
|
61
|
+
process.env.LLM_EXPECT_MODE = originalMode;
|
|
62
|
+
} else {
|
|
63
|
+
delete process.env.LLM_EXPECT_MODE;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
examples.forEach((example) => {
|
|
68
|
+
const description = example.inputs.constraint
|
|
69
|
+
? `${JSON.stringify(example.inputs.actual).slice(0, 30)}... - ${example.inputs.constraint}`
|
|
70
|
+
: `${JSON.stringify(example.inputs.actual)} === ${JSON.stringify(example.inputs.expected)}`;
|
|
71
|
+
|
|
72
|
+
it(
|
|
73
|
+
description,
|
|
74
|
+
async () => {
|
|
75
|
+
const [result, details] = await aiExpect(
|
|
76
|
+
example.inputs.actual,
|
|
77
|
+
example.inputs.expected,
|
|
78
|
+
example.inputs.constraint
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect(result).toBe(example.want.result);
|
|
82
|
+
expect(details).toHaveProperty('passed', example.want.result);
|
|
83
|
+
expect(details).toHaveProperty('file');
|
|
84
|
+
expect(details).toHaveProperty('line');
|
|
85
|
+
expect(typeof details.line).toBe('number');
|
|
86
|
+
},
|
|
87
|
+
longTestTimeout
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it(
|
|
92
|
+
'should provide detailed debugging information on failure',
|
|
93
|
+
async () => {
|
|
94
|
+
const [result, details] = await aiExpect(
|
|
95
|
+
'This is clearly wrong content',
|
|
96
|
+
undefined,
|
|
97
|
+
'Is this a professional business email?'
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(result).toBe(false);
|
|
101
|
+
expect(details.passed).toBe(false);
|
|
102
|
+
expect(details.file).toBeDefined();
|
|
103
|
+
expect(details.line).toBeGreaterThan(0);
|
|
104
|
+
|
|
105
|
+
// In none mode, advice should be null for failures
|
|
106
|
+
expect(details.advice).toBeNull();
|
|
107
|
+
},
|
|
108
|
+
longTestTimeout
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
it(
|
|
112
|
+
'should handle complex business logic validation',
|
|
113
|
+
async () => {
|
|
114
|
+
const businessRecommendation =
|
|
115
|
+
'Increase marketing budget by 20% for Q4 to boost holiday sales and target demographics aged 25-45 through social media campaigns';
|
|
116
|
+
|
|
117
|
+
const [result, details] = await aiExpect(
|
|
118
|
+
businessRecommendation,
|
|
119
|
+
undefined,
|
|
120
|
+
'Is this recommendation specific, actionable, and includes measurable targets?'
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
expect(result).toBe(true);
|
|
124
|
+
expect(details.passed).toBe(true);
|
|
125
|
+
expect(details.file).toBeDefined();
|
|
126
|
+
},
|
|
127
|
+
longTestTimeout
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
it(
|
|
131
|
+
'should validate creative content quality',
|
|
132
|
+
async () => {
|
|
133
|
+
const storyOpening =
|
|
134
|
+
'Once upon a time, in a land far away, there lived a brave knight who embarked on a quest to save the kingdom from an ancient curse that had plagued the realm for centuries.';
|
|
135
|
+
|
|
136
|
+
const [result] = await aiExpect(
|
|
137
|
+
storyOpening,
|
|
138
|
+
undefined,
|
|
139
|
+
'Is this story opening engaging, sets up clear conflict, and follows good narrative structure?'
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(result).toBe(true);
|
|
143
|
+
},
|
|
144
|
+
longTestTimeout
|
|
145
|
+
);
|
|
146
|
+
});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import chatgpt from '../../lib/chatgpt/index.js';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get the calling file and line number from stack trace
|
|
7
|
+
*/
|
|
8
|
+
function getCallerInfo() {
|
|
9
|
+
const { stack } = new Error();
|
|
10
|
+
const lines = stack.split('\n');
|
|
11
|
+
|
|
12
|
+
// Find the first line that's not this file
|
|
13
|
+
for (let i = 2; i < lines.length; i++) {
|
|
14
|
+
const line = lines[i];
|
|
15
|
+
if (line.includes('at ') && !line.includes('expect')) {
|
|
16
|
+
const match = line.match(/at .* \((.+):(\d+):\d+\)/);
|
|
17
|
+
if (match) {
|
|
18
|
+
return { file: match[1], line: parseInt(match[2]) };
|
|
19
|
+
}
|
|
20
|
+
// Handle cases without parentheses
|
|
21
|
+
const simpleMatch = line.match(/at (.+):(\d+):\d+/);
|
|
22
|
+
if (simpleMatch) {
|
|
23
|
+
return { file: simpleMatch[1], line: parseInt(simpleMatch[2]) };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return { file: 'unknown', line: 0 };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Read code context around the assertion
|
|
32
|
+
*/
|
|
33
|
+
function getCodeContext(filePath, lineNumber) {
|
|
34
|
+
try {
|
|
35
|
+
if (!fs.existsSync(filePath)) return null;
|
|
36
|
+
|
|
37
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
38
|
+
const lines = content.split('\n');
|
|
39
|
+
|
|
40
|
+
// Get 400 lines before and 100 lines after
|
|
41
|
+
const start = Math.max(0, lineNumber - 400);
|
|
42
|
+
const end = Math.min(lines.length, lineNumber + 100);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
lines: lines.slice(start, end),
|
|
46
|
+
startLine: start + 1,
|
|
47
|
+
assertionLine: lineNumber,
|
|
48
|
+
};
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Generate intelligent advice for failed assertions
|
|
56
|
+
*/
|
|
57
|
+
async function generateAdvice(actual, expected, constraint, codeContext) {
|
|
58
|
+
const contextInfo = codeContext
|
|
59
|
+
? `
|
|
60
|
+
Code context around assertion (line ${codeContext.assertionLine}):
|
|
61
|
+
\`\`\`
|
|
62
|
+
${codeContext.lines.join('\n')}
|
|
63
|
+
\`\`\`
|
|
64
|
+
`
|
|
65
|
+
: '';
|
|
66
|
+
|
|
67
|
+
const prompt = `You are a debugging assistant helping with a failed LLM assertion.
|
|
68
|
+
|
|
69
|
+
ASSERTION DETAILS:
|
|
70
|
+
- Actual value: ${JSON.stringify(actual, null, 2)}
|
|
71
|
+
- Expected value: ${expected ? JSON.stringify(expected, null, 2) : 'N/A'}
|
|
72
|
+
- Constraint: ${constraint || 'N/A'}
|
|
73
|
+
|
|
74
|
+
${contextInfo}
|
|
75
|
+
|
|
76
|
+
Provide structured debugging advice in this format:
|
|
77
|
+
|
|
78
|
+
ISSUE: [Brief description of why the assertion failed]
|
|
79
|
+
FIX: [Specific actionable steps to resolve the issue]
|
|
80
|
+
CONTEXT: [Additional context about the problem and potential root causes]
|
|
81
|
+
|
|
82
|
+
Keep your response concise but actionable. Focus on practical solutions.`;
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
return await chatgpt(prompt);
|
|
86
|
+
} catch {
|
|
87
|
+
return 'Unable to generate debugging advice due to LLM error.';
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Enhanced LLM expectation with debugging features
|
|
93
|
+
*/
|
|
94
|
+
export async function expect(actual, expected, constraint) {
|
|
95
|
+
const mode = process.env.LLM_EXPECT_MODE || 'none';
|
|
96
|
+
const callerInfo = getCallerInfo();
|
|
97
|
+
|
|
98
|
+
// Build the assertion prompt
|
|
99
|
+
let prompt;
|
|
100
|
+
if (constraint && expected === undefined) {
|
|
101
|
+
// Constraint-only mode
|
|
102
|
+
prompt = `Given this constraint: "${constraint}"
|
|
103
|
+
|
|
104
|
+
Actual value: ${JSON.stringify(actual, null, 2)}
|
|
105
|
+
|
|
106
|
+
Does the actual value satisfy the constraint? Answer only "True" or "False".`;
|
|
107
|
+
} else if (constraint && expected !== undefined) {
|
|
108
|
+
// Both expected and constraint provided - use constraint
|
|
109
|
+
prompt = `Given this constraint: "${constraint}"
|
|
110
|
+
|
|
111
|
+
Actual value: ${JSON.stringify(actual, null, 2)}
|
|
112
|
+
Expected value: ${JSON.stringify(expected, null, 2)}
|
|
113
|
+
|
|
114
|
+
Does the actual value satisfy the constraint? Answer only "True" or "False".`;
|
|
115
|
+
} else if (expected !== undefined) {
|
|
116
|
+
// Expected value only
|
|
117
|
+
prompt = `Does the actual value strictly equal the expected value?
|
|
118
|
+
|
|
119
|
+
Actual: ${JSON.stringify(actual, null, 2)}
|
|
120
|
+
Expected: ${JSON.stringify(expected, null, 2)}
|
|
121
|
+
|
|
122
|
+
Answer only "True" or "False".`;
|
|
123
|
+
} else {
|
|
124
|
+
throw new Error('Either expected value or constraint must be provided');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
const response = await chatgpt(prompt);
|
|
129
|
+
const passed = response.trim().toLowerCase() === 'true';
|
|
130
|
+
|
|
131
|
+
// Prepare result structure
|
|
132
|
+
const result = {
|
|
133
|
+
passed,
|
|
134
|
+
advice: null,
|
|
135
|
+
file: callerInfo.file,
|
|
136
|
+
line: callerInfo.line,
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// Handle failure cases based on mode
|
|
140
|
+
if (!passed) {
|
|
141
|
+
if (mode === 'info' || mode === 'error') {
|
|
142
|
+
const codeContext = getCodeContext(callerInfo.file, callerInfo.line);
|
|
143
|
+
result.advice = await generateAdvice(actual, expected, constraint, codeContext);
|
|
144
|
+
|
|
145
|
+
const message = `LLM Assertion Failed at ${path.basename(callerInfo.file)}:${
|
|
146
|
+
callerInfo.line
|
|
147
|
+
}
|
|
148
|
+
${result.advice}`;
|
|
149
|
+
|
|
150
|
+
if (mode === 'error') {
|
|
151
|
+
throw new Error(message);
|
|
152
|
+
} else if (mode === 'info') {
|
|
153
|
+
console.info(message);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return [passed, result];
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (error.message.includes('LLM Assertion Failed')) {
|
|
161
|
+
throw error; // Re-throw our custom errors
|
|
162
|
+
}
|
|
163
|
+
throw new Error(`LLM expectation failed due to error: ${error.message}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Simple LLM expectation (backward compatibility)
|
|
169
|
+
*/
|
|
170
|
+
export default async function expectSimple(actual, expected, constraint) {
|
|
171
|
+
const [passed] = await expect(actual, expected, constraint);
|
|
172
|
+
return passed;
|
|
173
|
+
}
|