langchain 0.0.148 → 0.0.150

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