langchain 0.0.194 → 0.0.196

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 (241) hide show
  1. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  2. package/dist/base_language/count_tokens.cjs +5 -70
  3. package/dist/base_language/count_tokens.d.ts +1 -10
  4. package/dist/base_language/count_tokens.js +1 -65
  5. package/dist/base_language/index.cjs +6 -196
  6. package/dist/base_language/index.d.ts +1 -111
  7. package/dist/base_language/index.js +1 -191
  8. package/dist/cache/base.cjs +15 -37
  9. package/dist/cache/base.d.ts +1 -20
  10. package/dist/cache/base.js +1 -33
  11. package/dist/cache/index.cjs +2 -46
  12. package/dist/cache/index.d.ts +1 -29
  13. package/dist/cache/index.js +1 -45
  14. package/dist/callbacks/base.cjs +3 -139
  15. package/dist/callbacks/base.d.ts +1 -266
  16. package/dist/callbacks/base.js +1 -126
  17. package/dist/callbacks/handlers/console.cjs +14 -221
  18. package/dist/callbacks/handlers/console.d.ts +1 -117
  19. package/dist/callbacks/handlers/console.js +1 -217
  20. package/dist/callbacks/handlers/initialize.cjs +15 -30
  21. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  22. package/dist/callbacks/handlers/initialize.js +1 -27
  23. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  24. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  25. package/dist/callbacks/handlers/log_stream.js +1 -289
  26. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  27. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  28. package/dist/callbacks/handlers/run_collector.js +1 -46
  29. package/dist/callbacks/handlers/tracer.cjs +15 -375
  30. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  31. package/dist/callbacks/handlers/tracer.js +1 -373
  32. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  33. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  34. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  35. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  36. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  37. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  38. package/dist/callbacks/manager.cjs +15 -676
  39. package/dist/callbacks/manager.d.ts +1 -180
  40. package/dist/callbacks/manager.js +1 -666
  41. package/dist/callbacks/promises.cjs +14 -42
  42. package/dist/callbacks/promises.d.ts +1 -11
  43. package/dist/callbacks/promises.js +1 -37
  44. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  45. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  46. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  47. package/dist/chains/openai_functions/structured_output.js +1 -1
  48. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  49. package/dist/chat_models/base.cjs +15 -296
  50. package/dist/chat_models/base.d.ts +1 -122
  51. package/dist/chat_models/base.js +1 -292
  52. package/dist/chat_models/bedrock/web.d.ts +1 -1
  53. package/dist/chat_models/fireworks.d.ts +1 -1
  54. package/dist/document.cjs +2 -24
  55. package/dist/document.d.ts +1 -12
  56. package/dist/document.js +1 -23
  57. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  58. package/dist/document_loaders/web/s3.d.ts +1 -1
  59. package/dist/embeddings/base.cjs +15 -22
  60. package/dist/embeddings/base.d.ts +1 -33
  61. package/dist/embeddings/base.js +1 -20
  62. package/dist/embeddings/cache_backed.cjs +2 -2
  63. package/dist/embeddings/cache_backed.js +1 -1
  64. package/dist/embeddings/hf.cjs +1 -2
  65. package/dist/embeddings/hf.js +1 -2
  66. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  67. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  68. package/dist/evaluation/qa/prompt.d.ts +2 -2
  69. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  70. package/dist/experimental/openai_assistant/index.cjs +2 -1
  71. package/dist/experimental/openai_assistant/index.d.ts +2 -1
  72. package/dist/experimental/openai_assistant/index.js +2 -1
  73. package/dist/experimental/openai_files/index.cjs +88 -0
  74. package/dist/experimental/openai_files/index.d.ts +79 -0
  75. package/dist/experimental/openai_files/index.js +84 -0
  76. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  77. package/dist/llms/base.cjs +15 -278
  78. package/dist/llms/base.d.ts +1 -115
  79. package/dist/llms/base.js +1 -275
  80. package/dist/llms/bedrock/web.d.ts +1 -1
  81. package/dist/llms/fireworks.d.ts +1 -1
  82. package/dist/load/import_constants.cjs +1 -0
  83. package/dist/load/import_constants.js +1 -0
  84. package/dist/load/import_map.cjs +4 -2
  85. package/dist/load/import_map.d.ts +2 -0
  86. package/dist/load/import_map.js +2 -0
  87. package/dist/load/index.cjs +7 -148
  88. package/dist/load/index.js +7 -148
  89. package/dist/load/map_keys.cjs +0 -24
  90. package/dist/load/map_keys.d.ts +0 -6
  91. package/dist/load/map_keys.js +1 -17
  92. package/dist/load/serializable.cjs +15 -178
  93. package/dist/load/serializable.d.ts +1 -66
  94. package/dist/load/serializable.js +1 -175
  95. package/dist/memory/base.cjs +17 -92
  96. package/dist/memory/base.d.ts +2 -68
  97. package/dist/memory/base.js +2 -87
  98. package/dist/output_parsers/openai_functions.cjs +2 -2
  99. package/dist/output_parsers/openai_functions.d.ts +1 -1
  100. package/dist/output_parsers/openai_functions.js +1 -1
  101. package/dist/output_parsers/regex.d.ts +1 -1
  102. package/dist/output_parsers/structured.d.ts +1 -1
  103. package/dist/prompts/base.cjs +8 -183
  104. package/dist/prompts/base.d.ts +3 -132
  105. package/dist/prompts/base.js +3 -178
  106. package/dist/prompts/chat.cjs +15 -477
  107. package/dist/prompts/chat.d.ts +1 -219
  108. package/dist/prompts/chat.js +1 -466
  109. package/dist/prompts/few_shot.cjs +15 -353
  110. package/dist/prompts/few_shot.d.ts +1 -192
  111. package/dist/prompts/few_shot.js +1 -350
  112. package/dist/prompts/index.cjs +3 -2
  113. package/dist/prompts/index.d.ts +2 -1
  114. package/dist/prompts/index.js +2 -1
  115. package/dist/prompts/pipeline.cjs +15 -142
  116. package/dist/prompts/pipeline.d.ts +1 -98
  117. package/dist/prompts/pipeline.js +1 -140
  118. package/dist/prompts/prompt.cjs +15 -146
  119. package/dist/prompts/prompt.d.ts +1 -92
  120. package/dist/prompts/prompt.js +1 -144
  121. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +15 -148
  122. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  123. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  124. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  125. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  126. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  127. package/dist/prompts/selectors/conditional.cjs +15 -74
  128. package/dist/prompts/selectors/conditional.d.ts +1 -63
  129. package/dist/prompts/selectors/conditional.js +1 -69
  130. package/dist/prompts/serde.cjs +15 -0
  131. package/dist/prompts/serde.d.ts +1 -43
  132. package/dist/prompts/serde.js +1 -1
  133. package/dist/prompts/template.cjs +14 -88
  134. package/dist/prompts/template.d.ts +1 -36
  135. package/dist/prompts/template.js +1 -83
  136. package/dist/retrievers/chaindesk.cjs +9 -1
  137. package/dist/retrievers/chaindesk.d.ts +3 -1
  138. package/dist/retrievers/chaindesk.js +9 -1
  139. package/dist/retrievers/self_query/vectara.cjs +138 -0
  140. package/dist/retrievers/self_query/vectara.d.ts +41 -0
  141. package/dist/retrievers/self_query/vectara.js +134 -0
  142. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  143. package/dist/runnables/index.d.ts +1 -0
  144. package/dist/runnables/index.js +1 -0
  145. package/dist/schema/document.cjs +3 -34
  146. package/dist/schema/document.d.ts +2 -29
  147. package/dist/schema/document.js +2 -32
  148. package/dist/schema/index.cjs +37 -612
  149. package/dist/schema/index.d.ts +11 -311
  150. package/dist/schema/index.js +8 -583
  151. package/dist/schema/output_parser.cjs +15 -309
  152. package/dist/schema/output_parser.d.ts +1 -173
  153. package/dist/schema/output_parser.js +1 -301
  154. package/dist/schema/retriever.cjs +15 -77
  155. package/dist/schema/retriever.d.ts +1 -43
  156. package/dist/schema/retriever.js +1 -75
  157. package/dist/schema/runnable/base.cjs +10 -1072
  158. package/dist/schema/runnable/base.d.ts +1 -356
  159. package/dist/schema/runnable/base.js +1 -1060
  160. package/dist/schema/runnable/branch.cjs +2 -131
  161. package/dist/schema/runnable/branch.d.ts +1 -94
  162. package/dist/schema/runnable/branch.js +1 -130
  163. package/dist/schema/runnable/config.cjs +0 -6
  164. package/dist/schema/runnable/config.d.ts +1 -3
  165. package/dist/schema/runnable/config.js +1 -4
  166. package/dist/schema/runnable/index.cjs +15 -16
  167. package/dist/schema/runnable/index.d.ts +1 -5
  168. package/dist/schema/runnable/index.js +1 -4
  169. package/dist/schema/runnable/passthrough.cjs +3 -113
  170. package/dist/schema/runnable/passthrough.d.ts +1 -72
  171. package/dist/schema/runnable/passthrough.js +1 -111
  172. package/dist/schema/runnable/router.cjs +2 -71
  173. package/dist/schema/runnable/router.d.ts +1 -29
  174. package/dist/schema/runnable/router.js +1 -70
  175. package/dist/schema/storage.cjs +15 -8
  176. package/dist/schema/storage.d.ts +1 -57
  177. package/dist/schema/storage.js +1 -6
  178. package/dist/tools/bingserpapi.d.ts +1 -1
  179. package/dist/tools/searchapi.d.ts +1 -1
  180. package/dist/tools/serpapi.d.ts +1 -1
  181. package/dist/tools/serper.d.ts +1 -1
  182. package/dist/util/async_caller.cjs +14 -128
  183. package/dist/util/async_caller.d.ts +1 -45
  184. package/dist/util/async_caller.js +1 -124
  185. package/dist/vectorstores/vectara.cjs +77 -7
  186. package/dist/vectorstores/vectara.d.ts +9 -3
  187. package/dist/vectorstores/vectara.js +54 -7
  188. package/experimental/openai_files.cjs +1 -0
  189. package/experimental/openai_files.d.ts +1 -0
  190. package/experimental/openai_files.js +1 -0
  191. package/package.json +27 -5
  192. package/retrievers/self_query/vectara.cjs +1 -0
  193. package/retrievers/self_query/vectara.d.ts +1 -0
  194. package/retrievers/self_query/vectara.js +1 -0
  195. package/runnables.cjs +1 -0
  196. package/runnables.d.ts +1 -0
  197. package/runnables.js +1 -0
  198. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  199. package/dist/util/@cfworker/json-schema/index.js +0 -1
  200. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  201. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  202. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  203. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  204. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  205. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  206. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  207. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  208. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  209. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  210. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  211. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  212. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  213. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  214. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  215. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  216. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  217. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  218. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  219. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  220. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  221. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  222. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  223. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  224. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  225. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  226. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  227. package/dist/util/fast-json-patch/index.cjs +0 -49
  228. package/dist/util/fast-json-patch/index.d.ts +0 -22
  229. package/dist/util/fast-json-patch/index.js +0 -16
  230. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  231. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  232. package/dist/util/fast-json-patch/src/core.js +0 -459
  233. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  234. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  235. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  236. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  237. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  238. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  239. package/dist/util/js-sha1/hash.cjs +0 -358
  240. package/dist/util/js-sha1/hash.d.ts +0 -1
  241. package/dist/util/js-sha1/hash.js +0 -355
@@ -1,150 +1,17 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LengthBasedExampleSelector = void 0;
4
- const base_js_1 = require("../base.cjs");
5
- /**
6
- * Calculates the length of a text based on the number of words and lines.
7
- */
8
- function getLengthBased(text) {
9
- return text.split(/\n| /).length;
10
- }
11
- /**
12
- * A specialized example selector that selects examples based on their
13
- * length, ensuring that the total length of the selected examples does
14
- * not exceed a specified maximum length.
15
- * @example
16
- * ```typescript
17
- * const exampleSelector = new LengthBasedExampleSelector(
18
- * [
19
- * { input: "happy", output: "sad" },
20
- * { input: "tall", output: "short" },
21
- * { input: "energetic", output: "lethargic" },
22
- * { input: "sunny", output: "gloomy" },
23
- * { input: "windy", output: "calm" },
24
- * ],
25
- * {
26
- * examplePrompt: new PromptTemplate({
27
- * inputVariables: ["input", "output"],
28
- * template: "Input: {input}\nOutput: {output}",
29
- * }),
30
- * maxLength: 25,
31
- * },
32
- * );
33
- * const dynamicPrompt = new FewShotPromptTemplate({
34
- * exampleSelector,
35
- * examplePrompt: new PromptTemplate({
36
- * inputVariables: ["input", "output"],
37
- * template: "Input: {input}\nOutput: {output}",
38
- * }),
39
- * prefix: "Give the antonym of every input",
40
- * suffix: "Input: {adjective}\nOutput:",
41
- * inputVariables: ["adjective"],
42
- * });
43
- * console.log(dynamicPrompt.format({ adjective: "big" }));
44
- * console.log(
45
- * dynamicPrompt.format({
46
- * adjective:
47
- * "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else",
48
- * }),
49
- * );
50
- * ```
51
- */
52
- class LengthBasedExampleSelector extends base_js_1.BaseExampleSelector {
53
- constructor(data) {
54
- super(data);
55
- Object.defineProperty(this, "examples", {
56
- enumerable: true,
57
- configurable: true,
58
- writable: true,
59
- value: []
60
- });
61
- Object.defineProperty(this, "examplePrompt", {
62
- enumerable: true,
63
- configurable: true,
64
- writable: true,
65
- value: void 0
66
- });
67
- Object.defineProperty(this, "getTextLength", {
68
- enumerable: true,
69
- configurable: true,
70
- writable: true,
71
- value: getLengthBased
72
- });
73
- Object.defineProperty(this, "maxLength", {
74
- enumerable: true,
75
- configurable: true,
76
- writable: true,
77
- value: 2048
78
- });
79
- Object.defineProperty(this, "exampleTextLengths", {
80
- enumerable: true,
81
- configurable: true,
82
- writable: true,
83
- value: []
84
- });
85
- this.examplePrompt = data.examplePrompt;
86
- this.maxLength = data.maxLength ?? 2048;
87
- this.getTextLength = data.getTextLength ?? getLengthBased;
88
- }
89
- /**
90
- * Adds an example to the list of examples and calculates its length.
91
- * @param example The example to be added.
92
- * @returns Promise that resolves when the example has been added and its length calculated.
93
- */
94
- async addExample(example) {
95
- this.examples.push(example);
96
- const stringExample = await this.examplePrompt.format(example);
97
- this.exampleTextLengths.push(this.getTextLength(stringExample));
98
- }
99
- /**
100
- * Calculates the lengths of the examples.
101
- * @param v Array of lengths of the examples.
102
- * @param values Instance of LengthBasedExampleSelector.
103
- * @returns Promise that resolves with an array of lengths of the examples.
104
- */
105
- async calculateExampleTextLengths(v, values) {
106
- if (v.length > 0) {
107
- return v;
108
- }
109
- const { examples, examplePrompt } = values;
110
- const stringExamples = await Promise.all(examples.map((eg) => examplePrompt.format(eg)));
111
- return stringExamples.map((eg) => this.getTextLength(eg));
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
112
7
  }
113
- /**
114
- * Selects examples until the total length of the selected examples
115
- * reaches the maxLength.
116
- * @param inputVariables The input variables for the examples.
117
- * @returns Promise that resolves with an array of selected examples.
118
- */
119
- async selectExamples(inputVariables) {
120
- const inputs = Object.values(inputVariables).join(" ");
121
- let remainingLength = this.maxLength - this.getTextLength(inputs);
122
- let i = 0;
123
- const examples = [];
124
- while (remainingLength > 0 && i < this.examples.length) {
125
- const newLength = remainingLength - this.exampleTextLengths[i];
126
- if (newLength < 0) {
127
- break;
128
- }
129
- else {
130
- examples.push(this.examples[i]);
131
- remainingLength = newLength;
132
- }
133
- i += 1;
134
- }
135
- return examples;
136
- }
137
- /**
138
- * Creates a new instance of LengthBasedExampleSelector and adds a list of
139
- * examples to it.
140
- * @param examples Array of examples to be added.
141
- * @param args Input parameters for the LengthBasedExampleSelector.
142
- * @returns Promise that resolves with a new instance of LengthBasedExampleSelector with the examples added.
143
- */
144
- static async fromExamples(examples, args) {
145
- const selector = new LengthBasedExampleSelector(args);
146
- await Promise.all(examples.map((eg) => selector.addExample(eg)));
147
- return selector;
148
- }
149
- }
150
- exports.LengthBasedExampleSelector = LengthBasedExampleSelector;
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("langchain-core/prompts/example_selector/length_based"), exports);
@@ -1,89 +1 @@
1
- import { Example } from "../../schema/index.js";
2
- import { BaseExampleSelector } from "../base.js";
3
- import { PromptTemplate } from "../prompt.js";
4
- /**
5
- * Interface for the input parameters of the LengthBasedExampleSelector
6
- * class.
7
- */
8
- export interface LengthBasedExampleSelectorInput {
9
- examplePrompt: PromptTemplate;
10
- maxLength?: number;
11
- getTextLength?: (text: string) => number;
12
- }
13
- /**
14
- * A specialized example selector that selects examples based on their
15
- * length, ensuring that the total length of the selected examples does
16
- * not exceed a specified maximum length.
17
- * @example
18
- * ```typescript
19
- * const exampleSelector = new LengthBasedExampleSelector(
20
- * [
21
- * { input: "happy", output: "sad" },
22
- * { input: "tall", output: "short" },
23
- * { input: "energetic", output: "lethargic" },
24
- * { input: "sunny", output: "gloomy" },
25
- * { input: "windy", output: "calm" },
26
- * ],
27
- * {
28
- * examplePrompt: new PromptTemplate({
29
- * inputVariables: ["input", "output"],
30
- * template: "Input: {input}\nOutput: {output}",
31
- * }),
32
- * maxLength: 25,
33
- * },
34
- * );
35
- * const dynamicPrompt = new FewShotPromptTemplate({
36
- * exampleSelector,
37
- * examplePrompt: new PromptTemplate({
38
- * inputVariables: ["input", "output"],
39
- * template: "Input: {input}\nOutput: {output}",
40
- * }),
41
- * prefix: "Give the antonym of every input",
42
- * suffix: "Input: {adjective}\nOutput:",
43
- * inputVariables: ["adjective"],
44
- * });
45
- * console.log(dynamicPrompt.format({ adjective: "big" }));
46
- * console.log(
47
- * dynamicPrompt.format({
48
- * adjective:
49
- * "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else",
50
- * }),
51
- * );
52
- * ```
53
- */
54
- export declare class LengthBasedExampleSelector extends BaseExampleSelector {
55
- protected examples: Example[];
56
- examplePrompt: PromptTemplate;
57
- getTextLength: (text: string) => number;
58
- maxLength: number;
59
- exampleTextLengths: number[];
60
- constructor(data: LengthBasedExampleSelectorInput);
61
- /**
62
- * Adds an example to the list of examples and calculates its length.
63
- * @param example The example to be added.
64
- * @returns Promise that resolves when the example has been added and its length calculated.
65
- */
66
- addExample(example: Example): Promise<void>;
67
- /**
68
- * Calculates the lengths of the examples.
69
- * @param v Array of lengths of the examples.
70
- * @param values Instance of LengthBasedExampleSelector.
71
- * @returns Promise that resolves with an array of lengths of the examples.
72
- */
73
- calculateExampleTextLengths(v: number[], values: LengthBasedExampleSelector): Promise<number[]>;
74
- /**
75
- * Selects examples until the total length of the selected examples
76
- * reaches the maxLength.
77
- * @param inputVariables The input variables for the examples.
78
- * @returns Promise that resolves with an array of selected examples.
79
- */
80
- selectExamples(inputVariables: Example): Promise<Example[]>;
81
- /**
82
- * Creates a new instance of LengthBasedExampleSelector and adds a list of
83
- * examples to it.
84
- * @param examples Array of examples to be added.
85
- * @param args Input parameters for the LengthBasedExampleSelector.
86
- * @returns Promise that resolves with a new instance of LengthBasedExampleSelector with the examples added.
87
- */
88
- static fromExamples(examples: Example[], args: LengthBasedExampleSelectorInput): Promise<LengthBasedExampleSelector>;
89
- }
1
+ export * from "langchain-core/prompts/example_selector/length_based";
@@ -1,146 +1 @@
1
- import { BaseExampleSelector } from "../base.js";
2
- /**
3
- * Calculates the length of a text based on the number of words and lines.
4
- */
5
- function getLengthBased(text) {
6
- return text.split(/\n| /).length;
7
- }
8
- /**
9
- * A specialized example selector that selects examples based on their
10
- * length, ensuring that the total length of the selected examples does
11
- * not exceed a specified maximum length.
12
- * @example
13
- * ```typescript
14
- * const exampleSelector = new LengthBasedExampleSelector(
15
- * [
16
- * { input: "happy", output: "sad" },
17
- * { input: "tall", output: "short" },
18
- * { input: "energetic", output: "lethargic" },
19
- * { input: "sunny", output: "gloomy" },
20
- * { input: "windy", output: "calm" },
21
- * ],
22
- * {
23
- * examplePrompt: new PromptTemplate({
24
- * inputVariables: ["input", "output"],
25
- * template: "Input: {input}\nOutput: {output}",
26
- * }),
27
- * maxLength: 25,
28
- * },
29
- * );
30
- * const dynamicPrompt = new FewShotPromptTemplate({
31
- * exampleSelector,
32
- * examplePrompt: new PromptTemplate({
33
- * inputVariables: ["input", "output"],
34
- * template: "Input: {input}\nOutput: {output}",
35
- * }),
36
- * prefix: "Give the antonym of every input",
37
- * suffix: "Input: {adjective}\nOutput:",
38
- * inputVariables: ["adjective"],
39
- * });
40
- * console.log(dynamicPrompt.format({ adjective: "big" }));
41
- * console.log(
42
- * dynamicPrompt.format({
43
- * adjective:
44
- * "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else",
45
- * }),
46
- * );
47
- * ```
48
- */
49
- export class LengthBasedExampleSelector extends BaseExampleSelector {
50
- constructor(data) {
51
- super(data);
52
- Object.defineProperty(this, "examples", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
56
- value: []
57
- });
58
- Object.defineProperty(this, "examplePrompt", {
59
- enumerable: true,
60
- configurable: true,
61
- writable: true,
62
- value: void 0
63
- });
64
- Object.defineProperty(this, "getTextLength", {
65
- enumerable: true,
66
- configurable: true,
67
- writable: true,
68
- value: getLengthBased
69
- });
70
- Object.defineProperty(this, "maxLength", {
71
- enumerable: true,
72
- configurable: true,
73
- writable: true,
74
- value: 2048
75
- });
76
- Object.defineProperty(this, "exampleTextLengths", {
77
- enumerable: true,
78
- configurable: true,
79
- writable: true,
80
- value: []
81
- });
82
- this.examplePrompt = data.examplePrompt;
83
- this.maxLength = data.maxLength ?? 2048;
84
- this.getTextLength = data.getTextLength ?? getLengthBased;
85
- }
86
- /**
87
- * Adds an example to the list of examples and calculates its length.
88
- * @param example The example to be added.
89
- * @returns Promise that resolves when the example has been added and its length calculated.
90
- */
91
- async addExample(example) {
92
- this.examples.push(example);
93
- const stringExample = await this.examplePrompt.format(example);
94
- this.exampleTextLengths.push(this.getTextLength(stringExample));
95
- }
96
- /**
97
- * Calculates the lengths of the examples.
98
- * @param v Array of lengths of the examples.
99
- * @param values Instance of LengthBasedExampleSelector.
100
- * @returns Promise that resolves with an array of lengths of the examples.
101
- */
102
- async calculateExampleTextLengths(v, values) {
103
- if (v.length > 0) {
104
- return v;
105
- }
106
- const { examples, examplePrompt } = values;
107
- const stringExamples = await Promise.all(examples.map((eg) => examplePrompt.format(eg)));
108
- return stringExamples.map((eg) => this.getTextLength(eg));
109
- }
110
- /**
111
- * Selects examples until the total length of the selected examples
112
- * reaches the maxLength.
113
- * @param inputVariables The input variables for the examples.
114
- * @returns Promise that resolves with an array of selected examples.
115
- */
116
- async selectExamples(inputVariables) {
117
- const inputs = Object.values(inputVariables).join(" ");
118
- let remainingLength = this.maxLength - this.getTextLength(inputs);
119
- let i = 0;
120
- const examples = [];
121
- while (remainingLength > 0 && i < this.examples.length) {
122
- const newLength = remainingLength - this.exampleTextLengths[i];
123
- if (newLength < 0) {
124
- break;
125
- }
126
- else {
127
- examples.push(this.examples[i]);
128
- remainingLength = newLength;
129
- }
130
- i += 1;
131
- }
132
- return examples;
133
- }
134
- /**
135
- * Creates a new instance of LengthBasedExampleSelector and adds a list of
136
- * examples to it.
137
- * @param examples Array of examples to be added.
138
- * @param args Input parameters for the LengthBasedExampleSelector.
139
- * @returns Promise that resolves with a new instance of LengthBasedExampleSelector with the examples added.
140
- */
141
- static async fromExamples(examples, args) {
142
- const selector = new LengthBasedExampleSelector(args);
143
- await Promise.all(examples.map((eg) => selector.addExample(eg)));
144
- return selector;
145
- }
146
- }
1
+ export * from "langchain-core/prompts/example_selector/length_based";
@@ -1,139 +1,17 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SemanticSimilarityExampleSelector = void 0;
4
- const document_js_1 = require("../../document.cjs");
5
- const base_js_1 = require("../base.cjs");
6
- function sortedValues(values) {
7
- return Object.keys(values)
8
- .sort()
9
- .map((key) => values[key]);
10
- }
11
- /**
12
- * Class that selects examples based on semantic similarity. It extends
13
- * the BaseExampleSelector class.
14
- * @example
15
- * ```typescript
16
- * const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
17
- * [
18
- * { input: "happy", output: "sad" },
19
- * { input: "tall", output: "short" },
20
- * { input: "energetic", output: "lethargic" },
21
- * { input: "sunny", output: "gloomy" },
22
- * { input: "windy", output: "calm" },
23
- * ],
24
- * new OpenAIEmbeddings(),
25
- * HNSWLib,
26
- * { k: 1 },
27
- * );
28
- * const dynamicPrompt = new FewShotPromptTemplate({
29
- * exampleSelector,
30
- * examplePrompt: PromptTemplate.fromTemplate(
31
- * "Input: {input}\nOutput: {output}",
32
- * ),
33
- * prefix: "Give the antonym of every input",
34
- * suffix: "Input: {adjective}\nOutput:",
35
- * inputVariables: ["adjective"],
36
- * });
37
- * console.log(await dynamicPrompt.format({ adjective: "rainy" }));
38
- * ```
39
- */
40
- class SemanticSimilarityExampleSelector extends base_js_1.BaseExampleSelector {
41
- constructor(data) {
42
- super(data);
43
- Object.defineProperty(this, "vectorStoreRetriever", {
44
- enumerable: true,
45
- configurable: true,
46
- writable: true,
47
- value: void 0
48
- });
49
- Object.defineProperty(this, "exampleKeys", {
50
- enumerable: true,
51
- configurable: true,
52
- writable: true,
53
- value: void 0
54
- });
55
- Object.defineProperty(this, "inputKeys", {
56
- enumerable: true,
57
- configurable: true,
58
- writable: true,
59
- value: void 0
60
- });
61
- this.exampleKeys = data.exampleKeys;
62
- this.inputKeys = data.inputKeys;
63
- if (data.vectorStore !== undefined) {
64
- this.vectorStoreRetriever = data.vectorStore.asRetriever({
65
- k: data.k ?? 4,
66
- filter: data.filter,
67
- });
68
- }
69
- else if (data.vectorStoreRetriever) {
70
- this.vectorStoreRetriever = data.vectorStoreRetriever;
71
- }
72
- else {
73
- throw new Error(`You must specify one of "vectorStore" and "vectorStoreRetriever".`);
74
- }
75
- }
76
- /**
77
- * Method that adds a new example to the vectorStore. The example is
78
- * converted to a string and added to the vectorStore as a document.
79
- * @param example The example to be added to the vectorStore.
80
- * @returns Promise that resolves when the example has been added to the vectorStore.
81
- */
82
- async addExample(example) {
83
- const inputKeys = this.inputKeys ?? Object.keys(example);
84
- const stringExample = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: example[key] }), {})).join(" ");
85
- await this.vectorStoreRetriever.addDocuments([
86
- new document_js_1.Document({
87
- pageContent: stringExample,
88
- metadata: example,
89
- }),
90
- ]);
91
- }
92
- /**
93
- * Method that selects which examples to use based on semantic similarity.
94
- * It performs a similarity search in the vectorStore using the input
95
- * variables and returns the examples with the highest similarity.
96
- * @param inputVariables The input variables used for the similarity search.
97
- * @returns Promise that resolves with an array of the selected examples.
98
- */
99
- async selectExamples(inputVariables) {
100
- const inputKeys = this.inputKeys ?? Object.keys(inputVariables);
101
- const query = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: inputVariables[key] }), {})).join(" ");
102
- const exampleDocs = await this.vectorStoreRetriever.invoke(query);
103
- const examples = exampleDocs.map((doc) => doc.metadata);
104
- if (this.exampleKeys) {
105
- // If example keys are provided, filter examples to those keys.
106
- return examples.map((example) => this.exampleKeys.reduce((acc, key) => ({ ...acc, [key]: example[key] }), {}));
107
- }
108
- return examples;
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
109
7
  }
110
- /**
111
- * Static method that creates a new instance of
112
- * SemanticSimilarityExampleSelector. It takes a list of examples, an
113
- * instance of Embeddings, a VectorStore class, and an options object as
114
- * parameters. It converts the examples to strings, creates a VectorStore
115
- * from the strings and the embeddings, and returns a new
116
- * SemanticSimilarityExampleSelector with the created VectorStore and the
117
- * options provided.
118
- * @param examples The list of examples to be used.
119
- * @param embeddings The instance of Embeddings to be used.
120
- * @param vectorStoreCls The VectorStore class to be used.
121
- * @param options The options object for the SemanticSimilarityExampleSelector.
122
- * @returns Promise that resolves with a new instance of SemanticSimilarityExampleSelector.
123
- */
124
- static async fromExamples(examples, embeddings, vectorStoreCls, options = {}) {
125
- const inputKeys = options.inputKeys ?? null;
126
- const stringExamples = examples.map((example) => sortedValues(inputKeys
127
- ? inputKeys.reduce((acc, key) => ({ ...acc, [key]: example[key] }), {})
128
- : example).join(" "));
129
- const vectorStore = await vectorStoreCls.fromTexts(stringExamples, examples, // metadatas
130
- embeddings, options);
131
- return new SemanticSimilarityExampleSelector({
132
- vectorStore,
133
- k: options.k ?? 4,
134
- exampleKeys: options.exampleKeys,
135
- inputKeys: options.inputKeys,
136
- });
137
- }
138
- }
139
- exports.SemanticSimilarityExampleSelector = SemanticSimilarityExampleSelector;
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("langchain-core/prompts/example_selector/semantic_similarity"), exports);
@@ -1,91 +1 @@
1
- import { Embeddings } from "../../embeddings/base.js";
2
- import { VectorStore, VectorStoreRetriever } from "../../vectorstores/base.js";
3
- import { Example } from "../../schema/index.js";
4
- import { BaseExampleSelector } from "../base.js";
5
- /**
6
- * Interface for the input data of the SemanticSimilarityExampleSelector
7
- * class.
8
- */
9
- export type SemanticSimilarityExampleSelectorInput<V extends VectorStore = VectorStore> = {
10
- vectorStore: V;
11
- k?: number;
12
- filter?: V["FilterType"];
13
- exampleKeys?: string[];
14
- inputKeys?: string[];
15
- vectorStoreRetriever?: never;
16
- } | {
17
- vectorStoreRetriever: VectorStoreRetriever<V>;
18
- exampleKeys?: string[];
19
- inputKeys?: string[];
20
- vectorStore?: never;
21
- k?: never;
22
- filter?: never;
23
- };
24
- /**
25
- * Class that selects examples based on semantic similarity. It extends
26
- * the BaseExampleSelector class.
27
- * @example
28
- * ```typescript
29
- * const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
30
- * [
31
- * { input: "happy", output: "sad" },
32
- * { input: "tall", output: "short" },
33
- * { input: "energetic", output: "lethargic" },
34
- * { input: "sunny", output: "gloomy" },
35
- * { input: "windy", output: "calm" },
36
- * ],
37
- * new OpenAIEmbeddings(),
38
- * HNSWLib,
39
- * { k: 1 },
40
- * );
41
- * const dynamicPrompt = new FewShotPromptTemplate({
42
- * exampleSelector,
43
- * examplePrompt: PromptTemplate.fromTemplate(
44
- * "Input: {input}\nOutput: {output}",
45
- * ),
46
- * prefix: "Give the antonym of every input",
47
- * suffix: "Input: {adjective}\nOutput:",
48
- * inputVariables: ["adjective"],
49
- * });
50
- * console.log(await dynamicPrompt.format({ adjective: "rainy" }));
51
- * ```
52
- */
53
- export declare class SemanticSimilarityExampleSelector<V extends VectorStore = VectorStore> extends BaseExampleSelector {
54
- vectorStoreRetriever: VectorStoreRetriever<V>;
55
- exampleKeys?: string[];
56
- inputKeys?: string[];
57
- constructor(data: SemanticSimilarityExampleSelectorInput<V>);
58
- /**
59
- * Method that adds a new example to the vectorStore. The example is
60
- * converted to a string and added to the vectorStore as a document.
61
- * @param example The example to be added to the vectorStore.
62
- * @returns Promise that resolves when the example has been added to the vectorStore.
63
- */
64
- addExample(example: Example): Promise<void>;
65
- /**
66
- * Method that selects which examples to use based on semantic similarity.
67
- * It performs a similarity search in the vectorStore using the input
68
- * variables and returns the examples with the highest similarity.
69
- * @param inputVariables The input variables used for the similarity search.
70
- * @returns Promise that resolves with an array of the selected examples.
71
- */
72
- selectExamples<T>(inputVariables: Record<string, T>): Promise<Example[]>;
73
- /**
74
- * Static method that creates a new instance of
75
- * SemanticSimilarityExampleSelector. It takes a list of examples, an
76
- * instance of Embeddings, a VectorStore class, and an options object as
77
- * parameters. It converts the examples to strings, creates a VectorStore
78
- * from the strings and the embeddings, and returns a new
79
- * SemanticSimilarityExampleSelector with the created VectorStore and the
80
- * options provided.
81
- * @param examples The list of examples to be used.
82
- * @param embeddings The instance of Embeddings to be used.
83
- * @param vectorStoreCls The VectorStore class to be used.
84
- * @param options The options object for the SemanticSimilarityExampleSelector.
85
- * @returns Promise that resolves with a new instance of SemanticSimilarityExampleSelector.
86
- */
87
- static fromExamples<C extends typeof VectorStore>(examples: Record<string, string>[], embeddings: Embeddings, vectorStoreCls: C, options?: {
88
- k?: number;
89
- inputKeys?: string[];
90
- } & Parameters<C["fromTexts"]>[3]): Promise<SemanticSimilarityExampleSelector>;
91
- }
1
+ export * from "langchain-core/prompts/example_selector/semantic_similarity";