@langchain/core 0.1.59 → 0.1.60

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.
@@ -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;
@@ -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
+ }
@@ -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
+ }
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;
@@ -0,0 +1,69 @@
1
+ import { VectorStore } from "../vectorstores.js";
2
+ import { Comparator, Comparison, Operation, Operator, StructuredQuery, Visitor, VisitorComparisonResult, VisitorOperationResult, VisitorStructuredQueryResult } from "./ir.js";
3
+ /**
4
+ * Options object for the BasicTranslator class. Specifies the allowed
5
+ * operators and comparators.
6
+ */
7
+ export type TranslatorOpts = {
8
+ allowedOperators: Operator[];
9
+ allowedComparators: Comparator[];
10
+ };
11
+ /**
12
+ * Abstract class that provides a blueprint for creating specific
13
+ * translator classes. Defines two abstract methods: formatFunction and
14
+ * mergeFilters.
15
+ */
16
+ export declare abstract class BaseTranslator<T extends VectorStore = VectorStore> extends Visitor<T> {
17
+ /**
18
+ * Formats a given function (either an operator or a comparator) into a
19
+ * string.
20
+ * @param func The function to format.
21
+ * @returns Formatted string representation of the function.
22
+ */
23
+ abstract formatFunction(func: Operator | Comparator): string;
24
+ /**
25
+ * Merges two filters into one, using a specified merge type.
26
+ * @param defaultFilter The default filter.
27
+ * @param generatedFilter The generated filter.
28
+ * @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'.
29
+ * @param forceDefaultFilter If true, the default filter will be used even if the generated filter is not empty.
30
+ * @returns The merged filter, or undefined if both filters are empty.
31
+ */
32
+ abstract mergeFilters(defaultFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, generatedFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, mergeType?: "and" | "or" | "replace", forceDefaultFilter?: boolean): this["VisitStructuredQueryOutput"]["filter"] | undefined;
33
+ }
34
+ /**
35
+ * Class that extends the BaseTranslator class and provides concrete
36
+ * implementations for the abstract methods. Also declares three types:
37
+ * VisitOperationOutput, VisitComparisonOutput, and
38
+ * VisitStructuredQueryOutput, which are used as the return types for the
39
+ * visitOperation, visitComparison, and visitStructuredQuery methods
40
+ * respectively.
41
+ */
42
+ export declare class BasicTranslator<T extends VectorStore = VectorStore> extends BaseTranslator<T> {
43
+ VisitOperationOutput: VisitorOperationResult;
44
+ VisitComparisonOutput: VisitorComparisonResult;
45
+ VisitStructuredQueryOutput: VisitorStructuredQueryResult;
46
+ allowedOperators: Operator[];
47
+ allowedComparators: Comparator[];
48
+ constructor(opts?: TranslatorOpts);
49
+ formatFunction(func: Operator | Comparator): string;
50
+ /**
51
+ * Visits an operation and returns a result.
52
+ * @param operation The operation to visit.
53
+ * @returns The result of visiting the operation.
54
+ */
55
+ visitOperation(operation: Operation): this["VisitOperationOutput"];
56
+ /**
57
+ * Visits a comparison and returns a result.
58
+ * @param comparison The comparison to visit.
59
+ * @returns The result of visiting the comparison.
60
+ */
61
+ visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
62
+ /**
63
+ * Visits a structured query and returns a result.
64
+ * @param query The structured query to visit.
65
+ * @returns The result of visiting the structured query.
66
+ */
67
+ visitStructuredQuery(query: StructuredQuery): this["VisitStructuredQueryOutput"];
68
+ mergeFilters(defaultFilter: VisitorStructuredQueryResult["filter"] | undefined, generatedFilter: VisitorStructuredQueryResult["filter"] | undefined, mergeType?: string, forceDefaultFilter?: boolean): VisitorStructuredQueryResult["filter"] | undefined;
69
+ }