@langchain/core 0.1.16 → 0.1.17
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/runnables/base.cjs +17 -5
- package/dist/runnables/base.js +18 -6
- package/dist/utils/stream.cjs +6 -1
- package/dist/utils/stream.d.ts +1 -1
- package/dist/utils/stream.js +6 -1
- package/dist/utils/testing/index.cjs +13 -0
- package/dist/utils/testing/index.d.ts +2 -0
- package/dist/utils/testing/index.js +13 -0
- package/package.json +3 -3
package/dist/runnables/base.cjs
CHANGED
|
@@ -154,7 +154,11 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
154
154
|
* @returns A readable stream that is also an iterable.
|
|
155
155
|
*/
|
|
156
156
|
async stream(input, options) {
|
|
157
|
-
|
|
157
|
+
// Buffer the first streamed chunk to allow for initial errors
|
|
158
|
+
// to surface immediately.
|
|
159
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup(this._streamIterator(input, options));
|
|
160
|
+
await wrappedGenerator.setup;
|
|
161
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
158
162
|
}
|
|
159
163
|
_separateRunnableConfigFromCallOptions(options = {}) {
|
|
160
164
|
const runnableConfig = (0, config_js_1.ensureConfig)({
|
|
@@ -1043,7 +1047,9 @@ class RunnableMap extends Runnable {
|
|
|
1043
1047
|
async function* generator() {
|
|
1044
1048
|
yield input;
|
|
1045
1049
|
}
|
|
1046
|
-
|
|
1050
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1051
|
+
await wrappedGenerator.setup;
|
|
1052
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1047
1053
|
}
|
|
1048
1054
|
}
|
|
1049
1055
|
exports.RunnableMap = RunnableMap;
|
|
@@ -1132,7 +1138,9 @@ class RunnableLambda extends Runnable {
|
|
|
1132
1138
|
async function* generator() {
|
|
1133
1139
|
yield input;
|
|
1134
1140
|
}
|
|
1135
|
-
|
|
1141
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1142
|
+
await wrappedGenerator.setup;
|
|
1143
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1136
1144
|
}
|
|
1137
1145
|
}
|
|
1138
1146
|
exports.RunnableLambda = RunnableLambda;
|
|
@@ -1329,7 +1337,9 @@ class RunnableAssign extends Runnable {
|
|
|
1329
1337
|
async function* generator() {
|
|
1330
1338
|
yield input;
|
|
1331
1339
|
}
|
|
1332
|
-
|
|
1340
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1341
|
+
await wrappedGenerator.setup;
|
|
1342
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1333
1343
|
}
|
|
1334
1344
|
}
|
|
1335
1345
|
exports.RunnableAssign = RunnableAssign;
|
|
@@ -1395,7 +1405,9 @@ class RunnablePick extends Runnable {
|
|
|
1395
1405
|
async function* generator() {
|
|
1396
1406
|
yield input;
|
|
1397
1407
|
}
|
|
1398
|
-
|
|
1408
|
+
const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1409
|
+
await wrappedGenerator.setup;
|
|
1410
|
+
return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1399
1411
|
}
|
|
1400
1412
|
}
|
|
1401
1413
|
exports.RunnablePick = RunnablePick;
|
package/dist/runnables/base.js
CHANGED
|
@@ -2,7 +2,7 @@ import pRetry from "p-retry";
|
|
|
2
2
|
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
|
-
import { IterableReadableStream, concat, atee, pipeGeneratorWithSetup, } from "../utils/stream.js";
|
|
5
|
+
import { IterableReadableStream, concat, atee, pipeGeneratorWithSetup, AsyncGeneratorWithSetup, } from "../utils/stream.js";
|
|
6
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";
|
|
@@ -148,7 +148,11 @@ export class Runnable extends Serializable {
|
|
|
148
148
|
* @returns A readable stream that is also an iterable.
|
|
149
149
|
*/
|
|
150
150
|
async stream(input, options) {
|
|
151
|
-
|
|
151
|
+
// Buffer the first streamed chunk to allow for initial errors
|
|
152
|
+
// to surface immediately.
|
|
153
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup(this._streamIterator(input, options));
|
|
154
|
+
await wrappedGenerator.setup;
|
|
155
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
152
156
|
}
|
|
153
157
|
_separateRunnableConfigFromCallOptions(options = {}) {
|
|
154
158
|
const runnableConfig = ensureConfig({
|
|
@@ -1032,7 +1036,9 @@ export class RunnableMap extends Runnable {
|
|
|
1032
1036
|
async function* generator() {
|
|
1033
1037
|
yield input;
|
|
1034
1038
|
}
|
|
1035
|
-
|
|
1039
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1040
|
+
await wrappedGenerator.setup;
|
|
1041
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1036
1042
|
}
|
|
1037
1043
|
}
|
|
1038
1044
|
/**
|
|
@@ -1120,7 +1126,9 @@ export class RunnableLambda extends Runnable {
|
|
|
1120
1126
|
async function* generator() {
|
|
1121
1127
|
yield input;
|
|
1122
1128
|
}
|
|
1123
|
-
|
|
1129
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1130
|
+
await wrappedGenerator.setup;
|
|
1131
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1124
1132
|
}
|
|
1125
1133
|
}
|
|
1126
1134
|
export class RunnableParallel extends RunnableMap {
|
|
@@ -1313,7 +1321,9 @@ export class RunnableAssign extends Runnable {
|
|
|
1313
1321
|
async function* generator() {
|
|
1314
1322
|
yield input;
|
|
1315
1323
|
}
|
|
1316
|
-
|
|
1324
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1325
|
+
await wrappedGenerator.setup;
|
|
1326
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1317
1327
|
}
|
|
1318
1328
|
}
|
|
1319
1329
|
/**
|
|
@@ -1378,6 +1388,8 @@ export class RunnablePick extends Runnable {
|
|
|
1378
1388
|
async function* generator() {
|
|
1379
1389
|
yield input;
|
|
1380
1390
|
}
|
|
1381
|
-
|
|
1391
|
+
const wrappedGenerator = new AsyncGeneratorWithSetup(this.transform(generator(), options));
|
|
1392
|
+
await wrappedGenerator.setup;
|
|
1393
|
+
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1382
1394
|
}
|
|
1383
1395
|
}
|
package/dist/utils/stream.cjs
CHANGED
|
@@ -196,7 +196,12 @@ class AsyncGeneratorWithSetup {
|
|
|
196
196
|
// to each generator is available.
|
|
197
197
|
this.setup = new Promise((resolve, reject) => {
|
|
198
198
|
this.firstResult = generator.next();
|
|
199
|
-
|
|
199
|
+
if (startSetup) {
|
|
200
|
+
this.firstResult.then(startSetup).then(resolve, reject);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
this.firstResult.then((_result) => resolve(undefined), reject);
|
|
204
|
+
}
|
|
200
205
|
});
|
|
201
206
|
}
|
|
202
207
|
async next(...args) {
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn =
|
|
|
17
17
|
setup: Promise<S>;
|
|
18
18
|
private firstResult;
|
|
19
19
|
private firstResultUsed;
|
|
20
|
-
constructor(generator: AsyncGenerator<T>, startSetup
|
|
20
|
+
constructor(generator: AsyncGenerator<T>, startSetup?: () => Promise<S>);
|
|
21
21
|
next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
|
|
22
22
|
return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
|
|
23
23
|
throw(e: Error): Promise<IteratorResult<T>>;
|
package/dist/utils/stream.js
CHANGED
|
@@ -190,7 +190,12 @@ export class AsyncGeneratorWithSetup {
|
|
|
190
190
|
// to each generator is available.
|
|
191
191
|
this.setup = new Promise((resolve, reject) => {
|
|
192
192
|
this.firstResult = generator.next();
|
|
193
|
-
|
|
193
|
+
if (startSetup) {
|
|
194
|
+
this.firstResult.then(startSetup).then(resolve, reject);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
this.firstResult.then((_result) => resolve(undefined), reject);
|
|
198
|
+
}
|
|
194
199
|
});
|
|
195
200
|
}
|
|
196
201
|
async next(...args) {
|
|
@@ -106,18 +106,31 @@ class FakeStreamingLLM extends llms_js_1.LLM {
|
|
|
106
106
|
writable: true,
|
|
107
107
|
value: void 0
|
|
108
108
|
});
|
|
109
|
+
Object.defineProperty(this, "thrownErrorString", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: void 0
|
|
114
|
+
});
|
|
109
115
|
this.sleep = fields.sleep ?? this.sleep;
|
|
110
116
|
this.responses = fields.responses;
|
|
117
|
+
this.thrownErrorString = fields.thrownErrorString;
|
|
111
118
|
}
|
|
112
119
|
_llmType() {
|
|
113
120
|
return "fake";
|
|
114
121
|
}
|
|
115
122
|
async _call(prompt) {
|
|
123
|
+
if (this.thrownErrorString) {
|
|
124
|
+
throw new Error(this.thrownErrorString);
|
|
125
|
+
}
|
|
116
126
|
const response = this.responses?.[0];
|
|
117
127
|
this.responses = this.responses?.slice(1);
|
|
118
128
|
return response ?? prompt;
|
|
119
129
|
}
|
|
120
130
|
async *_streamResponseChunks(input) {
|
|
131
|
+
if (this.thrownErrorString) {
|
|
132
|
+
throw new Error(this.thrownErrorString);
|
|
133
|
+
}
|
|
121
134
|
const response = this.responses?.[0];
|
|
122
135
|
this.responses = this.responses?.slice(1);
|
|
123
136
|
for (const c of response ?? input) {
|
|
@@ -42,9 +42,11 @@ export declare class FakeLLM extends LLM {
|
|
|
42
42
|
export declare class FakeStreamingLLM extends LLM {
|
|
43
43
|
sleep?: number;
|
|
44
44
|
responses?: string[];
|
|
45
|
+
thrownErrorString?: string;
|
|
45
46
|
constructor(fields: {
|
|
46
47
|
sleep?: number;
|
|
47
48
|
responses?: string[];
|
|
49
|
+
thrownErrorString?: string;
|
|
48
50
|
} & BaseLLMParams);
|
|
49
51
|
_llmType(): string;
|
|
50
52
|
_call(prompt: string): Promise<string>;
|
|
@@ -100,18 +100,31 @@ export class FakeStreamingLLM extends LLM {
|
|
|
100
100
|
writable: true,
|
|
101
101
|
value: void 0
|
|
102
102
|
});
|
|
103
|
+
Object.defineProperty(this, "thrownErrorString", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
108
|
+
});
|
|
103
109
|
this.sleep = fields.sleep ?? this.sleep;
|
|
104
110
|
this.responses = fields.responses;
|
|
111
|
+
this.thrownErrorString = fields.thrownErrorString;
|
|
105
112
|
}
|
|
106
113
|
_llmType() {
|
|
107
114
|
return "fake";
|
|
108
115
|
}
|
|
109
116
|
async _call(prompt) {
|
|
117
|
+
if (this.thrownErrorString) {
|
|
118
|
+
throw new Error(this.thrownErrorString);
|
|
119
|
+
}
|
|
110
120
|
const response = this.responses?.[0];
|
|
111
121
|
this.responses = this.responses?.slice(1);
|
|
112
122
|
return response ?? prompt;
|
|
113
123
|
}
|
|
114
124
|
async *_streamResponseChunks(input) {
|
|
125
|
+
if (this.thrownErrorString) {
|
|
126
|
+
throw new Error(this.thrownErrorString);
|
|
127
|
+
}
|
|
115
128
|
const response = this.responses?.[0];
|
|
116
129
|
this.responses = this.responses?.slice(1);
|
|
117
130
|
for (const c of response ?? input) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
|
|
29
29
|
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
|
|
30
30
|
"test:single": "NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
|
|
31
|
-
"format": "prettier --write \"src\"",
|
|
32
|
-
"format:check": "prettier --check \"src\""
|
|
31
|
+
"format": "prettier --write \"src\" \"scripts\"",
|
|
32
|
+
"format:check": "prettier --check \"src\" \"scripts\""
|
|
33
33
|
},
|
|
34
34
|
"author": "LangChain",
|
|
35
35
|
"license": "MIT",
|