langchain 0.0.195 → 0.0.197-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/openai/index.cjs +6 -2
  3. package/dist/agents/openai/index.js +6 -2
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +5 -70
  6. package/dist/base_language/count_tokens.d.ts +1 -10
  7. package/dist/base_language/count_tokens.js +1 -65
  8. package/dist/base_language/index.cjs +6 -196
  9. package/dist/base_language/index.d.ts +1 -111
  10. package/dist/base_language/index.js +1 -191
  11. package/dist/cache/base.cjs +15 -37
  12. package/dist/cache/base.d.ts +1 -20
  13. package/dist/cache/base.js +1 -33
  14. package/dist/cache/index.cjs +2 -46
  15. package/dist/cache/index.d.ts +1 -29
  16. package/dist/cache/index.js +1 -45
  17. package/dist/callbacks/base.cjs +3 -139
  18. package/dist/callbacks/base.d.ts +1 -266
  19. package/dist/callbacks/base.js +1 -126
  20. package/dist/callbacks/handlers/console.cjs +14 -221
  21. package/dist/callbacks/handlers/console.d.ts +1 -117
  22. package/dist/callbacks/handlers/console.js +1 -217
  23. package/dist/callbacks/handlers/initialize.cjs +15 -30
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  25. package/dist/callbacks/handlers/initialize.js +1 -27
  26. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  28. package/dist/callbacks/handlers/log_stream.js +1 -289
  29. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  31. package/dist/callbacks/handlers/run_collector.js +1 -46
  32. package/dist/callbacks/handlers/tracer.cjs +15 -375
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  34. package/dist/callbacks/handlers/tracer.js +1 -373
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  41. package/dist/callbacks/manager.cjs +15 -676
  42. package/dist/callbacks/manager.d.ts +1 -180
  43. package/dist/callbacks/manager.js +1 -666
  44. package/dist/callbacks/promises.cjs +14 -42
  45. package/dist/callbacks/promises.d.ts +1 -11
  46. package/dist/callbacks/promises.js +1 -37
  47. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  48. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  49. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  50. package/dist/chains/openai_functions/structured_output.js +1 -1
  51. package/dist/chat_models/anthropic.cjs +15 -348
  52. package/dist/chat_models/anthropic.d.ts +1 -156
  53. package/dist/chat_models/anthropic.js +1 -346
  54. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  55. package/dist/chat_models/base.cjs +15 -296
  56. package/dist/chat_models/base.d.ts +1 -122
  57. package/dist/chat_models/base.js +1 -292
  58. package/dist/chat_models/bedrock/web.cjs +21 -1
  59. package/dist/chat_models/bedrock/web.d.ts +2 -2
  60. package/dist/chat_models/bedrock/web.js +21 -1
  61. package/dist/chat_models/fireworks.d.ts +1 -1
  62. package/dist/document.cjs +2 -24
  63. package/dist/document.d.ts +1 -12
  64. package/dist/document.js +1 -23
  65. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  66. package/dist/document_loaders/web/github.cjs +105 -0
  67. package/dist/document_loaders/web/github.d.ts +26 -0
  68. package/dist/document_loaders/web/github.js +105 -0
  69. package/dist/document_loaders/web/s3.d.ts +1 -1
  70. package/dist/embeddings/base.cjs +15 -22
  71. package/dist/embeddings/base.d.ts +1 -33
  72. package/dist/embeddings/base.js +1 -20
  73. package/dist/embeddings/cache_backed.cjs +2 -2
  74. package/dist/embeddings/cache_backed.js +1 -1
  75. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  76. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  77. package/dist/evaluation/qa/prompt.d.ts +2 -2
  78. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  79. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  80. package/dist/llms/base.cjs +15 -278
  81. package/dist/llms/base.d.ts +1 -115
  82. package/dist/llms/base.js +1 -275
  83. package/dist/llms/bedrock/web.cjs +21 -1
  84. package/dist/llms/bedrock/web.d.ts +2 -2
  85. package/dist/llms/bedrock/web.js +21 -1
  86. package/dist/llms/fireworks.d.ts +1 -1
  87. package/dist/load/import_map.cjs +2 -1
  88. package/dist/load/import_map.d.ts +1 -0
  89. package/dist/load/import_map.js +1 -0
  90. package/dist/load/index.cjs +7 -148
  91. package/dist/load/index.js +7 -148
  92. package/dist/load/map_keys.cjs +0 -24
  93. package/dist/load/map_keys.d.ts +0 -6
  94. package/dist/load/map_keys.js +1 -17
  95. package/dist/load/serializable.cjs +15 -178
  96. package/dist/load/serializable.d.ts +1 -66
  97. package/dist/load/serializable.js +1 -175
  98. package/dist/memory/base.cjs +17 -92
  99. package/dist/memory/base.d.ts +2 -68
  100. package/dist/memory/base.js +2 -87
  101. package/dist/output_parsers/list.cjs +4 -122
  102. package/dist/output_parsers/list.d.ts +1 -57
  103. package/dist/output_parsers/list.js +1 -119
  104. package/dist/output_parsers/openai_functions.cjs +2 -2
  105. package/dist/output_parsers/openai_functions.d.ts +1 -1
  106. package/dist/output_parsers/openai_functions.js +1 -1
  107. package/dist/output_parsers/regex.d.ts +1 -1
  108. package/dist/output_parsers/structured.d.ts +1 -1
  109. package/dist/prompts/base.cjs +8 -183
  110. package/dist/prompts/base.d.ts +3 -132
  111. package/dist/prompts/base.js +3 -178
  112. package/dist/prompts/chat.cjs +13 -477
  113. package/dist/prompts/chat.d.ts +2 -219
  114. package/dist/prompts/chat.js +2 -466
  115. package/dist/prompts/few_shot.cjs +3 -352
  116. package/dist/prompts/few_shot.d.ts +1 -192
  117. package/dist/prompts/few_shot.js +1 -350
  118. package/dist/prompts/index.cjs +3 -2
  119. package/dist/prompts/index.d.ts +2 -1
  120. package/dist/prompts/index.js +2 -1
  121. package/dist/prompts/pipeline.cjs +2 -141
  122. package/dist/prompts/pipeline.d.ts +1 -98
  123. package/dist/prompts/pipeline.js +1 -140
  124. package/dist/prompts/prompt.cjs +2 -145
  125. package/dist/prompts/prompt.d.ts +1 -92
  126. package/dist/prompts/prompt.js +1 -144
  127. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +2 -147
  128. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  129. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  130. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  131. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  132. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  133. package/dist/prompts/selectors/conditional.cjs +5 -73
  134. package/dist/prompts/selectors/conditional.d.ts +1 -63
  135. package/dist/prompts/selectors/conditional.js +1 -69
  136. package/dist/prompts/serde.d.ts +1 -43
  137. package/dist/prompts/template.cjs +8 -88
  138. package/dist/prompts/template.d.ts +1 -36
  139. package/dist/prompts/template.js +1 -83
  140. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  141. package/dist/runnables/index.d.ts +1 -0
  142. package/dist/runnables/index.js +1 -0
  143. package/dist/schema/document.cjs +3 -34
  144. package/dist/schema/document.d.ts +2 -29
  145. package/dist/schema/document.js +2 -32
  146. package/dist/schema/index.cjs +37 -612
  147. package/dist/schema/index.d.ts +11 -311
  148. package/dist/schema/index.js +8 -583
  149. package/dist/schema/output_parser.cjs +15 -309
  150. package/dist/schema/output_parser.d.ts +1 -173
  151. package/dist/schema/output_parser.js +1 -301
  152. package/dist/schema/retriever.cjs +15 -77
  153. package/dist/schema/retriever.d.ts +1 -43
  154. package/dist/schema/retriever.js +1 -75
  155. package/dist/schema/runnable/base.cjs +10 -1072
  156. package/dist/schema/runnable/base.d.ts +1 -356
  157. package/dist/schema/runnable/base.js +1 -1060
  158. package/dist/schema/runnable/branch.cjs +2 -131
  159. package/dist/schema/runnable/branch.d.ts +1 -94
  160. package/dist/schema/runnable/branch.js +1 -130
  161. package/dist/schema/runnable/config.cjs +0 -6
  162. package/dist/schema/runnable/config.d.ts +1 -3
  163. package/dist/schema/runnable/config.js +1 -4
  164. package/dist/schema/runnable/index.cjs +15 -16
  165. package/dist/schema/runnable/index.d.ts +1 -5
  166. package/dist/schema/runnable/index.js +1 -4
  167. package/dist/schema/runnable/passthrough.cjs +3 -113
  168. package/dist/schema/runnable/passthrough.d.ts +1 -72
  169. package/dist/schema/runnable/passthrough.js +1 -111
  170. package/dist/schema/runnable/router.cjs +2 -71
  171. package/dist/schema/runnable/router.d.ts +1 -29
  172. package/dist/schema/runnable/router.js +1 -70
  173. package/dist/schema/storage.cjs +15 -8
  174. package/dist/schema/storage.d.ts +1 -57
  175. package/dist/schema/storage.js +1 -6
  176. package/dist/tools/bingserpapi.d.ts +1 -1
  177. package/dist/tools/searchapi.d.ts +1 -1
  178. package/dist/tools/serpapi.d.ts +1 -1
  179. package/dist/tools/serper.d.ts +1 -1
  180. package/dist/util/async_caller.cjs +14 -128
  181. package/dist/util/async_caller.d.ts +1 -45
  182. package/dist/util/async_caller.js +1 -124
  183. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  184. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  185. package/dist/vectorstores/momento_vector_index.js +40 -1
  186. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  187. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  188. package/dist/vectorstores/mongodb_atlas.js +22 -2
  189. package/package.json +18 -11
  190. package/runnables.cjs +1 -0
  191. package/runnables.d.ts +1 -0
  192. package/runnables.js +1 -0
  193. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  194. package/dist/util/@cfworker/json-schema/index.js +0 -1
  195. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  196. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  197. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  198. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  199. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  200. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  201. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  202. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  203. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  204. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  205. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  206. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  207. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  208. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  209. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  210. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  211. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  212. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  213. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  214. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  215. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  216. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  217. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  218. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  219. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  220. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  221. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  222. package/dist/util/fast-json-patch/index.cjs +0 -49
  223. package/dist/util/fast-json-patch/index.d.ts +0 -22
  224. package/dist/util/fast-json-patch/index.js +0 -16
  225. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  226. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  227. package/dist/util/fast-json-patch/src/core.js +0 -459
  228. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  229. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  230. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  231. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  232. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  233. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  234. package/dist/util/js-sha1/hash.cjs +0 -358
  235. package/dist/util/js-sha1/hash.d.ts +0 -1
  236. package/dist/util/js-sha1/hash.js +0 -355
@@ -1,1060 +1 @@
1
- import pRetry from "p-retry";
2
- import { CallbackManager, } from "../../callbacks/manager.js";
3
- import { LogStreamCallbackHandler, RunLogPatch, } from "../../callbacks/handlers/log_stream.js";
4
- import { Serializable } from "../../load/serializable.js";
5
- import { IterableReadableStream } from "../../util/stream.js";
6
- import { getCallbackMangerForConfig } from "./config.js";
7
- import { AsyncCaller } from "../../util/async_caller.js";
8
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
- function _coerceToDict(value, defaultKey) {
10
- return value && !Array.isArray(value) && typeof value === "object"
11
- ? value
12
- : { [defaultKey]: value };
13
- }
14
- /**
15
- * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
16
- * transformed.
17
- */
18
- export class Runnable extends Serializable {
19
- constructor() {
20
- super(...arguments);
21
- Object.defineProperty(this, "lc_runnable", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: true
26
- });
27
- }
28
- /**
29
- * Bind arguments to a Runnable, returning a new Runnable.
30
- * @param kwargs
31
- * @returns A new RunnableBinding that, when invoked, will apply the bound args.
32
- */
33
- bind(kwargs) {
34
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
35
- return new RunnableBinding({ bound: this, kwargs, config: {} });
36
- }
37
- /**
38
- * Return a new Runnable that maps a list of inputs to a list of outputs,
39
- * by calling invoke() with each input.
40
- */
41
- map() {
42
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
43
- return new RunnableEach({ bound: this });
44
- }
45
- /**
46
- * Add retry logic to an existing runnable.
47
- * @param kwargs
48
- * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
49
- */
50
- withRetry(fields) {
51
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
52
- return new RunnableRetry({
53
- bound: this,
54
- kwargs: {},
55
- config: {},
56
- maxAttemptNumber: fields?.stopAfterAttempt,
57
- ...fields,
58
- });
59
- }
60
- /**
61
- * Bind config to a Runnable, returning a new Runnable.
62
- * @param config New configuration parameters to attach to the new runnable.
63
- * @returns A new RunnableBinding with a config matching what's passed.
64
- */
65
- withConfig(config) {
66
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
67
- return new RunnableBinding({
68
- bound: this,
69
- config,
70
- kwargs: {},
71
- });
72
- }
73
- /**
74
- * Create a new runnable from the current one that will try invoking
75
- * other passed fallback runnables if the initial invocation fails.
76
- * @param fields.fallbacks Other runnables to call if the runnable errors.
77
- * @returns A new RunnableWithFallbacks.
78
- */
79
- withFallbacks(fields) {
80
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
81
- return new RunnableWithFallbacks({
82
- runnable: this,
83
- fallbacks: fields.fallbacks,
84
- });
85
- }
86
- _getOptionsList(options, length = 0) {
87
- if (Array.isArray(options)) {
88
- if (options.length !== length) {
89
- throw new Error(`Passed "options" must be an array with the same length as the inputs, but got ${options.length} options for ${length} inputs`);
90
- }
91
- return options;
92
- }
93
- return Array.from({ length }, () => options);
94
- }
95
- async batch(inputs, options, batchOptions) {
96
- const configList = this._getOptionsList(options ?? {}, inputs.length);
97
- const caller = new AsyncCaller({
98
- maxConcurrency: batchOptions?.maxConcurrency,
99
- onFailedAttempt: (e) => {
100
- throw e;
101
- },
102
- });
103
- const batchCalls = inputs.map((input, i) => caller.call(async () => {
104
- try {
105
- const result = await this.invoke(input, configList[i]);
106
- return result;
107
- }
108
- catch (e) {
109
- if (batchOptions?.returnExceptions) {
110
- return e;
111
- }
112
- throw e;
113
- }
114
- }));
115
- return Promise.all(batchCalls);
116
- }
117
- /**
118
- * Default streaming implementation.
119
- * Subclasses should override this method if they support streaming output.
120
- * @param input
121
- * @param options
122
- */
123
- async *_streamIterator(input, options) {
124
- yield this.invoke(input, options);
125
- }
126
- /**
127
- * Stream output in chunks.
128
- * @param input
129
- * @param options
130
- * @returns A readable stream that is also an iterable.
131
- */
132
- async stream(input, options) {
133
- return IterableReadableStream.fromAsyncGenerator(this._streamIterator(input, options));
134
- }
135
- _separateRunnableConfigFromCallOptions(options = {}) {
136
- const runnableConfig = {
137
- callbacks: options.callbacks,
138
- tags: options.tags,
139
- metadata: options.metadata,
140
- runName: options.runName,
141
- };
142
- const callOptions = { ...options };
143
- delete callOptions.callbacks;
144
- delete callOptions.tags;
145
- delete callOptions.metadata;
146
- delete callOptions.runName;
147
- return [runnableConfig, callOptions];
148
- }
149
- async _callWithConfig(func, input, options) {
150
- const callbackManager_ = await getCallbackMangerForConfig(options);
151
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName);
152
- let output;
153
- try {
154
- output = await func.bind(this)(input, options, runManager);
155
- }
156
- catch (e) {
157
- await runManager?.handleChainError(e);
158
- throw e;
159
- }
160
- await runManager?.handleChainEnd(_coerceToDict(output, "output"));
161
- return output;
162
- }
163
- /**
164
- * Internal method that handles batching and configuration for a runnable
165
- * It takes a function, input values, and optional configuration, and
166
- * returns a promise that resolves to the output values.
167
- * @param func The function to be executed for each input value.
168
- * @param input The input values to be processed.
169
- * @param config Optional configuration for the function execution.
170
- * @returns A promise that resolves to the output values.
171
- */
172
- async _batchWithConfig(func, inputs, options, batchOptions) {
173
- const optionsList = this._getOptionsList(options ?? {}, inputs.length);
174
- const callbackManagers = await Promise.all(optionsList.map(getCallbackMangerForConfig));
175
- const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, optionsList[i].runType, undefined, undefined, optionsList[i].runName)));
176
- let outputs;
177
- try {
178
- outputs = await func(inputs, optionsList, runManagers, batchOptions);
179
- }
180
- catch (e) {
181
- await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
182
- throw e;
183
- }
184
- await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(outputs, "output"))));
185
- return outputs;
186
- }
187
- /**
188
- * Helper method to transform an Iterator of Input values into an Iterator of
189
- * Output values, with callbacks.
190
- * Use this to implement `stream()` or `transform()` in Runnable subclasses.
191
- */
192
- async *_transformStreamWithConfig(inputGenerator, transformer, options) {
193
- let finalInput;
194
- let finalInputSupported = true;
195
- let finalOutput;
196
- let finalOutputSupported = true;
197
- const callbackManager_ = await getCallbackMangerForConfig(options);
198
- let runManager;
199
- const serializedRepresentation = this.toJSON();
200
- async function* wrapInputForTracing() {
201
- for await (const chunk of inputGenerator) {
202
- if (!runManager) {
203
- // Start the run manager AFTER the iterator starts to preserve
204
- // tracing order
205
- runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName);
206
- }
207
- if (finalInputSupported) {
208
- if (finalInput === undefined) {
209
- finalInput = chunk;
210
- }
211
- else {
212
- try {
213
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
214
- finalInput = finalInput.concat(chunk);
215
- }
216
- catch {
217
- finalInput = undefined;
218
- finalInputSupported = false;
219
- }
220
- }
221
- }
222
- yield chunk;
223
- }
224
- }
225
- const wrappedInputGenerator = wrapInputForTracing();
226
- try {
227
- const outputIterator = transformer(wrappedInputGenerator, runManager, options);
228
- for await (const chunk of outputIterator) {
229
- yield chunk;
230
- if (finalOutputSupported) {
231
- if (finalOutput === undefined) {
232
- finalOutput = chunk;
233
- }
234
- else {
235
- try {
236
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
237
- finalOutput = finalOutput.concat(chunk);
238
- }
239
- catch {
240
- finalOutput = undefined;
241
- finalOutputSupported = false;
242
- }
243
- }
244
- }
245
- }
246
- }
247
- catch (e) {
248
- await runManager?.handleChainError(e, undefined, undefined, undefined, {
249
- inputs: _coerceToDict(finalInput, "input"),
250
- });
251
- throw e;
252
- }
253
- await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
254
- }
255
- _patchConfig(config = {}, callbackManager = undefined) {
256
- const newConfig = { ...config };
257
- if (callbackManager !== undefined) {
258
- /**
259
- * If we're replacing callbacks we need to unset runName
260
- * since that should apply only to the same run as the original callbacks
261
- */
262
- delete newConfig.runName;
263
- return { ...newConfig, callbacks: callbackManager };
264
- }
265
- return newConfig;
266
- }
267
- /**
268
- * Create a new runnable sequence that runs each individual runnable in series,
269
- * piping the output of one runnable into another runnable or runnable-like.
270
- * @param coerceable A runnable, function, or object whose values are functions or runnables.
271
- * @returns A new runnable sequence.
272
- */
273
- pipe(coerceable) {
274
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
275
- return new RunnableSequence({
276
- first: this,
277
- last: _coerceToRunnable(coerceable),
278
- });
279
- }
280
- /**
281
- * Default implementation of transform, which buffers input and then calls stream.
282
- * Subclasses should override this method if they can start producing output while
283
- * input is still being generated.
284
- * @param generator
285
- * @param options
286
- */
287
- async *transform(generator, options) {
288
- let finalChunk;
289
- for await (const chunk of generator) {
290
- if (finalChunk === undefined) {
291
- finalChunk = chunk;
292
- }
293
- else {
294
- // Make a best effort to gather, for any type that supports concat.
295
- // This method should throw an error if gathering fails.
296
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
297
- finalChunk = finalChunk.concat(chunk);
298
- }
299
- }
300
- yield* this._streamIterator(finalChunk, options);
301
- }
302
- /**
303
- * Stream all output from a runnable, as reported to the callback system.
304
- * This includes all inner runs of LLMs, Retrievers, Tools, etc.
305
- * Output is streamed as Log objects, which include a list of
306
- * jsonpatch ops that describe how the state of the run has changed in each
307
- * step, and the final state of the run.
308
- * The jsonpatch ops can be applied in order to construct state.
309
- * @param input
310
- * @param options
311
- * @param streamOptions
312
- */
313
- async *streamLog(input, options, streamOptions) {
314
- const stream = new LogStreamCallbackHandler({
315
- ...streamOptions,
316
- autoClose: false,
317
- });
318
- const config = options ?? {};
319
- const { callbacks } = config;
320
- if (callbacks === undefined) {
321
- config.callbacks = [stream];
322
- }
323
- else if (Array.isArray(callbacks)) {
324
- config.callbacks = callbacks.concat([stream]);
325
- }
326
- else {
327
- const copiedCallbacks = callbacks.copy();
328
- copiedCallbacks.inheritableHandlers.push(stream);
329
- config.callbacks = copiedCallbacks;
330
- }
331
- const runnableStream = await this.stream(input, config);
332
- async function consumeRunnableStream() {
333
- try {
334
- for await (const chunk of runnableStream) {
335
- const patch = new RunLogPatch({
336
- ops: [
337
- {
338
- op: "add",
339
- path: "/streamed_output/-",
340
- value: chunk,
341
- },
342
- ],
343
- });
344
- await stream.writer.write(patch);
345
- }
346
- }
347
- finally {
348
- await stream.writer.close();
349
- }
350
- }
351
- const runnableStreamPromise = consumeRunnableStream();
352
- try {
353
- for await (const log of stream) {
354
- yield log;
355
- }
356
- }
357
- finally {
358
- await runnableStreamPromise;
359
- }
360
- }
361
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
362
- static isRunnable(thing) {
363
- return thing ? thing.lc_runnable : false;
364
- }
365
- }
366
- /**
367
- * A runnable that delegates calls to another runnable with a set of kwargs.
368
- */
369
- export class RunnableBinding extends Runnable {
370
- static lc_name() {
371
- return "RunnableBinding";
372
- }
373
- constructor(fields) {
374
- super(fields);
375
- Object.defineProperty(this, "lc_namespace", {
376
- enumerable: true,
377
- configurable: true,
378
- writable: true,
379
- value: ["langchain", "schema", "runnable"]
380
- });
381
- Object.defineProperty(this, "lc_serializable", {
382
- enumerable: true,
383
- configurable: true,
384
- writable: true,
385
- value: true
386
- });
387
- Object.defineProperty(this, "bound", {
388
- enumerable: true,
389
- configurable: true,
390
- writable: true,
391
- value: void 0
392
- });
393
- Object.defineProperty(this, "config", {
394
- enumerable: true,
395
- configurable: true,
396
- writable: true,
397
- value: void 0
398
- });
399
- Object.defineProperty(this, "kwargs", {
400
- enumerable: true,
401
- configurable: true,
402
- writable: true,
403
- value: void 0
404
- });
405
- this.bound = fields.bound;
406
- this.kwargs = fields.kwargs;
407
- this.config = fields.config;
408
- }
409
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
410
- _mergeConfig(options) {
411
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
412
- const copy = { ...this.config };
413
- if (options) {
414
- for (const key of Object.keys(options)) {
415
- if (key === "metadata") {
416
- copy[key] = { ...copy[key], ...options[key] };
417
- }
418
- else if (key === "tags") {
419
- copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
420
- }
421
- else {
422
- copy[key] = options[key] ?? copy[key];
423
- }
424
- }
425
- }
426
- return copy;
427
- }
428
- bind(kwargs) {
429
- return this.constructor({
430
- bound: this.bound,
431
- kwargs: { ...this.kwargs, ...kwargs },
432
- config: this.config,
433
- });
434
- }
435
- withConfig(config) {
436
- return this.constructor({
437
- bound: this.bound,
438
- kwargs: this.kwargs,
439
- config: { ...this.config, ...config },
440
- });
441
- }
442
- withRetry(fields) {
443
- return this.constructor({
444
- bound: this.bound.withRetry(fields),
445
- kwargs: this.kwargs,
446
- config: this.config,
447
- });
448
- }
449
- async invoke(input, options) {
450
- return this.bound.invoke(input, this._mergeConfig({ ...options, ...this.kwargs }));
451
- }
452
- async batch(inputs, options, batchOptions) {
453
- const mergedOptions = Array.isArray(options)
454
- ? options.map((individualOption) => this._mergeConfig({
455
- ...individualOption,
456
- ...this.kwargs,
457
- }))
458
- : this._mergeConfig({ ...options, ...this.kwargs });
459
- return this.bound.batch(inputs, mergedOptions, batchOptions);
460
- }
461
- async *_streamIterator(input, options) {
462
- yield* this.bound._streamIterator(input, this._mergeConfig({ ...options, ...this.kwargs }));
463
- }
464
- async stream(input, options) {
465
- return this.bound.stream(input, this._mergeConfig({ ...options, ...this.kwargs }));
466
- }
467
- async *transform(
468
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
469
- generator, options) {
470
- yield* this.bound.transform(generator, this._mergeConfig({ ...options, ...this.kwargs }));
471
- }
472
- static isRunnableBinding(
473
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
474
- thing
475
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
476
- ) {
477
- return thing.bound && Runnable.isRunnable(thing.bound);
478
- }
479
- }
480
- /**
481
- * A runnable that delegates calls to another runnable
482
- * with each element of the input sequence.
483
- */
484
- export class RunnableEach extends Runnable {
485
- static lc_name() {
486
- return "RunnableEach";
487
- }
488
- constructor(fields) {
489
- super(fields);
490
- Object.defineProperty(this, "lc_serializable", {
491
- enumerable: true,
492
- configurable: true,
493
- writable: true,
494
- value: true
495
- });
496
- Object.defineProperty(this, "lc_namespace", {
497
- enumerable: true,
498
- configurable: true,
499
- writable: true,
500
- value: ["langchain", "schema", "runnable"]
501
- });
502
- Object.defineProperty(this, "bound", {
503
- enumerable: true,
504
- configurable: true,
505
- writable: true,
506
- value: void 0
507
- });
508
- this.bound = fields.bound;
509
- }
510
- /**
511
- * Binds the runnable with the specified arguments.
512
- * @param args The arguments to bind the runnable with.
513
- * @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
514
- */
515
- bind(kwargs) {
516
- return new RunnableEach({
517
- bound: this.bound.bind(kwargs),
518
- });
519
- }
520
- /**
521
- * Invokes the runnable with the specified input and configuration.
522
- * @param input The input to invoke the runnable with.
523
- * @param config The configuration to invoke the runnable with.
524
- * @returns A promise that resolves to the output of the runnable.
525
- */
526
- async invoke(inputs, config) {
527
- return this._callWithConfig(this._invoke, inputs, config);
528
- }
529
- /**
530
- * A helper method that is used to invoke the runnable with the specified input and configuration.
531
- * @param input The input to invoke the runnable with.
532
- * @param config The configuration to invoke the runnable with.
533
- * @returns A promise that resolves to the output of the runnable.
534
- */
535
- async _invoke(inputs, config, runManager) {
536
- return this.bound.batch(inputs, this._patchConfig(config, runManager?.getChild()));
537
- }
538
- }
539
- /**
540
- * Base class for runnables that can be retried a
541
- * specified number of times.
542
- */
543
- export class RunnableRetry extends RunnableBinding {
544
- static lc_name() {
545
- return "RunnableRetry";
546
- }
547
- constructor(fields) {
548
- super(fields);
549
- Object.defineProperty(this, "lc_namespace", {
550
- enumerable: true,
551
- configurable: true,
552
- writable: true,
553
- value: ["langchain", "schema", "runnable"]
554
- });
555
- Object.defineProperty(this, "maxAttemptNumber", {
556
- enumerable: true,
557
- configurable: true,
558
- writable: true,
559
- value: 3
560
- });
561
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
562
- Object.defineProperty(this, "onFailedAttempt", {
563
- enumerable: true,
564
- configurable: true,
565
- writable: true,
566
- value: () => { }
567
- });
568
- this.maxAttemptNumber = fields.maxAttemptNumber ?? this.maxAttemptNumber;
569
- this.onFailedAttempt = fields.onFailedAttempt ?? this.onFailedAttempt;
570
- }
571
- _patchConfigForRetry(attempt, config, runManager) {
572
- const tag = attempt > 1 ? `retry:attempt:${attempt}` : undefined;
573
- return this._patchConfig(config, runManager?.getChild(tag));
574
- }
575
- async _invoke(input, config, runManager) {
576
- return pRetry((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
577
- onFailedAttempt: this.onFailedAttempt,
578
- retries: Math.max(this.maxAttemptNumber - 1, 0),
579
- randomize: true,
580
- });
581
- }
582
- /**
583
- * Method that invokes the runnable with the specified input, run manager,
584
- * and config. It handles the retry logic by catching any errors and
585
- * recursively invoking itself with the updated config for the next retry
586
- * attempt.
587
- * @param input The input for the runnable.
588
- * @param runManager The run manager for the runnable.
589
- * @param config The config for the runnable.
590
- * @returns A promise that resolves to the output of the runnable.
591
- */
592
- async invoke(input, config) {
593
- return this._callWithConfig(this._invoke, input, config);
594
- }
595
- async _batch(inputs, configs, runManagers, batchOptions) {
596
- const resultsMap = {};
597
- try {
598
- await pRetry(async (attemptNumber) => {
599
- const remainingIndexes = inputs
600
- .map((_, i) => i)
601
- .filter((i) => resultsMap[i.toString()] === undefined ||
602
- // eslint-disable-next-line no-instanceof/no-instanceof
603
- resultsMap[i.toString()] instanceof Error);
604
- const remainingInputs = remainingIndexes.map((i) => inputs[i]);
605
- const patchedConfigs = remainingIndexes.map((i) => this._patchConfigForRetry(attemptNumber, configs?.[i], runManagers?.[i]));
606
- const results = await super.batch(remainingInputs, patchedConfigs, {
607
- ...batchOptions,
608
- returnExceptions: true,
609
- });
610
- let firstException;
611
- for (let i = 0; i < results.length; i += 1) {
612
- const result = results[i];
613
- const resultMapIndex = remainingIndexes[i];
614
- // eslint-disable-next-line no-instanceof/no-instanceof
615
- if (result instanceof Error) {
616
- if (firstException === undefined) {
617
- firstException = result;
618
- }
619
- }
620
- resultsMap[resultMapIndex.toString()] = result;
621
- }
622
- if (firstException) {
623
- throw firstException;
624
- }
625
- return results;
626
- }, {
627
- onFailedAttempt: this.onFailedAttempt,
628
- retries: Math.max(this.maxAttemptNumber - 1, 0),
629
- randomize: true,
630
- });
631
- }
632
- catch (e) {
633
- if (batchOptions?.returnExceptions !== true) {
634
- throw e;
635
- }
636
- }
637
- return Object.keys(resultsMap)
638
- .sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
639
- .map((key) => resultsMap[parseInt(key, 10)]);
640
- }
641
- async batch(inputs, options, batchOptions) {
642
- return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
643
- }
644
- }
645
- /**
646
- * A sequence of runnables, where the output of each is the input of the next.
647
- * @example
648
- * ```typescript
649
- * const promptTemplate = PromptTemplate.fromTemplate(
650
- * "Tell me a joke about {topic}",
651
- * );
652
- * const chain = RunnableSequence.from([promptTemplate, new ChatOpenAI({})]);
653
- * const result = await chain.invoke({ topic: "bears" });
654
- * ```
655
- */
656
- export class RunnableSequence extends Runnable {
657
- static lc_name() {
658
- return "RunnableSequence";
659
- }
660
- constructor(fields) {
661
- super(fields);
662
- Object.defineProperty(this, "first", {
663
- enumerable: true,
664
- configurable: true,
665
- writable: true,
666
- value: void 0
667
- });
668
- Object.defineProperty(this, "middle", {
669
- enumerable: true,
670
- configurable: true,
671
- writable: true,
672
- value: []
673
- });
674
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
675
- Object.defineProperty(this, "last", {
676
- enumerable: true,
677
- configurable: true,
678
- writable: true,
679
- value: void 0
680
- });
681
- Object.defineProperty(this, "lc_serializable", {
682
- enumerable: true,
683
- configurable: true,
684
- writable: true,
685
- value: true
686
- });
687
- Object.defineProperty(this, "lc_namespace", {
688
- enumerable: true,
689
- configurable: true,
690
- writable: true,
691
- value: ["langchain", "schema", "runnable"]
692
- });
693
- this.first = fields.first;
694
- this.middle = fields.middle ?? this.middle;
695
- this.last = fields.last;
696
- }
697
- get steps() {
698
- return [this.first, ...this.middle, this.last];
699
- }
700
- async invoke(input, options) {
701
- const callbackManager_ = await getCallbackMangerForConfig(options);
702
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
703
- let nextStepInput = input;
704
- let finalOutput;
705
- try {
706
- const initialSteps = [this.first, ...this.middle];
707
- for (let i = 0; i < initialSteps.length; i += 1) {
708
- const step = initialSteps[i];
709
- nextStepInput = await step.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${i + 1}`)));
710
- }
711
- // TypeScript can't detect that the last output of the sequence returns RunOutput, so call it out of the loop here
712
- finalOutput = await this.last.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${this.steps.length}`)));
713
- }
714
- catch (e) {
715
- await runManager?.handleChainError(e);
716
- throw e;
717
- }
718
- await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
719
- return finalOutput;
720
- }
721
- async batch(inputs, options, batchOptions) {
722
- const configList = this._getOptionsList(options ?? {}, inputs.length);
723
- const callbackManagers = await Promise.all(configList.map(getCallbackMangerForConfig));
724
- const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
725
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
726
- let nextStepInputs = inputs;
727
- let finalOutputs;
728
- try {
729
- const initialSteps = [this.first, ...this.middle];
730
- for (let i = 0; i < initialSteps.length; i += 1) {
731
- const step = initialSteps[i];
732
- nextStepInputs = await step.batch(nextStepInputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild(`seq:step:${i + 1}`))), batchOptions);
733
- }
734
- finalOutputs = await this.last.batch(nextStepInputs, runManagers.map((runManager) => this._patchConfig(configList[this.steps.length - 1], runManager?.getChild(`seq:step:${this.steps.length}`))), batchOptions);
735
- }
736
- catch (e) {
737
- await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
738
- throw e;
739
- }
740
- await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(finalOutputs[i], "output"))));
741
- return finalOutputs;
742
- }
743
- async *_streamIterator(input, options) {
744
- const callbackManager_ = await getCallbackMangerForConfig(options);
745
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
746
- let nextStepInput = input;
747
- const steps = [this.first, ...this.middle, this.last];
748
- // Find the index of the last runnable in the sequence that doesn't have an overridden .transform() method
749
- // and start streaming from there
750
- const streamingStartStepIndex = Math.min(steps.length - 1, steps.length -
751
- [...steps].reverse().findIndex((step) => {
752
- const isDefaultImplementation = step.transform === Runnable.prototype.transform;
753
- const boundRunnableIsDefaultImplementation = RunnableBinding.isRunnableBinding(step) &&
754
- step.bound?.transform === Runnable.prototype.transform;
755
- return (isDefaultImplementation || boundRunnableIsDefaultImplementation);
756
- }) -
757
- 1);
758
- try {
759
- const invokeSteps = steps.slice(0, streamingStartStepIndex);
760
- for (let i = 0; i < invokeSteps.length; i += 1) {
761
- const step = invokeSteps[i];
762
- nextStepInput = await step.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${i + 1}`)));
763
- }
764
- }
765
- catch (e) {
766
- await runManager?.handleChainError(e);
767
- throw e;
768
- }
769
- let concatSupported = true;
770
- let finalOutput;
771
- try {
772
- let finalGenerator = await steps[streamingStartStepIndex]._streamIterator(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${streamingStartStepIndex + 1}`)));
773
- const finalSteps = steps.slice(streamingStartStepIndex + 1);
774
- for (let i = 0; i < finalSteps.length; i += 1) {
775
- const step = finalSteps[i];
776
- finalGenerator = await step.transform(finalGenerator, this._patchConfig(options, runManager?.getChild(`seq:step:${streamingStartStepIndex + i + 2}`)));
777
- }
778
- for await (const chunk of finalGenerator) {
779
- yield chunk;
780
- if (concatSupported) {
781
- if (finalOutput === undefined) {
782
- finalOutput = chunk;
783
- }
784
- else {
785
- try {
786
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
787
- finalOutput = finalOutput.concat(chunk);
788
- }
789
- catch (e) {
790
- finalOutput = undefined;
791
- concatSupported = false;
792
- }
793
- }
794
- }
795
- }
796
- }
797
- catch (e) {
798
- await runManager?.handleChainError(e);
799
- throw e;
800
- }
801
- await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
802
- }
803
- pipe(coerceable) {
804
- if (RunnableSequence.isRunnableSequence(coerceable)) {
805
- return new RunnableSequence({
806
- first: this.first,
807
- middle: this.middle.concat([
808
- this.last,
809
- coerceable.first,
810
- ...coerceable.middle,
811
- ]),
812
- last: coerceable.last,
813
- });
814
- }
815
- else {
816
- return new RunnableSequence({
817
- first: this.first,
818
- middle: [...this.middle, this.last],
819
- last: _coerceToRunnable(coerceable),
820
- });
821
- }
822
- }
823
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
824
- static isRunnableSequence(thing) {
825
- return Array.isArray(thing.middle) && Runnable.isRunnable(thing);
826
- }
827
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
828
- static from([first, ...runnables]) {
829
- return new RunnableSequence({
830
- first: _coerceToRunnable(first),
831
- middle: runnables.slice(0, -1).map(_coerceToRunnable),
832
- last: _coerceToRunnable(runnables[runnables.length - 1]),
833
- });
834
- }
835
- }
836
- /**
837
- * A runnable that runs a mapping of runnables in parallel,
838
- * and returns a mapping of their outputs.
839
- * @example
840
- * ```typescript
841
- * const mapChain = RunnableMap.from({
842
- * joke: PromptTemplate.fromTemplate("Tell me a joke about {topic}").pipe(
843
- * new ChatAnthropic({}),
844
- * ),
845
- * poem: PromptTemplate.fromTemplate("write a 2-line poem about {topic}").pipe(
846
- * new ChatAnthropic({}),
847
- * ),
848
- * });
849
- * const result = await mapChain.invoke({ topic: "bear" });
850
- * ```
851
- */
852
- export class RunnableMap extends Runnable {
853
- static lc_name() {
854
- return "RunnableMap";
855
- }
856
- getStepsKeys() {
857
- return Object.keys(this.steps);
858
- }
859
- constructor(fields) {
860
- super(fields);
861
- Object.defineProperty(this, "lc_namespace", {
862
- enumerable: true,
863
- configurable: true,
864
- writable: true,
865
- value: ["langchain", "schema", "runnable"]
866
- });
867
- Object.defineProperty(this, "lc_serializable", {
868
- enumerable: true,
869
- configurable: true,
870
- writable: true,
871
- value: true
872
- });
873
- Object.defineProperty(this, "steps", {
874
- enumerable: true,
875
- configurable: true,
876
- writable: true,
877
- value: void 0
878
- });
879
- this.steps = {};
880
- for (const [key, value] of Object.entries(fields.steps)) {
881
- this.steps[key] = _coerceToRunnable(value);
882
- }
883
- }
884
- static from(steps) {
885
- return new RunnableMap({ steps });
886
- }
887
- async invoke(input, options
888
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
889
- ) {
890
- const callbackManager_ = await getCallbackMangerForConfig(options);
891
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
892
- input,
893
- }, undefined, undefined, undefined, undefined, options?.runName);
894
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
895
- const output = {};
896
- try {
897
- await Promise.all(Object.entries(this.steps).map(async ([key, runnable]) => {
898
- output[key] = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild(key)));
899
- }));
900
- }
901
- catch (e) {
902
- await runManager?.handleChainError(e);
903
- throw e;
904
- }
905
- await runManager?.handleChainEnd(output);
906
- return output;
907
- }
908
- }
909
- /**
910
- * A runnable that runs a callable.
911
- */
912
- export class RunnableLambda extends Runnable {
913
- static lc_name() {
914
- return "RunnableLambda";
915
- }
916
- constructor(fields) {
917
- super(fields);
918
- Object.defineProperty(this, "lc_namespace", {
919
- enumerable: true,
920
- configurable: true,
921
- writable: true,
922
- value: ["langchain", "schema", "runnable"]
923
- });
924
- Object.defineProperty(this, "func", {
925
- enumerable: true,
926
- configurable: true,
927
- writable: true,
928
- value: void 0
929
- });
930
- this.func = fields.func;
931
- }
932
- static from(func) {
933
- return new RunnableLambda({
934
- func,
935
- });
936
- }
937
- async _invoke(input, config, runManager) {
938
- let output = await this.func(input);
939
- if (output && Runnable.isRunnable(output)) {
940
- output = await output.invoke(input, this._patchConfig(config, runManager?.getChild()));
941
- }
942
- return output;
943
- }
944
- async invoke(input, options) {
945
- return this._callWithConfig(this._invoke, input, options);
946
- }
947
- }
948
- /**
949
- * A Runnable that can fallback to other Runnables if it fails.
950
- */
951
- export class RunnableWithFallbacks extends Runnable {
952
- static lc_name() {
953
- return "RunnableWithFallbacks";
954
- }
955
- constructor(fields) {
956
- super(fields);
957
- Object.defineProperty(this, "lc_namespace", {
958
- enumerable: true,
959
- configurable: true,
960
- writable: true,
961
- value: ["langchain", "schema", "runnable"]
962
- });
963
- Object.defineProperty(this, "lc_serializable", {
964
- enumerable: true,
965
- configurable: true,
966
- writable: true,
967
- value: true
968
- });
969
- Object.defineProperty(this, "runnable", {
970
- enumerable: true,
971
- configurable: true,
972
- writable: true,
973
- value: void 0
974
- });
975
- Object.defineProperty(this, "fallbacks", {
976
- enumerable: true,
977
- configurable: true,
978
- writable: true,
979
- value: void 0
980
- });
981
- this.runnable = fields.runnable;
982
- this.fallbacks = fields.fallbacks;
983
- }
984
- *runnables() {
985
- yield this.runnable;
986
- for (const fallback of this.fallbacks) {
987
- yield fallback;
988
- }
989
- }
990
- async invoke(input, options) {
991
- const callbackManager_ = await CallbackManager.configure(options?.callbacks, undefined, options?.tags, undefined, options?.metadata);
992
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
993
- let firstError;
994
- for (const runnable of this.runnables()) {
995
- try {
996
- const output = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild()));
997
- await runManager?.handleChainEnd(_coerceToDict(output, "output"));
998
- return output;
999
- }
1000
- catch (e) {
1001
- if (firstError === undefined) {
1002
- firstError = e;
1003
- }
1004
- }
1005
- }
1006
- if (firstError === undefined) {
1007
- throw new Error("No error stored at end of fallback.");
1008
- }
1009
- await runManager?.handleChainError(firstError);
1010
- throw firstError;
1011
- }
1012
- async batch(inputs, options, batchOptions) {
1013
- if (batchOptions?.returnExceptions) {
1014
- throw new Error("Not implemented.");
1015
- }
1016
- const configList = this._getOptionsList(options ?? {}, inputs.length);
1017
- const callbackManagers = await Promise.all(configList.map((config) => CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata)));
1018
- const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
1019
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1020
- let firstError;
1021
- for (const runnable of this.runnables()) {
1022
- try {
1023
- const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild())), batchOptions);
1024
- await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));
1025
- return outputs;
1026
- }
1027
- catch (e) {
1028
- if (firstError === undefined) {
1029
- firstError = e;
1030
- }
1031
- }
1032
- }
1033
- if (!firstError) {
1034
- throw new Error("No error stored at end of fallbacks.");
1035
- }
1036
- await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(firstError)));
1037
- throw firstError;
1038
- }
1039
- }
1040
- // TODO: Figure out why the compiler needs help eliminating Error as a RunOutput type
1041
- export function _coerceToRunnable(coerceable) {
1042
- if (typeof coerceable === "function") {
1043
- return new RunnableLambda({ func: coerceable });
1044
- }
1045
- else if (Runnable.isRunnable(coerceable)) {
1046
- return coerceable;
1047
- }
1048
- else if (!Array.isArray(coerceable) && typeof coerceable === "object") {
1049
- const runnables = {};
1050
- for (const [key, value] of Object.entries(coerceable)) {
1051
- runnables[key] = _coerceToRunnable(value);
1052
- }
1053
- return new RunnableMap({
1054
- steps: runnables,
1055
- });
1056
- }
1057
- else {
1058
- throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
1059
- }
1060
- }
1
+ export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableLambda, RunnableWithFallbacks, _coerceToRunnable, } from "langchain-core/runnables";