@langchain/core 0.1.63 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/callbacks/base.cjs +9 -1
  2. package/dist/callbacks/base.d.ts +3 -0
  3. package/dist/callbacks/base.js +9 -1
  4. package/dist/callbacks/manager.cjs +51 -0
  5. package/dist/callbacks/manager.js +51 -0
  6. package/dist/document_loaders/base.cjs +24 -0
  7. package/dist/document_loaders/base.d.ts +28 -0
  8. package/dist/document_loaders/base.js +20 -0
  9. package/dist/documents/transformers.d.ts +1 -1
  10. package/dist/indexing/base.cjs +270 -0
  11. package/dist/indexing/base.d.ts +114 -0
  12. package/dist/indexing/base.js +261 -0
  13. package/dist/indexing/index.cjs +18 -0
  14. package/dist/indexing/index.d.ts +2 -0
  15. package/dist/indexing/index.js +2 -0
  16. package/dist/indexing/record_manager.cjs +18 -0
  17. package/dist/indexing/record_manager.d.ts +64 -0
  18. package/dist/indexing/record_manager.js +14 -0
  19. package/dist/language_models/chat_models.cjs +21 -3
  20. package/dist/language_models/chat_models.d.ts +9 -0
  21. package/dist/language_models/chat_models.js +21 -3
  22. package/dist/runnables/base.cjs +39 -14
  23. package/dist/runnables/base.d.ts +4 -4
  24. package/dist/runnables/base.js +39 -14
  25. package/dist/runnables/passthrough.cjs +1 -0
  26. package/dist/runnables/passthrough.d.ts +1 -1
  27. package/dist/runnables/passthrough.js +1 -0
  28. package/dist/runnables/remote.cjs +60 -48
  29. package/dist/runnables/remote.d.ts +6 -2
  30. package/dist/runnables/remote.js +61 -49
  31. package/dist/utils/stream.cjs +27 -11
  32. package/dist/utils/stream.d.ts +6 -1
  33. package/dist/utils/stream.js +27 -11
  34. package/document_loaders/base.cjs +1 -0
  35. package/document_loaders/base.d.cts +1 -0
  36. package/document_loaders/base.d.ts +1 -0
  37. package/document_loaders/base.js +1 -0
  38. package/indexing.cjs +1 -0
  39. package/indexing.d.cts +1 -0
  40. package/indexing.d.ts +1 -0
  41. package/indexing.js +1 -0
  42. package/package.json +27 -1
@@ -6,7 +6,7 @@ import { RunLogPatch, RunLog, } from "../tracers/log_stream.js";
6
6
  import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessage, FunctionMessageChunk, HumanMessage, HumanMessageChunk, SystemMessage, SystemMessageChunk, ToolMessage, ToolMessageChunk, isBaseMessage, } from "../messages/index.js";
7
7
  import { GenerationChunk, ChatGenerationChunk, RUN_KEY } from "../outputs.js";
8
8
  import { convertEventStreamToIterableReadableDataStream } from "../utils/event_source_parse.js";
9
- import { concat } from "../utils/stream.js";
9
+ import { IterableReadableStream, concat } from "../utils/stream.js";
10
10
  function isSuperset(set, subset) {
11
11
  for (const elem of subset) {
12
12
  if (!set.has(elem)) {
@@ -402,57 +402,69 @@ export class RemoteRunnable extends Runnable {
402
402
  }
403
403
  await runManager?.handleChainEnd(runLog?.state.final_output);
404
404
  }
405
- async *streamEvents(input, options, streamOptions) {
406
- if (options?.version !== "v1") {
407
- throw new Error(`Only version "v1" of the events schema is currently supported.`);
408
- }
409
- const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
410
- const callbackManager_ = await getCallbackManagerForConfig(options);
411
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
412
- // The type is in camelCase but the API only accepts snake_case.
413
- const camelCaseStreamOptions = {
414
- include_names: streamOptions?.includeNames,
415
- include_types: streamOptions?.includeTypes,
416
- include_tags: streamOptions?.includeTags,
417
- exclude_names: streamOptions?.excludeNames,
418
- exclude_types: streamOptions?.excludeTypes,
419
- exclude_tags: streamOptions?.excludeTags,
420
- };
421
- const events = [];
422
- try {
423
- const response = await this.post("/stream_events", {
424
- input,
425
- config: removeCallbacks(config),
426
- kwargs,
427
- ...camelCaseStreamOptions,
428
- diff: false,
429
- });
430
- const { body, ok } = response;
431
- if (!ok) {
432
- throw new Error(`${response.status} Error: ${await response.text()}`);
405
+ _streamEvents(input, options, streamOptions) {
406
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
407
+ const outerThis = this;
408
+ const generator = async function* () {
409
+ const [config, kwargs] = outerThis._separateRunnableConfigFromCallOptions(options);
410
+ const callbackManager_ = await getCallbackManagerForConfig(options);
411
+ const runManager = await callbackManager_?.handleChainStart(outerThis.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
412
+ // The type is in camelCase but the API only accepts snake_case.
413
+ const camelCaseStreamOptions = {
414
+ include_names: streamOptions?.includeNames,
415
+ include_types: streamOptions?.includeTypes,
416
+ include_tags: streamOptions?.includeTags,
417
+ exclude_names: streamOptions?.excludeNames,
418
+ exclude_types: streamOptions?.excludeTypes,
419
+ exclude_tags: streamOptions?.excludeTags,
420
+ };
421
+ const events = [];
422
+ try {
423
+ const response = await outerThis.post("/stream_events", {
424
+ input,
425
+ config: removeCallbacks(config),
426
+ kwargs,
427
+ ...camelCaseStreamOptions,
428
+ diff: false,
429
+ });
430
+ const { body, ok } = response;
431
+ if (!ok) {
432
+ throw new Error(`${response.status} Error: ${await response.text()}`);
433
+ }
434
+ if (!body) {
435
+ throw new Error("Could not begin remote stream events. Please check the given URL and try again.");
436
+ }
437
+ const runnableStream = convertEventStreamToIterableReadableDataStream(body);
438
+ for await (const log of runnableStream) {
439
+ const chunk = revive(JSON.parse(log));
440
+ const event = {
441
+ event: chunk.event,
442
+ name: chunk.name,
443
+ run_id: chunk.run_id,
444
+ tags: chunk.tags,
445
+ metadata: chunk.metadata,
446
+ data: chunk.data,
447
+ };
448
+ yield event;
449
+ events.push(event);
450
+ }
433
451
  }
434
- if (!body) {
435
- throw new Error("Could not begin remote stream events. Please check the given URL and try again.");
436
- }
437
- const runnableStream = convertEventStreamToIterableReadableDataStream(body);
438
- for await (const log of runnableStream) {
439
- const chunk = revive(JSON.parse(log));
440
- const event = {
441
- event: chunk.event,
442
- name: chunk.name,
443
- run_id: chunk.run_id,
444
- tags: chunk.tags,
445
- metadata: chunk.metadata,
446
- data: chunk.data,
447
- };
448
- yield event;
449
- events.push(event);
452
+ catch (err) {
453
+ await runManager?.handleChainError(err);
454
+ throw err;
450
455
  }
456
+ await runManager?.handleChainEnd(events);
457
+ };
458
+ return generator();
459
+ }
460
+ streamEvents(input, options, streamOptions) {
461
+ if (options?.version !== "v1") {
462
+ throw new Error(`Only version "v1" of the events schema is currently supported.`);
451
463
  }
452
- catch (err) {
453
- await runManager?.handleChainError(err);
454
- throw err;
464
+ if (options.encoding !== undefined) {
465
+ throw new Error("Special encodings are not supported for this runnable.");
455
466
  }
456
- await runManager?.handleChainEnd(events);
467
+ const eventStream = this._streamEvents(input, options, streamOptions);
468
+ return IterableReadableStream.fromAsyncGenerator(eventStream);
457
469
  }
458
470
  }
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pipeGeneratorWithSetup = exports.AsyncGeneratorWithSetup = exports.concat = exports.atee = exports.IterableReadableStream = void 0;
4
+ // Make this a type to override ReadableStream's async iterator type in case
5
+ // the popular web-streams-polyfill is imported - the supplied types
6
+ const index_js_1 = require("../singletons/index.cjs");
4
7
  /*
5
8
  * Support async iterator syntax for ReadableStreams in all environments.
6
9
  * Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
@@ -167,7 +170,7 @@ function concat(first, second) {
167
170
  }
168
171
  exports.concat = concat;
169
172
  class AsyncGeneratorWithSetup {
170
- constructor(generator, startSetup) {
173
+ constructor(params) {
171
174
  Object.defineProperty(this, "generator", {
172
175
  enumerable: true,
173
176
  configurable: true,
@@ -180,6 +183,12 @@ class AsyncGeneratorWithSetup {
180
183
  writable: true,
181
184
  value: void 0
182
185
  });
186
+ Object.defineProperty(this, "config", {
187
+ enumerable: true,
188
+ configurable: true,
189
+ writable: true,
190
+ value: void 0
191
+ });
183
192
  Object.defineProperty(this, "firstResult", {
184
193
  enumerable: true,
185
194
  configurable: true,
@@ -192,19 +201,23 @@ class AsyncGeneratorWithSetup {
192
201
  writable: true,
193
202
  value: false
194
203
  });
195
- this.generator = generator;
204
+ this.generator = params.generator;
205
+ this.config = params.config;
196
206
  // setup is a promise that resolves only after the first iterator value
197
207
  // is available. this is useful when setup of several piped generators
198
208
  // needs to happen in logical order, ie. in the order in which input to
199
209
  // to each generator is available.
200
210
  this.setup = new Promise((resolve, reject) => {
201
- this.firstResult = generator.next();
202
- if (startSetup) {
203
- this.firstResult.then(startSetup).then(resolve, reject);
204
- }
205
- else {
206
- this.firstResult.then((_result) => resolve(undefined), reject);
207
- }
211
+ const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
212
+ void storage.run(params.config, async () => {
213
+ this.firstResult = params.generator.next();
214
+ if (params.startSetup) {
215
+ this.firstResult.then(params.startSetup).then(resolve, reject);
216
+ }
217
+ else {
218
+ this.firstResult.then((_result) => resolve(undefined), reject);
219
+ }
220
+ });
208
221
  });
209
222
  }
210
223
  async next(...args) {
@@ -212,7 +225,10 @@ class AsyncGeneratorWithSetup {
212
225
  this.firstResultUsed = true;
213
226
  return this.firstResult;
214
227
  }
215
- return this.generator.next(...args);
228
+ const storage = index_js_1.AsyncLocalStorageProviderSingleton.getInstance();
229
+ return storage.run(this.config, async () => {
230
+ return this.generator.next(...args);
231
+ });
216
232
  }
217
233
  async return(value) {
218
234
  return this.generator.return(value);
@@ -226,7 +242,7 @@ class AsyncGeneratorWithSetup {
226
242
  }
227
243
  exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
228
244
  async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
229
- const gen = new AsyncGeneratorWithSetup(generator, startSetup);
245
+ const gen = new AsyncGeneratorWithSetup({ generator, startSetup });
230
246
  const setup = await gen.setup;
231
247
  return { output: to(gen, setup, ...args), setup };
232
248
  }
@@ -14,9 +14,14 @@ export declare function concat<T extends Array<any> | string | number | Record<s
14
14
  export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn = unknown, TNext = unknown> implements AsyncGenerator<T, TReturn, TNext> {
15
15
  private generator;
16
16
  setup: Promise<S>;
17
+ config?: unknown;
17
18
  private firstResult;
18
19
  private firstResultUsed;
19
- constructor(generator: AsyncGenerator<T>, startSetup?: () => Promise<S>);
20
+ constructor(params: {
21
+ generator: AsyncGenerator<T>;
22
+ startSetup?: () => Promise<S>;
23
+ config?: unknown;
24
+ });
20
25
  next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
21
26
  return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
22
27
  throw(e: Error): Promise<IteratorResult<T>>;
@@ -1,3 +1,6 @@
1
+ // Make this a type to override ReadableStream's async iterator type in case
2
+ // the popular web-streams-polyfill is imported - the supplied types
3
+ import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
1
4
  /*
2
5
  * Support async iterator syntax for ReadableStreams in all environments.
3
6
  * Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
@@ -161,7 +164,7 @@ export function concat(first, second) {
161
164
  }
162
165
  }
163
166
  export class AsyncGeneratorWithSetup {
164
- constructor(generator, startSetup) {
167
+ constructor(params) {
165
168
  Object.defineProperty(this, "generator", {
166
169
  enumerable: true,
167
170
  configurable: true,
@@ -174,6 +177,12 @@ export class AsyncGeneratorWithSetup {
174
177
  writable: true,
175
178
  value: void 0
176
179
  });
180
+ Object.defineProperty(this, "config", {
181
+ enumerable: true,
182
+ configurable: true,
183
+ writable: true,
184
+ value: void 0
185
+ });
177
186
  Object.defineProperty(this, "firstResult", {
178
187
  enumerable: true,
179
188
  configurable: true,
@@ -186,19 +195,23 @@ export class AsyncGeneratorWithSetup {
186
195
  writable: true,
187
196
  value: false
188
197
  });
189
- this.generator = generator;
198
+ this.generator = params.generator;
199
+ this.config = params.config;
190
200
  // setup is a promise that resolves only after the first iterator value
191
201
  // is available. this is useful when setup of several piped generators
192
202
  // needs to happen in logical order, ie. in the order in which input to
193
203
  // to each generator is available.
194
204
  this.setup = new Promise((resolve, reject) => {
195
- this.firstResult = generator.next();
196
- if (startSetup) {
197
- this.firstResult.then(startSetup).then(resolve, reject);
198
- }
199
- else {
200
- this.firstResult.then((_result) => resolve(undefined), reject);
201
- }
205
+ const storage = AsyncLocalStorageProviderSingleton.getInstance();
206
+ void storage.run(params.config, async () => {
207
+ this.firstResult = params.generator.next();
208
+ if (params.startSetup) {
209
+ this.firstResult.then(params.startSetup).then(resolve, reject);
210
+ }
211
+ else {
212
+ this.firstResult.then((_result) => resolve(undefined), reject);
213
+ }
214
+ });
202
215
  });
203
216
  }
204
217
  async next(...args) {
@@ -206,7 +219,10 @@ export class AsyncGeneratorWithSetup {
206
219
  this.firstResultUsed = true;
207
220
  return this.firstResult;
208
221
  }
209
- return this.generator.next(...args);
222
+ const storage = AsyncLocalStorageProviderSingleton.getInstance();
223
+ return storage.run(this.config, async () => {
224
+ return this.generator.next(...args);
225
+ });
210
226
  }
211
227
  async return(value) {
212
228
  return this.generator.return(value);
@@ -219,7 +235,7 @@ export class AsyncGeneratorWithSetup {
219
235
  }
220
236
  }
221
237
  export async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
222
- const gen = new AsyncGeneratorWithSetup(generator, startSetup);
238
+ const gen = new AsyncGeneratorWithSetup({ generator, startSetup });
223
239
  const setup = await gen.setup;
224
240
  return { output: to(gen, setup, ...args), setup };
225
241
  }
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/document_loaders/base.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/document_loaders/base.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/document_loaders/base.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/document_loaders/base.js'
package/indexing.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/indexing/index.cjs');
package/indexing.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/indexing/index.js'
package/indexing.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/indexing/index.js'
package/indexing.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/indexing/index.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.63",
3
+ "version": "0.2.0",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -158,6 +158,15 @@
158
158
  "import": "./documents.js",
159
159
  "require": "./documents.cjs"
160
160
  },
161
+ "./document_loaders/base": {
162
+ "types": {
163
+ "import": "./document_loaders/base.d.ts",
164
+ "require": "./document_loaders/base.d.cts",
165
+ "default": "./document_loaders/base.d.ts"
166
+ },
167
+ "import": "./document_loaders/base.js",
168
+ "require": "./document_loaders/base.cjs"
169
+ },
161
170
  "./embeddings": {
162
171
  "types": {
163
172
  "import": "./embeddings.d.ts",
@@ -176,6 +185,15 @@
176
185
  "import": "./example_selectors.js",
177
186
  "require": "./example_selectors.cjs"
178
187
  },
188
+ "./indexing": {
189
+ "types": {
190
+ "import": "./indexing.d.ts",
191
+ "require": "./indexing.d.cts",
192
+ "default": "./indexing.d.ts"
193
+ },
194
+ "import": "./indexing.js",
195
+ "require": "./indexing.cjs"
196
+ },
179
197
  "./language_models/base": {
180
198
  "types": {
181
199
  "import": "./language_models/base.d.ts",
@@ -604,6 +622,10 @@
604
622
  "documents.js",
605
623
  "documents.d.ts",
606
624
  "documents.d.cts",
625
+ "document_loaders/base.cjs",
626
+ "document_loaders/base.js",
627
+ "document_loaders/base.d.ts",
628
+ "document_loaders/base.d.cts",
607
629
  "embeddings.cjs",
608
630
  "embeddings.js",
609
631
  "embeddings.d.ts",
@@ -612,6 +634,10 @@
612
634
  "example_selectors.js",
613
635
  "example_selectors.d.ts",
614
636
  "example_selectors.d.cts",
637
+ "indexing.cjs",
638
+ "indexing.js",
639
+ "indexing.d.ts",
640
+ "indexing.d.cts",
615
641
  "language_models/base.cjs",
616
642
  "language_models/base.js",
617
643
  "language_models/base.d.ts",