langchain 0.0.148 → 0.0.149

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 (32) hide show
  1. package/dist/chains/question_answering/load.cjs +12 -4
  2. package/dist/chains/question_answering/load.d.ts +2 -0
  3. package/dist/chains/question_answering/load.js +12 -4
  4. package/dist/chains/summarization/load.cjs +8 -4
  5. package/dist/chains/summarization/load.d.ts +2 -0
  6. package/dist/chains/summarization/load.js +8 -4
  7. package/dist/llms/bedrock.cjs +9 -1
  8. package/dist/llms/bedrock.d.ts +3 -0
  9. package/dist/llms/bedrock.js +9 -1
  10. package/dist/retrievers/self_query/base.cjs +1 -1
  11. package/dist/retrievers/self_query/base.js +2 -2
  12. package/dist/retrievers/self_query/functional.cjs +1 -1
  13. package/dist/retrievers/self_query/functional.js +2 -2
  14. package/dist/retrievers/self_query/utils.cjs +46 -6
  15. package/dist/retrievers/self_query/utils.d.ts +7 -0
  16. package/dist/retrievers/self_query/utils.js +44 -5
  17. package/dist/schema/runnable/base.cjs +910 -0
  18. package/dist/schema/runnable/base.d.ts +300 -0
  19. package/dist/schema/runnable/base.js +896 -0
  20. package/dist/schema/runnable/index.cjs +19 -926
  21. package/dist/schema/runnable/index.d.ts +4 -298
  22. package/dist/schema/runnable/index.js +3 -914
  23. package/dist/schema/runnable/passthrough.cjs +31 -0
  24. package/dist/schema/runnable/passthrough.d.ts +11 -0
  25. package/dist/schema/runnable/passthrough.js +27 -0
  26. package/dist/schema/runnable/router.cjs +74 -0
  27. package/dist/schema/runnable/router.d.ts +29 -0
  28. package/dist/schema/runnable/router.js +70 -0
  29. package/dist/vectorstores/opensearch.cjs +4 -2
  30. package/dist/vectorstores/opensearch.d.ts +4 -1
  31. package/dist/vectorstores/opensearch.js +4 -2
  32. package/package.json +1 -1
@@ -0,0 +1,896 @@
1
+ import pRetry from "p-retry";
2
+ import { CallbackManager, } from "../../callbacks/manager.js";
3
+ import { Serializable } from "../../load/serializable.js";
4
+ import { IterableReadableStream } from "../../util/stream.js";
5
+ import { getCallbackMangerForConfig } from "./config.js";
6
+ import { AsyncCaller } from "../../util/async_caller.js";
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ function _coerceToDict(value, defaultKey) {
9
+ return value && !Array.isArray(value) && typeof value === "object"
10
+ ? value
11
+ : { [defaultKey]: value };
12
+ }
13
+ /**
14
+ * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
15
+ * transformed.
16
+ */
17
+ export class Runnable extends Serializable {
18
+ constructor() {
19
+ super(...arguments);
20
+ Object.defineProperty(this, "lc_runnable", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: true
25
+ });
26
+ }
27
+ /**
28
+ * Bind arguments to a Runnable, returning a new Runnable.
29
+ * @param kwargs
30
+ * @returns A new RunnableBinding that, when invoked, will apply the bound args.
31
+ */
32
+ bind(kwargs) {
33
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
34
+ return new RunnableBinding({ bound: this, kwargs });
35
+ }
36
+ /**
37
+ * Return a new Runnable that maps a list of inputs to a list of outputs,
38
+ * by calling invoke() with each input.
39
+ */
40
+ map() {
41
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
42
+ return new RunnableEach({ bound: this });
43
+ }
44
+ /**
45
+ * Bind arguments to a Runnable, returning a new Runnable.
46
+ * @param kwargs
47
+ * @returns A new RunnableBinding that, when invoked, will apply the bound args.
48
+ */
49
+ withRetry(fields) {
50
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
51
+ return new RunnableRetry({
52
+ bound: this,
53
+ kwargs: {},
54
+ maxAttemptNumber: fields?.stopAfterAttempt,
55
+ ...fields,
56
+ });
57
+ }
58
+ /**
59
+ * Create a new runnable from the current one that will try invoking
60
+ * other passed fallback runnables if the initial invocation fails.
61
+ * @param fields.fallbacks Other runnables to call if the runnable errors.
62
+ * @returns A new RunnableWithFallbacks.
63
+ */
64
+ withFallbacks(fields) {
65
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
66
+ return new RunnableWithFallbacks({
67
+ runnable: this,
68
+ fallbacks: fields.fallbacks,
69
+ });
70
+ }
71
+ _getOptionsList(options, length = 0) {
72
+ if (Array.isArray(options)) {
73
+ if (options.length !== length) {
74
+ throw new Error(`Passed "options" must be an array with the same length as the inputs, but got ${options.length} options for ${length} inputs`);
75
+ }
76
+ return options;
77
+ }
78
+ return Array.from({ length }, () => options);
79
+ }
80
+ async batch(inputs, options, batchOptions) {
81
+ const configList = this._getOptionsList(options ?? {}, inputs.length);
82
+ const caller = new AsyncCaller({
83
+ maxConcurrency: batchOptions?.maxConcurrency,
84
+ onFailedAttempt: (e) => {
85
+ throw e;
86
+ },
87
+ });
88
+ const batchCalls = inputs.map((input, i) => caller.call(async () => {
89
+ try {
90
+ const result = await this.invoke(input, configList[i]);
91
+ return result;
92
+ }
93
+ catch (e) {
94
+ if (batchOptions?.returnExceptions) {
95
+ return e;
96
+ }
97
+ throw e;
98
+ }
99
+ }));
100
+ return Promise.all(batchCalls);
101
+ }
102
+ /**
103
+ * Default streaming implementation.
104
+ * Subclasses should override this method if they support streaming output.
105
+ * @param input
106
+ * @param options
107
+ */
108
+ async *_streamIterator(input, options) {
109
+ yield this.invoke(input, options);
110
+ }
111
+ /**
112
+ * Stream output in chunks.
113
+ * @param input
114
+ * @param options
115
+ * @returns A readable stream that is also an iterable.
116
+ */
117
+ async stream(input, options) {
118
+ return IterableReadableStream.fromAsyncGenerator(this._streamIterator(input, options));
119
+ }
120
+ _separateRunnableConfigFromCallOptions(options = {}) {
121
+ const runnableConfig = {
122
+ callbacks: options.callbacks,
123
+ tags: options.tags,
124
+ metadata: options.metadata,
125
+ };
126
+ const callOptions = { ...options };
127
+ delete callOptions.callbacks;
128
+ delete callOptions.tags;
129
+ delete callOptions.metadata;
130
+ return [runnableConfig, callOptions];
131
+ }
132
+ async _callWithConfig(func, input, options) {
133
+ const callbackManager_ = await getCallbackMangerForConfig(options);
134
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType);
135
+ let output;
136
+ try {
137
+ output = await func.bind(this)(input, options, runManager);
138
+ }
139
+ catch (e) {
140
+ await runManager?.handleChainError(e);
141
+ throw e;
142
+ }
143
+ await runManager?.handleChainEnd(_coerceToDict(output, "output"));
144
+ return output;
145
+ }
146
+ /**
147
+ * Internal method that handles batching and configuration for a runnable
148
+ * It takes a function, input values, and optional configuration, and
149
+ * returns a promise that resolves to the output values.
150
+ * @param func The function to be executed for each input value.
151
+ * @param input The input values to be processed.
152
+ * @param config Optional configuration for the function execution.
153
+ * @returns A promise that resolves to the output values.
154
+ */
155
+ async _batchWithConfig(func, inputs, options, batchOptions) {
156
+ const configs = this._getOptionsList((options ?? {}), inputs.length);
157
+ const callbackManagers = await Promise.all(configs.map(getCallbackMangerForConfig));
158
+ const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
159
+ let outputs;
160
+ try {
161
+ outputs = await func(inputs, configs, runManagers, batchOptions);
162
+ }
163
+ catch (e) {
164
+ await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
165
+ throw e;
166
+ }
167
+ await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(outputs, "output"))));
168
+ return outputs;
169
+ }
170
+ /**
171
+ * Helper method to transform an Iterator of Input values into an Iterator of
172
+ * Output values, with callbacks.
173
+ * Use this to implement `stream()` or `transform()` in Runnable subclasses.
174
+ */
175
+ async *_transformStreamWithConfig(inputGenerator, transformer, options) {
176
+ let finalInput;
177
+ let finalInputSupported = true;
178
+ let finalOutput;
179
+ let finalOutputSupported = true;
180
+ const callbackManager_ = await getCallbackMangerForConfig(options);
181
+ let runManager;
182
+ const serializedRepresentation = this.toJSON();
183
+ async function* wrapInputForTracing() {
184
+ for await (const chunk of inputGenerator) {
185
+ if (!runManager) {
186
+ // Start the run manager AFTER the iterator starts to preserve
187
+ // tracing order
188
+ runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType);
189
+ }
190
+ if (finalInputSupported) {
191
+ if (finalInput === undefined) {
192
+ finalInput = chunk;
193
+ }
194
+ else {
195
+ try {
196
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
197
+ finalInput = finalInput.concat(chunk);
198
+ }
199
+ catch {
200
+ finalInput = undefined;
201
+ finalInputSupported = false;
202
+ }
203
+ }
204
+ }
205
+ yield chunk;
206
+ }
207
+ }
208
+ const wrappedInputGenerator = wrapInputForTracing();
209
+ try {
210
+ const outputIterator = transformer(wrappedInputGenerator, runManager, options);
211
+ for await (const chunk of outputIterator) {
212
+ yield chunk;
213
+ if (finalOutputSupported) {
214
+ if (finalOutput === undefined) {
215
+ finalOutput = chunk;
216
+ }
217
+ else {
218
+ try {
219
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
220
+ finalOutput = finalOutput.concat(chunk);
221
+ }
222
+ catch {
223
+ finalOutput = undefined;
224
+ finalOutputSupported = false;
225
+ }
226
+ }
227
+ }
228
+ }
229
+ }
230
+ catch (e) {
231
+ await runManager?.handleChainError(e, undefined, undefined, undefined, {
232
+ inputs: _coerceToDict(finalInput, "input"),
233
+ });
234
+ throw e;
235
+ }
236
+ await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
237
+ }
238
+ _patchConfig(config = {}, callbackManager = undefined) {
239
+ return { ...config, callbacks: callbackManager };
240
+ }
241
+ /**
242
+ * Create a new runnable sequence that runs each individual runnable in series,
243
+ * piping the output of one runnable into another runnable or runnable-like.
244
+ * @param coerceable A runnable, function, or object whose values are functions or runnables.
245
+ * @returns A new runnable sequence.
246
+ */
247
+ pipe(coerceable) {
248
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
249
+ return new RunnableSequence({
250
+ first: this,
251
+ last: _coerceToRunnable(coerceable),
252
+ });
253
+ }
254
+ /**
255
+ * Default implementation of transform, which buffers input and then calls stream.
256
+ * Subclasses should override this method if they can start producing output while
257
+ * input is still being generated.
258
+ * @param generator
259
+ * @param options
260
+ */
261
+ async *transform(generator, options) {
262
+ let finalChunk;
263
+ for await (const chunk of generator) {
264
+ if (!finalChunk) {
265
+ finalChunk = chunk;
266
+ }
267
+ else {
268
+ // Make a best effort to gather, for any type that supports concat.
269
+ // This method should throw an error if gathering fails.
270
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
271
+ finalChunk = finalChunk.concat(chunk);
272
+ }
273
+ }
274
+ yield* this._streamIterator(finalChunk, options);
275
+ }
276
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
277
+ static isRunnable(thing) {
278
+ return thing.lc_runnable;
279
+ }
280
+ }
281
+ /**
282
+ * A runnable that delegates calls to another runnable with a set of kwargs.
283
+ */
284
+ export class RunnableBinding extends Runnable {
285
+ static lc_name() {
286
+ return "RunnableBinding";
287
+ }
288
+ constructor(fields) {
289
+ super(fields);
290
+ Object.defineProperty(this, "lc_namespace", {
291
+ enumerable: true,
292
+ configurable: true,
293
+ writable: true,
294
+ value: ["langchain", "schema", "runnable"]
295
+ });
296
+ Object.defineProperty(this, "lc_serializable", {
297
+ enumerable: true,
298
+ configurable: true,
299
+ writable: true,
300
+ value: true
301
+ });
302
+ Object.defineProperty(this, "bound", {
303
+ enumerable: true,
304
+ configurable: true,
305
+ writable: true,
306
+ value: void 0
307
+ });
308
+ Object.defineProperty(this, "kwargs", {
309
+ enumerable: true,
310
+ configurable: true,
311
+ writable: true,
312
+ value: void 0
313
+ });
314
+ this.bound = fields.bound;
315
+ this.kwargs = fields.kwargs;
316
+ }
317
+ bind(kwargs) {
318
+ return new RunnableBinding({
319
+ bound: this.bound,
320
+ kwargs: { ...this.kwargs, ...kwargs },
321
+ });
322
+ }
323
+ async invoke(input, options) {
324
+ return this.bound.invoke(input, { ...options, ...this.kwargs });
325
+ }
326
+ async batch(inputs, options, batchOptions) {
327
+ const mergedOptions = Array.isArray(options)
328
+ ? options.map((individualOption) => ({
329
+ ...individualOption,
330
+ ...this.kwargs,
331
+ }))
332
+ : { ...options, ...this.kwargs };
333
+ return this.bound.batch(inputs, mergedOptions, batchOptions);
334
+ }
335
+ async *_streamIterator(input, options) {
336
+ yield* this.bound._streamIterator(input, { ...options, ...this.kwargs });
337
+ }
338
+ async stream(input, options) {
339
+ return this.bound.stream(input, { ...options, ...this.kwargs });
340
+ }
341
+ async *transform(
342
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
343
+ generator, options) {
344
+ yield* this.bound.transform(generator, options);
345
+ }
346
+ static isRunnableBinding(
347
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
348
+ thing
349
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
350
+ ) {
351
+ return thing.bound && Runnable.isRunnable(thing.bound);
352
+ }
353
+ }
354
+ /**
355
+ * A runnable that delegates calls to another runnable
356
+ * with each element of the input sequence.
357
+ */
358
+ export class RunnableEach extends Runnable {
359
+ static lc_name() {
360
+ return "RunnableEach";
361
+ }
362
+ constructor(fields) {
363
+ super(fields);
364
+ Object.defineProperty(this, "lc_serializable", {
365
+ enumerable: true,
366
+ configurable: true,
367
+ writable: true,
368
+ value: true
369
+ });
370
+ Object.defineProperty(this, "lc_namespace", {
371
+ enumerable: true,
372
+ configurable: true,
373
+ writable: true,
374
+ value: ["langchain", "schema", "runnable"]
375
+ });
376
+ Object.defineProperty(this, "bound", {
377
+ enumerable: true,
378
+ configurable: true,
379
+ writable: true,
380
+ value: void 0
381
+ });
382
+ this.bound = fields.bound;
383
+ }
384
+ /**
385
+ * Binds the runnable with the specified arguments.
386
+ * @param args The arguments to bind the runnable with.
387
+ * @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
388
+ */
389
+ bind(kwargs) {
390
+ return new RunnableEach({
391
+ bound: this.bound.bind(kwargs),
392
+ });
393
+ }
394
+ /**
395
+ * Invokes the runnable with the specified input and configuration.
396
+ * @param input The input to invoke the runnable with.
397
+ * @param config The configuration to invoke the runnable with.
398
+ * @returns A promise that resolves to the output of the runnable.
399
+ */
400
+ async invoke(inputs, config) {
401
+ return this._callWithConfig(this._invoke, inputs, config);
402
+ }
403
+ /**
404
+ * A helper method that is used to invoke the runnable with the specified input and configuration.
405
+ * @param input The input to invoke the runnable with.
406
+ * @param config The configuration to invoke the runnable with.
407
+ * @returns A promise that resolves to the output of the runnable.
408
+ */
409
+ async _invoke(inputs, config, runManager) {
410
+ return this.bound.batch(inputs, this._patchConfig(config, runManager?.getChild()));
411
+ }
412
+ }
413
+ /**
414
+ * Base class for runnables that can be retried a
415
+ * specified number of times.
416
+ */
417
+ export class RunnableRetry extends RunnableBinding {
418
+ static lc_name() {
419
+ return "RunnableRetry";
420
+ }
421
+ constructor(fields) {
422
+ super(fields);
423
+ Object.defineProperty(this, "lc_namespace", {
424
+ enumerable: true,
425
+ configurable: true,
426
+ writable: true,
427
+ value: ["langchain", "schema", "runnable"]
428
+ });
429
+ Object.defineProperty(this, "maxAttemptNumber", {
430
+ enumerable: true,
431
+ configurable: true,
432
+ writable: true,
433
+ value: 3
434
+ });
435
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
436
+ Object.defineProperty(this, "onFailedAttempt", {
437
+ enumerable: true,
438
+ configurable: true,
439
+ writable: true,
440
+ value: () => { }
441
+ });
442
+ this.maxAttemptNumber = fields.maxAttemptNumber ?? this.maxAttemptNumber;
443
+ this.onFailedAttempt = fields.onFailedAttempt ?? this.onFailedAttempt;
444
+ }
445
+ _patchConfigForRetry(attempt, config, runManager) {
446
+ const tag = attempt > 1 ? `retry:attempt:${attempt}` : undefined;
447
+ return this._patchConfig(config, runManager?.getChild(tag));
448
+ }
449
+ async _invoke(input, config, runManager) {
450
+ return pRetry((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
451
+ onFailedAttempt: this.onFailedAttempt,
452
+ retries: Math.max(this.maxAttemptNumber - 1, 0),
453
+ randomize: true,
454
+ });
455
+ }
456
+ /**
457
+ * Method that invokes the runnable with the specified input, run manager,
458
+ * and config. It handles the retry logic by catching any errors and
459
+ * recursively invoking itself with the updated config for the next retry
460
+ * attempt.
461
+ * @param input The input for the runnable.
462
+ * @param runManager The run manager for the runnable.
463
+ * @param config The config for the runnable.
464
+ * @returns A promise that resolves to the output of the runnable.
465
+ */
466
+ async invoke(input, config) {
467
+ return this._callWithConfig(this._invoke, input, config);
468
+ }
469
+ async _batch(inputs, configs, runManagers, batchOptions) {
470
+ const resultsMap = {};
471
+ try {
472
+ await pRetry(async (attemptNumber) => {
473
+ const remainingIndexes = inputs
474
+ .map((_, i) => i)
475
+ .filter((i) => resultsMap[i.toString()] === undefined ||
476
+ // eslint-disable-next-line no-instanceof/no-instanceof
477
+ resultsMap[i.toString()] instanceof Error);
478
+ const remainingInputs = remainingIndexes.map((i) => inputs[i]);
479
+ const patchedConfigs = remainingIndexes.map((i) => this._patchConfigForRetry(attemptNumber, configs?.[i], runManagers?.[i]));
480
+ const results = await super.batch(remainingInputs, patchedConfigs, {
481
+ ...batchOptions,
482
+ returnExceptions: true,
483
+ });
484
+ let firstException;
485
+ for (let i = 0; i < results.length; i += 1) {
486
+ const result = results[i];
487
+ const resultMapIndex = remainingIndexes[i];
488
+ // eslint-disable-next-line no-instanceof/no-instanceof
489
+ if (result instanceof Error) {
490
+ if (firstException === undefined) {
491
+ firstException = result;
492
+ }
493
+ }
494
+ resultsMap[resultMapIndex.toString()] = result;
495
+ }
496
+ if (firstException) {
497
+ throw firstException;
498
+ }
499
+ return results;
500
+ }, {
501
+ onFailedAttempt: this.onFailedAttempt,
502
+ retries: Math.max(this.maxAttemptNumber - 1, 0),
503
+ randomize: true,
504
+ });
505
+ }
506
+ catch (e) {
507
+ if (batchOptions?.returnExceptions !== true) {
508
+ throw e;
509
+ }
510
+ }
511
+ return Object.keys(resultsMap)
512
+ .sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
513
+ .map((key) => resultsMap[parseInt(key, 10)]);
514
+ }
515
+ async batch(inputs, options, batchOptions) {
516
+ return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
517
+ }
518
+ }
519
+ /**
520
+ * A sequence of runnables, where the output of each is the input of the next.
521
+ */
522
+ export class RunnableSequence extends Runnable {
523
+ static lc_name() {
524
+ return "RunnableSequence";
525
+ }
526
+ constructor(fields) {
527
+ super(fields);
528
+ Object.defineProperty(this, "first", {
529
+ enumerable: true,
530
+ configurable: true,
531
+ writable: true,
532
+ value: void 0
533
+ });
534
+ Object.defineProperty(this, "middle", {
535
+ enumerable: true,
536
+ configurable: true,
537
+ writable: true,
538
+ value: []
539
+ });
540
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
541
+ Object.defineProperty(this, "last", {
542
+ enumerable: true,
543
+ configurable: true,
544
+ writable: true,
545
+ value: void 0
546
+ });
547
+ Object.defineProperty(this, "lc_serializable", {
548
+ enumerable: true,
549
+ configurable: true,
550
+ writable: true,
551
+ value: true
552
+ });
553
+ Object.defineProperty(this, "lc_namespace", {
554
+ enumerable: true,
555
+ configurable: true,
556
+ writable: true,
557
+ value: ["langchain", "schema", "runnable"]
558
+ });
559
+ this.first = fields.first;
560
+ this.middle = fields.middle ?? this.middle;
561
+ this.last = fields.last;
562
+ }
563
+ get steps() {
564
+ return [this.first, ...this.middle, this.last];
565
+ }
566
+ async invoke(input, options) {
567
+ const callbackManager_ = await getCallbackMangerForConfig(options);
568
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
569
+ let nextStepInput = input;
570
+ let finalOutput;
571
+ try {
572
+ const initialSteps = [this.first, ...this.middle];
573
+ for (let i = 0; i < initialSteps.length; i += 1) {
574
+ const step = initialSteps[i];
575
+ nextStepInput = await step.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${i + 1}`)));
576
+ }
577
+ // TypeScript can't detect that the last output of the sequence returns RunOutput, so call it out of the loop here
578
+ finalOutput = await this.last.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${this.steps.length}`)));
579
+ }
580
+ catch (e) {
581
+ await runManager?.handleChainError(e);
582
+ throw e;
583
+ }
584
+ await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
585
+ return finalOutput;
586
+ }
587
+ async batch(inputs, options, batchOptions) {
588
+ const configList = this._getOptionsList(options ?? {}, inputs.length);
589
+ const callbackManagers = await Promise.all(configList.map(getCallbackMangerForConfig));
590
+ const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
591
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
592
+ let nextStepInputs = inputs;
593
+ let finalOutputs;
594
+ try {
595
+ const initialSteps = [this.first, ...this.middle];
596
+ for (let i = 0; i < initialSteps.length; i += 1) {
597
+ const step = initialSteps[i];
598
+ nextStepInputs = await step.batch(nextStepInputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild(`seq:step:${i + 1}`))), batchOptions);
599
+ }
600
+ finalOutputs = await this.last.batch(nextStepInputs, runManagers.map((runManager) => this._patchConfig(configList[this.steps.length - 1], runManager?.getChild(`seq:step:${this.steps.length}`))), batchOptions);
601
+ }
602
+ catch (e) {
603
+ await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
604
+ throw e;
605
+ }
606
+ await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(finalOutputs[i], "output"))));
607
+ return finalOutputs;
608
+ }
609
+ async *_streamIterator(input, options) {
610
+ const callbackManager_ = await getCallbackMangerForConfig(options);
611
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
612
+ let nextStepInput = input;
613
+ const steps = [this.first, ...this.middle, this.last];
614
+ // Find the index of the last runnable in the sequence that doesn't have an overridden .transform() method
615
+ // and start streaming from there
616
+ const streamingStartStepIndex = Math.min(steps.length - 1, steps.length -
617
+ [...steps].reverse().findIndex((step) => {
618
+ const isDefaultImplementation = step.transform === Runnable.prototype.transform;
619
+ const boundRunnableIsDefaultImplementation = RunnableBinding.isRunnableBinding(step) &&
620
+ step.bound?.transform === Runnable.prototype.transform;
621
+ return (isDefaultImplementation || boundRunnableIsDefaultImplementation);
622
+ }) -
623
+ 1);
624
+ try {
625
+ const invokeSteps = steps.slice(0, streamingStartStepIndex);
626
+ for (let i = 0; i < invokeSteps.length; i += 1) {
627
+ const step = invokeSteps[i];
628
+ nextStepInput = await step.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${i + 1}`)));
629
+ }
630
+ }
631
+ catch (e) {
632
+ await runManager?.handleChainError(e);
633
+ throw e;
634
+ }
635
+ let concatSupported = true;
636
+ let finalOutput;
637
+ try {
638
+ let finalGenerator = await steps[streamingStartStepIndex]._streamIterator(nextStepInput, this._patchConfig(options, runManager?.getChild(`seq:step:${streamingStartStepIndex + 1}`)));
639
+ const finalSteps = steps.slice(streamingStartStepIndex + 1);
640
+ for (let i = 0; i < finalSteps.length; i += 1) {
641
+ const step = finalSteps[i];
642
+ finalGenerator = await step.transform(finalGenerator, this._patchConfig(options, runManager?.getChild(`seq:step:${streamingStartStepIndex + i + 2}`)));
643
+ }
644
+ for await (const chunk of finalGenerator) {
645
+ yield chunk;
646
+ if (concatSupported) {
647
+ if (finalOutput === undefined) {
648
+ finalOutput = chunk;
649
+ }
650
+ else {
651
+ try {
652
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
653
+ finalOutput = finalOutput.concat(chunk);
654
+ }
655
+ catch (e) {
656
+ finalOutput = undefined;
657
+ concatSupported = false;
658
+ }
659
+ }
660
+ }
661
+ }
662
+ }
663
+ catch (e) {
664
+ await runManager?.handleChainError(e);
665
+ throw e;
666
+ }
667
+ await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
668
+ }
669
+ pipe(coerceable) {
670
+ if (RunnableSequence.isRunnableSequence(coerceable)) {
671
+ return new RunnableSequence({
672
+ first: this.first,
673
+ middle: this.middle.concat([
674
+ this.last,
675
+ coerceable.first,
676
+ ...coerceable.middle,
677
+ ]),
678
+ last: coerceable.last,
679
+ });
680
+ }
681
+ else {
682
+ return new RunnableSequence({
683
+ first: this.first,
684
+ middle: [...this.middle, this.last],
685
+ last: _coerceToRunnable(coerceable),
686
+ });
687
+ }
688
+ }
689
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
690
+ static isRunnableSequence(thing) {
691
+ return Array.isArray(thing.middle) && Runnable.isRunnable(thing);
692
+ }
693
+ static from([first, ...runnables]) {
694
+ return new RunnableSequence({
695
+ first: _coerceToRunnable(first),
696
+ middle: runnables.slice(0, -1).map(_coerceToRunnable),
697
+ last: _coerceToRunnable(runnables[runnables.length - 1]),
698
+ });
699
+ }
700
+ }
701
+ /**
702
+ * A runnable that runs a mapping of runnables in parallel,
703
+ * and returns a mapping of their outputs.
704
+ */
705
+ export class RunnableMap extends Runnable {
706
+ static lc_name() {
707
+ return "RunnableMap";
708
+ }
709
+ constructor(fields) {
710
+ super(fields);
711
+ Object.defineProperty(this, "lc_namespace", {
712
+ enumerable: true,
713
+ configurable: true,
714
+ writable: true,
715
+ value: ["langchain", "schema", "runnable"]
716
+ });
717
+ Object.defineProperty(this, "lc_serializable", {
718
+ enumerable: true,
719
+ configurable: true,
720
+ writable: true,
721
+ value: true
722
+ });
723
+ Object.defineProperty(this, "steps", {
724
+ enumerable: true,
725
+ configurable: true,
726
+ writable: true,
727
+ value: void 0
728
+ });
729
+ this.steps = {};
730
+ for (const [key, value] of Object.entries(fields.steps)) {
731
+ this.steps[key] = _coerceToRunnable(value);
732
+ }
733
+ }
734
+ async invoke(input, options
735
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
736
+ ) {
737
+ const callbackManager_ = await getCallbackMangerForConfig(options);
738
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
739
+ input,
740
+ });
741
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
742
+ const output = {};
743
+ try {
744
+ for (const [key, runnable] of Object.entries(this.steps)) {
745
+ const result = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild()));
746
+ output[key] = result;
747
+ }
748
+ }
749
+ catch (e) {
750
+ await runManager?.handleChainError(e);
751
+ throw e;
752
+ }
753
+ await runManager?.handleChainEnd(output);
754
+ return output;
755
+ }
756
+ }
757
+ /**
758
+ * A runnable that runs a callable.
759
+ */
760
+ export class RunnableLambda extends Runnable {
761
+ static lc_name() {
762
+ return "RunnableLambda";
763
+ }
764
+ constructor(fields) {
765
+ super(fields);
766
+ Object.defineProperty(this, "lc_namespace", {
767
+ enumerable: true,
768
+ configurable: true,
769
+ writable: true,
770
+ value: ["langchain", "schema", "runnable"]
771
+ });
772
+ Object.defineProperty(this, "func", {
773
+ enumerable: true,
774
+ configurable: true,
775
+ writable: true,
776
+ value: void 0
777
+ });
778
+ this.func = fields.func;
779
+ }
780
+ async invoke(input, options) {
781
+ return this._callWithConfig(async (input) => this.func(input), input, options);
782
+ }
783
+ }
784
+ /**
785
+ * A Runnable that can fallback to other Runnables if it fails.
786
+ */
787
+ export class RunnableWithFallbacks extends Runnable {
788
+ static lc_name() {
789
+ return "RunnableWithFallbacks";
790
+ }
791
+ constructor(fields) {
792
+ super(fields);
793
+ Object.defineProperty(this, "lc_namespace", {
794
+ enumerable: true,
795
+ configurable: true,
796
+ writable: true,
797
+ value: ["langchain", "schema", "runnable"]
798
+ });
799
+ Object.defineProperty(this, "lc_serializable", {
800
+ enumerable: true,
801
+ configurable: true,
802
+ writable: true,
803
+ value: true
804
+ });
805
+ Object.defineProperty(this, "runnable", {
806
+ enumerable: true,
807
+ configurable: true,
808
+ writable: true,
809
+ value: void 0
810
+ });
811
+ Object.defineProperty(this, "fallbacks", {
812
+ enumerable: true,
813
+ configurable: true,
814
+ writable: true,
815
+ value: void 0
816
+ });
817
+ this.runnable = fields.runnable;
818
+ this.fallbacks = fields.fallbacks;
819
+ }
820
+ *runnables() {
821
+ yield this.runnable;
822
+ for (const fallback of this.fallbacks) {
823
+ yield fallback;
824
+ }
825
+ }
826
+ async invoke(input, options) {
827
+ const callbackManager_ = await CallbackManager.configure(options?.callbacks, undefined, options?.tags, undefined, options?.metadata);
828
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
829
+ let firstError;
830
+ for (const runnable of this.runnables()) {
831
+ try {
832
+ const output = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild()));
833
+ await runManager?.handleChainEnd(_coerceToDict(output, "output"));
834
+ return output;
835
+ }
836
+ catch (e) {
837
+ if (firstError === undefined) {
838
+ firstError = e;
839
+ }
840
+ }
841
+ }
842
+ if (firstError === undefined) {
843
+ throw new Error("No error stored at end of fallback.");
844
+ }
845
+ await runManager?.handleChainError(firstError);
846
+ throw firstError;
847
+ }
848
+ async batch(inputs, options, batchOptions) {
849
+ if (batchOptions?.returnExceptions) {
850
+ throw new Error("Not implemented.");
851
+ }
852
+ const configList = this._getOptionsList(options ?? {}, inputs.length);
853
+ const callbackManagers = await Promise.all(configList.map((config) => CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata)));
854
+ const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
855
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
856
+ let firstError;
857
+ for (const runnable of this.runnables()) {
858
+ try {
859
+ const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild())), batchOptions);
860
+ await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));
861
+ return outputs;
862
+ }
863
+ catch (e) {
864
+ if (firstError === undefined) {
865
+ firstError = e;
866
+ }
867
+ }
868
+ }
869
+ if (!firstError) {
870
+ throw new Error("No error stored at end of fallbacks.");
871
+ }
872
+ await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(firstError)));
873
+ throw firstError;
874
+ }
875
+ }
876
+ // TODO: Figure out why the compiler needs help eliminating Error as a RunOutput type
877
+ function _coerceToRunnable(coerceable) {
878
+ if (typeof coerceable === "function") {
879
+ return new RunnableLambda({ func: coerceable });
880
+ }
881
+ else if (Runnable.isRunnable(coerceable)) {
882
+ return coerceable;
883
+ }
884
+ else if (!Array.isArray(coerceable) && typeof coerceable === "object") {
885
+ const runnables = {};
886
+ for (const [key, value] of Object.entries(coerceable)) {
887
+ runnables[key] = _coerceToRunnable(value);
888
+ }
889
+ return new RunnableMap({
890
+ steps: runnables,
891
+ });
892
+ }
893
+ else {
894
+ throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
895
+ }
896
+ }