@langchain/core 0.1.14 → 0.1.16
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/callbacks/manager.cjs +2 -1
- package/dist/callbacks/manager.d.ts +3 -2
- package/dist/callbacks/manager.js +1 -1
- package/dist/language_models/base.d.ts +2 -2
- package/dist/retrievers.cjs +2 -1
- package/dist/retrievers.js +2 -1
- package/dist/runnables/base.cjs +76 -63
- package/dist/runnables/base.d.ts +6 -7
- package/dist/runnables/base.js +77 -64
- package/dist/runnables/branch.cjs +10 -3
- package/dist/runnables/branch.js +10 -3
- package/dist/runnables/config.cjs +67 -12
- package/dist/runnables/config.d.ts +11 -1
- package/dist/runnables/config.js +65 -12
- package/dist/runnables/index.cjs +2 -1
- package/dist/runnables/index.d.ts +1 -1
- package/dist/runnables/index.js +1 -1
- package/dist/runnables/router.cjs +2 -3
- package/dist/runnables/router.js +2 -3
- package/dist/tools.cjs +102 -1
- package/dist/tools.d.ts +49 -0
- package/dist/tools.js +99 -0
- package/dist/tracers/base.cjs +8 -1
- package/dist/tracers/base.js +8 -1
- package/dist/tracers/log_stream.cjs +34 -3
- package/dist/tracers/log_stream.d.ts +4 -0
- package/dist/tracers/log_stream.js +34 -3
- package/dist/utils/testing/index.cjs +105 -3
- package/dist/utils/testing/index.d.ts +55 -0
- package/dist/utils/testing/index.js +102 -2
- package/package.json +4 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.traceAsGroup = exports.TraceGroup = exports.CallbackManager = exports.CallbackManagerForToolRun = exports.CallbackManagerForChainRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForRetrieverRun = exports.BaseCallbackManager = exports.parseCallbackConfigArg = void 0;
|
|
3
|
+
exports.traceAsGroup = exports.TraceGroup = exports.ensureHandler = exports.CallbackManager = exports.CallbackManagerForToolRun = exports.CallbackManagerForChainRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForRetrieverRun = exports.BaseCallbackManager = exports.parseCallbackConfigArg = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
6
|
const console_js_1 = require("../tracers/console.cjs");
|
|
@@ -571,6 +571,7 @@ function ensureHandler(handler) {
|
|
|
571
571
|
}
|
|
572
572
|
return base_js_1.BaseCallbackHandler.fromMethods(handler);
|
|
573
573
|
}
|
|
574
|
+
exports.ensureHandler = ensureHandler;
|
|
574
575
|
/**
|
|
575
576
|
* @example
|
|
576
577
|
* ```typescript
|
|
@@ -50,7 +50,7 @@ export declare abstract class BaseCallbackManager {
|
|
|
50
50
|
*/
|
|
51
51
|
declare class BaseRunManager {
|
|
52
52
|
readonly runId: string;
|
|
53
|
-
|
|
53
|
+
readonly handlers: BaseCallbackHandler[];
|
|
54
54
|
protected readonly inheritableHandlers: BaseCallbackHandler[];
|
|
55
55
|
protected readonly tags: string[];
|
|
56
56
|
protected readonly inheritableTags: string[];
|
|
@@ -115,7 +115,7 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
|
|
|
115
115
|
metadata: Record<string, unknown>;
|
|
116
116
|
inheritableMetadata: Record<string, unknown>;
|
|
117
117
|
name: string;
|
|
118
|
-
|
|
118
|
+
readonly _parentRunId?: string;
|
|
119
119
|
constructor(parentRunId?: string, options?: {
|
|
120
120
|
handlers?: BaseCallbackHandler[];
|
|
121
121
|
inheritableHandlers?: BaseCallbackHandler[];
|
|
@@ -140,6 +140,7 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
|
|
|
140
140
|
static fromHandlers(handlers: CallbackHandlerMethods): CallbackManager;
|
|
141
141
|
static configure(inheritableHandlers?: Callbacks, localHandlers?: Callbacks, inheritableTags?: string[], localTags?: string[], inheritableMetadata?: Record<string, unknown>, localMetadata?: Record<string, unknown>, options?: CallbackManagerOptions): Promise<CallbackManager | undefined>;
|
|
142
142
|
}
|
|
143
|
+
export declare function ensureHandler(handler: BaseCallbackHandler | CallbackHandlerMethods): BaseCallbackHandler;
|
|
143
144
|
/**
|
|
144
145
|
* @example
|
|
145
146
|
* ```typescript
|
|
@@ -3,7 +3,7 @@ import { type BaseCache } from "../caches.js";
|
|
|
3
3
|
import { type BasePromptValueInterface } from "../prompt_values.js";
|
|
4
4
|
import { type BaseMessage, type BaseMessageLike, type MessageContent } from "../messages/index.js";
|
|
5
5
|
import { type LLMResult } from "../outputs.js";
|
|
6
|
-
import {
|
|
6
|
+
import { CallbackManager, Callbacks } from "../callbacks/manager.js";
|
|
7
7
|
import { AsyncCaller, AsyncCallerParams } from "../utils/async_caller.js";
|
|
8
8
|
import { Runnable, type RunnableInterface } from "../runnables/base.js";
|
|
9
9
|
import { RunnableConfig } from "../runnables/config.js";
|
|
@@ -53,7 +53,7 @@ export interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChai
|
|
|
53
53
|
callbackManager?: CallbackManager;
|
|
54
54
|
cache?: BaseCache | boolean;
|
|
55
55
|
}
|
|
56
|
-
export interface BaseLanguageModelCallOptions extends
|
|
56
|
+
export interface BaseLanguageModelCallOptions extends RunnableConfig {
|
|
57
57
|
/**
|
|
58
58
|
* Stop tokens to use for this call.
|
|
59
59
|
* If not provided, the default stop tokens for the model will be used.
|
package/dist/retrievers.cjs
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseRetriever = void 0;
|
|
4
4
|
const manager_js_1 = require("./callbacks/manager.cjs");
|
|
5
5
|
const base_js_1 = require("./runnables/base.cjs");
|
|
6
|
+
const config_js_1 = require("./runnables/config.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* Abstract base class for a Document retrieval system. A retrieval system
|
|
8
9
|
* is defined as something that can take string queries and return the
|
|
@@ -62,7 +63,7 @@ class BaseRetriever extends base_js_1.Runnable {
|
|
|
62
63
|
* @returns A promise that resolves to an array of `Document` objects.
|
|
63
64
|
*/
|
|
64
65
|
async getRelevantDocuments(query, config) {
|
|
65
|
-
const parsedConfig = (0, manager_js_1.parseCallbackConfigArg)(config);
|
|
66
|
+
const parsedConfig = (0, config_js_1.ensureConfig)((0, manager_js_1.parseCallbackConfigArg)(config));
|
|
66
67
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
67
68
|
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
68
69
|
try {
|
package/dist/retrievers.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CallbackManager, parseCallbackConfigArg, } from "./callbacks/manager.js";
|
|
2
2
|
import { Runnable } from "./runnables/base.js";
|
|
3
|
+
import { ensureConfig } from "./runnables/config.js";
|
|
3
4
|
/**
|
|
4
5
|
* Abstract base class for a Document retrieval system. A retrieval system
|
|
5
6
|
* is defined as something that can take string queries and return the
|
|
@@ -59,7 +60,7 @@ export class BaseRetriever extends Runnable {
|
|
|
59
60
|
* @returns A promise that resolves to an array of `Document` objects.
|
|
60
61
|
*/
|
|
61
62
|
async getRelevantDocuments(query, config) {
|
|
62
|
-
const parsedConfig = parseCallbackConfigArg(config);
|
|
63
|
+
const parsedConfig = ensureConfig(parseCallbackConfigArg(config));
|
|
63
64
|
const callbackManager_ = await CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
64
65
|
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
65
66
|
try {
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -111,14 +111,15 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
111
111
|
if (options.length !== length) {
|
|
112
112
|
throw new Error(`Passed "options" must be an array with the same length as the inputs, but got ${options.length} options for ${length} inputs`);
|
|
113
113
|
}
|
|
114
|
-
return options;
|
|
114
|
+
return options.map(config_js_1.ensureConfig);
|
|
115
115
|
}
|
|
116
|
-
return Array.from({ length }, () => options);
|
|
116
|
+
return Array.from({ length }, () => (0, config_js_1.ensureConfig)(options));
|
|
117
117
|
}
|
|
118
118
|
async batch(inputs, options, batchOptions) {
|
|
119
119
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
120
|
+
const maxConcurrency = configList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
|
|
120
121
|
const caller = new async_caller_js_1.AsyncCaller({
|
|
121
|
-
maxConcurrency
|
|
122
|
+
maxConcurrency,
|
|
122
123
|
onFailedAttempt: (e) => {
|
|
123
124
|
throw e;
|
|
124
125
|
},
|
|
@@ -156,27 +157,32 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
156
157
|
return stream_js_1.IterableReadableStream.fromAsyncGenerator(this._streamIterator(input, options));
|
|
157
158
|
}
|
|
158
159
|
_separateRunnableConfigFromCallOptions(options = {}) {
|
|
159
|
-
const runnableConfig = {
|
|
160
|
+
const runnableConfig = (0, config_js_1.ensureConfig)({
|
|
160
161
|
callbacks: options.callbacks,
|
|
161
162
|
tags: options.tags,
|
|
162
163
|
metadata: options.metadata,
|
|
163
164
|
runName: options.runName,
|
|
164
165
|
configurable: options.configurable,
|
|
165
|
-
|
|
166
|
+
recursionLimit: options.recursionLimit,
|
|
167
|
+
maxConcurrency: options.maxConcurrency,
|
|
168
|
+
});
|
|
166
169
|
const callOptions = { ...options };
|
|
167
170
|
delete callOptions.callbacks;
|
|
168
171
|
delete callOptions.tags;
|
|
169
172
|
delete callOptions.metadata;
|
|
170
173
|
delete callOptions.runName;
|
|
171
174
|
delete callOptions.configurable;
|
|
175
|
+
delete callOptions.recursionLimit;
|
|
176
|
+
delete callOptions.maxConcurrency;
|
|
172
177
|
return [runnableConfig, callOptions];
|
|
173
178
|
}
|
|
174
179
|
async _callWithConfig(func, input, options) {
|
|
175
|
-
const
|
|
176
|
-
const
|
|
180
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
181
|
+
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
|
|
182
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, config?.runType, undefined, undefined, config?.runName ?? this.getName());
|
|
177
183
|
let output;
|
|
178
184
|
try {
|
|
179
|
-
output = await func.
|
|
185
|
+
output = await func.call(this, input, config, runManager);
|
|
180
186
|
}
|
|
181
187
|
catch (e) {
|
|
182
188
|
await runManager?.handleChainError(e);
|
|
@@ -200,7 +206,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
200
206
|
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, optionsList[i].runType, undefined, undefined, optionsList[i].runName ?? this.getName())));
|
|
201
207
|
let outputs;
|
|
202
208
|
try {
|
|
203
|
-
outputs = await func(inputs, optionsList, runManagers, batchOptions);
|
|
209
|
+
outputs = await func.call(this, inputs, optionsList, runManagers, batchOptions);
|
|
204
210
|
}
|
|
205
211
|
catch (e) {
|
|
206
212
|
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
|
|
@@ -219,7 +225,8 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
219
225
|
let finalInputSupported = true;
|
|
220
226
|
let finalOutput;
|
|
221
227
|
let finalOutputSupported = true;
|
|
222
|
-
const
|
|
228
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
229
|
+
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
|
|
223
230
|
async function* wrapInputForTracing() {
|
|
224
231
|
for await (const chunk of inputGenerator) {
|
|
225
232
|
if (finalInputSupported) {
|
|
@@ -242,9 +249,15 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
242
249
|
}
|
|
243
250
|
let runManager;
|
|
244
251
|
try {
|
|
245
|
-
const pipe = await (0, stream_js_1.pipeGeneratorWithSetup)(transformer, wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, undefined,
|
|
252
|
+
const pipe = await (0, stream_js_1.pipeGeneratorWithSetup)(transformer.bind(this), wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, undefined, config?.runType, undefined, undefined, config?.runName ?? this.getName()), config);
|
|
246
253
|
runManager = pipe.setup;
|
|
247
|
-
|
|
254
|
+
const isLogStreamHandler = (handler) => handler.name === "log_stream_tracer";
|
|
255
|
+
const streamLogHandler = runManager?.handlers.find(isLogStreamHandler);
|
|
256
|
+
let iterator = pipe.output;
|
|
257
|
+
if (streamLogHandler !== undefined && runManager !== undefined) {
|
|
258
|
+
iterator = await streamLogHandler.tapOutputIterable(runManager.runId, pipe.output);
|
|
259
|
+
}
|
|
260
|
+
for await (const chunk of iterator) {
|
|
248
261
|
yield chunk;
|
|
249
262
|
if (finalOutputSupported) {
|
|
250
263
|
if (finalOutput === undefined) {
|
|
@@ -271,21 +284,6 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
271
284
|
}
|
|
272
285
|
await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
|
|
273
286
|
}
|
|
274
|
-
_patchConfig(config = {}, callbackManager = undefined, recursionLimit = undefined) {
|
|
275
|
-
const newConfig = { ...config };
|
|
276
|
-
if (callbackManager !== undefined) {
|
|
277
|
-
/**
|
|
278
|
-
* If we're replacing callbacks we need to unset runName
|
|
279
|
-
* since that should apply only to the same run as the original callbacks
|
|
280
|
-
*/
|
|
281
|
-
delete newConfig.runName;
|
|
282
|
-
return { ...newConfig, callbacks: callbackManager };
|
|
283
|
-
}
|
|
284
|
-
if (recursionLimit !== undefined) {
|
|
285
|
-
newConfig.recursionLimit = recursionLimit;
|
|
286
|
-
}
|
|
287
|
-
return newConfig;
|
|
288
|
-
}
|
|
289
287
|
/**
|
|
290
288
|
* Create a new runnable sequence that runs each individual runnable in series,
|
|
291
289
|
* piping the output of one runnable into another runnable or runnable-like.
|
|
@@ -354,7 +352,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
354
352
|
...streamOptions,
|
|
355
353
|
autoClose: false,
|
|
356
354
|
});
|
|
357
|
-
const config = options
|
|
355
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
358
356
|
const { callbacks } = config;
|
|
359
357
|
if (callbacks === undefined) {
|
|
360
358
|
config.callbacks = [stream];
|
|
@@ -486,10 +484,8 @@ class RunnableBinding extends Runnable {
|
|
|
486
484
|
getName(suffix) {
|
|
487
485
|
return this.bound.getName(suffix);
|
|
488
486
|
}
|
|
489
|
-
async _mergeConfig(
|
|
490
|
-
|
|
491
|
-
options) {
|
|
492
|
-
const config = (0, config_js_1.mergeConfigs)(this.config, options);
|
|
487
|
+
async _mergeConfig(...options) {
|
|
488
|
+
const config = (0, config_js_1.mergeConfigs)(this.config, ...options);
|
|
493
489
|
return (0, config_js_1.mergeConfigs)(config, ...(this.configFactories
|
|
494
490
|
? await Promise.all(this.configFactories.map(async (configFactory) => await configFactory(config)))
|
|
495
491
|
: []));
|
|
@@ -519,27 +515,24 @@ class RunnableBinding extends Runnable {
|
|
|
519
515
|
});
|
|
520
516
|
}
|
|
521
517
|
async invoke(input, options) {
|
|
522
|
-
return this.bound.invoke(input, await this._mergeConfig(
|
|
518
|
+
return this.bound.invoke(input, await this._mergeConfig(options, this.kwargs));
|
|
523
519
|
}
|
|
524
520
|
async batch(inputs, options, batchOptions) {
|
|
525
521
|
const mergedOptions = Array.isArray(options)
|
|
526
|
-
? await Promise.all(options.map(async (individualOption) => this._mergeConfig(
|
|
527
|
-
|
|
528
|
-
...this.kwargs,
|
|
529
|
-
})))
|
|
530
|
-
: await this._mergeConfig({ ...options, ...this.kwargs });
|
|
522
|
+
? await Promise.all(options.map(async (individualOption) => this._mergeConfig(individualOption, this.kwargs)))
|
|
523
|
+
: await this._mergeConfig(options, this.kwargs);
|
|
531
524
|
return this.bound.batch(inputs, mergedOptions, batchOptions);
|
|
532
525
|
}
|
|
533
526
|
async *_streamIterator(input, options) {
|
|
534
|
-
yield* this.bound._streamIterator(input, await this._mergeConfig(
|
|
527
|
+
yield* this.bound._streamIterator(input, await this._mergeConfig(options, this.kwargs));
|
|
535
528
|
}
|
|
536
529
|
async stream(input, options) {
|
|
537
|
-
return this.bound.stream(input, await this._mergeConfig(
|
|
530
|
+
return this.bound.stream(input, await this._mergeConfig(options, this.kwargs));
|
|
538
531
|
}
|
|
539
532
|
async *transform(
|
|
540
533
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
541
534
|
generator, options) {
|
|
542
|
-
yield* this.bound.transform(generator, await this._mergeConfig(
|
|
535
|
+
yield* this.bound.transform(generator, await this._mergeConfig(options, this.kwargs));
|
|
543
536
|
}
|
|
544
537
|
static isRunnableBinding(
|
|
545
538
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -612,7 +605,7 @@ class RunnableEach extends Runnable {
|
|
|
612
605
|
}
|
|
613
606
|
/**
|
|
614
607
|
* Binds the runnable with the specified arguments.
|
|
615
|
-
* @param
|
|
608
|
+
* @param kwargs The arguments to bind the runnable with.
|
|
616
609
|
* @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
|
|
617
610
|
*/
|
|
618
611
|
bind(kwargs) {
|
|
@@ -636,7 +629,7 @@ class RunnableEach extends Runnable {
|
|
|
636
629
|
* @returns A promise that resolves to the output of the runnable.
|
|
637
630
|
*/
|
|
638
631
|
async _invoke(inputs, config, runManager) {
|
|
639
|
-
return this.bound.batch(inputs,
|
|
632
|
+
return this.bound.batch(inputs, (0, config_js_1.patchConfig)(config, { callbacks: runManager?.getChild() }));
|
|
640
633
|
}
|
|
641
634
|
/**
|
|
642
635
|
* Bind lifecycle listeners to a Runnable, returning a new Runnable.
|
|
@@ -690,7 +683,7 @@ class RunnableRetry extends RunnableBinding {
|
|
|
690
683
|
}
|
|
691
684
|
_patchConfigForRetry(attempt, config, runManager) {
|
|
692
685
|
const tag = attempt > 1 ? `retry:attempt:${attempt}` : undefined;
|
|
693
|
-
return
|
|
686
|
+
return (0, config_js_1.patchConfig)(config, { callbacks: runManager?.getChild(tag) });
|
|
694
687
|
}
|
|
695
688
|
async _invoke(input, config, runManager) {
|
|
696
689
|
return (0, p_retry_1.default)((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
|
|
@@ -828,10 +821,14 @@ class RunnableSequence extends Runnable {
|
|
|
828
821
|
const initialSteps = [this.first, ...this.middle];
|
|
829
822
|
for (let i = 0; i < initialSteps.length; i += 1) {
|
|
830
823
|
const step = initialSteps[i];
|
|
831
|
-
nextStepInput = await step.invoke(nextStepInput,
|
|
824
|
+
nextStepInput = await step.invoke(nextStepInput, (0, config_js_1.patchConfig)(options, {
|
|
825
|
+
callbacks: runManager?.getChild(`seq:step:${i + 1}`),
|
|
826
|
+
}));
|
|
832
827
|
}
|
|
833
828
|
// TypeScript can't detect that the last output of the sequence returns RunOutput, so call it out of the loop here
|
|
834
|
-
finalOutput = await this.last.invoke(nextStepInput,
|
|
829
|
+
finalOutput = await this.last.invoke(nextStepInput, (0, config_js_1.patchConfig)(options, {
|
|
830
|
+
callbacks: runManager?.getChild(`seq:step:${this.steps.length}`),
|
|
831
|
+
}));
|
|
835
832
|
}
|
|
836
833
|
catch (e) {
|
|
837
834
|
await runManager?.handleChainError(e);
|
|
@@ -846,21 +843,21 @@ class RunnableSequence extends Runnable {
|
|
|
846
843
|
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
847
844
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
848
845
|
let nextStepInputs = inputs;
|
|
849
|
-
let finalOutputs;
|
|
850
846
|
try {
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
847
|
+
for (let i = 0; i < this.steps.length; i += 1) {
|
|
848
|
+
const step = this.steps[i];
|
|
849
|
+
nextStepInputs = await step.batch(nextStepInputs, runManagers.map((runManager, j) => {
|
|
850
|
+
const childRunManager = runManager?.getChild(`seq:step:${i + 1}`);
|
|
851
|
+
return (0, config_js_1.patchConfig)(configList[j], { callbacks: childRunManager });
|
|
852
|
+
}), batchOptions);
|
|
855
853
|
}
|
|
856
|
-
finalOutputs = await this.last.batch(nextStepInputs, runManagers.map((runManager) => this._patchConfig(configList[this.steps.length - 1], runManager?.getChild(`seq:step:${this.steps.length}`))), batchOptions);
|
|
857
854
|
}
|
|
858
855
|
catch (e) {
|
|
859
856
|
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
|
|
860
857
|
throw e;
|
|
861
858
|
}
|
|
862
|
-
await Promise.all(runManagers.map((runManager
|
|
863
|
-
return
|
|
859
|
+
await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(nextStepInputs, "output"))));
|
|
860
|
+
return nextStepInputs;
|
|
864
861
|
}
|
|
865
862
|
async *_streamIterator(input, options) {
|
|
866
863
|
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(options);
|
|
@@ -872,10 +869,14 @@ class RunnableSequence extends Runnable {
|
|
|
872
869
|
yield input;
|
|
873
870
|
}
|
|
874
871
|
try {
|
|
875
|
-
let finalGenerator = steps[0].transform(inputGenerator(),
|
|
872
|
+
let finalGenerator = steps[0].transform(inputGenerator(), (0, config_js_1.patchConfig)(options, {
|
|
873
|
+
callbacks: runManager?.getChild(`seq:step:1`),
|
|
874
|
+
}));
|
|
876
875
|
for (let i = 1; i < steps.length; i += 1) {
|
|
877
876
|
const step = steps[i];
|
|
878
|
-
finalGenerator = await step.transform(finalGenerator,
|
|
877
|
+
finalGenerator = await step.transform(finalGenerator, (0, config_js_1.patchConfig)(options, {
|
|
878
|
+
callbacks: runManager?.getChild(`seq:step:${i + 1}`),
|
|
879
|
+
}));
|
|
879
880
|
}
|
|
880
881
|
for await (const chunk of finalGenerator) {
|
|
881
882
|
yield chunk;
|
|
@@ -999,7 +1000,9 @@ class RunnableMap extends Runnable {
|
|
|
999
1000
|
const output = {};
|
|
1000
1001
|
try {
|
|
1001
1002
|
await Promise.all(Object.entries(this.steps).map(async ([key, runnable]) => {
|
|
1002
|
-
output[key] = await runnable.invoke(input,
|
|
1003
|
+
output[key] = await runnable.invoke(input, (0, config_js_1.patchConfig)(options, {
|
|
1004
|
+
callbacks: runManager?.getChild(`map:key:${key}`),
|
|
1005
|
+
}));
|
|
1003
1006
|
}));
|
|
1004
1007
|
}
|
|
1005
1008
|
catch (e) {
|
|
@@ -1016,7 +1019,9 @@ class RunnableMap extends Runnable {
|
|
|
1016
1019
|
const inputCopies = (0, stream_js_1.atee)(generator, Object.keys(steps).length);
|
|
1017
1020
|
// start the first iteration of each output iterator
|
|
1018
1021
|
const tasks = new Map(Object.entries(steps).map(([key, runnable], i) => {
|
|
1019
|
-
const gen = runnable.transform(inputCopies[i],
|
|
1022
|
+
const gen = runnable.transform(inputCopies[i], (0, config_js_1.patchConfig)(options, {
|
|
1023
|
+
callbacks: runManager?.getChild(`map:key:${key}`),
|
|
1024
|
+
}));
|
|
1020
1025
|
return [key, gen.next().then((result) => ({ key, gen, result }))];
|
|
1021
1026
|
}));
|
|
1022
1027
|
// yield chunks as they become available,
|
|
@@ -1076,7 +1081,10 @@ class RunnableLambda extends Runnable {
|
|
|
1076
1081
|
if (config?.recursionLimit === 0) {
|
|
1077
1082
|
throw new Error("Recursion limit reached.");
|
|
1078
1083
|
}
|
|
1079
|
-
output = await output.invoke(input,
|
|
1084
|
+
output = await output.invoke(input, (0, config_js_1.patchConfig)(config, {
|
|
1085
|
+
callbacks: runManager?.getChild(),
|
|
1086
|
+
recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1087
|
+
}));
|
|
1080
1088
|
}
|
|
1081
1089
|
return output;
|
|
1082
1090
|
}
|
|
@@ -1105,7 +1113,10 @@ class RunnableLambda extends Runnable {
|
|
|
1105
1113
|
if (config?.recursionLimit === 0) {
|
|
1106
1114
|
throw new Error("Recursion limit reached.");
|
|
1107
1115
|
}
|
|
1108
|
-
const stream = await output.stream(finalChunk,
|
|
1116
|
+
const stream = await output.stream(finalChunk, (0, config_js_1.patchConfig)(config, {
|
|
1117
|
+
callbacks: runManager?.getChild(),
|
|
1118
|
+
recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1119
|
+
}));
|
|
1109
1120
|
for await (const chunk of stream) {
|
|
1110
1121
|
yield chunk;
|
|
1111
1122
|
}
|
|
@@ -1176,7 +1187,7 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
1176
1187
|
let firstError;
|
|
1177
1188
|
for (const runnable of this.runnables()) {
|
|
1178
1189
|
try {
|
|
1179
|
-
const output = await runnable.invoke(input,
|
|
1190
|
+
const output = await runnable.invoke(input, (0, config_js_1.patchConfig)(options, { callbacks: runManager?.getChild() }));
|
|
1180
1191
|
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1181
1192
|
return output;
|
|
1182
1193
|
}
|
|
@@ -1203,7 +1214,9 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
1203
1214
|
let firstError;
|
|
1204
1215
|
for (const runnable of this.runnables()) {
|
|
1205
1216
|
try {
|
|
1206
|
-
const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) =>
|
|
1217
|
+
const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => (0, config_js_1.patchConfig)(configList[j], {
|
|
1218
|
+
callbacks: runManager?.getChild(),
|
|
1219
|
+
})), batchOptions);
|
|
1207
1220
|
await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));
|
|
1208
1221
|
return outputs;
|
|
1209
1222
|
}
|
|
@@ -1290,7 +1303,7 @@ class RunnableAssign extends Runnable {
|
|
|
1290
1303
|
// create two input gens, one for the mapper, one for the input
|
|
1291
1304
|
const [forPassthrough, forMapper] = (0, stream_js_1.atee)(generator);
|
|
1292
1305
|
// create mapper output gen
|
|
1293
|
-
const mapperOutput = this.mapper.transform(forMapper,
|
|
1306
|
+
const mapperOutput = this.mapper.transform(forMapper, (0, config_js_1.patchConfig)(options, { callbacks: runManager?.getChild() }));
|
|
1294
1307
|
// start the mapper
|
|
1295
1308
|
const firstMapperChunkPromise = mapperOutput.next();
|
|
1296
1309
|
// yield the passthrough
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
2
2
|
import { LogStreamCallbackHandlerInput, RunLogPatch } from "../tracers/log_stream.js";
|
|
3
3
|
import { Serializable } from "../load/serializable.js";
|
|
4
4
|
import { IterableReadableStream, type IterableReadableStreamInterface } from "../utils/stream.js";
|
|
@@ -33,6 +33,7 @@ export type RunnableMapLike<RunInput, RunOutput> = {
|
|
|
33
33
|
};
|
|
34
34
|
export type RunnableLike<RunInput = any, RunOutput = any> = RunnableInterface<RunInput, RunOutput> | RunnableFunc<RunInput, RunOutput> | RunnableMapLike<RunInput, RunOutput>;
|
|
35
35
|
export type RunnableBatchOptions = {
|
|
36
|
+
/** @deprecated Pass in via the standard runnable config object instead */
|
|
36
37
|
maxConcurrency?: number;
|
|
37
38
|
returnExceptions?: boolean;
|
|
38
39
|
};
|
|
@@ -81,15 +82,14 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
81
82
|
withFallbacks(fields: {
|
|
82
83
|
fallbacks: Runnable<RunInput, RunOutput>[];
|
|
83
84
|
}): RunnableWithFallbacks<RunInput, RunOutput>;
|
|
84
|
-
protected _getOptionsList
|
|
85
|
+
protected _getOptionsList<O extends CallOptions & {
|
|
85
86
|
runType?: string;
|
|
86
|
-
}>[];
|
|
87
|
+
}>(options: Partial<O> | Partial<O>[], length?: number): Partial<O>[];
|
|
87
88
|
/**
|
|
88
89
|
* Default implementation of batch, which calls invoke N times.
|
|
89
90
|
* Subclasses should override this method if they can batch more efficiently.
|
|
90
91
|
* @param inputs Array of inputs to each batch call.
|
|
91
92
|
* @param options Either a single call options object to apply to each batch call or an array for each call.
|
|
92
|
-
* @param batchOptions.maxConcurrency Maximum number of calls to run at once.
|
|
93
93
|
* @param batchOptions.returnExceptions Whether to return errors rather than throwing on the first one
|
|
94
94
|
* @returns An array of RunOutputs, or mixed RunOutputs and errors if batchOptions.returnExceptions is set
|
|
95
95
|
*/
|
|
@@ -140,7 +140,6 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
140
140
|
protected _transformStreamWithConfig<I extends RunInput, O extends RunOutput>(inputGenerator: AsyncGenerator<I>, transformer: (generator: AsyncGenerator<I>, runManager?: CallbackManagerForChainRun, options?: Partial<CallOptions>) => AsyncGenerator<O>, options?: CallOptions & {
|
|
141
141
|
runType?: string;
|
|
142
142
|
}): AsyncGenerator<O>;
|
|
143
|
-
_patchConfig(config?: Partial<CallOptions>, callbackManager?: CallbackManager | undefined, recursionLimit?: number | undefined): Partial<CallOptions>;
|
|
144
143
|
/**
|
|
145
144
|
* Create a new runnable sequence that runs each individual runnable in series,
|
|
146
145
|
* piping the output of one runnable into another runnable or runnable-like.
|
|
@@ -213,7 +212,7 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
|
|
|
213
212
|
configFactories?: Array<(config: RunnableConfig) => RunnableConfig | Promise<RunnableConfig>>;
|
|
214
213
|
constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions>);
|
|
215
214
|
getName(suffix?: string | undefined): string;
|
|
216
|
-
_mergeConfig(options
|
|
215
|
+
_mergeConfig(...options: (Partial<CallOptions> | RunnableConfig | undefined)[]): Promise<Partial<CallOptions>>;
|
|
217
216
|
bind(kwargs: Partial<CallOptions>): RunnableBinding<RunInput, RunOutput, CallOptions>;
|
|
218
217
|
withConfig(config: RunnableConfig): RunnableBinding<RunInput, RunOutput, CallOptions>;
|
|
219
218
|
withRetry(fields?: {
|
|
@@ -263,7 +262,7 @@ export declare class RunnableEach<RunInputItem, RunOutputItem, CallOptions exten
|
|
|
263
262
|
});
|
|
264
263
|
/**
|
|
265
264
|
* Binds the runnable with the specified arguments.
|
|
266
|
-
* @param
|
|
265
|
+
* @param kwargs The arguments to bind the runnable with.
|
|
267
266
|
* @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
|
|
268
267
|
*/
|
|
269
268
|
bind(kwargs: Partial<CallOptions>): RunnableEach<RunInputItem, RunOutputItem, CallOptions>;
|