@langchain/core 0.2.0-rc.0 → 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.
- package/dist/callbacks/base.cjs +9 -1
- package/dist/callbacks/base.d.ts +3 -0
- package/dist/callbacks/base.js +9 -1
- package/dist/callbacks/manager.cjs +51 -0
- package/dist/callbacks/manager.js +51 -0
- package/dist/language_models/base.cjs +3 -0
- package/dist/language_models/base.js +3 -0
- package/dist/language_models/chat_models.cjs +21 -3
- package/dist/language_models/chat_models.d.ts +9 -0
- package/dist/language_models/chat_models.js +21 -3
- package/dist/runnables/base.cjs +88 -14
- package/dist/runnables/base.d.ts +4 -4
- package/dist/runnables/base.js +88 -14
- package/dist/runnables/iter.cjs +46 -0
- package/dist/runnables/iter.d.ts +5 -0
- package/dist/runnables/iter.js +39 -0
- package/dist/runnables/passthrough.cjs +1 -0
- package/dist/runnables/passthrough.d.ts +1 -1
- package/dist/runnables/passthrough.js +1 -0
- package/dist/runnables/remote.cjs +60 -48
- package/dist/runnables/remote.d.ts +6 -2
- package/dist/runnables/remote.js +61 -49
- package/dist/singletons/index.cjs +1 -1
- package/dist/singletons/index.d.ts +2 -2
- package/dist/singletons/index.js +1 -1
- package/dist/utils/stream.cjs +27 -11
- package/dist/utils/stream.d.ts +6 -1
- package/dist/utils/stream.js +27 -11
- package/package.json +2 -2
package/dist/runnables/remote.js
CHANGED
|
@@ -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
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
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
|
-
|
|
435
|
-
|
|
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
|
-
|
|
453
|
-
|
|
454
|
-
throw err;
|
|
464
|
+
if (options.encoding !== undefined) {
|
|
465
|
+
throw new Error("Special encodings are not supported for this runnable.");
|
|
455
466
|
}
|
|
456
|
-
|
|
467
|
+
const eventStream = this._streamEvents(input, options, streamOptions);
|
|
468
|
+
return IterableReadableStream.fromAsyncGenerator(eventStream);
|
|
457
469
|
}
|
|
458
470
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export interface AsyncLocalStorageInterface {
|
|
2
2
|
getStore: () => any | undefined;
|
|
3
|
-
run: (store: any, callback: () =>
|
|
3
|
+
run: <T>(store: any, callback: () => T) => T;
|
|
4
4
|
}
|
|
5
5
|
export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface {
|
|
6
6
|
getStore(): any;
|
|
7
|
-
run(_store: any, callback: () =>
|
|
7
|
+
run<T>(_store: any, callback: () => T): T;
|
|
8
8
|
}
|
|
9
9
|
declare class AsyncLocalStorageProvider {
|
|
10
10
|
private asyncLocalStorage;
|
package/dist/singletons/index.js
CHANGED
package/dist/utils/stream.cjs
CHANGED
|
@@ -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(
|
|
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
|
-
|
|
202
|
-
|
|
203
|
-
this.firstResult.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
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
|
}
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -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(
|
|
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>>;
|
package/dist/utils/stream.js
CHANGED
|
@@ -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(
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
this.firstResult.
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
-
|
|
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
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.2.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"ansi-styles": "^5.0.0",
|
|
44
44
|
"camelcase": "6",
|
|
45
45
|
"decamelize": "1.2.0",
|
|
46
|
-
"js-tiktoken": "^1.0.
|
|
46
|
+
"js-tiktoken": "^1.0.12",
|
|
47
47
|
"langsmith": "~0.1.7",
|
|
48
48
|
"ml-distance": "^4.0.0",
|
|
49
49
|
"mustache": "^4.2.0",
|