@langchain/core 0.1.4 → 0.1.6
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/README.md +1 -1
- package/dist/callbacks/manager.d.ts +0 -5
- package/dist/chat_history.d.ts +1 -0
- package/dist/language_models/base.d.ts +2 -0
- package/dist/output_parsers/index.cjs +1 -0
- package/dist/output_parsers/index.d.ts +1 -0
- package/dist/output_parsers/index.js +1 -0
- package/dist/output_parsers/json.cjs +137 -0
- package/dist/output_parsers/json.d.ts +17 -0
- package/dist/output_parsers/json.js +131 -0
- package/dist/output_parsers/list.cjs +121 -2
- package/dist/output_parsers/list.d.ts +21 -2
- package/dist/output_parsers/list.js +119 -2
- package/dist/prompt_values.cjs +3 -0
- package/dist/prompt_values.d.ts +1 -0
- package/dist/prompt_values.js +3 -0
- package/dist/prompts/chat.cjs +22 -2
- package/dist/prompts/chat.d.ts +4 -2
- package/dist/prompts/chat.js +22 -2
- package/dist/runnables/base.cjs +290 -31
- package/dist/runnables/base.d.ts +72 -20
- package/dist/runnables/base.js +289 -32
- package/dist/runnables/config.cjs +7 -3
- package/dist/runnables/config.d.ts +13 -2
- package/dist/runnables/config.js +5 -1
- package/dist/runnables/history.cjs +3 -3
- package/dist/runnables/history.d.ts +5 -6
- package/dist/runnables/history.js +3 -3
- package/dist/runnables/index.cjs +5 -2
- package/dist/runnables/index.d.ts +3 -3
- package/dist/runnables/index.js +3 -2
- package/dist/runnables/passthrough.cjs +5 -31
- package/dist/runnables/passthrough.d.ts +3 -11
- package/dist/runnables/passthrough.js +4 -29
- package/dist/utils/stream.cjs +113 -1
- package/dist/utils/stream.d.ts +13 -0
- package/dist/utils/stream.js +109 -0
- package/dist/utils/testing/index.cjs +14 -1
- package/dist/utils/testing/index.d.ts +5 -0
- package/dist/utils/testing/index.js +14 -1
- package/package.json +1 -1
package/dist/runnables/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.
|
|
3
|
+
exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.getCallbackManagerForConfig = exports._coerceToRunnable = exports.RunnablePick = exports.RunnableAssign = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableParallel = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
|
|
4
4
|
var base_js_1 = require("./base.cjs");
|
|
5
5
|
Object.defineProperty(exports, "Runnable", { enumerable: true, get: function () { return base_js_1.Runnable; } });
|
|
6
6
|
Object.defineProperty(exports, "RunnableBinding", { enumerable: true, get: function () { return base_js_1.RunnableBinding; } });
|
|
@@ -11,10 +11,13 @@ Object.defineProperty(exports, "RunnableMap", { enumerable: true, get: function
|
|
|
11
11
|
Object.defineProperty(exports, "RunnableParallel", { enumerable: true, get: function () { return base_js_1.RunnableParallel; } });
|
|
12
12
|
Object.defineProperty(exports, "RunnableLambda", { enumerable: true, get: function () { return base_js_1.RunnableLambda; } });
|
|
13
13
|
Object.defineProperty(exports, "RunnableWithFallbacks", { enumerable: true, get: function () { return base_js_1.RunnableWithFallbacks; } });
|
|
14
|
+
Object.defineProperty(exports, "RunnableAssign", { enumerable: true, get: function () { return base_js_1.RunnableAssign; } });
|
|
15
|
+
Object.defineProperty(exports, "RunnablePick", { enumerable: true, get: function () { return base_js_1.RunnablePick; } });
|
|
14
16
|
Object.defineProperty(exports, "_coerceToRunnable", { enumerable: true, get: function () { return base_js_1._coerceToRunnable; } });
|
|
17
|
+
var config_js_1 = require("./config.cjs");
|
|
18
|
+
Object.defineProperty(exports, "getCallbackManagerForConfig", { enumerable: true, get: function () { return config_js_1.getCallbackManagerForConfig; } });
|
|
15
19
|
var passthrough_js_1 = require("./passthrough.cjs");
|
|
16
20
|
Object.defineProperty(exports, "RunnablePassthrough", { enumerable: true, get: function () { return passthrough_js_1.RunnablePassthrough; } });
|
|
17
|
-
Object.defineProperty(exports, "RunnableAssign", { enumerable: true, get: function () { return passthrough_js_1.RunnableAssign; } });
|
|
18
21
|
var router_js_1 = require("./router.cjs");
|
|
19
22
|
Object.defineProperty(exports, "RouterRunnable", { enumerable: true, get: function () { return router_js_1.RouterRunnable; } });
|
|
20
23
|
var branch_js_1 = require("./branch.cjs");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { type RunnableFunc, type RunnableLike, type RunnableBatchOptions, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableInterface, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, _coerceToRunnable, } from "./base.js";
|
|
2
|
-
export type
|
|
3
|
-
export { RunnablePassthrough
|
|
1
|
+
export { type RunnableFunc, type RunnableLike, type RunnableBatchOptions, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableInterface, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
|
|
2
|
+
export { type RunnableConfig, getCallbackManagerForConfig } from "./config.js";
|
|
3
|
+
export { RunnablePassthrough } from "./passthrough.js";
|
|
4
4
|
export { type RouterInput, RouterRunnable } from "./router.js";
|
|
5
5
|
export { RunnableBranch, type Branch, type BranchLike } from "./branch.js";
|
|
6
6
|
export { type RunnableWithMessageHistoryInputs, RunnableWithMessageHistory, } from "./history.js";
|
package/dist/runnables/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, _coerceToRunnable, } from "./base.js";
|
|
2
|
-
export {
|
|
1
|
+
export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
|
|
2
|
+
export { getCallbackManagerForConfig } from "./config.js";
|
|
3
|
+
export { RunnablePassthrough } from "./passthrough.js";
|
|
3
4
|
export { RouterRunnable } from "./router.js";
|
|
4
5
|
export { RunnableBranch } from "./branch.js";
|
|
5
6
|
export { RunnableWithMessageHistory, } from "./history.js";
|
|
@@ -1,36 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunnablePassthrough =
|
|
3
|
+
exports.RunnablePassthrough = void 0;
|
|
4
4
|
const base_js_1 = require("./base.cjs");
|
|
5
|
-
/**
|
|
6
|
-
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
7
|
-
*/
|
|
8
|
-
class RunnableAssign extends base_js_1.Runnable {
|
|
9
|
-
constructor(mapper) {
|
|
10
|
-
super();
|
|
11
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
configurable: true,
|
|
14
|
-
writable: true,
|
|
15
|
-
value: ["langchain_core", "runnables"]
|
|
16
|
-
});
|
|
17
|
-
Object.defineProperty(this, "mapper", {
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true,
|
|
21
|
-
value: void 0
|
|
22
|
-
});
|
|
23
|
-
this.mapper = mapper;
|
|
24
|
-
}
|
|
25
|
-
async invoke(input, options) {
|
|
26
|
-
const mapperResult = await this.mapper.invoke(input, options);
|
|
27
|
-
return {
|
|
28
|
-
...input,
|
|
29
|
-
...mapperResult,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
exports.RunnableAssign = RunnableAssign;
|
|
34
5
|
/**
|
|
35
6
|
* A runnable to passthrough inputs unchanged or with additional keys.
|
|
36
7
|
*
|
|
@@ -79,6 +50,9 @@ class RunnablePassthrough extends base_js_1.Runnable {
|
|
|
79
50
|
async invoke(input, options) {
|
|
80
51
|
return this._callWithConfig((input) => Promise.resolve(input), input, options);
|
|
81
52
|
}
|
|
53
|
+
transform(generator, options) {
|
|
54
|
+
return this._transformStreamWithConfig(generator, (input) => input, options);
|
|
55
|
+
}
|
|
82
56
|
/**
|
|
83
57
|
* A runnable that assigns key-value pairs to the input.
|
|
84
58
|
*
|
|
@@ -110,7 +84,7 @@ class RunnablePassthrough extends base_js_1.Runnable {
|
|
|
110
84
|
static assign(
|
|
111
85
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
86
|
mapping) {
|
|
113
|
-
return new RunnableAssign(new base_js_1.RunnableMap({ steps: mapping }));
|
|
87
|
+
return new base_js_1.RunnableAssign(new base_js_1.RunnableMap({ steps: mapping }));
|
|
114
88
|
}
|
|
115
89
|
}
|
|
116
90
|
exports.RunnablePassthrough = RunnablePassthrough;
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
import { Runnable,
|
|
1
|
+
import { Runnable, RunnableAssign, RunnableMapLike } from "./base.js";
|
|
2
2
|
import type { RunnableConfig } from "./config.js";
|
|
3
|
-
/**
|
|
4
|
-
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
5
|
-
*/
|
|
6
|
-
export declare class RunnableAssign<RunInput extends Record<string, any> = any, RunOutput extends Record<string, any> = any, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput> {
|
|
7
|
-
lc_namespace: string[];
|
|
8
|
-
mapper: RunnableMap<RunInput>;
|
|
9
|
-
constructor(mapper: RunnableMap<RunInput>);
|
|
10
|
-
invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
|
|
11
|
-
}
|
|
12
3
|
/**
|
|
13
4
|
* A runnable to passthrough inputs unchanged or with additional keys.
|
|
14
5
|
*
|
|
@@ -40,6 +31,7 @@ export declare class RunnablePassthrough<RunInput> extends Runnable<RunInput, Ru
|
|
|
40
31
|
lc_namespace: string[];
|
|
41
32
|
lc_serializable: boolean;
|
|
42
33
|
invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunInput>;
|
|
34
|
+
transform(generator: AsyncGenerator<RunInput>, options: Partial<RunnableConfig>): AsyncGenerator<RunInput>;
|
|
43
35
|
/**
|
|
44
36
|
* A runnable that assigns key-value pairs to the input.
|
|
45
37
|
*
|
|
@@ -68,5 +60,5 @@ export declare class RunnablePassthrough<RunInput> extends Runnable<RunInput, Ru
|
|
|
68
60
|
* });
|
|
69
61
|
* ```
|
|
70
62
|
*/
|
|
71
|
-
static assign(mapping: Record<string,
|
|
63
|
+
static assign(mapping: RunnableMapLike<Record<string, unknown>, Record<string, unknown>>): RunnableAssign<Record<string, unknown>, Record<string, unknown>>;
|
|
72
64
|
}
|
|
@@ -1,32 +1,4 @@
|
|
|
1
|
-
import { Runnable, RunnableMap } from "./base.js";
|
|
2
|
-
/**
|
|
3
|
-
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
4
|
-
*/
|
|
5
|
-
export class RunnableAssign extends Runnable {
|
|
6
|
-
constructor(mapper) {
|
|
7
|
-
super();
|
|
8
|
-
Object.defineProperty(this, "lc_namespace", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
configurable: true,
|
|
11
|
-
writable: true,
|
|
12
|
-
value: ["langchain_core", "runnables"]
|
|
13
|
-
});
|
|
14
|
-
Object.defineProperty(this, "mapper", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
configurable: true,
|
|
17
|
-
writable: true,
|
|
18
|
-
value: void 0
|
|
19
|
-
});
|
|
20
|
-
this.mapper = mapper;
|
|
21
|
-
}
|
|
22
|
-
async invoke(input, options) {
|
|
23
|
-
const mapperResult = await this.mapper.invoke(input, options);
|
|
24
|
-
return {
|
|
25
|
-
...input,
|
|
26
|
-
...mapperResult,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
}
|
|
1
|
+
import { Runnable, RunnableAssign, RunnableMap, } from "./base.js";
|
|
30
2
|
/**
|
|
31
3
|
* A runnable to passthrough inputs unchanged or with additional keys.
|
|
32
4
|
*
|
|
@@ -75,6 +47,9 @@ export class RunnablePassthrough extends Runnable {
|
|
|
75
47
|
async invoke(input, options) {
|
|
76
48
|
return this._callWithConfig((input) => Promise.resolve(input), input, options);
|
|
77
49
|
}
|
|
50
|
+
transform(generator, options) {
|
|
51
|
+
return this._transformStreamWithConfig(generator, (input) => input, options);
|
|
52
|
+
}
|
|
78
53
|
/**
|
|
79
54
|
* A runnable that assigns key-value pairs to the input.
|
|
80
55
|
*
|
package/dist/utils/stream.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IterableReadableStream = void 0;
|
|
3
|
+
exports.AsyncGeneratorWithSetup = exports.concat = exports.atee = exports.IterableReadableStream = void 0;
|
|
4
4
|
/*
|
|
5
5
|
* Support async iterator syntax for ReadableStreams in all environments.
|
|
6
6
|
* Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
|
|
@@ -98,3 +98,115 @@ class IterableReadableStream extends ReadableStream {
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
exports.IterableReadableStream = IterableReadableStream;
|
|
101
|
+
function atee(iter, length = 2) {
|
|
102
|
+
const buffers = Array.from({ length }, () => []);
|
|
103
|
+
return buffers.map(async function* makeIter(buffer) {
|
|
104
|
+
while (true) {
|
|
105
|
+
if (buffer.length === 0) {
|
|
106
|
+
const result = await iter.next();
|
|
107
|
+
for (const buffer of buffers) {
|
|
108
|
+
buffer.push(result);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (buffer[0].done) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
116
|
+
yield buffer.shift().value;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
exports.atee = atee;
|
|
122
|
+
function concat(first, second) {
|
|
123
|
+
if (Array.isArray(first) && Array.isArray(second)) {
|
|
124
|
+
return first.concat(second);
|
|
125
|
+
}
|
|
126
|
+
else if (typeof first === "string" && typeof second === "string") {
|
|
127
|
+
return (first + second);
|
|
128
|
+
}
|
|
129
|
+
else if (typeof first === "number" && typeof second === "number") {
|
|
130
|
+
return (first + second);
|
|
131
|
+
}
|
|
132
|
+
else if (
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
134
|
+
"concat" in first &&
|
|
135
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
136
|
+
typeof first.concat === "function") {
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
138
|
+
return first.concat(second);
|
|
139
|
+
}
|
|
140
|
+
else if (typeof first === "object" && typeof second === "object") {
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
142
|
+
const chunk = { ...first };
|
|
143
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
144
|
+
for (const [key, value] of Object.entries(second)) {
|
|
145
|
+
if (key in chunk) {
|
|
146
|
+
chunk[key] = concat(chunk[key], value);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
chunk[key] = value;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return chunk;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
throw new Error(`Cannot concat ${typeof first} and ${typeof second}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.concat = concat;
|
|
159
|
+
class AsyncGeneratorWithSetup {
|
|
160
|
+
constructor(generator, startSetup) {
|
|
161
|
+
Object.defineProperty(this, "generator", {
|
|
162
|
+
enumerable: true,
|
|
163
|
+
configurable: true,
|
|
164
|
+
writable: true,
|
|
165
|
+
value: void 0
|
|
166
|
+
});
|
|
167
|
+
Object.defineProperty(this, "setup", {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
configurable: true,
|
|
170
|
+
writable: true,
|
|
171
|
+
value: void 0
|
|
172
|
+
});
|
|
173
|
+
Object.defineProperty(this, "firstResult", {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
configurable: true,
|
|
176
|
+
writable: true,
|
|
177
|
+
value: void 0
|
|
178
|
+
});
|
|
179
|
+
Object.defineProperty(this, "firstResultUsed", {
|
|
180
|
+
enumerable: true,
|
|
181
|
+
configurable: true,
|
|
182
|
+
writable: true,
|
|
183
|
+
value: false
|
|
184
|
+
});
|
|
185
|
+
this.generator = generator;
|
|
186
|
+
// setup is a promise that resolves only after the first iterator value
|
|
187
|
+
// is available. this is useful when setup of several piped generators
|
|
188
|
+
// needs to happen in logical order, ie. in the order in which input to
|
|
189
|
+
// to each generator is available.
|
|
190
|
+
this.setup = new Promise((resolve, reject) => {
|
|
191
|
+
this.firstResult = generator.next();
|
|
192
|
+
this.firstResult.then(startSetup).then(resolve, reject);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
async next(...args) {
|
|
196
|
+
if (!this.firstResultUsed) {
|
|
197
|
+
this.firstResultUsed = true;
|
|
198
|
+
return this.firstResult;
|
|
199
|
+
}
|
|
200
|
+
return this.generator.next(...args);
|
|
201
|
+
}
|
|
202
|
+
async return(value) {
|
|
203
|
+
return this.generator.return(value);
|
|
204
|
+
}
|
|
205
|
+
async throw(e) {
|
|
206
|
+
return this.generator.throw(e);
|
|
207
|
+
}
|
|
208
|
+
[Symbol.asyncIterator]() {
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -16,3 +16,16 @@ export declare class IterableReadableStream<T> extends ReadableStream<T> impleme
|
|
|
16
16
|
static fromReadableStream<T>(stream: ReadableStream<T>): IterableReadableStream<T>;
|
|
17
17
|
static fromAsyncGenerator<T>(generator: AsyncGenerator<T>): IterableReadableStream<T>;
|
|
18
18
|
}
|
|
19
|
+
export declare function atee<T>(iter: AsyncGenerator<T>, length?: number): AsyncGenerator<T>[];
|
|
20
|
+
export declare function concat<T extends Array<any> | string | number | Record<string, any> | any>(first: T, second: T): T;
|
|
21
|
+
export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn = unknown, TNext = unknown> implements AsyncGenerator<T, TReturn, TNext> {
|
|
22
|
+
private generator;
|
|
23
|
+
setup: Promise<S>;
|
|
24
|
+
private firstResult;
|
|
25
|
+
private firstResultUsed;
|
|
26
|
+
constructor(generator: AsyncGenerator<T>, startSetup: () => Promise<S>);
|
|
27
|
+
next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
|
|
28
|
+
return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
|
|
29
|
+
throw(e: Error): Promise<IteratorResult<T>>;
|
|
30
|
+
[Symbol.asyncIterator](): this;
|
|
31
|
+
}
|
package/dist/utils/stream.js
CHANGED
|
@@ -94,3 +94,112 @@ export class IterableReadableStream extends ReadableStream {
|
|
|
94
94
|
});
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
+
export function atee(iter, length = 2) {
|
|
98
|
+
const buffers = Array.from({ length }, () => []);
|
|
99
|
+
return buffers.map(async function* makeIter(buffer) {
|
|
100
|
+
while (true) {
|
|
101
|
+
if (buffer.length === 0) {
|
|
102
|
+
const result = await iter.next();
|
|
103
|
+
for (const buffer of buffers) {
|
|
104
|
+
buffer.push(result);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (buffer[0].done) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
112
|
+
yield buffer.shift().value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
export function concat(first, second) {
|
|
118
|
+
if (Array.isArray(first) && Array.isArray(second)) {
|
|
119
|
+
return first.concat(second);
|
|
120
|
+
}
|
|
121
|
+
else if (typeof first === "string" && typeof second === "string") {
|
|
122
|
+
return (first + second);
|
|
123
|
+
}
|
|
124
|
+
else if (typeof first === "number" && typeof second === "number") {
|
|
125
|
+
return (first + second);
|
|
126
|
+
}
|
|
127
|
+
else if (
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
129
|
+
"concat" in first &&
|
|
130
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
131
|
+
typeof first.concat === "function") {
|
|
132
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
133
|
+
return first.concat(second);
|
|
134
|
+
}
|
|
135
|
+
else if (typeof first === "object" && typeof second === "object") {
|
|
136
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
137
|
+
const chunk = { ...first };
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
139
|
+
for (const [key, value] of Object.entries(second)) {
|
|
140
|
+
if (key in chunk) {
|
|
141
|
+
chunk[key] = concat(chunk[key], value);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
chunk[key] = value;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return chunk;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
throw new Error(`Cannot concat ${typeof first} and ${typeof second}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export class AsyncGeneratorWithSetup {
|
|
154
|
+
constructor(generator, startSetup) {
|
|
155
|
+
Object.defineProperty(this, "generator", {
|
|
156
|
+
enumerable: true,
|
|
157
|
+
configurable: true,
|
|
158
|
+
writable: true,
|
|
159
|
+
value: void 0
|
|
160
|
+
});
|
|
161
|
+
Object.defineProperty(this, "setup", {
|
|
162
|
+
enumerable: true,
|
|
163
|
+
configurable: true,
|
|
164
|
+
writable: true,
|
|
165
|
+
value: void 0
|
|
166
|
+
});
|
|
167
|
+
Object.defineProperty(this, "firstResult", {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
configurable: true,
|
|
170
|
+
writable: true,
|
|
171
|
+
value: void 0
|
|
172
|
+
});
|
|
173
|
+
Object.defineProperty(this, "firstResultUsed", {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
configurable: true,
|
|
176
|
+
writable: true,
|
|
177
|
+
value: false
|
|
178
|
+
});
|
|
179
|
+
this.generator = generator;
|
|
180
|
+
// setup is a promise that resolves only after the first iterator value
|
|
181
|
+
// is available. this is useful when setup of several piped generators
|
|
182
|
+
// needs to happen in logical order, ie. in the order in which input to
|
|
183
|
+
// to each generator is available.
|
|
184
|
+
this.setup = new Promise((resolve, reject) => {
|
|
185
|
+
this.firstResult = generator.next();
|
|
186
|
+
this.firstResult.then(startSetup).then(resolve, reject);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async next(...args) {
|
|
190
|
+
if (!this.firstResultUsed) {
|
|
191
|
+
this.firstResultUsed = true;
|
|
192
|
+
return this.firstResult;
|
|
193
|
+
}
|
|
194
|
+
return this.generator.next(...args);
|
|
195
|
+
}
|
|
196
|
+
async return(value) {
|
|
197
|
+
return this.generator.return(value);
|
|
198
|
+
}
|
|
199
|
+
async throw(e) {
|
|
200
|
+
return this.generator.throw(e);
|
|
201
|
+
}
|
|
202
|
+
[Symbol.asyncIterator]() {
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -91,6 +91,16 @@ class FakeLLM extends llms_js_1.LLM {
|
|
|
91
91
|
}
|
|
92
92
|
exports.FakeLLM = FakeLLM;
|
|
93
93
|
class FakeStreamingLLM extends llms_js_1.LLM {
|
|
94
|
+
constructor(fields) {
|
|
95
|
+
super(fields);
|
|
96
|
+
Object.defineProperty(this, "sleep", {
|
|
97
|
+
enumerable: true,
|
|
98
|
+
configurable: true,
|
|
99
|
+
writable: true,
|
|
100
|
+
value: 50
|
|
101
|
+
});
|
|
102
|
+
this.sleep = fields.sleep ?? this.sleep;
|
|
103
|
+
}
|
|
94
104
|
_llmType() {
|
|
95
105
|
return "fake";
|
|
96
106
|
}
|
|
@@ -99,7 +109,7 @@ class FakeStreamingLLM extends llms_js_1.LLM {
|
|
|
99
109
|
}
|
|
100
110
|
async *_streamResponseChunks(input) {
|
|
101
111
|
for (const c of input) {
|
|
102
|
-
await new Promise((resolve) => setTimeout(resolve,
|
|
112
|
+
await new Promise((resolve) => setTimeout(resolve, this.sleep));
|
|
103
113
|
yield { text: c, generationInfo: {} };
|
|
104
114
|
}
|
|
105
115
|
}
|
|
@@ -328,5 +338,8 @@ class FakeListChatMessageHistory extends chat_history_js_1.BaseListChatMessageHi
|
|
|
328
338
|
async addMessage(message) {
|
|
329
339
|
this.messages.push(message);
|
|
330
340
|
}
|
|
341
|
+
async getMessages() {
|
|
342
|
+
return this.messages;
|
|
343
|
+
}
|
|
331
344
|
}
|
|
332
345
|
exports.FakeListChatMessageHistory = FakeListChatMessageHistory;
|
|
@@ -36,6 +36,10 @@ export declare class FakeLLM extends LLM {
|
|
|
36
36
|
_call(prompt: string, _options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
37
37
|
}
|
|
38
38
|
export declare class FakeStreamingLLM extends LLM {
|
|
39
|
+
sleep?: number;
|
|
40
|
+
constructor(fields: {
|
|
41
|
+
sleep?: number;
|
|
42
|
+
} & BaseLLMParams);
|
|
39
43
|
_llmType(): string;
|
|
40
44
|
_call(prompt: string): Promise<string>;
|
|
41
45
|
_streamResponseChunks(input: string): AsyncGenerator<GenerationChunk, void, unknown>;
|
|
@@ -117,4 +121,5 @@ export declare class FakeListChatMessageHistory extends BaseListChatMessageHisto
|
|
|
117
121
|
messages: Array<BaseMessage>;
|
|
118
122
|
constructor();
|
|
119
123
|
addMessage(message: BaseMessage): Promise<void>;
|
|
124
|
+
getMessages(): Promise<BaseMessage[]>;
|
|
120
125
|
}
|
|
@@ -85,6 +85,16 @@ export class FakeLLM extends LLM {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
export class FakeStreamingLLM extends LLM {
|
|
88
|
+
constructor(fields) {
|
|
89
|
+
super(fields);
|
|
90
|
+
Object.defineProperty(this, "sleep", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: true,
|
|
93
|
+
writable: true,
|
|
94
|
+
value: 50
|
|
95
|
+
});
|
|
96
|
+
this.sleep = fields.sleep ?? this.sleep;
|
|
97
|
+
}
|
|
88
98
|
_llmType() {
|
|
89
99
|
return "fake";
|
|
90
100
|
}
|
|
@@ -93,7 +103,7 @@ export class FakeStreamingLLM extends LLM {
|
|
|
93
103
|
}
|
|
94
104
|
async *_streamResponseChunks(input) {
|
|
95
105
|
for (const c of input) {
|
|
96
|
-
await new Promise((resolve) => setTimeout(resolve,
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, this.sleep));
|
|
97
107
|
yield { text: c, generationInfo: {} };
|
|
98
108
|
}
|
|
99
109
|
}
|
|
@@ -317,4 +327,7 @@ export class FakeListChatMessageHistory extends BaseListChatMessageHistory {
|
|
|
317
327
|
async addMessage(message) {
|
|
318
328
|
this.messages.push(message);
|
|
319
329
|
}
|
|
330
|
+
async getMessages() {
|
|
331
|
+
return this.messages;
|
|
332
|
+
}
|
|
320
333
|
}
|