@langchain/core 0.1.15 → 0.1.17-rc.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/manager.cjs +2 -1
- package/dist/callbacks/manager.d.ts +2 -1
- package/dist/callbacks/manager.js +1 -1
- package/dist/retrievers.cjs +2 -1
- package/dist/retrievers.js +2 -1
- package/dist/runnables/base.cjs +42 -29
- package/dist/runnables/base.d.ts +3 -3
- package/dist/runnables/base.js +44 -31
- package/dist/runnables/config.cjs +27 -13
- package/dist/runnables/config.d.ts +2 -2
- package/dist/runnables/config.js +28 -14
- package/dist/tools.cjs +102 -1
- package/dist/tools.d.ts +49 -0
- package/dist/tools.js +99 -0
- 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 +105 -1
- package/dist/utils/testing/index.d.ts +55 -0
- package/dist/utils/testing/index.js +102 -0
- package/package.json +5 -5
package/dist/runnables/config.js
CHANGED
|
@@ -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(
|
|
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 =
|
|
11
|
-
|
|
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]
|
|
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
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
}
|
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) {
|
|
@@ -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.
|
|
@@ -105,18 +106,31 @@ class FakeStreamingLLM extends llms_js_1.LLM {
|
|
|
105
106
|
writable: true,
|
|
106
107
|
value: void 0
|
|
107
108
|
});
|
|
109
|
+
Object.defineProperty(this, "thrownErrorString", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: void 0
|
|
114
|
+
});
|
|
108
115
|
this.sleep = fields.sleep ?? this.sleep;
|
|
109
116
|
this.responses = fields.responses;
|
|
117
|
+
this.thrownErrorString = fields.thrownErrorString;
|
|
110
118
|
}
|
|
111
119
|
_llmType() {
|
|
112
120
|
return "fake";
|
|
113
121
|
}
|
|
114
122
|
async _call(prompt) {
|
|
123
|
+
if (this.thrownErrorString) {
|
|
124
|
+
throw new Error(this.thrownErrorString);
|
|
125
|
+
}
|
|
115
126
|
const response = this.responses?.[0];
|
|
116
127
|
this.responses = this.responses?.slice(1);
|
|
117
128
|
return response ?? prompt;
|
|
118
129
|
}
|
|
119
130
|
async *_streamResponseChunks(input) {
|
|
131
|
+
if (this.thrownErrorString) {
|
|
132
|
+
throw new Error(this.thrownErrorString);
|
|
133
|
+
}
|
|
120
134
|
const response = this.responses?.[0];
|
|
121
135
|
this.responses = this.responses?.slice(1);
|
|
122
136
|
for (const c of response ?? input) {
|
|
@@ -406,3 +420,93 @@ class FakeTool extends tools_js_1.StructuredTool {
|
|
|
406
420
|
}
|
|
407
421
|
}
|
|
408
422
|
exports.FakeTool = FakeTool;
|
|
423
|
+
/**
|
|
424
|
+
* A class that provides fake embeddings by overriding the embedDocuments
|
|
425
|
+
* and embedQuery methods to return fixed values.
|
|
426
|
+
*/
|
|
427
|
+
class FakeEmbeddings extends embeddings_js_1.Embeddings {
|
|
428
|
+
constructor(params) {
|
|
429
|
+
super(params ?? {});
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Generates fixed embeddings for a list of documents.
|
|
433
|
+
* @param documents List of documents to generate embeddings for.
|
|
434
|
+
* @returns A promise that resolves with a list of fixed embeddings for each document.
|
|
435
|
+
*/
|
|
436
|
+
embedDocuments(documents) {
|
|
437
|
+
return Promise.resolve(documents.map(() => [0.1, 0.2, 0.3, 0.4]));
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Generates a fixed embedding for a query.
|
|
441
|
+
* @param _ The query to generate an embedding for.
|
|
442
|
+
* @returns A promise that resolves with a fixed embedding for the query.
|
|
443
|
+
*/
|
|
444
|
+
embedQuery(_) {
|
|
445
|
+
return Promise.resolve([0.1, 0.2, 0.3, 0.4]);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
exports.FakeEmbeddings = FakeEmbeddings;
|
|
449
|
+
/**
|
|
450
|
+
* A class that provides synthetic embeddings by overriding the
|
|
451
|
+
* embedDocuments and embedQuery methods to generate embeddings based on
|
|
452
|
+
* the input documents. The embeddings are generated by converting each
|
|
453
|
+
* document into chunks, calculating a numerical value for each chunk, and
|
|
454
|
+
* returning an array of these values as the embedding.
|
|
455
|
+
*/
|
|
456
|
+
class SyntheticEmbeddings extends embeddings_js_1.Embeddings {
|
|
457
|
+
constructor(params) {
|
|
458
|
+
super(params ?? {});
|
|
459
|
+
Object.defineProperty(this, "vectorSize", {
|
|
460
|
+
enumerable: true,
|
|
461
|
+
configurable: true,
|
|
462
|
+
writable: true,
|
|
463
|
+
value: void 0
|
|
464
|
+
});
|
|
465
|
+
this.vectorSize = params?.vectorSize ?? 4;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Generates synthetic embeddings for a list of documents.
|
|
469
|
+
* @param documents List of documents to generate embeddings for.
|
|
470
|
+
* @returns A promise that resolves with a list of synthetic embeddings for each document.
|
|
471
|
+
*/
|
|
472
|
+
async embedDocuments(documents) {
|
|
473
|
+
return Promise.all(documents.map((doc) => this.embedQuery(doc)));
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Generates a synthetic embedding for a document. The document is
|
|
477
|
+
* converted into chunks, a numerical value is calculated for each chunk,
|
|
478
|
+
* and an array of these values is returned as the embedding.
|
|
479
|
+
* @param document The document to generate an embedding for.
|
|
480
|
+
* @returns A promise that resolves with a synthetic embedding for the document.
|
|
481
|
+
*/
|
|
482
|
+
async embedQuery(document) {
|
|
483
|
+
let doc = document;
|
|
484
|
+
// Only use the letters (and space) from the document, and make them lower case
|
|
485
|
+
doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, "");
|
|
486
|
+
// Pad the document to make sure it has a divisible number of chunks
|
|
487
|
+
const padMod = doc.length % this.vectorSize;
|
|
488
|
+
const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;
|
|
489
|
+
const padSize = doc.length + padGapSize;
|
|
490
|
+
doc = doc.padEnd(padSize, " ");
|
|
491
|
+
// Break it into chunks
|
|
492
|
+
const chunkSize = doc.length / this.vectorSize;
|
|
493
|
+
const docChunk = [];
|
|
494
|
+
for (let co = 0; co < doc.length; co += chunkSize) {
|
|
495
|
+
docChunk.push(doc.slice(co, co + chunkSize));
|
|
496
|
+
}
|
|
497
|
+
// Turn each chunk into a number
|
|
498
|
+
const ret = docChunk.map((s) => {
|
|
499
|
+
let sum = 0;
|
|
500
|
+
// Get a total value by adding the value of each character in the string
|
|
501
|
+
for (let co = 0; co < s.length; co += 1) {
|
|
502
|
+
sum += s === " " ? 0 : s.charCodeAt(co);
|
|
503
|
+
}
|
|
504
|
+
// Reduce this to a number between 0 and 25 inclusive
|
|
505
|
+
// Then get the fractional number by dividing it by 26
|
|
506
|
+
const ret = (sum % 26) / 26;
|
|
507
|
+
return ret;
|
|
508
|
+
});
|
|
509
|
+
return ret;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
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.
|
|
@@ -41,9 +42,11 @@ export declare class FakeLLM extends LLM {
|
|
|
41
42
|
export declare class FakeStreamingLLM extends LLM {
|
|
42
43
|
sleep?: number;
|
|
43
44
|
responses?: string[];
|
|
45
|
+
thrownErrorString?: string;
|
|
44
46
|
constructor(fields: {
|
|
45
47
|
sleep?: number;
|
|
46
48
|
responses?: string[];
|
|
49
|
+
thrownErrorString?: string;
|
|
47
50
|
} & BaseLLMParams);
|
|
48
51
|
_llmType(): string;
|
|
49
52
|
_call(prompt: string): Promise<string>;
|
|
@@ -146,3 +149,55 @@ export declare class FakeTool<T extends z.ZodObject<any, any, any, any> = z.ZodO
|
|
|
146
149
|
constructor(fields: FakeToolParams<T>);
|
|
147
150
|
protected _call(arg: z.output<T>, _runManager?: CallbackManagerForToolRun): Promise<string>;
|
|
148
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* A class that provides fake embeddings by overriding the embedDocuments
|
|
154
|
+
* and embedQuery methods to return fixed values.
|
|
155
|
+
*/
|
|
156
|
+
export declare class FakeEmbeddings extends Embeddings {
|
|
157
|
+
constructor(params?: EmbeddingsParams);
|
|
158
|
+
/**
|
|
159
|
+
* Generates fixed embeddings for a list of documents.
|
|
160
|
+
* @param documents List of documents to generate embeddings for.
|
|
161
|
+
* @returns A promise that resolves with a list of fixed embeddings for each document.
|
|
162
|
+
*/
|
|
163
|
+
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
164
|
+
/**
|
|
165
|
+
* Generates a fixed embedding for a query.
|
|
166
|
+
* @param _ The query to generate an embedding for.
|
|
167
|
+
* @returns A promise that resolves with a fixed embedding for the query.
|
|
168
|
+
*/
|
|
169
|
+
embedQuery(_: string): Promise<number[]>;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* An interface that defines additional parameters specific to the
|
|
173
|
+
* SyntheticEmbeddings class.
|
|
174
|
+
*/
|
|
175
|
+
interface SyntheticEmbeddingsParams extends EmbeddingsParams {
|
|
176
|
+
vectorSize: number;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* A class that provides synthetic embeddings by overriding the
|
|
180
|
+
* embedDocuments and embedQuery methods to generate embeddings based on
|
|
181
|
+
* the input documents. The embeddings are generated by converting each
|
|
182
|
+
* document into chunks, calculating a numerical value for each chunk, and
|
|
183
|
+
* returning an array of these values as the embedding.
|
|
184
|
+
*/
|
|
185
|
+
export declare class SyntheticEmbeddings extends Embeddings implements SyntheticEmbeddingsParams {
|
|
186
|
+
vectorSize: number;
|
|
187
|
+
constructor(params?: SyntheticEmbeddingsParams);
|
|
188
|
+
/**
|
|
189
|
+
* Generates synthetic embeddings for a list of documents.
|
|
190
|
+
* @param documents List of documents to generate embeddings for.
|
|
191
|
+
* @returns A promise that resolves with a list of synthetic embeddings for each document.
|
|
192
|
+
*/
|
|
193
|
+
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
194
|
+
/**
|
|
195
|
+
* Generates a synthetic embedding for a document. The document is
|
|
196
|
+
* converted into chunks, a numerical value is calculated for each chunk,
|
|
197
|
+
* and an array of these values is returned as the embedding.
|
|
198
|
+
* @param document The document to generate an embedding for.
|
|
199
|
+
* @returns A promise that resolves with a synthetic embedding for the document.
|
|
200
|
+
*/
|
|
201
|
+
embedQuery(document: string): Promise<number[]>;
|
|
202
|
+
}
|
|
203
|
+
export {};
|