@far-world-labs/verblets 0.1.1

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.
Files changed (167) hide show
  1. package/.eslintrc.json +42 -0
  2. package/.husky/pre-commit +4 -0
  3. package/.release-it.json +9 -0
  4. package/.vite.config.examples.js +8 -0
  5. package/.vite.config.js +8 -0
  6. package/docker-compose.yml +7 -0
  7. package/docs/README.md +41 -0
  8. package/docs/babel.config.js +3 -0
  9. package/docs/blog/2019-05-28-first-blog-post.md +12 -0
  10. package/docs/blog/2019-05-29-long-blog-post.md +44 -0
  11. package/docs/blog/2021-08-01-mdx-blog-post.mdx +20 -0
  12. package/docs/blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg +0 -0
  13. package/docs/blog/2021-08-26-welcome/index.md +25 -0
  14. package/docs/blog/authors.yml +17 -0
  15. package/docs/docs/api/bool.md +74 -0
  16. package/docs/docs/api/search.md +51 -0
  17. package/docs/docs/intro.md +47 -0
  18. package/docs/docs/tutorial-basics/_category_.json +8 -0
  19. package/docs/docs/tutorial-basics/congratulations.md +23 -0
  20. package/docs/docs/tutorial-basics/create-a-blog-post.md +34 -0
  21. package/docs/docs/tutorial-basics/create-a-document.md +57 -0
  22. package/docs/docs/tutorial-basics/create-a-page.md +43 -0
  23. package/docs/docs/tutorial-basics/deploy-your-site.md +31 -0
  24. package/docs/docs/tutorial-basics/markdown-features.mdx +152 -0
  25. package/docs/docs/tutorial-extras/_category_.json +7 -0
  26. package/docs/docs/tutorial-extras/img/docsVersionDropdown.png +0 -0
  27. package/docs/docs/tutorial-extras/img/localeDropdown.png +0 -0
  28. package/docs/docs/tutorial-extras/manage-docs-versions.md +55 -0
  29. package/docs/docs/tutorial-extras/translate-your-site.md +88 -0
  30. package/docs/docusaurus.config.js +120 -0
  31. package/docs/package.json +44 -0
  32. package/docs/sidebars.js +31 -0
  33. package/docs/src/components/HomepageFeatures/index.js +61 -0
  34. package/docs/src/components/HomepageFeatures/styles.module.css +11 -0
  35. package/docs/src/css/custom.css +30 -0
  36. package/docs/src/pages/index.js +43 -0
  37. package/docs/src/pages/index.module.css +23 -0
  38. package/docs/src/pages/markdown-page.md +7 -0
  39. package/docs/static/.nojekyll +0 -0
  40. package/docs/static/img/docusaurus-social-card.jpg +0 -0
  41. package/docs/static/img/docusaurus.png +0 -0
  42. package/docs/static/img/favicon.ico +0 -0
  43. package/docs/static/img/logo.svg +1 -0
  44. package/docs/static/img/undraw_docusaurus_mountain.svg +171 -0
  45. package/docs/static/img/undraw_docusaurus_react.svg +170 -0
  46. package/docs/static/img/undraw_docusaurus_tree.svg +40 -0
  47. package/package.json +75 -0
  48. package/scripts/generate-chain/index.js +111 -0
  49. package/scripts/generate-lib/index.js +68 -0
  50. package/scripts/generate-test/index.js +111 -0
  51. package/scripts/generate-verblet/README.md +17 -0
  52. package/scripts/generate-verblet/index.js +110 -0
  53. package/scripts/run.sh +15 -0
  54. package/scripts/runner/index.js +30 -0
  55. package/scripts/simple-editor/README.md +34 -0
  56. package/scripts/simple-editor/index.js +68 -0
  57. package/scripts/summarize-files/index.js +46 -0
  58. package/src/chains/dismantle/dismantle.examples.js +0 -0
  59. package/src/chains/dismantle/index.examples.js +30 -0
  60. package/src/chains/dismantle/index.js +314 -0
  61. package/src/chains/dismantle/index.spec.js +33 -0
  62. package/src/chains/list/index.examples.js +72 -0
  63. package/src/chains/list/index.js +161 -0
  64. package/src/chains/list/index.spec.js +68 -0
  65. package/src/chains/list/schema.json +24 -0
  66. package/src/chains/questions/index.examples.js +68 -0
  67. package/src/chains/questions/index.js +136 -0
  68. package/src/chains/questions/index.spec.js +29 -0
  69. package/src/chains/scan-js/index.js +119 -0
  70. package/src/chains/sort/index.examples.js +40 -0
  71. package/src/chains/sort/index.js +113 -0
  72. package/src/chains/sort/index.spec.js +115 -0
  73. package/src/chains/summary-map/README.md +33 -0
  74. package/src/chains/summary-map/index.examples.js +57 -0
  75. package/src/chains/summary-map/index.js +208 -0
  76. package/src/chains/summary-map/index.spec.js +78 -0
  77. package/src/chains/test/index.js +118 -0
  78. package/src/chains/test-advice/index.js +36 -0
  79. package/src/constants/common.js +9 -0
  80. package/src/constants/messages.js +3 -0
  81. package/src/constants/openai.js +65 -0
  82. package/src/index.js +33 -0
  83. package/src/json-schemas/cars-test.json +11 -0
  84. package/src/json-schemas/index.js +18 -0
  85. package/src/json-schemas/intent.json +38 -0
  86. package/src/json-schemas/schema-dot-org-photograph.json +127 -0
  87. package/src/json-schemas/schema-dot-org-place.json +56 -0
  88. package/src/lib/any-signal/index.js +28 -0
  89. package/src/lib/chatgpt/index.js +143 -0
  90. package/src/lib/editor/index.js +31 -0
  91. package/src/lib/parse-js-parts/index.js +333 -0
  92. package/src/lib/parse-js-parts/index.spec.js +156 -0
  93. package/src/lib/path-aliases/index.js +39 -0
  94. package/src/lib/path-aliases/index.spec.js +70 -0
  95. package/src/lib/pave/index.js +34 -0
  96. package/src/lib/pave/index.spec.js +73 -0
  97. package/src/lib/prompt-cache/index.js +46 -0
  98. package/src/lib/retry/index.js +63 -0
  99. package/src/lib/retry/index.spec.js +86 -0
  100. package/src/lib/search-best-first/index.js +66 -0
  101. package/src/lib/search-js-files/code-features-property-definitions.json +123 -0
  102. package/src/lib/search-js-files/index.examples.js +22 -0
  103. package/src/lib/search-js-files/index.js +158 -0
  104. package/src/lib/search-js-files/index.spec.js +34 -0
  105. package/src/lib/search-js-files/scan-file.js +253 -0
  106. package/src/lib/shorten-text/index.js +30 -0
  107. package/src/lib/shorten-text/index.spec.js +68 -0
  108. package/src/lib/strip-numeric/index.js +5 -0
  109. package/src/lib/strip-response/index.js +35 -0
  110. package/src/lib/timed-abort-controller/index.js +41 -0
  111. package/src/lib/to-bool/index.js +8 -0
  112. package/src/lib/to-enum/index.js +14 -0
  113. package/src/lib/to-number/index.js +12 -0
  114. package/src/lib/to-number-with-units/index.js +51 -0
  115. package/src/lib/transcribe/index.js +61 -0
  116. package/src/prompts/README.md +15 -0
  117. package/src/prompts/as-enum.js +5 -0
  118. package/src/prompts/as-json-schema.js +9 -0
  119. package/src/prompts/as-object-with-schema.js +31 -0
  120. package/src/prompts/as-schema-org-text.js +17 -0
  121. package/src/prompts/as-schema-org-type.js +1 -0
  122. package/src/prompts/blog-post.js +7 -0
  123. package/src/prompts/code-features.js +28 -0
  124. package/src/prompts/constants.js +101 -0
  125. package/src/prompts/features-json-schema.js +27 -0
  126. package/src/prompts/generate-collection.js +26 -0
  127. package/src/prompts/generate-list.js +48 -0
  128. package/src/prompts/generate-questions.js +19 -0
  129. package/src/prompts/index.js +20 -0
  130. package/src/prompts/intent.js +66 -0
  131. package/src/prompts/output-succinct-names.js +3 -0
  132. package/src/prompts/select-from-threshold.js +18 -0
  133. package/src/prompts/sort.js +35 -0
  134. package/src/prompts/style.js +41 -0
  135. package/src/prompts/summarize.js +13 -0
  136. package/src/prompts/token-budget.js +3 -0
  137. package/src/prompts/wrap-list.js +14 -0
  138. package/src/prompts/wrap-variable.js +36 -0
  139. package/src/services/llm-model/index.js +114 -0
  140. package/src/services/llm-model/model.js +21 -0
  141. package/src/services/redis/index.js +84 -0
  142. package/src/verblets/auto/index.examples.js +28 -0
  143. package/src/verblets/auto/index.js +28 -0
  144. package/src/verblets/auto/index.spec.js +34 -0
  145. package/src/verblets/bool/index.examples.js +28 -0
  146. package/src/verblets/bool/index.js +28 -0
  147. package/src/verblets/bool/index.schema.json +14 -0
  148. package/src/verblets/bool/index.spec.js +35 -0
  149. package/src/verblets/enum/index.examples.js +33 -0
  150. package/src/verblets/enum/index.js +15 -0
  151. package/src/verblets/enum/index.spec.js +35 -0
  152. package/src/verblets/intent/index.examples.js +51 -0
  153. package/src/verblets/intent/index.js +72 -0
  154. package/src/verblets/intent/index.spec.js +31 -0
  155. package/src/verblets/number/index.examples.js +33 -0
  156. package/src/verblets/number/index.js +22 -0
  157. package/src/verblets/number/index.spec.js +35 -0
  158. package/src/verblets/number-with-units/index.examples.js +34 -0
  159. package/src/verblets/number-with-units/index.js +19 -0
  160. package/src/verblets/number-with-units/index.spec.js +46 -0
  161. package/src/verblets/schema-org/index.examples.js +56 -0
  162. package/src/verblets/schema-org/index.js +8 -0
  163. package/src/verblets/schema-org/index.spec.js +39 -0
  164. package/src/verblets/to-object/README.md +38 -0
  165. package/src/verblets/to-object/index.examples.js +29 -0
  166. package/src/verblets/to-object/index.js +136 -0
  167. package/src/verblets/to-object/index.spec.js +74 -0
@@ -0,0 +1,127 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://example.com/schemas/photograph.json",
4
+ "type": "object",
5
+ "properties": {
6
+ "@context": {
7
+ "type": "string",
8
+ "pattern": "^http[s]?://schema.org$"
9
+ },
10
+ "@type": {
11
+ "type": "string",
12
+ "pattern": "^Photograph$"
13
+ },
14
+ "name": {
15
+ "type": "string"
16
+ },
17
+ "caption": {
18
+ "type": "string"
19
+ },
20
+ "description": {
21
+ "type": "string"
22
+ },
23
+ "keywords": {
24
+ "type": "array",
25
+ "items": {
26
+ "type": "string"
27
+ }
28
+ },
29
+ "thumbnail": {
30
+ "type": "object",
31
+ "properties": {
32
+ "@type": {
33
+ "type": "string",
34
+ "pattern": "^ImageObject$"
35
+ },
36
+ "url": {
37
+ "type": "string"
38
+ },
39
+ "width": {
40
+ "type": "integer",
41
+ "minimum": 1
42
+ },
43
+ "height": {
44
+ "type": "integer",
45
+ "minimum": 1
46
+ }
47
+ },
48
+ "required": ["url"]
49
+ },
50
+ "image": {
51
+ "type": "object",
52
+ "properties": {
53
+ "@type": {
54
+ "type": "string",
55
+ "pattern": "^ImageObject$"
56
+ },
57
+ "url": {
58
+ "type": "string"
59
+ },
60
+ "width": {
61
+ "type": "integer",
62
+ "minimum": 1
63
+ },
64
+ "height": {
65
+ "type": "integer",
66
+ "minimum": 1
67
+ }
68
+ },
69
+ "required": ["url"]
70
+ },
71
+ "contentLocation": {
72
+ "type": "object",
73
+ "properties": {
74
+ "@type": {
75
+ "type": "string",
76
+ "pattern": "^Place$"
77
+ },
78
+ "name": {
79
+ "type": "string"
80
+ },
81
+ "address": {
82
+ "type": "object",
83
+ "properties": {
84
+ "@type": {
85
+ "type": "string",
86
+ "pattern": "^PostalAddress$"
87
+ },
88
+ "streetAddress": {
89
+ "type": "string"
90
+ },
91
+ "addressLocality": {
92
+ "type": "string"
93
+ },
94
+ "addressRegion": {
95
+ "type": "string"
96
+ },
97
+ "postalCode": {
98
+ "type": "string"
99
+ },
100
+ "addressCountry": {
101
+ "type": "string"
102
+ }
103
+ },
104
+ "required": ["streetAddress", "addressLocality", "addressRegion", "postalCode", "addressCountry"]
105
+ },
106
+ "geo": {
107
+ "type": "object",
108
+ "properties": {
109
+ "@type": {
110
+ "type": "string",
111
+ "pattern": "^GeoCoordinates$"
112
+ },
113
+ "latitude": {
114
+ "type": "number"
115
+ },
116
+ "longitude": {
117
+ "type": "number"
118
+ }
119
+ },
120
+ "required": ["latitude", "longitude"]
121
+ }
122
+ },
123
+ "required": ["name"]
124
+ }
125
+ },
126
+ "required": ["@context", "@type", "name"]
127
+ }
@@ -0,0 +1,56 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "type": "object",
4
+ "properties": {
5
+ "@type": {
6
+ "type": "string",
7
+ "enum": ["Place", "City"]
8
+ },
9
+ "name": {
10
+ "type": "string"
11
+ },
12
+ "address": {
13
+ "type": "object",
14
+ "properties": {
15
+ "@type": {
16
+ "type": "string",
17
+ "enum": ["PostalAddress"]
18
+ },
19
+ "streetAddress": {
20
+ "type": "string"
21
+ },
22
+ "addressLocality": {
23
+ "type": "string"
24
+ },
25
+ "addressRegion": {
26
+ "type": "string"
27
+ },
28
+ "postalCode": {
29
+ "type": "string"
30
+ },
31
+ "addressCountry": {
32
+ "type": "string"
33
+ }
34
+ },
35
+ "additionalProperties": true
36
+ },
37
+ "geo": {
38
+ "type": "object",
39
+ "properties": {
40
+ "@type": {
41
+ "type": "string",
42
+ "enum": ["GeoCoordinates"]
43
+ },
44
+ "latitude": {
45
+ "type": "number"
46
+ },
47
+ "longitude": {
48
+ "type": "number"
49
+ }
50
+ },
51
+ "additionalProperties": true
52
+ }
53
+ },
54
+ "additionalProperties": true,
55
+ "required": ["@type", "name"]
56
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Creates a new AbortSignal that is triggered when any of the given signals are aborted.
3
+ *
4
+ * @param {AbortSignal[]} signals - An array of AbortSignal instances to listen to.
5
+ * @returns {AbortSignal} - A new AbortSignal that is aborted when any of the input signals are aborted.
6
+ *
7
+ * @example
8
+ * const abortController1 = new AbortController();
9
+ * const abortController2 = new AbortController();
10
+ * const combinedSignal = anySignal([abortController1.signal, abortController2.signal]);
11
+ * fetch('https://example.com', { signal: combinedSignal });
12
+ * abortController1.abort(); // This will abort the fetch operation
13
+ */
14
+ export default (signalsInitial) => {
15
+ const signals = signalsInitial.filter((s) => s);
16
+
17
+ const controller = new AbortController();
18
+ for (const signal of signals) {
19
+ if (signal.aborted) {
20
+ controller.abort();
21
+ break;
22
+ }
23
+ signal.addEventListener('abort', () => {
24
+ controller.abort();
25
+ });
26
+ }
27
+ return controller.signal;
28
+ };
@@ -0,0 +1,143 @@
1
+ import fetch from 'node-fetch';
2
+
3
+ import {
4
+ apiKey,
5
+ apiUrl,
6
+ debugPromptGlobally,
7
+ debugPromptGloballyIfChanged,
8
+ debugResultGlobally,
9
+ debugResultGloballyIfChanged,
10
+ } from '../../constants/openai.js';
11
+ import anySignal from '../any-signal/index.js';
12
+ import {
13
+ get as getPromptResult,
14
+ set as setPromptResult,
15
+ } from '../prompt-cache/index.js';
16
+ import TimedAbortController from '../timed-abort-controller/index.js';
17
+ import modelService from '../../services/llm-model/index.js';
18
+ import { getClient as getRedis } from '../../services/redis/index.js';
19
+
20
+ const shapeOutputDefault = (result) => {
21
+ // GPT-4
22
+ if (result.choices[0].message.tool_calls?.length) {
23
+ const toolCall = result.choices[0].message.tool_calls[0]
24
+ return {
25
+ name: toolCall['function'].name,
26
+ arguments: JSON.parse(
27
+ toolCall['function'].arguments
28
+ ),
29
+ result: result.choices[0].message.tool_calls[0].function
30
+ };
31
+ }
32
+ if (result.choices[0].message) {
33
+ return result.choices[0].message.content.trim();
34
+ }
35
+ return result.choices[0].text.trim();
36
+ };
37
+
38
+ const onBeforeRequestDefault = ({ debugPrompt, isCached, prompt }) => {
39
+ if (
40
+ debugPrompt ||
41
+ debugPromptGlobally ||
42
+ (debugPromptGloballyIfChanged && !isCached)
43
+ ) {
44
+ console.error(`+++ DEBUG PROMPT +++`);
45
+ console.error(prompt);
46
+ console.error('+++ DEBUG PROMPT END +++');
47
+ }
48
+ };
49
+
50
+ const onAfterRequestDefault = ({ debugResult, isCached, resultShaped }) => {
51
+ if (
52
+ debugResult ||
53
+ debugResultGlobally ||
54
+ (debugResultGloballyIfChanged && !isCached)
55
+ ) {
56
+ console.error(`+++ DEBUG RESULT +++`);
57
+ console.error(resultShaped);
58
+ console.error('+++ DEBUG RESULT END +++');
59
+ }
60
+ };
61
+
62
+ export const run = async (prompt, options = {}) => {
63
+ const {
64
+ abortSignal,
65
+ debugPrompt,
66
+ debugResult,
67
+ forceQuery,
68
+ modelOptions = {},
69
+ onAfterRequest = onAfterRequestDefault,
70
+ onBeforeRequest = onBeforeRequestDefault,
71
+ shapeOutput = shapeOutputDefault,
72
+ } = options;
73
+
74
+ debugger;
75
+
76
+ const modelFound = modelService.getModel(modelOptions.modelName);
77
+
78
+ const requestConfig = modelService.getRequestConfig({
79
+ prompt,
80
+ ...modelOptions,
81
+ });
82
+
83
+ const cache = await getRedis();
84
+ const { result: cacheResult } = await getPromptResult(cache, requestConfig);
85
+
86
+ onBeforeRequest({
87
+ isCached: !!cacheResult,
88
+ debugPrompt,
89
+ prompt,
90
+ requestConfig,
91
+ });
92
+
93
+ let result = cacheResult;
94
+ if (!cacheResult || forceQuery) {
95
+ const timeoutController = new TimedAbortController(
96
+ modelService.getModel(modelOptions.modelName).requestTimeout
97
+ );
98
+
99
+ // console.log(requestConfig, `${apiUrl}${modelFound.endpoint}`)
100
+
101
+ const response = await fetch(`${apiUrl}${modelFound.endpoint}`, {
102
+ method: 'POST',
103
+ headers: {
104
+ Authorization: `Bearer ${apiKey}`,
105
+ 'Content-Type': 'application/json',
106
+ },
107
+ body: JSON.stringify(requestConfig),
108
+ signal: anySignal([abortSignal, timeoutController.signal]),
109
+ });
110
+
111
+ result = await response.json();
112
+
113
+ if (!response.ok) {
114
+ const vars = [
115
+ `status: ${response?.status}`,
116
+ `type: ${result?.error?.type}`,
117
+ ].join(', ');
118
+ throw new Error(
119
+ `Completions request [error]: ${result?.error?.message} (${vars})`
120
+ );
121
+ }
122
+
123
+ timeoutController.clearTimeout();
124
+
125
+ await setPromptResult(cache, requestConfig, result);
126
+ }
127
+
128
+ debugger;
129
+ const resultShaped = shapeOutput(result);
130
+
131
+ onAfterRequest({
132
+ debugResult,
133
+ isCached: !!cacheResult,
134
+ prompt,
135
+ requestConfig,
136
+ result,
137
+ resultShaped,
138
+ });
139
+
140
+ return resultShaped;
141
+ };
142
+
143
+ export default run;
@@ -0,0 +1,31 @@
1
+ import fs from 'node:fs/promises';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { spawn } from 'node:child_process';
5
+
6
+ export default async () => {
7
+ // Create a temporary file for editing
8
+ const tempFile = path.join(os.tmpdir(), 'verblets_editor.txt');
9
+ await fs.writeFile(tempFile, '');
10
+
11
+ // Determine the editor to use (use 'nano' as the default editor)
12
+ const editor = process.env.EDITOR || 'nano';
13
+
14
+ // Spawn the editor process
15
+ const editorProcess = spawn(editor, [tempFile], {
16
+ stdio: 'inherit',
17
+ shell: true,
18
+ });
19
+
20
+ return new Promise((resolve) => {
21
+ // Listen for the editor process exit event
22
+ editorProcess.on('exit', async () => {
23
+ // Read the content of the temporary file and echo it
24
+ const content = await fs.readFile(tempFile, 'utf-8');
25
+ resolve(content);
26
+
27
+ // Clean up the temporary file
28
+ await fs.unlink(tempFile);
29
+ });
30
+ });
31
+ };