@langchain/core 0.1.15 → 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.
@@ -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
@@ -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
- private readonly _parentRunId?;
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
@@ -555,7 +555,7 @@ export class CallbackManager extends BaseCallbackManager {
555
555
  return callbackManager;
556
556
  }
557
557
  }
558
- function ensureHandler(handler) {
558
+ export function ensureHandler(handler) {
559
559
  if ("name" in handler) {
560
560
  return handler;
561
561
  }
@@ -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 {
@@ -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 {
@@ -111,9 +111,9 @@ 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);
@@ -157,27 +157,32 @@ class Runnable extends serializable_js_1.Serializable {
157
157
  return stream_js_1.IterableReadableStream.fromAsyncGenerator(this._streamIterator(input, options));
158
158
  }
159
159
  _separateRunnableConfigFromCallOptions(options = {}) {
160
- const runnableConfig = {
160
+ const runnableConfig = (0, config_js_1.ensureConfig)({
161
161
  callbacks: options.callbacks,
162
162
  tags: options.tags,
163
163
  metadata: options.metadata,
164
164
  runName: options.runName,
165
165
  configurable: options.configurable,
166
- };
166
+ recursionLimit: options.recursionLimit,
167
+ maxConcurrency: options.maxConcurrency,
168
+ });
167
169
  const callOptions = { ...options };
168
170
  delete callOptions.callbacks;
169
171
  delete callOptions.tags;
170
172
  delete callOptions.metadata;
171
173
  delete callOptions.runName;
172
174
  delete callOptions.configurable;
175
+ delete callOptions.recursionLimit;
176
+ delete callOptions.maxConcurrency;
173
177
  return [runnableConfig, callOptions];
174
178
  }
175
179
  async _callWithConfig(func, input, options) {
176
- const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(options);
177
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName ?? this.getName());
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());
178
183
  let output;
179
184
  try {
180
- output = await func.bind(this)(input, options, runManager);
185
+ output = await func.call(this, input, config, runManager);
181
186
  }
182
187
  catch (e) {
183
188
  await runManager?.handleChainError(e);
@@ -201,7 +206,7 @@ class Runnable extends serializable_js_1.Serializable {
201
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())));
202
207
  let outputs;
203
208
  try {
204
- outputs = await func(inputs, optionsList, runManagers, batchOptions);
209
+ outputs = await func.call(this, inputs, optionsList, runManagers, batchOptions);
205
210
  }
206
211
  catch (e) {
207
212
  await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
@@ -220,7 +225,8 @@ class Runnable extends serializable_js_1.Serializable {
220
225
  let finalInputSupported = true;
221
226
  let finalOutput;
222
227
  let finalOutputSupported = true;
223
- const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(options);
228
+ const config = (0, config_js_1.ensureConfig)(options);
229
+ const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
224
230
  async function* wrapInputForTracing() {
225
231
  for await (const chunk of inputGenerator) {
226
232
  if (finalInputSupported) {
@@ -243,7 +249,7 @@ class Runnable extends serializable_js_1.Serializable {
243
249
  }
244
250
  let runManager;
245
251
  try {
246
- const pipe = await (0, stream_js_1.pipeGeneratorWithSetup)(transformer, wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName ?? this.getName()), options);
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);
247
253
  runManager = pipe.setup;
248
254
  const isLogStreamHandler = (handler) => handler.name === "log_stream_tracer";
249
255
  const streamLogHandler = runManager?.handlers.find(isLogStreamHandler);
@@ -346,7 +352,7 @@ class Runnable extends serializable_js_1.Serializable {
346
352
  ...streamOptions,
347
353
  autoClose: false,
348
354
  });
349
- const config = options ?? {};
355
+ const config = (0, config_js_1.ensureConfig)(options);
350
356
  const { callbacks } = config;
351
357
  if (callbacks === undefined) {
352
358
  config.callbacks = [stream];
@@ -478,10 +484,8 @@ class RunnableBinding extends Runnable {
478
484
  getName(suffix) {
479
485
  return this.bound.getName(suffix);
480
486
  }
481
- async _mergeConfig(
482
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
483
- options) {
484
- 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);
485
489
  return (0, config_js_1.mergeConfigs)(config, ...(this.configFactories
486
490
  ? await Promise.all(this.configFactories.map(async (configFactory) => await configFactory(config)))
487
491
  : []));
@@ -511,27 +515,24 @@ class RunnableBinding extends Runnable {
511
515
  });
512
516
  }
513
517
  async invoke(input, options) {
514
- return this.bound.invoke(input, await this._mergeConfig({ ...options, ...this.kwargs }));
518
+ return this.bound.invoke(input, await this._mergeConfig(options, this.kwargs));
515
519
  }
516
520
  async batch(inputs, options, batchOptions) {
517
521
  const mergedOptions = Array.isArray(options)
518
- ? await Promise.all(options.map(async (individualOption) => this._mergeConfig({
519
- ...individualOption,
520
- ...this.kwargs,
521
- })))
522
- : 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);
523
524
  return this.bound.batch(inputs, mergedOptions, batchOptions);
524
525
  }
525
526
  async *_streamIterator(input, options) {
526
- yield* this.bound._streamIterator(input, await this._mergeConfig({ ...options, ...this.kwargs }));
527
+ yield* this.bound._streamIterator(input, await this._mergeConfig(options, this.kwargs));
527
528
  }
528
529
  async stream(input, options) {
529
- return this.bound.stream(input, await this._mergeConfig({ ...options, ...this.kwargs }));
530
+ return this.bound.stream(input, await this._mergeConfig(options, this.kwargs));
530
531
  }
531
532
  async *transform(
532
533
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
533
534
  generator, options) {
534
- yield* this.bound.transform(generator, await this._mergeConfig({ ...options, ...this.kwargs }));
535
+ yield* this.bound.transform(generator, await this._mergeConfig(options, this.kwargs));
535
536
  }
536
537
  static isRunnableBinding(
537
538
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -82,9 +82,9 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
82
82
  withFallbacks(fields: {
83
83
  fallbacks: Runnable<RunInput, RunOutput>[];
84
84
  }): RunnableWithFallbacks<RunInput, RunOutput>;
85
- protected _getOptionsList(options: Partial<CallOptions> | Partial<CallOptions>[], length?: number): Partial<CallOptions & {
85
+ protected _getOptionsList<O extends CallOptions & {
86
86
  runType?: string;
87
- }>[];
87
+ }>(options: Partial<O> | Partial<O>[], length?: number): Partial<O>[];
88
88
  /**
89
89
  * Default implementation of batch, which calls invoke N times.
90
90
  * Subclasses should override this method if they can batch more efficiently.
@@ -212,7 +212,7 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
212
212
  configFactories?: Array<(config: RunnableConfig) => RunnableConfig | Promise<RunnableConfig>>;
213
213
  constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions>);
214
214
  getName(suffix?: string | undefined): string;
215
- _mergeConfig(options?: Record<string, any>): Promise<Partial<CallOptions>>;
215
+ _mergeConfig(...options: (Partial<CallOptions> | RunnableConfig | undefined)[]): Promise<Partial<CallOptions>>;
216
216
  bind(kwargs: Partial<CallOptions>): RunnableBinding<RunInput, RunOutput, CallOptions>;
217
217
  withConfig(config: RunnableConfig): RunnableBinding<RunInput, RunOutput, CallOptions>;
218
218
  withRetry(fields?: {
@@ -3,7 +3,7 @@ import { CallbackManager, } from "../callbacks/manager.js";
3
3
  import { LogStreamCallbackHandler, RunLogPatch, } from "../tracers/log_stream.js";
4
4
  import { Serializable } from "../load/serializable.js";
5
5
  import { IterableReadableStream, concat, atee, pipeGeneratorWithSetup, } from "../utils/stream.js";
6
- import { DEFAULT_RECURSION_LIMIT, getCallbackManagerForConfig, mergeConfigs, patchConfig, } from "./config.js";
6
+ import { DEFAULT_RECURSION_LIMIT, ensureConfig, getCallbackManagerForConfig, mergeConfigs, patchConfig, } from "./config.js";
7
7
  import { AsyncCaller } from "../utils/async_caller.js";
8
8
  import { RootListenersTracer } from "../tracers/root_listener.js";
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -105,9 +105,9 @@ export class Runnable extends Serializable {
105
105
  if (options.length !== length) {
106
106
  throw new Error(`Passed "options" must be an array with the same length as the inputs, but got ${options.length} options for ${length} inputs`);
107
107
  }
108
- return options;
108
+ return options.map(ensureConfig);
109
109
  }
110
- return Array.from({ length }, () => options);
110
+ return Array.from({ length }, () => ensureConfig(options));
111
111
  }
112
112
  async batch(inputs, options, batchOptions) {
113
113
  const configList = this._getOptionsList(options ?? {}, inputs.length);
@@ -151,27 +151,32 @@ export class Runnable extends Serializable {
151
151
  return IterableReadableStream.fromAsyncGenerator(this._streamIterator(input, options));
152
152
  }
153
153
  _separateRunnableConfigFromCallOptions(options = {}) {
154
- const runnableConfig = {
154
+ const runnableConfig = ensureConfig({
155
155
  callbacks: options.callbacks,
156
156
  tags: options.tags,
157
157
  metadata: options.metadata,
158
158
  runName: options.runName,
159
159
  configurable: options.configurable,
160
- };
160
+ recursionLimit: options.recursionLimit,
161
+ maxConcurrency: options.maxConcurrency,
162
+ });
161
163
  const callOptions = { ...options };
162
164
  delete callOptions.callbacks;
163
165
  delete callOptions.tags;
164
166
  delete callOptions.metadata;
165
167
  delete callOptions.runName;
166
168
  delete callOptions.configurable;
169
+ delete callOptions.recursionLimit;
170
+ delete callOptions.maxConcurrency;
167
171
  return [runnableConfig, callOptions];
168
172
  }
169
173
  async _callWithConfig(func, input, options) {
170
- const callbackManager_ = await getCallbackManagerForConfig(options);
171
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName ?? this.getName());
174
+ const config = ensureConfig(options);
175
+ const callbackManager_ = await getCallbackManagerForConfig(config);
176
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, config?.runType, undefined, undefined, config?.runName ?? this.getName());
172
177
  let output;
173
178
  try {
174
- output = await func.bind(this)(input, options, runManager);
179
+ output = await func.call(this, input, config, runManager);
175
180
  }
176
181
  catch (e) {
177
182
  await runManager?.handleChainError(e);
@@ -195,7 +200,7 @@ export class Runnable extends Serializable {
195
200
  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())));
196
201
  let outputs;
197
202
  try {
198
- outputs = await func(inputs, optionsList, runManagers, batchOptions);
203
+ outputs = await func.call(this, inputs, optionsList, runManagers, batchOptions);
199
204
  }
200
205
  catch (e) {
201
206
  await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
@@ -214,7 +219,8 @@ export class Runnable extends Serializable {
214
219
  let finalInputSupported = true;
215
220
  let finalOutput;
216
221
  let finalOutputSupported = true;
217
- const callbackManager_ = await getCallbackManagerForConfig(options);
222
+ const config = ensureConfig(options);
223
+ const callbackManager_ = await getCallbackManagerForConfig(config);
218
224
  async function* wrapInputForTracing() {
219
225
  for await (const chunk of inputGenerator) {
220
226
  if (finalInputSupported) {
@@ -237,7 +243,7 @@ export class Runnable extends Serializable {
237
243
  }
238
244
  let runManager;
239
245
  try {
240
- const pipe = await pipeGeneratorWithSetup(transformer, wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName ?? this.getName()), options);
246
+ const pipe = await pipeGeneratorWithSetup(transformer.bind(this), wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, undefined, config?.runType, undefined, undefined, config?.runName ?? this.getName()), config);
241
247
  runManager = pipe.setup;
242
248
  const isLogStreamHandler = (handler) => handler.name === "log_stream_tracer";
243
249
  const streamLogHandler = runManager?.handlers.find(isLogStreamHandler);
@@ -340,7 +346,7 @@ export class Runnable extends Serializable {
340
346
  ...streamOptions,
341
347
  autoClose: false,
342
348
  });
343
- const config = options ?? {};
349
+ const config = ensureConfig(options);
344
350
  const { callbacks } = config;
345
351
  if (callbacks === undefined) {
346
352
  config.callbacks = [stream];
@@ -471,10 +477,8 @@ export class RunnableBinding extends Runnable {
471
477
  getName(suffix) {
472
478
  return this.bound.getName(suffix);
473
479
  }
474
- async _mergeConfig(
475
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
476
- options) {
477
- const config = mergeConfigs(this.config, options);
480
+ async _mergeConfig(...options) {
481
+ const config = mergeConfigs(this.config, ...options);
478
482
  return mergeConfigs(config, ...(this.configFactories
479
483
  ? await Promise.all(this.configFactories.map(async (configFactory) => await configFactory(config)))
480
484
  : []));
@@ -504,27 +508,24 @@ export class RunnableBinding extends Runnable {
504
508
  });
505
509
  }
506
510
  async invoke(input, options) {
507
- return this.bound.invoke(input, await this._mergeConfig({ ...options, ...this.kwargs }));
511
+ return this.bound.invoke(input, await this._mergeConfig(options, this.kwargs));
508
512
  }
509
513
  async batch(inputs, options, batchOptions) {
510
514
  const mergedOptions = Array.isArray(options)
511
- ? await Promise.all(options.map(async (individualOption) => this._mergeConfig({
512
- ...individualOption,
513
- ...this.kwargs,
514
- })))
515
- : await this._mergeConfig({ ...options, ...this.kwargs });
515
+ ? await Promise.all(options.map(async (individualOption) => this._mergeConfig(individualOption, this.kwargs)))
516
+ : await this._mergeConfig(options, this.kwargs);
516
517
  return this.bound.batch(inputs, mergedOptions, batchOptions);
517
518
  }
518
519
  async *_streamIterator(input, options) {
519
- yield* this.bound._streamIterator(input, await this._mergeConfig({ ...options, ...this.kwargs }));
520
+ yield* this.bound._streamIterator(input, await this._mergeConfig(options, this.kwargs));
520
521
  }
521
522
  async stream(input, options) {
522
- return this.bound.stream(input, await this._mergeConfig({ ...options, ...this.kwargs }));
523
+ return this.bound.stream(input, await this._mergeConfig(options, this.kwargs));
523
524
  }
524
525
  async *transform(
525
526
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
526
527
  generator, options) {
527
- yield* this.bound.transform(generator, await this._mergeConfig({ ...options, ...this.kwargs }));
528
+ yield* this.bound.transform(generator, await this._mergeConfig(options, this.kwargs));
528
529
  }
529
530
  static isRunnableBinding(
530
531
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -7,25 +7,23 @@ async function getCallbackManagerForConfig(config) {
7
7
  return manager_js_1.CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
8
8
  }
9
9
  exports.getCallbackManagerForConfig = getCallbackManagerForConfig;
10
- function mergeConfigs(config,
11
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
- options) {
10
+ function mergeConfigs(...configs) {
13
11
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- const copy = { ...config };
15
- if (options) {
12
+ const copy = ensureConfig();
13
+ for (const options of configs.filter((c) => !!c)) {
16
14
  for (const key of Object.keys(options)) {
17
15
  if (key === "metadata") {
18
16
  copy[key] = { ...copy[key], ...options[key] };
19
17
  }
20
18
  else if (key === "tags") {
21
- copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
19
+ copy[key] = [...new Set(copy[key].concat(options[key] ?? []))];
22
20
  }
23
21
  else if (key === "configurable") {
24
22
  copy[key] = { ...copy[key], ...options[key] };
25
23
  }
26
24
  else if (key === "callbacks") {
27
25
  const baseCallbacks = copy.callbacks;
28
- const providedCallbacks = options.callbacks ?? config.callbacks;
26
+ const providedCallbacks = options.callbacks;
29
27
  // callbacks can be either undefined, Array<handler> or manager
30
28
  // so merging two callbacks values has 6 cases
31
29
  if (Array.isArray(providedCallbacks)) {
@@ -39,7 +37,7 @@ options) {
39
37
  // baseCallbacks is a manager
40
38
  const manager = baseCallbacks.copy();
41
39
  for (const callback of providedCallbacks) {
42
- manager.addHandler(callback, true);
40
+ manager.addHandler((0, manager_js_1.ensureHandler)(callback), true);
43
41
  }
44
42
  copy.callbacks = manager;
45
43
  }
@@ -52,13 +50,13 @@ options) {
52
50
  else if (Array.isArray(baseCallbacks)) {
53
51
  const manager = providedCallbacks.copy();
54
52
  for (const callback of baseCallbacks) {
55
- manager.addHandler(callback, true);
53
+ manager.addHandler((0, manager_js_1.ensureHandler)(callback), true);
56
54
  }
57
55
  copy.callbacks = manager;
58
56
  }
59
57
  else {
60
58
  // baseCallbacks is also a manager
61
- copy.callbacks = new manager_js_1.CallbackManager(providedCallbacks.parentRunId, {
59
+ copy.callbacks = new manager_js_1.CallbackManager(providedCallbacks._parentRunId, {
62
60
  handlers: baseCallbacks.handlers.concat(providedCallbacks.handlers),
63
61
  inheritableHandlers: baseCallbacks.inheritableHandlers.concat(providedCallbacks.inheritableHandlers),
64
62
  tags: Array.from(new Set(baseCallbacks.tags.concat(providedCallbacks.tags))),
@@ -72,24 +70,40 @@ options) {
72
70
  }
73
71
  }
74
72
  else {
75
- copy[key] = options[key] ?? copy[key];
73
+ const typedKey = key;
74
+ copy[typedKey] = options[typedKey] ?? copy[typedKey];
76
75
  }
77
76
  }
78
77
  }
79
78
  return copy;
80
79
  }
81
80
  exports.mergeConfigs = mergeConfigs;
81
+ const PRIMITIVES = new Set(["string", "number", "boolean"]);
82
82
  /**
83
83
  * Ensure that a passed config is an object with all required keys present.
84
84
  */
85
85
  function ensureConfig(config) {
86
- return {
86
+ let empty = {
87
87
  tags: [],
88
88
  metadata: {},
89
89
  callbacks: undefined,
90
90
  recursionLimit: 25,
91
- ...config,
92
91
  };
92
+ if (config) {
93
+ empty = { ...empty, ...config };
94
+ }
95
+ if (config?.configurable) {
96
+ for (const key of Object.keys(config.configurable)) {
97
+ if (PRIMITIVES.has(typeof config.configurable[key]) &&
98
+ !empty.metadata?.[key]) {
99
+ if (!empty.metadata) {
100
+ empty.metadata = {};
101
+ }
102
+ empty.metadata[key] = config.configurable[key];
103
+ }
104
+ }
105
+ }
106
+ return empty;
93
107
  }
94
108
  exports.ensureConfig = ensureConfig;
95
109
  /**
@@ -14,11 +14,11 @@ export interface RunnableConfig extends BaseCallbackConfig {
14
14
  maxConcurrency?: number;
15
15
  }
16
16
  export declare function getCallbackManagerForConfig(config?: RunnableConfig): Promise<CallbackManager | undefined>;
17
- export declare function mergeConfigs<CallOptions extends RunnableConfig>(config: RunnableConfig, options?: Record<string, any>): Partial<CallOptions>;
17
+ export declare function mergeConfigs<CallOptions extends RunnableConfig>(...configs: (CallOptions | RunnableConfig | undefined | null)[]): Partial<CallOptions>;
18
18
  /**
19
19
  * Ensure that a passed config is an object with all required keys present.
20
20
  */
21
- export declare function ensureConfig<CallOptions extends RunnableConfig>(config?: CallOptions): Partial<CallOptions>;
21
+ export declare function ensureConfig<CallOptions extends RunnableConfig>(config?: CallOptions): CallOptions;
22
22
  /**
23
23
  * Helper function that patches runnable configs with updated properties.
24
24
  */
@@ -1,27 +1,25 @@
1
- import { CallbackManager, } from "../callbacks/manager.js";
1
+ import { CallbackManager, ensureHandler, } from "../callbacks/manager.js";
2
2
  export const DEFAULT_RECURSION_LIMIT = 25;
3
3
  export async function getCallbackManagerForConfig(config) {
4
4
  return CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
5
5
  }
6
- export function mergeConfigs(config,
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- options) {
6
+ export function mergeConfigs(...configs) {
9
7
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
- const copy = { ...config };
11
- if (options) {
8
+ const copy = ensureConfig();
9
+ for (const options of configs.filter((c) => !!c)) {
12
10
  for (const key of Object.keys(options)) {
13
11
  if (key === "metadata") {
14
12
  copy[key] = { ...copy[key], ...options[key] };
15
13
  }
16
14
  else if (key === "tags") {
17
- copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
15
+ copy[key] = [...new Set(copy[key].concat(options[key] ?? []))];
18
16
  }
19
17
  else if (key === "configurable") {
20
18
  copy[key] = { ...copy[key], ...options[key] };
21
19
  }
22
20
  else if (key === "callbacks") {
23
21
  const baseCallbacks = copy.callbacks;
24
- const providedCallbacks = options.callbacks ?? config.callbacks;
22
+ const providedCallbacks = options.callbacks;
25
23
  // callbacks can be either undefined, Array<handler> or manager
26
24
  // so merging two callbacks values has 6 cases
27
25
  if (Array.isArray(providedCallbacks)) {
@@ -35,7 +33,7 @@ options) {
35
33
  // baseCallbacks is a manager
36
34
  const manager = baseCallbacks.copy();
37
35
  for (const callback of providedCallbacks) {
38
- manager.addHandler(callback, true);
36
+ manager.addHandler(ensureHandler(callback), true);
39
37
  }
40
38
  copy.callbacks = manager;
41
39
  }
@@ -48,13 +46,13 @@ options) {
48
46
  else if (Array.isArray(baseCallbacks)) {
49
47
  const manager = providedCallbacks.copy();
50
48
  for (const callback of baseCallbacks) {
51
- manager.addHandler(callback, true);
49
+ manager.addHandler(ensureHandler(callback), true);
52
50
  }
53
51
  copy.callbacks = manager;
54
52
  }
55
53
  else {
56
54
  // baseCallbacks is also a manager
57
- copy.callbacks = new CallbackManager(providedCallbacks.parentRunId, {
55
+ copy.callbacks = new CallbackManager(providedCallbacks._parentRunId, {
58
56
  handlers: baseCallbacks.handlers.concat(providedCallbacks.handlers),
59
57
  inheritableHandlers: baseCallbacks.inheritableHandlers.concat(providedCallbacks.inheritableHandlers),
60
58
  tags: Array.from(new Set(baseCallbacks.tags.concat(providedCallbacks.tags))),
@@ -68,23 +66,39 @@ options) {
68
66
  }
69
67
  }
70
68
  else {
71
- copy[key] = options[key] ?? copy[key];
69
+ const typedKey = key;
70
+ copy[typedKey] = options[typedKey] ?? copy[typedKey];
72
71
  }
73
72
  }
74
73
  }
75
74
  return copy;
76
75
  }
76
+ const PRIMITIVES = new Set(["string", "number", "boolean"]);
77
77
  /**
78
78
  * Ensure that a passed config is an object with all required keys present.
79
79
  */
80
80
  export function ensureConfig(config) {
81
- return {
81
+ let empty = {
82
82
  tags: [],
83
83
  metadata: {},
84
84
  callbacks: undefined,
85
85
  recursionLimit: 25,
86
- ...config,
87
86
  };
87
+ if (config) {
88
+ empty = { ...empty, ...config };
89
+ }
90
+ if (config?.configurable) {
91
+ for (const key of Object.keys(config.configurable)) {
92
+ if (PRIMITIVES.has(typeof config.configurable[key]) &&
93
+ !empty.metadata?.[key]) {
94
+ if (!empty.metadata) {
95
+ empty.metadata = {};
96
+ }
97
+ empty.metadata[key] = config.configurable[key];
98
+ }
99
+ }
100
+ }
101
+ return empty;
88
102
  }
89
103
  /**
90
104
  * Helper function that patches runnable configs with updated properties.
package/dist/tools.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Tool = exports.StructuredTool = exports.ToolInputParsingException = void 0;
3
+ exports.DynamicStructuredTool = exports.DynamicTool = exports.Tool = exports.StructuredTool = exports.ToolInputParsingException = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const manager_js_1 = require("./callbacks/manager.cjs");
6
6
  const base_js_1 = require("./language_models/base.cjs");
@@ -109,3 +109,104 @@ class Tool extends StructuredTool {
109
109
  }
110
110
  }
111
111
  exports.Tool = Tool;
112
+ /**
113
+ * A tool that can be created dynamically from a function, name, and description.
114
+ */
115
+ class DynamicTool extends Tool {
116
+ static lc_name() {
117
+ return "DynamicTool";
118
+ }
119
+ constructor(fields) {
120
+ super(fields);
121
+ Object.defineProperty(this, "name", {
122
+ enumerable: true,
123
+ configurable: true,
124
+ writable: true,
125
+ value: void 0
126
+ });
127
+ Object.defineProperty(this, "description", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: void 0
132
+ });
133
+ Object.defineProperty(this, "func", {
134
+ enumerable: true,
135
+ configurable: true,
136
+ writable: true,
137
+ value: void 0
138
+ });
139
+ this.name = fields.name;
140
+ this.description = fields.description;
141
+ this.func = fields.func;
142
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
143
+ }
144
+ async call(arg, configArg) {
145
+ const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
146
+ if (config.runName === undefined) {
147
+ config.runName = this.name;
148
+ }
149
+ return super.call(arg, config);
150
+ }
151
+ /** @ignore */
152
+ async _call(input, runManager) {
153
+ return this.func(input, runManager);
154
+ }
155
+ }
156
+ exports.DynamicTool = DynamicTool;
157
+ /**
158
+ * A tool that can be created dynamically from a function, name, and
159
+ * description, designed to work with structured data. It extends the
160
+ * StructuredTool class and overrides the _call method to execute the
161
+ * provided function when the tool is called.
162
+ */
163
+ class DynamicStructuredTool extends StructuredTool {
164
+ static lc_name() {
165
+ return "DynamicStructuredTool";
166
+ }
167
+ constructor(fields) {
168
+ super(fields);
169
+ Object.defineProperty(this, "name", {
170
+ enumerable: true,
171
+ configurable: true,
172
+ writable: true,
173
+ value: void 0
174
+ });
175
+ Object.defineProperty(this, "description", {
176
+ enumerable: true,
177
+ configurable: true,
178
+ writable: true,
179
+ value: void 0
180
+ });
181
+ Object.defineProperty(this, "func", {
182
+ enumerable: true,
183
+ configurable: true,
184
+ writable: true,
185
+ value: void 0
186
+ });
187
+ Object.defineProperty(this, "schema", {
188
+ enumerable: true,
189
+ configurable: true,
190
+ writable: true,
191
+ value: void 0
192
+ });
193
+ this.name = fields.name;
194
+ this.description = fields.description;
195
+ this.func = fields.func;
196
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
197
+ this.schema = fields.schema;
198
+ }
199
+ async call(arg, configArg,
200
+ /** @deprecated */
201
+ tags) {
202
+ const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
203
+ if (config.runName === undefined) {
204
+ config.runName = this.name;
205
+ }
206
+ return super.call(arg, config, tags);
207
+ }
208
+ _call(arg, runManager) {
209
+ return this.func(arg, runManager);
210
+ }
211
+ }
212
+ exports.DynamicStructuredTool = DynamicStructuredTool;
package/dist/tools.d.ts CHANGED
@@ -100,3 +100,52 @@ export declare abstract class Tool extends StructuredTool {
100
100
  */
101
101
  call(arg: string | undefined | z.input<this["schema"]>, callbacks?: Callbacks | RunnableConfig): Promise<string>;
102
102
  }
103
+ export interface BaseDynamicToolInput extends ToolParams {
104
+ name: string;
105
+ description: string;
106
+ returnDirect?: boolean;
107
+ }
108
+ /**
109
+ * Interface for the input parameters of the DynamicTool class.
110
+ */
111
+ export interface DynamicToolInput extends BaseDynamicToolInput {
112
+ func: (input: string, runManager?: CallbackManagerForToolRun) => Promise<string>;
113
+ }
114
+ /**
115
+ * Interface for the input parameters of the DynamicStructuredTool class.
116
+ */
117
+ export interface DynamicStructuredToolInput<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends BaseDynamicToolInput {
118
+ func: (input: z.infer<T>, runManager?: CallbackManagerForToolRun) => Promise<string>;
119
+ schema: T;
120
+ }
121
+ /**
122
+ * A tool that can be created dynamically from a function, name, and description.
123
+ */
124
+ export declare class DynamicTool extends Tool {
125
+ static lc_name(): string;
126
+ name: string;
127
+ description: string;
128
+ func: DynamicToolInput["func"];
129
+ constructor(fields: DynamicToolInput);
130
+ call(arg: string | undefined | z.input<this["schema"]>, configArg?: RunnableConfig | Callbacks): Promise<string>;
131
+ /** @ignore */
132
+ _call(input: string, runManager?: CallbackManagerForToolRun): Promise<string>;
133
+ }
134
+ /**
135
+ * A tool that can be created dynamically from a function, name, and
136
+ * description, designed to work with structured data. It extends the
137
+ * StructuredTool class and overrides the _call method to execute the
138
+ * provided function when the tool is called.
139
+ */
140
+ export declare class DynamicStructuredTool<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends StructuredTool {
141
+ static lc_name(): string;
142
+ name: string;
143
+ description: string;
144
+ func: DynamicStructuredToolInput["func"];
145
+ schema: T;
146
+ constructor(fields: DynamicStructuredToolInput<T>);
147
+ call(arg: z.output<T>, configArg?: RunnableConfig | Callbacks,
148
+ /** @deprecated */
149
+ tags?: string[]): Promise<string>;
150
+ protected _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun): Promise<string>;
151
+ }
package/dist/tools.js CHANGED
@@ -103,3 +103,102 @@ export class Tool extends StructuredTool {
103
103
  return super.call(typeof arg === "string" || !arg ? { input: arg } : arg, callbacks);
104
104
  }
105
105
  }
106
+ /**
107
+ * A tool that can be created dynamically from a function, name, and description.
108
+ */
109
+ export class DynamicTool extends Tool {
110
+ static lc_name() {
111
+ return "DynamicTool";
112
+ }
113
+ constructor(fields) {
114
+ super(fields);
115
+ Object.defineProperty(this, "name", {
116
+ enumerable: true,
117
+ configurable: true,
118
+ writable: true,
119
+ value: void 0
120
+ });
121
+ Object.defineProperty(this, "description", {
122
+ enumerable: true,
123
+ configurable: true,
124
+ writable: true,
125
+ value: void 0
126
+ });
127
+ Object.defineProperty(this, "func", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: void 0
132
+ });
133
+ this.name = fields.name;
134
+ this.description = fields.description;
135
+ this.func = fields.func;
136
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
137
+ }
138
+ async call(arg, configArg) {
139
+ const config = parseCallbackConfigArg(configArg);
140
+ if (config.runName === undefined) {
141
+ config.runName = this.name;
142
+ }
143
+ return super.call(arg, config);
144
+ }
145
+ /** @ignore */
146
+ async _call(input, runManager) {
147
+ return this.func(input, runManager);
148
+ }
149
+ }
150
+ /**
151
+ * A tool that can be created dynamically from a function, name, and
152
+ * description, designed to work with structured data. It extends the
153
+ * StructuredTool class and overrides the _call method to execute the
154
+ * provided function when the tool is called.
155
+ */
156
+ export class DynamicStructuredTool extends StructuredTool {
157
+ static lc_name() {
158
+ return "DynamicStructuredTool";
159
+ }
160
+ constructor(fields) {
161
+ super(fields);
162
+ Object.defineProperty(this, "name", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: void 0
167
+ });
168
+ Object.defineProperty(this, "description", {
169
+ enumerable: true,
170
+ configurable: true,
171
+ writable: true,
172
+ value: void 0
173
+ });
174
+ Object.defineProperty(this, "func", {
175
+ enumerable: true,
176
+ configurable: true,
177
+ writable: true,
178
+ value: void 0
179
+ });
180
+ Object.defineProperty(this, "schema", {
181
+ enumerable: true,
182
+ configurable: true,
183
+ writable: true,
184
+ value: void 0
185
+ });
186
+ this.name = fields.name;
187
+ this.description = fields.description;
188
+ this.func = fields.func;
189
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
190
+ this.schema = fields.schema;
191
+ }
192
+ async call(arg, configArg,
193
+ /** @deprecated */
194
+ tags) {
195
+ const config = parseCallbackConfigArg(configArg);
196
+ if (config.runName === undefined) {
197
+ config.runName = this.name;
198
+ }
199
+ return super.call(arg, config, tags);
200
+ }
201
+ _call(arg, runManager) {
202
+ return this.func(arg, runManager);
203
+ }
204
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FakeTool = exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
3
+ exports.SyntheticEmbeddings = exports.FakeEmbeddings = exports.FakeTool = exports.FakeTracer = exports.FakeListChatMessageHistory = exports.FakeChatMessageHistory = exports.FakeListChatModel = exports.FakeRetriever = exports.FakeChatModel = exports.FakeStreamingLLM = exports.FakeLLM = exports.FakeRunnable = exports.FakeSplitIntoListParser = void 0;
4
4
  const chat_history_js_1 = require("../../chat_history.cjs");
5
5
  const document_js_1 = require("../../documents/document.cjs");
6
6
  const chat_models_js_1 = require("../../language_models/chat_models.cjs");
@@ -12,6 +12,7 @@ const retrievers_js_1 = require("../../retrievers.cjs");
12
12
  const base_js_2 = require("../../runnables/base.cjs");
13
13
  const tools_js_1 = require("../../tools.cjs");
14
14
  const base_js_3 = require("../../tracers/base.cjs");
15
+ const embeddings_js_1 = require("../../embeddings.cjs");
15
16
  /**
16
17
  * Parser for comma-separated values. It splits the input text by commas
17
18
  * and trims the resulting values.
@@ -406,3 +407,93 @@ class FakeTool extends tools_js_1.StructuredTool {
406
407
  }
407
408
  }
408
409
  exports.FakeTool = FakeTool;
410
+ /**
411
+ * A class that provides fake embeddings by overriding the embedDocuments
412
+ * and embedQuery methods to return fixed values.
413
+ */
414
+ class FakeEmbeddings extends embeddings_js_1.Embeddings {
415
+ constructor(params) {
416
+ super(params ?? {});
417
+ }
418
+ /**
419
+ * Generates fixed embeddings for a list of documents.
420
+ * @param documents List of documents to generate embeddings for.
421
+ * @returns A promise that resolves with a list of fixed embeddings for each document.
422
+ */
423
+ embedDocuments(documents) {
424
+ return Promise.resolve(documents.map(() => [0.1, 0.2, 0.3, 0.4]));
425
+ }
426
+ /**
427
+ * Generates a fixed embedding for a query.
428
+ * @param _ The query to generate an embedding for.
429
+ * @returns A promise that resolves with a fixed embedding for the query.
430
+ */
431
+ embedQuery(_) {
432
+ return Promise.resolve([0.1, 0.2, 0.3, 0.4]);
433
+ }
434
+ }
435
+ exports.FakeEmbeddings = FakeEmbeddings;
436
+ /**
437
+ * A class that provides synthetic embeddings by overriding the
438
+ * embedDocuments and embedQuery methods to generate embeddings based on
439
+ * the input documents. The embeddings are generated by converting each
440
+ * document into chunks, calculating a numerical value for each chunk, and
441
+ * returning an array of these values as the embedding.
442
+ */
443
+ class SyntheticEmbeddings extends embeddings_js_1.Embeddings {
444
+ constructor(params) {
445
+ super(params ?? {});
446
+ Object.defineProperty(this, "vectorSize", {
447
+ enumerable: true,
448
+ configurable: true,
449
+ writable: true,
450
+ value: void 0
451
+ });
452
+ this.vectorSize = params?.vectorSize ?? 4;
453
+ }
454
+ /**
455
+ * Generates synthetic embeddings for a list of documents.
456
+ * @param documents List of documents to generate embeddings for.
457
+ * @returns A promise that resolves with a list of synthetic embeddings for each document.
458
+ */
459
+ async embedDocuments(documents) {
460
+ return Promise.all(documents.map((doc) => this.embedQuery(doc)));
461
+ }
462
+ /**
463
+ * Generates a synthetic embedding for a document. The document is
464
+ * converted into chunks, a numerical value is calculated for each chunk,
465
+ * and an array of these values is returned as the embedding.
466
+ * @param document The document to generate an embedding for.
467
+ * @returns A promise that resolves with a synthetic embedding for the document.
468
+ */
469
+ async embedQuery(document) {
470
+ let doc = document;
471
+ // Only use the letters (and space) from the document, and make them lower case
472
+ doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, "");
473
+ // Pad the document to make sure it has a divisible number of chunks
474
+ const padMod = doc.length % this.vectorSize;
475
+ const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;
476
+ const padSize = doc.length + padGapSize;
477
+ doc = doc.padEnd(padSize, " ");
478
+ // Break it into chunks
479
+ const chunkSize = doc.length / this.vectorSize;
480
+ const docChunk = [];
481
+ for (let co = 0; co < doc.length; co += chunkSize) {
482
+ docChunk.push(doc.slice(co, co + chunkSize));
483
+ }
484
+ // Turn each chunk into a number
485
+ const ret = docChunk.map((s) => {
486
+ let sum = 0;
487
+ // Get a total value by adding the value of each character in the string
488
+ for (let co = 0; co < s.length; co += 1) {
489
+ sum += s === " " ? 0 : s.charCodeAt(co);
490
+ }
491
+ // Reduce this to a number between 0 and 25 inclusive
492
+ // Then get the fractional number by dividing it by 26
493
+ const ret = (sum % 26) / 26;
494
+ return ret;
495
+ });
496
+ return ret;
497
+ }
498
+ }
499
+ exports.SyntheticEmbeddings = SyntheticEmbeddings;
@@ -11,6 +11,7 @@ import { BaseRetriever } from "../../retrievers.js";
11
11
  import { Runnable } from "../../runnables/base.js";
12
12
  import { StructuredTool, ToolParams } from "../../tools.js";
13
13
  import { BaseTracer, Run } from "../../tracers/base.js";
14
+ import { Embeddings, EmbeddingsParams } from "../../embeddings.js";
14
15
  /**
15
16
  * Parser for comma-separated values. It splits the input text by commas
16
17
  * and trims the resulting values.
@@ -146,3 +147,55 @@ export declare class FakeTool<T extends z.ZodObject<any, any, any, any> = z.ZodO
146
147
  constructor(fields: FakeToolParams<T>);
147
148
  protected _call(arg: z.output<T>, _runManager?: CallbackManagerForToolRun): Promise<string>;
148
149
  }
150
+ /**
151
+ * A class that provides fake embeddings by overriding the embedDocuments
152
+ * and embedQuery methods to return fixed values.
153
+ */
154
+ export declare class FakeEmbeddings extends Embeddings {
155
+ constructor(params?: EmbeddingsParams);
156
+ /**
157
+ * Generates fixed embeddings for a list of documents.
158
+ * @param documents List of documents to generate embeddings for.
159
+ * @returns A promise that resolves with a list of fixed embeddings for each document.
160
+ */
161
+ embedDocuments(documents: string[]): Promise<number[][]>;
162
+ /**
163
+ * Generates a fixed embedding for a query.
164
+ * @param _ The query to generate an embedding for.
165
+ * @returns A promise that resolves with a fixed embedding for the query.
166
+ */
167
+ embedQuery(_: string): Promise<number[]>;
168
+ }
169
+ /**
170
+ * An interface that defines additional parameters specific to the
171
+ * SyntheticEmbeddings class.
172
+ */
173
+ interface SyntheticEmbeddingsParams extends EmbeddingsParams {
174
+ vectorSize: number;
175
+ }
176
+ /**
177
+ * A class that provides synthetic embeddings by overriding the
178
+ * embedDocuments and embedQuery methods to generate embeddings based on
179
+ * the input documents. The embeddings are generated by converting each
180
+ * document into chunks, calculating a numerical value for each chunk, and
181
+ * returning an array of these values as the embedding.
182
+ */
183
+ export declare class SyntheticEmbeddings extends Embeddings implements SyntheticEmbeddingsParams {
184
+ vectorSize: number;
185
+ constructor(params?: SyntheticEmbeddingsParams);
186
+ /**
187
+ * Generates synthetic embeddings for a list of documents.
188
+ * @param documents List of documents to generate embeddings for.
189
+ * @returns A promise that resolves with a list of synthetic embeddings for each document.
190
+ */
191
+ embedDocuments(documents: string[]): Promise<number[][]>;
192
+ /**
193
+ * Generates a synthetic embedding for a document. The document is
194
+ * converted into chunks, a numerical value is calculated for each chunk,
195
+ * and an array of these values is returned as the embedding.
196
+ * @param document The document to generate an embedding for.
197
+ * @returns A promise that resolves with a synthetic embedding for the document.
198
+ */
199
+ embedQuery(document: string): Promise<number[]>;
200
+ }
201
+ export {};
@@ -9,6 +9,7 @@ import { BaseRetriever } from "../../retrievers.js";
9
9
  import { Runnable } from "../../runnables/base.js";
10
10
  import { StructuredTool } from "../../tools.js";
11
11
  import { BaseTracer } from "../../tracers/base.js";
12
+ import { Embeddings } from "../../embeddings.js";
12
13
  /**
13
14
  * Parser for comma-separated values. It splits the input text by commas
14
15
  * and trims the resulting values.
@@ -392,3 +393,91 @@ export class FakeTool extends StructuredTool {
392
393
  return JSON.stringify(arg);
393
394
  }
394
395
  }
396
+ /**
397
+ * A class that provides fake embeddings by overriding the embedDocuments
398
+ * and embedQuery methods to return fixed values.
399
+ */
400
+ export class FakeEmbeddings extends Embeddings {
401
+ constructor(params) {
402
+ super(params ?? {});
403
+ }
404
+ /**
405
+ * Generates fixed embeddings for a list of documents.
406
+ * @param documents List of documents to generate embeddings for.
407
+ * @returns A promise that resolves with a list of fixed embeddings for each document.
408
+ */
409
+ embedDocuments(documents) {
410
+ return Promise.resolve(documents.map(() => [0.1, 0.2, 0.3, 0.4]));
411
+ }
412
+ /**
413
+ * Generates a fixed embedding for a query.
414
+ * @param _ The query to generate an embedding for.
415
+ * @returns A promise that resolves with a fixed embedding for the query.
416
+ */
417
+ embedQuery(_) {
418
+ return Promise.resolve([0.1, 0.2, 0.3, 0.4]);
419
+ }
420
+ }
421
+ /**
422
+ * A class that provides synthetic embeddings by overriding the
423
+ * embedDocuments and embedQuery methods to generate embeddings based on
424
+ * the input documents. The embeddings are generated by converting each
425
+ * document into chunks, calculating a numerical value for each chunk, and
426
+ * returning an array of these values as the embedding.
427
+ */
428
+ export class SyntheticEmbeddings extends Embeddings {
429
+ constructor(params) {
430
+ super(params ?? {});
431
+ Object.defineProperty(this, "vectorSize", {
432
+ enumerable: true,
433
+ configurable: true,
434
+ writable: true,
435
+ value: void 0
436
+ });
437
+ this.vectorSize = params?.vectorSize ?? 4;
438
+ }
439
+ /**
440
+ * Generates synthetic embeddings for a list of documents.
441
+ * @param documents List of documents to generate embeddings for.
442
+ * @returns A promise that resolves with a list of synthetic embeddings for each document.
443
+ */
444
+ async embedDocuments(documents) {
445
+ return Promise.all(documents.map((doc) => this.embedQuery(doc)));
446
+ }
447
+ /**
448
+ * Generates a synthetic embedding for a document. The document is
449
+ * converted into chunks, a numerical value is calculated for each chunk,
450
+ * and an array of these values is returned as the embedding.
451
+ * @param document The document to generate an embedding for.
452
+ * @returns A promise that resolves with a synthetic embedding for the document.
453
+ */
454
+ async embedQuery(document) {
455
+ let doc = document;
456
+ // Only use the letters (and space) from the document, and make them lower case
457
+ doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, "");
458
+ // Pad the document to make sure it has a divisible number of chunks
459
+ const padMod = doc.length % this.vectorSize;
460
+ const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;
461
+ const padSize = doc.length + padGapSize;
462
+ doc = doc.padEnd(padSize, " ");
463
+ // Break it into chunks
464
+ const chunkSize = doc.length / this.vectorSize;
465
+ const docChunk = [];
466
+ for (let co = 0; co < doc.length; co += chunkSize) {
467
+ docChunk.push(doc.slice(co, co + chunkSize));
468
+ }
469
+ // Turn each chunk into a number
470
+ const ret = docChunk.map((s) => {
471
+ let sum = 0;
472
+ // Get a total value by adding the value of each character in the string
473
+ for (let co = 0; co < s.length; co += 1) {
474
+ sum += s === " " ? 0 : s.charCodeAt(co);
475
+ }
476
+ // Reduce this to a number between 0 and 25 inclusive
477
+ // Then get the fractional number by dividing it by 26
478
+ const ret = (sum % 26) / 26;
479
+ return ret;
480
+ });
481
+ return ret;
482
+ }
483
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -43,8 +43,8 @@
43
43
  "p-queue": "^6.6.2",
44
44
  "p-retry": "4",
45
45
  "uuid": "^9.0.0",
46
- "zod": "^3.22.3",
47
- "zod-to-json-schema": "3.20.3"
46
+ "zod": "^3.22.4",
47
+ "zod-to-json-schema": "^3.22.3"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@jest/globals": "^29.5.0",