@langchain/core 0.1.59 → 0.1.61
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/chat_history.cjs +49 -1
- package/dist/chat_history.d.ts +27 -0
- package/dist/chat_history.js +47 -0
- package/dist/language_models/base.cjs +3 -1
- package/dist/language_models/base.d.ts +3 -1
- package/dist/language_models/base.js +3 -1
- package/dist/retrievers/index.cjs +2 -0
- package/dist/retrievers/index.d.ts +2 -0
- package/dist/retrievers/index.js +2 -0
- package/dist/runnables/index.cjs +2 -1
- package/dist/runnables/index.d.ts +1 -1
- package/dist/runnables/index.js +1 -1
- package/dist/stores.cjs +81 -1
- package/dist/stores.d.ts +48 -0
- package/dist/stores.js +79 -0
- package/dist/structured_query/base.cjs +139 -0
- package/dist/structured_query/base.d.ts +69 -0
- package/dist/structured_query/base.js +134 -0
- package/dist/structured_query/functional.cjs +198 -0
- package/dist/structured_query/functional.d.ts +87 -0
- package/dist/structured_query/functional.js +194 -0
- package/dist/structured_query/index.cjs +20 -0
- package/dist/structured_query/index.d.ts +4 -0
- package/dist/structured_query/index.js +4 -0
- package/dist/structured_query/ir.cjs +141 -0
- package/dist/structured_query/ir.d.ts +138 -0
- package/dist/structured_query/ir.js +132 -0
- package/dist/structured_query/utils.cjs +94 -0
- package/dist/structured_query/utils.d.ts +29 -0
- package/dist/structured_query/utils.js +85 -0
- package/dist/tools.cjs +22 -1
- package/dist/tools.d.ts +23 -0
- package/dist/tools.js +20 -0
- package/package.json +14 -1
- package/structured_query.cjs +1 -0
- package/structured_query.d.cts +1 -0
- package/structured_query.d.ts +1 -0
- package/structured_query.js +1 -0
package/dist/chat_history.cjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = void 0;
|
|
3
|
+
exports.InMemoryChatMessageHistory = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = void 0;
|
|
4
4
|
const serializable_js_1 = require("./load/serializable.cjs");
|
|
5
5
|
const index_js_1 = require("./messages/index.cjs");
|
|
6
|
+
// TODO: Combine into one class for 0.2
|
|
6
7
|
/**
|
|
7
8
|
* Base class for all chat message histories. All chat message histories
|
|
8
9
|
* should extend this class.
|
|
@@ -60,3 +61,50 @@ class BaseListChatMessageHistory extends serializable_js_1.Serializable {
|
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
exports.BaseListChatMessageHistory = BaseListChatMessageHistory;
|
|
64
|
+
/**
|
|
65
|
+
* Class for storing chat message history in-memory. It extends the
|
|
66
|
+
* BaseListChatMessageHistory class and provides methods to get, add, and
|
|
67
|
+
* clear messages.
|
|
68
|
+
*/
|
|
69
|
+
class InMemoryChatMessageHistory extends BaseListChatMessageHistory {
|
|
70
|
+
constructor(messages) {
|
|
71
|
+
super(...arguments);
|
|
72
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
configurable: true,
|
|
75
|
+
writable: true,
|
|
76
|
+
value: ["langchain", "stores", "message", "in_memory"]
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(this, "messages", {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
configurable: true,
|
|
81
|
+
writable: true,
|
|
82
|
+
value: []
|
|
83
|
+
});
|
|
84
|
+
this.messages = messages ?? [];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Method to get all the messages stored in the ChatMessageHistory
|
|
88
|
+
* instance.
|
|
89
|
+
* @returns Array of stored BaseMessage instances.
|
|
90
|
+
*/
|
|
91
|
+
async getMessages() {
|
|
92
|
+
return this.messages;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Method to add a new message to the ChatMessageHistory instance.
|
|
96
|
+
* @param message The BaseMessage instance to add.
|
|
97
|
+
* @returns A promise that resolves when the message has been added.
|
|
98
|
+
*/
|
|
99
|
+
async addMessage(message) {
|
|
100
|
+
this.messages.push(message);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Method to clear all the messages from the ChatMessageHistory instance.
|
|
104
|
+
* @returns A promise that resolves when all messages have been cleared.
|
|
105
|
+
*/
|
|
106
|
+
async clear() {
|
|
107
|
+
this.messages = [];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.InMemoryChatMessageHistory = InMemoryChatMessageHistory;
|
package/dist/chat_history.d.ts
CHANGED
|
@@ -54,3 +54,30 @@ export declare abstract class BaseListChatMessageHistory extends Serializable {
|
|
|
54
54
|
*/
|
|
55
55
|
clear(): Promise<void>;
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Class for storing chat message history in-memory. It extends the
|
|
59
|
+
* BaseListChatMessageHistory class and provides methods to get, add, and
|
|
60
|
+
* clear messages.
|
|
61
|
+
*/
|
|
62
|
+
export declare class InMemoryChatMessageHistory extends BaseListChatMessageHistory {
|
|
63
|
+
lc_namespace: string[];
|
|
64
|
+
private messages;
|
|
65
|
+
constructor(messages?: BaseMessage[]);
|
|
66
|
+
/**
|
|
67
|
+
* Method to get all the messages stored in the ChatMessageHistory
|
|
68
|
+
* instance.
|
|
69
|
+
* @returns Array of stored BaseMessage instances.
|
|
70
|
+
*/
|
|
71
|
+
getMessages(): Promise<BaseMessage[]>;
|
|
72
|
+
/**
|
|
73
|
+
* Method to add a new message to the ChatMessageHistory instance.
|
|
74
|
+
* @param message The BaseMessage instance to add.
|
|
75
|
+
* @returns A promise that resolves when the message has been added.
|
|
76
|
+
*/
|
|
77
|
+
addMessage(message: BaseMessage): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Method to clear all the messages from the ChatMessageHistory instance.
|
|
80
|
+
* @returns A promise that resolves when all messages have been cleared.
|
|
81
|
+
*/
|
|
82
|
+
clear(): Promise<void>;
|
|
83
|
+
}
|
package/dist/chat_history.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Serializable } from "./load/serializable.js";
|
|
2
2
|
import { HumanMessage, AIMessage } from "./messages/index.js";
|
|
3
|
+
// TODO: Combine into one class for 0.2
|
|
3
4
|
/**
|
|
4
5
|
* Base class for all chat message histories. All chat message histories
|
|
5
6
|
* should extend this class.
|
|
@@ -55,3 +56,49 @@ export class BaseListChatMessageHistory extends Serializable {
|
|
|
55
56
|
throw new Error("Not implemented.");
|
|
56
57
|
}
|
|
57
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Class for storing chat message history in-memory. It extends the
|
|
61
|
+
* BaseListChatMessageHistory class and provides methods to get, add, and
|
|
62
|
+
* clear messages.
|
|
63
|
+
*/
|
|
64
|
+
export class InMemoryChatMessageHistory extends BaseListChatMessageHistory {
|
|
65
|
+
constructor(messages) {
|
|
66
|
+
super(...arguments);
|
|
67
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: ["langchain", "stores", "message", "in_memory"]
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "messages", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: []
|
|
78
|
+
});
|
|
79
|
+
this.messages = messages ?? [];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Method to get all the messages stored in the ChatMessageHistory
|
|
83
|
+
* instance.
|
|
84
|
+
* @returns Array of stored BaseMessage instances.
|
|
85
|
+
*/
|
|
86
|
+
async getMessages() {
|
|
87
|
+
return this.messages;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Method to add a new message to the ChatMessageHistory instance.
|
|
91
|
+
* @param message The BaseMessage instance to add.
|
|
92
|
+
* @returns A promise that resolves when the message has been added.
|
|
93
|
+
*/
|
|
94
|
+
async addMessage(message) {
|
|
95
|
+
this.messages.push(message);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Method to clear all the messages from the ChatMessageHistory instance.
|
|
99
|
+
* @returns A promise that resolves when all messages have been cleared.
|
|
100
|
+
*/
|
|
101
|
+
async clear() {
|
|
102
|
+
this.messages = [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -220,7 +220,9 @@ class BaseLanguageModel extends BaseLangChain {
|
|
|
220
220
|
* @param callOptions Call options for the model
|
|
221
221
|
* @returns A unique cache key.
|
|
222
222
|
*/
|
|
223
|
-
_getSerializedCacheKeyParametersForCall(
|
|
223
|
+
_getSerializedCacheKeyParametersForCall(
|
|
224
|
+
// TODO: Fix when we remove the RunnableLambda backwards compatibility shim.
|
|
225
|
+
{ config, ...callOptions }) {
|
|
224
226
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
225
227
|
const params = {
|
|
226
228
|
...this._identifyingParams(),
|
|
@@ -180,7 +180,9 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
180
180
|
* @param callOptions Call options for the model
|
|
181
181
|
* @returns A unique cache key.
|
|
182
182
|
*/
|
|
183
|
-
protected _getSerializedCacheKeyParametersForCall(callOptions: CallOptions
|
|
183
|
+
protected _getSerializedCacheKeyParametersForCall({ config, ...callOptions }: CallOptions & {
|
|
184
|
+
config?: RunnableConfig;
|
|
185
|
+
}): string;
|
|
184
186
|
/**
|
|
185
187
|
* @deprecated
|
|
186
188
|
* Return a json-like object representing this LLM.
|
|
@@ -212,7 +212,9 @@ export class BaseLanguageModel extends BaseLangChain {
|
|
|
212
212
|
* @param callOptions Call options for the model
|
|
213
213
|
* @returns A unique cache key.
|
|
214
214
|
*/
|
|
215
|
-
_getSerializedCacheKeyParametersForCall(
|
|
215
|
+
_getSerializedCacheKeyParametersForCall(
|
|
216
|
+
// TODO: Fix when we remove the RunnableLambda backwards compatibility shim.
|
|
217
|
+
{ config, ...callOptions }) {
|
|
216
218
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
217
219
|
const params = {
|
|
218
220
|
...this._identifyingParams(),
|
|
@@ -53,6 +53,8 @@ class BaseRetriever extends base_js_1.Runnable {
|
|
|
53
53
|
return this.getRelevantDocuments(input, (0, config_js_1.ensureConfig)(options));
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
+
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
|
|
57
|
+
*
|
|
56
58
|
* Main method used to retrieve relevant documents. It takes a query
|
|
57
59
|
* string and an optional configuration object, and returns a promise that
|
|
58
60
|
* resolves to an array of `Document` objects. This method handles the
|
|
@@ -33,6 +33,8 @@ export declare abstract class BaseRetriever<Metadata extends Record<string, any>
|
|
|
33
33
|
_getRelevantDocuments(_query: string, _callbacks?: CallbackManagerForRetrieverRun): Promise<DocumentInterface<Metadata>[]>;
|
|
34
34
|
invoke(input: string, options?: RunnableConfig): Promise<DocumentInterface<Metadata>[]>;
|
|
35
35
|
/**
|
|
36
|
+
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
|
|
37
|
+
*
|
|
36
38
|
* Main method used to retrieve relevant documents. It takes a query
|
|
37
39
|
* string and an optional configuration object, and returns a promise that
|
|
38
40
|
* resolves to an array of `Document` objects. This method handles the
|
package/dist/retrievers/index.js
CHANGED
|
@@ -50,6 +50,8 @@ export class BaseRetriever extends Runnable {
|
|
|
50
50
|
return this.getRelevantDocuments(input, ensureConfig(options));
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
+
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
|
|
54
|
+
*
|
|
53
55
|
* Main method used to retrieve relevant documents. It takes a query
|
|
54
56
|
* string and an optional configuration object, and returns a promise that
|
|
55
57
|
* resolves to an array of `Document` objects. This method handles the
|
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.RunnablePassthrough = exports.ensureConfig = exports.patchConfig = 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;
|
|
3
|
+
exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.mergeConfigs = exports.ensureConfig = exports.patchConfig = 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; } });
|
|
@@ -18,6 +18,7 @@ var config_js_1 = require("./config.cjs");
|
|
|
18
18
|
Object.defineProperty(exports, "getCallbackManagerForConfig", { enumerable: true, get: function () { return config_js_1.getCallbackManagerForConfig; } });
|
|
19
19
|
Object.defineProperty(exports, "patchConfig", { enumerable: true, get: function () { return config_js_1.patchConfig; } });
|
|
20
20
|
Object.defineProperty(exports, "ensureConfig", { enumerable: true, get: function () { return config_js_1.ensureConfig; } });
|
|
21
|
+
Object.defineProperty(exports, "mergeConfigs", { enumerable: true, get: function () { return config_js_1.mergeConfigs; } });
|
|
21
22
|
var passthrough_js_1 = require("./passthrough.cjs");
|
|
22
23
|
Object.defineProperty(exports, "RunnablePassthrough", { enumerable: true, get: function () { return passthrough_js_1.RunnablePassthrough; } });
|
|
23
24
|
var router_js_1 = require("./router.cjs");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { type RunnableFunc, type RunnableLike, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
|
|
2
2
|
export { type RunnableBatchOptions, type RunnableInterface, type RunnableIOSchema, } from "./types.js";
|
|
3
|
-
export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, ensureConfig, } from "./config.js";
|
|
3
|
+
export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
|
|
4
4
|
export { RunnablePassthrough } from "./passthrough.js";
|
|
5
5
|
export { type RouterInput, RouterRunnable } from "./router.js";
|
|
6
6
|
export { RunnableBranch, type Branch, type BranchLike } from "./branch.js";
|
package/dist/runnables/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
|
|
2
|
-
export { getCallbackManagerForConfig, patchConfig, ensureConfig, } from "./config.js";
|
|
2
|
+
export { getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
|
|
3
3
|
export { RunnablePassthrough } from "./passthrough.js";
|
|
4
4
|
export { RouterRunnable } from "./router.js";
|
|
5
5
|
export { RunnableBranch } from "./branch.js";
|
package/dist/stores.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseStore = void 0;
|
|
3
|
+
exports.InMemoryStore = exports.BaseStore = void 0;
|
|
4
4
|
const serializable_js_1 = require("./load/serializable.cjs");
|
|
5
5
|
/**
|
|
6
6
|
* Abstract interface for a key-value store.
|
|
@@ -8,3 +8,83 @@ const serializable_js_1 = require("./load/serializable.cjs");
|
|
|
8
8
|
class BaseStore extends serializable_js_1.Serializable {
|
|
9
9
|
}
|
|
10
10
|
exports.BaseStore = BaseStore;
|
|
11
|
+
/**
|
|
12
|
+
* In-memory implementation of the BaseStore using a dictionary. Used for
|
|
13
|
+
* storing key-value pairs in memory.
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const store = new InMemoryStore<BaseMessage>();
|
|
17
|
+
* await store.mset(
|
|
18
|
+
* Array.from({ length: 5 }).map((_, index) => [
|
|
19
|
+
* `message:id:${index}`,
|
|
20
|
+
* index % 2 === 0
|
|
21
|
+
* ? new AIMessage("ai stuff...")
|
|
22
|
+
* : new HumanMessage("human stuff..."),
|
|
23
|
+
* ]),
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* const retrievedMessages = await store.mget(["message:id:0", "message:id:1"]);
|
|
27
|
+
* await store.mdelete(await store.yieldKeys("message:id:").toArray());
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
class InMemoryStore extends BaseStore {
|
|
32
|
+
constructor() {
|
|
33
|
+
super(...arguments);
|
|
34
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: ["langchain", "storage"]
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(this, "store", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: {}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves the values associated with the given keys from the store.
|
|
49
|
+
* @param keys Keys to retrieve values for.
|
|
50
|
+
* @returns Array of values associated with the given keys.
|
|
51
|
+
*/
|
|
52
|
+
async mget(keys) {
|
|
53
|
+
return keys.map((key) => this.store[key]);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sets the values for the given keys in the store.
|
|
57
|
+
* @param keyValuePairs Array of key-value pairs to set in the store.
|
|
58
|
+
* @returns Promise that resolves when all key-value pairs have been set.
|
|
59
|
+
*/
|
|
60
|
+
async mset(keyValuePairs) {
|
|
61
|
+
for (const [key, value] of keyValuePairs) {
|
|
62
|
+
this.store[key] = value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Deletes the given keys and their associated values from the store.
|
|
67
|
+
* @param keys Keys to delete from the store.
|
|
68
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
69
|
+
*/
|
|
70
|
+
async mdelete(keys) {
|
|
71
|
+
for (const key of keys) {
|
|
72
|
+
delete this.store[key];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Asynchronous generator that yields keys from the store. If a prefix is
|
|
77
|
+
* provided, it only yields keys that start with the prefix.
|
|
78
|
+
* @param prefix Optional prefix to filter keys.
|
|
79
|
+
* @returns AsyncGenerator that yields keys from the store.
|
|
80
|
+
*/
|
|
81
|
+
async *yieldKeys(prefix) {
|
|
82
|
+
const keys = Object.keys(this.store);
|
|
83
|
+
for (const key of keys) {
|
|
84
|
+
if (prefix === undefined || key.startsWith(prefix)) {
|
|
85
|
+
yield key;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.InMemoryStore = InMemoryStore;
|
package/dist/stores.d.ts
CHANGED
|
@@ -55,3 +55,51 @@ export declare abstract class BaseStore<K, V> extends Serializable implements Ba
|
|
|
55
55
|
*/
|
|
56
56
|
abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;
|
|
57
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* In-memory implementation of the BaseStore using a dictionary. Used for
|
|
60
|
+
* storing key-value pairs in memory.
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const store = new InMemoryStore<BaseMessage>();
|
|
64
|
+
* await store.mset(
|
|
65
|
+
* Array.from({ length: 5 }).map((_, index) => [
|
|
66
|
+
* `message:id:${index}`,
|
|
67
|
+
* index % 2 === 0
|
|
68
|
+
* ? new AIMessage("ai stuff...")
|
|
69
|
+
* : new HumanMessage("human stuff..."),
|
|
70
|
+
* ]),
|
|
71
|
+
* );
|
|
72
|
+
*
|
|
73
|
+
* const retrievedMessages = await store.mget(["message:id:0", "message:id:1"]);
|
|
74
|
+
* await store.mdelete(await store.yieldKeys("message:id:").toArray());
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare class InMemoryStore<T = any> extends BaseStore<string, T> {
|
|
78
|
+
lc_namespace: string[];
|
|
79
|
+
protected store: Record<string, T>;
|
|
80
|
+
/**
|
|
81
|
+
* Retrieves the values associated with the given keys from the store.
|
|
82
|
+
* @param keys Keys to retrieve values for.
|
|
83
|
+
* @returns Array of values associated with the given keys.
|
|
84
|
+
*/
|
|
85
|
+
mget(keys: string[]): Promise<T[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Sets the values for the given keys in the store.
|
|
88
|
+
* @param keyValuePairs Array of key-value pairs to set in the store.
|
|
89
|
+
* @returns Promise that resolves when all key-value pairs have been set.
|
|
90
|
+
*/
|
|
91
|
+
mset(keyValuePairs: [string, T][]): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Deletes the given keys and their associated values from the store.
|
|
94
|
+
* @param keys Keys to delete from the store.
|
|
95
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
96
|
+
*/
|
|
97
|
+
mdelete(keys: string[]): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Asynchronous generator that yields keys from the store. If a prefix is
|
|
100
|
+
* provided, it only yields keys that start with the prefix.
|
|
101
|
+
* @param prefix Optional prefix to filter keys.
|
|
102
|
+
* @returns AsyncGenerator that yields keys from the store.
|
|
103
|
+
*/
|
|
104
|
+
yieldKeys(prefix?: string | undefined): AsyncGenerator<string>;
|
|
105
|
+
}
|
package/dist/stores.js
CHANGED
|
@@ -4,3 +4,82 @@ import { Serializable } from "./load/serializable.js";
|
|
|
4
4
|
*/
|
|
5
5
|
export class BaseStore extends Serializable {
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* In-memory implementation of the BaseStore using a dictionary. Used for
|
|
9
|
+
* storing key-value pairs in memory.
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const store = new InMemoryStore<BaseMessage>();
|
|
13
|
+
* await store.mset(
|
|
14
|
+
* Array.from({ length: 5 }).map((_, index) => [
|
|
15
|
+
* `message:id:${index}`,
|
|
16
|
+
* index % 2 === 0
|
|
17
|
+
* ? new AIMessage("ai stuff...")
|
|
18
|
+
* : new HumanMessage("human stuff..."),
|
|
19
|
+
* ]),
|
|
20
|
+
* );
|
|
21
|
+
*
|
|
22
|
+
* const retrievedMessages = await store.mget(["message:id:0", "message:id:1"]);
|
|
23
|
+
* await store.mdelete(await store.yieldKeys("message:id:").toArray());
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
export class InMemoryStore extends BaseStore {
|
|
28
|
+
constructor() {
|
|
29
|
+
super(...arguments);
|
|
30
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: ["langchain", "storage"]
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(this, "store", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: {}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves the values associated with the given keys from the store.
|
|
45
|
+
* @param keys Keys to retrieve values for.
|
|
46
|
+
* @returns Array of values associated with the given keys.
|
|
47
|
+
*/
|
|
48
|
+
async mget(keys) {
|
|
49
|
+
return keys.map((key) => this.store[key]);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sets the values for the given keys in the store.
|
|
53
|
+
* @param keyValuePairs Array of key-value pairs to set in the store.
|
|
54
|
+
* @returns Promise that resolves when all key-value pairs have been set.
|
|
55
|
+
*/
|
|
56
|
+
async mset(keyValuePairs) {
|
|
57
|
+
for (const [key, value] of keyValuePairs) {
|
|
58
|
+
this.store[key] = value;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Deletes the given keys and their associated values from the store.
|
|
63
|
+
* @param keys Keys to delete from the store.
|
|
64
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
65
|
+
*/
|
|
66
|
+
async mdelete(keys) {
|
|
67
|
+
for (const key of keys) {
|
|
68
|
+
delete this.store[key];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Asynchronous generator that yields keys from the store. If a prefix is
|
|
73
|
+
* provided, it only yields keys that start with the prefix.
|
|
74
|
+
* @param prefix Optional prefix to filter keys.
|
|
75
|
+
* @returns AsyncGenerator that yields keys from the store.
|
|
76
|
+
*/
|
|
77
|
+
async *yieldKeys(prefix) {
|
|
78
|
+
const keys = Object.keys(this.store);
|
|
79
|
+
for (const key of keys) {
|
|
80
|
+
if (prefix === undefined || key.startsWith(prefix)) {
|
|
81
|
+
yield key;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BasicTranslator = exports.BaseTranslator = void 0;
|
|
4
|
+
const ir_js_1 = require("./ir.cjs");
|
|
5
|
+
const utils_js_1 = require("./utils.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Abstract class that provides a blueprint for creating specific
|
|
8
|
+
* translator classes. Defines two abstract methods: formatFunction and
|
|
9
|
+
* mergeFilters.
|
|
10
|
+
*/
|
|
11
|
+
class BaseTranslator extends ir_js_1.Visitor {
|
|
12
|
+
}
|
|
13
|
+
exports.BaseTranslator = BaseTranslator;
|
|
14
|
+
/**
|
|
15
|
+
* Class that extends the BaseTranslator class and provides concrete
|
|
16
|
+
* implementations for the abstract methods. Also declares three types:
|
|
17
|
+
* VisitOperationOutput, VisitComparisonOutput, and
|
|
18
|
+
* VisitStructuredQueryOutput, which are used as the return types for the
|
|
19
|
+
* visitOperation, visitComparison, and visitStructuredQuery methods
|
|
20
|
+
* respectively.
|
|
21
|
+
*/
|
|
22
|
+
class BasicTranslator extends BaseTranslator {
|
|
23
|
+
constructor(opts) {
|
|
24
|
+
super();
|
|
25
|
+
Object.defineProperty(this, "allowedOperators", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "allowedComparators", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: void 0
|
|
36
|
+
});
|
|
37
|
+
this.allowedOperators = opts?.allowedOperators ?? [
|
|
38
|
+
ir_js_1.Operators.and,
|
|
39
|
+
ir_js_1.Operators.or,
|
|
40
|
+
];
|
|
41
|
+
this.allowedComparators = opts?.allowedComparators ?? [
|
|
42
|
+
ir_js_1.Comparators.eq,
|
|
43
|
+
ir_js_1.Comparators.ne,
|
|
44
|
+
ir_js_1.Comparators.gt,
|
|
45
|
+
ir_js_1.Comparators.gte,
|
|
46
|
+
ir_js_1.Comparators.lt,
|
|
47
|
+
ir_js_1.Comparators.lte,
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
formatFunction(func) {
|
|
51
|
+
if (func in ir_js_1.Comparators) {
|
|
52
|
+
if (this.allowedComparators.length > 0 &&
|
|
53
|
+
this.allowedComparators.indexOf(func) === -1) {
|
|
54
|
+
throw new Error(`Comparator ${func} not allowed. Allowed operators: ${this.allowedComparators.join(", ")}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else if (func in ir_js_1.Operators) {
|
|
58
|
+
if (this.allowedOperators.length > 0 &&
|
|
59
|
+
this.allowedOperators.indexOf(func) === -1) {
|
|
60
|
+
throw new Error(`Operator ${func} not allowed. Allowed operators: ${this.allowedOperators.join(", ")}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw new Error("Unknown comparator or operator");
|
|
65
|
+
}
|
|
66
|
+
return `$${func}`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Visits an operation and returns a result.
|
|
70
|
+
* @param operation The operation to visit.
|
|
71
|
+
* @returns The result of visiting the operation.
|
|
72
|
+
*/
|
|
73
|
+
visitOperation(operation) {
|
|
74
|
+
const args = operation.args?.map((arg) => arg.accept(this));
|
|
75
|
+
return {
|
|
76
|
+
[this.formatFunction(operation.operator)]: args,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Visits a comparison and returns a result.
|
|
81
|
+
* @param comparison The comparison to visit.
|
|
82
|
+
* @returns The result of visiting the comparison.
|
|
83
|
+
*/
|
|
84
|
+
visitComparison(comparison) {
|
|
85
|
+
return {
|
|
86
|
+
[comparison.attribute]: {
|
|
87
|
+
[this.formatFunction(comparison.comparator)]: (0, utils_js_1.castValue)(comparison.value),
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Visits a structured query and returns a result.
|
|
93
|
+
* @param query The structured query to visit.
|
|
94
|
+
* @returns The result of visiting the structured query.
|
|
95
|
+
*/
|
|
96
|
+
visitStructuredQuery(query) {
|
|
97
|
+
let nextArg = {};
|
|
98
|
+
if (query.filter) {
|
|
99
|
+
nextArg = {
|
|
100
|
+
filter: query.filter.accept(this),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return nextArg;
|
|
104
|
+
}
|
|
105
|
+
mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
|
|
106
|
+
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) && (0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
if ((0, utils_js_1.isFilterEmpty)(defaultFilter) || mergeType === "replace") {
|
|
110
|
+
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
return generatedFilter;
|
|
114
|
+
}
|
|
115
|
+
if ((0, utils_js_1.isFilterEmpty)(generatedFilter)) {
|
|
116
|
+
if (forceDefaultFilter) {
|
|
117
|
+
return defaultFilter;
|
|
118
|
+
}
|
|
119
|
+
if (mergeType === "and") {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
return defaultFilter;
|
|
123
|
+
}
|
|
124
|
+
if (mergeType === "and") {
|
|
125
|
+
return {
|
|
126
|
+
$and: [defaultFilter, generatedFilter],
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
else if (mergeType === "or") {
|
|
130
|
+
return {
|
|
131
|
+
$or: [defaultFilter, generatedFilter],
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
throw new Error("Unknown merge type");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.BasicTranslator = BasicTranslator;
|