langchain 0.0.146 → 0.0.147
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.
- package/dist/base_language/index.cjs +2 -2
- package/dist/base_language/index.d.ts +2 -1
- package/dist/base_language/index.js +1 -1
- package/dist/chains/base.d.ts +1 -1
- package/dist/chains/openai_functions/openapi.cjs +32 -27
- package/dist/chains/openai_functions/openapi.d.ts +9 -0
- package/dist/chains/openai_functions/openapi.js +31 -27
- package/dist/chat_models/base.d.ts +1 -1
- package/dist/chat_models/openai.cjs +1 -1
- package/dist/chat_models/openai.js +1 -1
- package/dist/experimental/llms/bittensor.cjs +141 -0
- package/dist/experimental/llms/bittensor.d.ts +33 -0
- package/dist/experimental/llms/bittensor.js +137 -0
- package/dist/hub.d.ts +1 -1
- package/dist/llms/base.d.ts +1 -1
- package/dist/llms/openai-chat.cjs +1 -1
- package/dist/llms/openai-chat.js +1 -1
- package/dist/llms/openai.cjs +1 -1
- package/dist/llms/openai.js +1 -1
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/load/import_map.cjs +1 -1
- package/dist/load/import_map.d.ts +1 -1
- package/dist/load/import_map.js +1 -1
- package/dist/load/index.cjs +2 -1
- package/dist/load/index.js +2 -1
- package/dist/prompts/base.cjs +2 -2
- package/dist/prompts/base.d.ts +1 -1
- package/dist/prompts/base.js +1 -1
- package/dist/prompts/chat.cjs +2 -2
- package/dist/prompts/chat.d.ts +1 -1
- package/dist/prompts/chat.js +1 -1
- package/dist/schema/document.cjs +2 -2
- package/dist/schema/document.d.ts +1 -1
- package/dist/schema/document.js +1 -1
- package/dist/schema/output_parser.cjs +2 -2
- package/dist/schema/output_parser.d.ts +2 -1
- package/dist/schema/output_parser.js +1 -1
- package/dist/schema/retriever.cjs +2 -2
- package/dist/schema/retriever.d.ts +2 -1
- package/dist/schema/retriever.js +1 -1
- package/dist/schema/runnable/config.cjs +8 -0
- package/dist/schema/runnable/config.d.ts +3 -0
- package/dist/schema/runnable/config.js +4 -0
- package/dist/schema/{runnable.cjs → runnable/index.cjs} +290 -101
- package/dist/schema/{runnable.d.ts → runnable/index.d.ts} +127 -41
- package/dist/schema/{runnable.js → runnable/index.js} +284 -99
- package/dist/tools/base.d.ts +1 -1
- package/dist/util/async_caller.cjs +35 -25
- package/dist/util/async_caller.d.ts +8 -0
- package/dist/util/async_caller.js +35 -25
- package/dist/vectorstores/pinecone.cjs +30 -22
- package/dist/vectorstores/pinecone.d.ts +3 -1
- package/dist/vectorstores/pinecone.js +30 -22
- package/dist/vectorstores/vectara.cjs +20 -23
- package/dist/vectorstores/vectara.d.ts +9 -2
- package/dist/vectorstores/vectara.js +20 -23
- package/experimental/llms/bittensor.cjs +1 -0
- package/experimental/llms/bittensor.d.ts +1 -0
- package/experimental/llms/bittensor.js +1 -0
- package/package.json +9 -1
- package/schema/runnable.cjs +1 -1
- package/schema/runnable.d.ts +1 -1
- package/schema/runnable.js +1 -1
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunnableWithFallbacks = exports.RouterRunnable = exports.
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
6
|
+
exports.RunnableWithFallbacks = exports.RouterRunnable = exports.RunnablePassthrough = exports.RunnableLambda = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableBinding = exports.Runnable = void 0;
|
|
7
|
+
const p_retry_1 = __importDefault(require("p-retry"));
|
|
8
|
+
const manager_js_1 = require("../../callbacks/manager.cjs");
|
|
9
|
+
const serializable_js_1 = require("../../load/serializable.cjs");
|
|
10
|
+
const stream_js_1 = require("../../util/stream.cjs");
|
|
11
|
+
const config_js_1 = require("./config.cjs");
|
|
12
|
+
const async_caller_js_1 = require("../../util/async_caller.cjs");
|
|
7
13
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
14
|
function _coerceToDict(value, defaultKey) {
|
|
9
15
|
return value && !Array.isArray(value) && typeof value === "object"
|
|
@@ -33,6 +39,20 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
33
39
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
34
40
|
return new RunnableBinding({ bound: this, kwargs });
|
|
35
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Bind arguments to a Runnable, returning a new Runnable.
|
|
44
|
+
* @param kwargs
|
|
45
|
+
* @returns A new RunnableBinding that, when invoked, will apply the bound args.
|
|
46
|
+
*/
|
|
47
|
+
withRetry(fields) {
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
49
|
+
return new RunnableRetry({
|
|
50
|
+
bound: this,
|
|
51
|
+
kwargs: {},
|
|
52
|
+
maxAttemptNumber: fields?.stopAfterAttempt,
|
|
53
|
+
...fields,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
36
56
|
/**
|
|
37
57
|
* Create a new runnable from the current one that will try invoking
|
|
38
58
|
* other passed fallback runnables if the initial invocation fails.
|
|
@@ -55,28 +75,27 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
55
75
|
}
|
|
56
76
|
return Array.from({ length }, () => options);
|
|
57
77
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Default implementation of batch, which calls invoke N times.
|
|
60
|
-
* Subclasses should override this method if they can batch more efficiently.
|
|
61
|
-
* @param inputs Array of inputs to each batch call.
|
|
62
|
-
* @param options Either a single call options object to apply to each batch call or an array for each call.
|
|
63
|
-
* @param batchOptions.maxConcurrency Maximum number of calls to run at once.
|
|
64
|
-
* @returns An array of RunOutputs
|
|
65
|
-
*/
|
|
66
78
|
async batch(inputs, options, batchOptions) {
|
|
67
79
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
:
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
const caller = new async_caller_js_1.AsyncCaller({
|
|
81
|
+
maxConcurrency: batchOptions?.maxConcurrency,
|
|
82
|
+
onFailedAttempt: (e) => {
|
|
83
|
+
throw e;
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
const batchCalls = inputs.map((input, i) => caller.call(async () => {
|
|
87
|
+
try {
|
|
88
|
+
const result = await this.invoke(input, configList[i]);
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
if (batchOptions?.returnExceptions) {
|
|
93
|
+
return e;
|
|
94
|
+
}
|
|
95
|
+
throw e;
|
|
96
|
+
}
|
|
97
|
+
}));
|
|
98
|
+
return Promise.all(batchCalls);
|
|
80
99
|
}
|
|
81
100
|
/**
|
|
82
101
|
* Default streaming implementation.
|
|
@@ -109,11 +128,11 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
109
128
|
return [runnableConfig, callOptions];
|
|
110
129
|
}
|
|
111
130
|
async _callWithConfig(func, input, options) {
|
|
112
|
-
const callbackManager_ = await
|
|
131
|
+
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
113
132
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType);
|
|
114
133
|
let output;
|
|
115
134
|
try {
|
|
116
|
-
output = await func.bind(this)(input);
|
|
135
|
+
output = await func.bind(this)(input, options, runManager);
|
|
117
136
|
}
|
|
118
137
|
catch (e) {
|
|
119
138
|
await runManager?.handleChainError(e);
|
|
@@ -122,6 +141,30 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
122
141
|
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
123
142
|
return output;
|
|
124
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Internal method that handles batching and configuration for a runnable
|
|
146
|
+
* It takes a function, input values, and optional configuration, and
|
|
147
|
+
* returns a promise that resolves to the output values.
|
|
148
|
+
* @param func The function to be executed for each input value.
|
|
149
|
+
* @param input The input values to be processed.
|
|
150
|
+
* @param config Optional configuration for the function execution.
|
|
151
|
+
* @returns A promise that resolves to the output values.
|
|
152
|
+
*/
|
|
153
|
+
async _batchWithConfig(func, inputs, options, batchOptions) {
|
|
154
|
+
const configs = this._getOptionsList((options ?? {}), inputs.length);
|
|
155
|
+
const callbackManagers = await Promise.all(configs.map(config_js_1.getCallbackMangerForConfig));
|
|
156
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
157
|
+
let outputs;
|
|
158
|
+
try {
|
|
159
|
+
outputs = await func(inputs, configs, runManagers, batchOptions);
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
|
|
163
|
+
throw e;
|
|
164
|
+
}
|
|
165
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(outputs, "output"))));
|
|
166
|
+
return outputs;
|
|
167
|
+
}
|
|
125
168
|
/**
|
|
126
169
|
* Helper method to transform an Iterator of Input values into an Iterator of
|
|
127
170
|
* Output values, with callbacks.
|
|
@@ -132,7 +175,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
132
175
|
let finalInputSupported = true;
|
|
133
176
|
let finalOutput;
|
|
134
177
|
let finalOutputSupported = true;
|
|
135
|
-
const callbackManager_ = await
|
|
178
|
+
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
136
179
|
let runManager;
|
|
137
180
|
const serializedRepresentation = this.toJSON();
|
|
138
181
|
async function* wrapInputForTracing() {
|
|
@@ -206,12 +249,215 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
206
249
|
last: _coerceToRunnable(coerceable),
|
|
207
250
|
});
|
|
208
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* Default implementation of transform, which buffers input and then calls stream.
|
|
254
|
+
* Subclasses should override this method if they can start producing output while
|
|
255
|
+
* input is still being generated.
|
|
256
|
+
* @param generator
|
|
257
|
+
* @param options
|
|
258
|
+
*/
|
|
259
|
+
async *transform(generator, options) {
|
|
260
|
+
let finalChunk;
|
|
261
|
+
for await (const chunk of generator) {
|
|
262
|
+
if (!finalChunk) {
|
|
263
|
+
finalChunk = chunk;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
// Make a best effort to gather, for any type that supports concat.
|
|
267
|
+
// This method should throw an error if gathering fails.
|
|
268
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
269
|
+
finalChunk = finalChunk.concat(chunk);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
yield* this._streamIterator(finalChunk, options);
|
|
273
|
+
}
|
|
209
274
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
210
275
|
static isRunnable(thing) {
|
|
211
276
|
return thing.lc_runnable;
|
|
212
277
|
}
|
|
213
278
|
}
|
|
214
279
|
exports.Runnable = Runnable;
|
|
280
|
+
/**
|
|
281
|
+
* A runnable that delegates calls to another runnable with a set of kwargs.
|
|
282
|
+
*/
|
|
283
|
+
class RunnableBinding extends Runnable {
|
|
284
|
+
static lc_name() {
|
|
285
|
+
return "RunnableBinding";
|
|
286
|
+
}
|
|
287
|
+
constructor(fields) {
|
|
288
|
+
super(fields);
|
|
289
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
290
|
+
enumerable: true,
|
|
291
|
+
configurable: true,
|
|
292
|
+
writable: true,
|
|
293
|
+
value: ["langchain", "schema", "runnable"]
|
|
294
|
+
});
|
|
295
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
296
|
+
enumerable: true,
|
|
297
|
+
configurable: true,
|
|
298
|
+
writable: true,
|
|
299
|
+
value: true
|
|
300
|
+
});
|
|
301
|
+
Object.defineProperty(this, "bound", {
|
|
302
|
+
enumerable: true,
|
|
303
|
+
configurable: true,
|
|
304
|
+
writable: true,
|
|
305
|
+
value: void 0
|
|
306
|
+
});
|
|
307
|
+
Object.defineProperty(this, "kwargs", {
|
|
308
|
+
enumerable: true,
|
|
309
|
+
configurable: true,
|
|
310
|
+
writable: true,
|
|
311
|
+
value: void 0
|
|
312
|
+
});
|
|
313
|
+
this.bound = fields.bound;
|
|
314
|
+
this.kwargs = fields.kwargs;
|
|
315
|
+
}
|
|
316
|
+
bind(kwargs) {
|
|
317
|
+
return new RunnableBinding({
|
|
318
|
+
bound: this.bound,
|
|
319
|
+
kwargs: { ...this.kwargs, ...kwargs },
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
async invoke(input, options) {
|
|
323
|
+
return this.bound.invoke(input, { ...options, ...this.kwargs });
|
|
324
|
+
}
|
|
325
|
+
async batch(inputs, options, batchOptions) {
|
|
326
|
+
const mergedOptions = Array.isArray(options)
|
|
327
|
+
? options.map((individualOption) => ({
|
|
328
|
+
...individualOption,
|
|
329
|
+
...this.kwargs,
|
|
330
|
+
}))
|
|
331
|
+
: { ...options, ...this.kwargs };
|
|
332
|
+
return this.bound.batch(inputs, mergedOptions, batchOptions);
|
|
333
|
+
}
|
|
334
|
+
async *_streamIterator(input, options) {
|
|
335
|
+
yield* this.bound._streamIterator(input, { ...options, ...this.kwargs });
|
|
336
|
+
}
|
|
337
|
+
async stream(input, options) {
|
|
338
|
+
return this.bound.stream(input, { ...options, ...this.kwargs });
|
|
339
|
+
}
|
|
340
|
+
async *transform(
|
|
341
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
342
|
+
generator, options) {
|
|
343
|
+
yield* this.bound.transform(generator, options);
|
|
344
|
+
}
|
|
345
|
+
static isRunnableBinding(
|
|
346
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
347
|
+
thing
|
|
348
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
349
|
+
) {
|
|
350
|
+
return thing.bound && Runnable.isRunnable(thing.bound);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
exports.RunnableBinding = RunnableBinding;
|
|
354
|
+
/**
|
|
355
|
+
* Base class for runnables that can be retried a
|
|
356
|
+
* specified number of times.
|
|
357
|
+
*/
|
|
358
|
+
class RunnableRetry extends RunnableBinding {
|
|
359
|
+
static lc_name() {
|
|
360
|
+
return "RunnableRetry";
|
|
361
|
+
}
|
|
362
|
+
constructor(fields) {
|
|
363
|
+
super(fields);
|
|
364
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
365
|
+
enumerable: true,
|
|
366
|
+
configurable: true,
|
|
367
|
+
writable: true,
|
|
368
|
+
value: ["langchain", "schema", "runnable"]
|
|
369
|
+
});
|
|
370
|
+
Object.defineProperty(this, "maxAttemptNumber", {
|
|
371
|
+
enumerable: true,
|
|
372
|
+
configurable: true,
|
|
373
|
+
writable: true,
|
|
374
|
+
value: 3
|
|
375
|
+
});
|
|
376
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
377
|
+
Object.defineProperty(this, "onFailedAttempt", {
|
|
378
|
+
enumerable: true,
|
|
379
|
+
configurable: true,
|
|
380
|
+
writable: true,
|
|
381
|
+
value: () => { }
|
|
382
|
+
});
|
|
383
|
+
this.maxAttemptNumber = fields.maxAttemptNumber ?? this.maxAttemptNumber;
|
|
384
|
+
this.onFailedAttempt = fields.onFailedAttempt ?? this.onFailedAttempt;
|
|
385
|
+
}
|
|
386
|
+
_patchConfigForRetry(attempt, config, runManager) {
|
|
387
|
+
const tag = attempt > 1 ? `retry:attempt:${attempt}` : undefined;
|
|
388
|
+
return this._patchConfig(config, runManager?.getChild(tag));
|
|
389
|
+
}
|
|
390
|
+
async _invoke(input, config, runManager) {
|
|
391
|
+
return (0, p_retry_1.default)((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
|
|
392
|
+
onFailedAttempt: this.onFailedAttempt,
|
|
393
|
+
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
394
|
+
randomize: true,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Method that invokes the runnable with the specified input, run manager,
|
|
399
|
+
* and config. It handles the retry logic by catching any errors and
|
|
400
|
+
* recursively invoking itself with the updated config for the next retry
|
|
401
|
+
* attempt.
|
|
402
|
+
* @param input The input for the runnable.
|
|
403
|
+
* @param runManager The run manager for the runnable.
|
|
404
|
+
* @param config The config for the runnable.
|
|
405
|
+
* @returns A promise that resolves to the output of the runnable.
|
|
406
|
+
*/
|
|
407
|
+
async invoke(input, config) {
|
|
408
|
+
return this._callWithConfig(this._invoke, input, config);
|
|
409
|
+
}
|
|
410
|
+
async _batch(inputs, configs, runManagers, batchOptions) {
|
|
411
|
+
const resultsMap = {};
|
|
412
|
+
try {
|
|
413
|
+
await (0, p_retry_1.default)(async (attemptNumber) => {
|
|
414
|
+
const remainingIndexes = inputs
|
|
415
|
+
.map((_, i) => i)
|
|
416
|
+
.filter((i) => resultsMap[i.toString()] === undefined ||
|
|
417
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
418
|
+
resultsMap[i.toString()] instanceof Error);
|
|
419
|
+
const remainingInputs = remainingIndexes.map((i) => inputs[i]);
|
|
420
|
+
const patchedConfigs = remainingIndexes.map((i) => this._patchConfigForRetry(attemptNumber, configs?.[i], runManagers?.[i]));
|
|
421
|
+
const results = await super.batch(remainingInputs, patchedConfigs, {
|
|
422
|
+
...batchOptions,
|
|
423
|
+
returnExceptions: true,
|
|
424
|
+
});
|
|
425
|
+
let firstException;
|
|
426
|
+
for (let i = 0; i < results.length; i += 1) {
|
|
427
|
+
const result = results[i];
|
|
428
|
+
const resultMapIndex = remainingIndexes[i];
|
|
429
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
430
|
+
if (result instanceof Error) {
|
|
431
|
+
if (firstException === undefined) {
|
|
432
|
+
firstException = result;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
resultsMap[resultMapIndex.toString()] = result;
|
|
436
|
+
}
|
|
437
|
+
if (firstException) {
|
|
438
|
+
throw firstException;
|
|
439
|
+
}
|
|
440
|
+
return results;
|
|
441
|
+
}, {
|
|
442
|
+
onFailedAttempt: this.onFailedAttempt,
|
|
443
|
+
retries: Math.max(this.maxAttemptNumber - 1, 0),
|
|
444
|
+
randomize: true,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
catch (e) {
|
|
448
|
+
if (batchOptions?.returnExceptions !== true) {
|
|
449
|
+
throw e;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return Object.keys(resultsMap)
|
|
453
|
+
.sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
|
|
454
|
+
.map((key) => resultsMap[parseInt(key, 10)]);
|
|
455
|
+
}
|
|
456
|
+
async batch(inputs, options, batchOptions) {
|
|
457
|
+
return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
exports.RunnableRetry = RunnableRetry;
|
|
215
461
|
/**
|
|
216
462
|
* A sequence of runnables, where the output of each is the input of the next.
|
|
217
463
|
*/
|
|
@@ -260,7 +506,7 @@ class RunnableSequence extends Runnable {
|
|
|
260
506
|
return [this.first, ...this.middle, this.last];
|
|
261
507
|
}
|
|
262
508
|
async invoke(input, options) {
|
|
263
|
-
const callbackManager_ = await
|
|
509
|
+
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
264
510
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
265
511
|
let nextStepInput = input;
|
|
266
512
|
let finalOutput;
|
|
@@ -280,7 +526,7 @@ class RunnableSequence extends Runnable {
|
|
|
280
526
|
}
|
|
281
527
|
async batch(inputs, options, batchOptions) {
|
|
282
528
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
283
|
-
const callbackManagers = await Promise.all(configList.map(
|
|
529
|
+
const callbackManagers = await Promise.all(configList.map(config_js_1.getCallbackMangerForConfig));
|
|
284
530
|
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
285
531
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
286
532
|
let nextStepInputs = inputs;
|
|
@@ -300,17 +546,20 @@ class RunnableSequence extends Runnable {
|
|
|
300
546
|
return finalOutputs;
|
|
301
547
|
}
|
|
302
548
|
async *_streamIterator(input, options) {
|
|
303
|
-
const callbackManager_ = await
|
|
549
|
+
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
304
550
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
305
551
|
let nextStepInput = input;
|
|
306
552
|
const steps = [this.first, ...this.middle, this.last];
|
|
307
|
-
// Find the index of the last runnable in the sequence that doesn't have
|
|
553
|
+
// Find the index of the last runnable in the sequence that doesn't have an overridden .transform() method
|
|
308
554
|
// and start streaming from there
|
|
309
|
-
const streamingStartStepIndex = steps.length -
|
|
310
|
-
[...steps]
|
|
311
|
-
.
|
|
312
|
-
|
|
313
|
-
|
|
555
|
+
const streamingStartStepIndex = Math.min(steps.length - 1, steps.length -
|
|
556
|
+
[...steps].reverse().findIndex((step) => {
|
|
557
|
+
const isDefaultImplementation = step.transform === Runnable.prototype.transform;
|
|
558
|
+
const boundRunnableIsDefaultImplementation = RunnableBinding.isRunnableBinding(step) &&
|
|
559
|
+
step.bound?.transform === Runnable.prototype.transform;
|
|
560
|
+
return (isDefaultImplementation || boundRunnableIsDefaultImplementation);
|
|
561
|
+
}) -
|
|
562
|
+
1);
|
|
314
563
|
try {
|
|
315
564
|
for (const step of steps.slice(0, streamingStartStepIndex)) {
|
|
316
565
|
nextStepInput = await step.invoke(nextStepInput, this._patchConfig(options, runManager?.getChild()));
|
|
@@ -325,7 +574,6 @@ class RunnableSequence extends Runnable {
|
|
|
325
574
|
try {
|
|
326
575
|
let finalGenerator = await steps[streamingStartStepIndex]._streamIterator(nextStepInput, this._patchConfig(options, runManager?.getChild()));
|
|
327
576
|
for (const step of steps.slice(streamingStartStepIndex + 1)) {
|
|
328
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
329
577
|
finalGenerator = await step.transform(finalGenerator, this._patchConfig(options, runManager?.getChild()));
|
|
330
578
|
}
|
|
331
579
|
for await (const chunk of finalGenerator) {
|
|
@@ -422,7 +670,7 @@ class RunnableMap extends Runnable {
|
|
|
422
670
|
async invoke(input, options
|
|
423
671
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
424
672
|
) {
|
|
425
|
-
const callbackManager_ = await
|
|
673
|
+
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
426
674
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
|
|
427
675
|
input,
|
|
428
676
|
});
|
|
@@ -498,68 +746,6 @@ class RunnablePassthrough extends Runnable {
|
|
|
498
746
|
}
|
|
499
747
|
}
|
|
500
748
|
exports.RunnablePassthrough = RunnablePassthrough;
|
|
501
|
-
/**
|
|
502
|
-
* A runnable that delegates calls to another runnable with a set of kwargs.
|
|
503
|
-
*/
|
|
504
|
-
class RunnableBinding extends Runnable {
|
|
505
|
-
static lc_name() {
|
|
506
|
-
return "RunnableBinding";
|
|
507
|
-
}
|
|
508
|
-
constructor(fields) {
|
|
509
|
-
super(fields);
|
|
510
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
511
|
-
enumerable: true,
|
|
512
|
-
configurable: true,
|
|
513
|
-
writable: true,
|
|
514
|
-
value: ["langchain", "schema", "runnable"]
|
|
515
|
-
});
|
|
516
|
-
Object.defineProperty(this, "lc_serializable", {
|
|
517
|
-
enumerable: true,
|
|
518
|
-
configurable: true,
|
|
519
|
-
writable: true,
|
|
520
|
-
value: true
|
|
521
|
-
});
|
|
522
|
-
Object.defineProperty(this, "bound", {
|
|
523
|
-
enumerable: true,
|
|
524
|
-
configurable: true,
|
|
525
|
-
writable: true,
|
|
526
|
-
value: void 0
|
|
527
|
-
});
|
|
528
|
-
Object.defineProperty(this, "kwargs", {
|
|
529
|
-
enumerable: true,
|
|
530
|
-
configurable: true,
|
|
531
|
-
writable: true,
|
|
532
|
-
value: void 0
|
|
533
|
-
});
|
|
534
|
-
this.bound = fields.bound;
|
|
535
|
-
this.kwargs = fields.kwargs;
|
|
536
|
-
}
|
|
537
|
-
bind(kwargs) {
|
|
538
|
-
return new RunnableBinding({
|
|
539
|
-
bound: this.bound,
|
|
540
|
-
kwargs: { ...this.kwargs, ...kwargs },
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
async invoke(input, options) {
|
|
544
|
-
return this.bound.invoke(input, { ...options, ...this.kwargs });
|
|
545
|
-
}
|
|
546
|
-
async batch(inputs, options, batchOptions) {
|
|
547
|
-
const mergedOptions = Array.isArray(options)
|
|
548
|
-
? options.map((individualOption) => ({
|
|
549
|
-
...individualOption,
|
|
550
|
-
...this.kwargs,
|
|
551
|
-
}))
|
|
552
|
-
: { ...options, ...this.kwargs };
|
|
553
|
-
return this.bound.batch(inputs, mergedOptions, batchOptions);
|
|
554
|
-
}
|
|
555
|
-
async *_streamIterator(input, options) {
|
|
556
|
-
yield* this.bound._streamIterator(input, { ...options, ...this.kwargs });
|
|
557
|
-
}
|
|
558
|
-
async stream(input, options) {
|
|
559
|
-
return this.bound.stream(input, { ...options, ...this.kwargs });
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
exports.RunnableBinding = RunnableBinding;
|
|
563
749
|
/**
|
|
564
750
|
* A runnable that routes to a set of runnables based on Input['key'].
|
|
565
751
|
* Returns the output of the selected runnable.
|
|
@@ -720,6 +906,7 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
720
906
|
}
|
|
721
907
|
}
|
|
722
908
|
exports.RunnableWithFallbacks = RunnableWithFallbacks;
|
|
909
|
+
// TODO: Figure out why the compiler needs help eliminating Error as a RunOutput type
|
|
723
910
|
function _coerceToRunnable(coerceable) {
|
|
724
911
|
if (typeof coerceable === "function") {
|
|
725
912
|
return new RunnableLambda({ func: coerceable });
|
|
@@ -732,7 +919,9 @@ function _coerceToRunnable(coerceable) {
|
|
|
732
919
|
for (const [key, value] of Object.entries(coerceable)) {
|
|
733
920
|
runnables[key] = _coerceToRunnable(value);
|
|
734
921
|
}
|
|
735
|
-
return new RunnableMap({
|
|
922
|
+
return new RunnableMap({
|
|
923
|
+
steps: runnables,
|
|
924
|
+
});
|
|
736
925
|
}
|
|
737
926
|
else {
|
|
738
927
|
throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
|