langchain 0.0.166 → 0.0.168
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 +4 -4
- package/chat_models/cloudflare_workersai.cjs +1 -0
- package/chat_models/cloudflare_workersai.d.ts +1 -0
- package/chat_models/cloudflare_workersai.js +1 -0
- package/chat_models/fake.cjs +1 -0
- package/chat_models/fake.d.ts +1 -0
- package/chat_models/fake.js +1 -0
- package/dist/agents/chat/index.cjs +3 -2
- package/dist/agents/chat/index.d.ts +3 -0
- package/dist/agents/chat/index.js +3 -2
- package/dist/chat_models/cloudflare_workersai.cjs +140 -0
- package/dist/chat_models/cloudflare_workersai.d.ts +46 -0
- package/dist/chat_models/cloudflare_workersai.js +136 -0
- package/dist/chat_models/fake.cjs +101 -0
- package/dist/chat_models/fake.d.ts +36 -0
- package/dist/chat_models/fake.js +97 -0
- package/dist/embeddings/bedrock.cjs +43 -22
- package/dist/embeddings/bedrock.d.ts +11 -4
- package/dist/embeddings/bedrock.js +43 -22
- package/dist/llms/cloudflare_workersai.cjs +117 -0
- package/dist/llms/cloudflare_workersai.d.ts +49 -0
- package/dist/llms/cloudflare_workersai.js +113 -0
- package/dist/llms/fake.cjs +82 -0
- package/dist/llms/fake.d.ts +31 -0
- package/dist/llms/fake.js +78 -0
- package/dist/llms/sagemaker_endpoint.cjs +9 -7
- package/dist/llms/sagemaker_endpoint.d.ts +3 -3
- package/dist/llms/sagemaker_endpoint.js +9 -7
- package/dist/llms/yandex.cjs +100 -0
- package/dist/llms/yandex.d.ts +40 -0
- package/dist/llms/yandex.js +96 -0
- package/dist/load/import_constants.cjs +4 -0
- package/dist/load/import_constants.js +4 -0
- package/dist/load/import_map.cjs +8 -2
- package/dist/load/import_map.d.ts +6 -0
- package/dist/load/import_map.js +6 -0
- package/dist/retrievers/multi_vector.d.ts +3 -3
- package/dist/retrievers/parent_document.cjs +6 -16
- package/dist/retrievers/parent_document.d.ts +5 -12
- package/dist/retrievers/parent_document.js +6 -16
- package/dist/schema/storage.d.ts +28 -1
- package/dist/storage/encoder_backed.cjs +14 -2
- package/dist/storage/encoder_backed.d.ts +2 -0
- package/dist/storage/encoder_backed.js +12 -1
- package/dist/storage/in_memory.cjs +1 -1
- package/dist/storage/in_memory.js +1 -1
- package/dist/storage/ioredis.cjs +4 -4
- package/dist/storage/ioredis.js +4 -4
- package/dist/storage/vercel_kv.cjs +146 -0
- package/dist/storage/vercel_kv.d.ts +46 -0
- package/dist/storage/vercel_kv.js +142 -0
- package/dist/stores/doc/in_memory.cjs +13 -0
- package/dist/stores/doc/in_memory.d.ts +6 -1
- package/dist/stores/doc/in_memory.js +13 -0
- package/dist/util/axios-fetch-adapter.cjs +1 -1
- package/dist/util/axios-fetch-adapter.js +1 -1
- package/dist/util/env.cjs +1 -1
- package/dist/util/env.js +1 -1
- package/dist/util/event-source-parse.cjs +1 -1
- package/dist/util/event-source-parse.js +1 -1
- package/dist/vectorstores/cassandra.cjs +4 -2
- package/dist/vectorstores/cassandra.js +4 -2
- package/dist/vectorstores/closevector/common.cjs +128 -0
- package/dist/vectorstores/closevector/common.d.ts +82 -0
- package/dist/vectorstores/closevector/common.js +124 -0
- package/dist/vectorstores/closevector/node.cjs +109 -0
- package/dist/vectorstores/closevector/node.d.ts +83 -0
- package/dist/vectorstores/closevector/node.js +105 -0
- package/dist/vectorstores/closevector/web.cjs +109 -0
- package/dist/vectorstores/closevector/web.d.ts +80 -0
- package/dist/vectorstores/closevector/web.js +105 -0
- package/dist/vectorstores/elasticsearch.cjs +3 -1
- package/dist/vectorstores/elasticsearch.js +3 -1
- package/dist/vectorstores/neo4j_vector.cjs +578 -0
- package/dist/vectorstores/neo4j_vector.d.ts +61 -0
- package/dist/vectorstores/neo4j_vector.js +548 -0
- package/llms/cloudflare_workersai.cjs +1 -0
- package/llms/cloudflare_workersai.d.ts +1 -0
- package/llms/cloudflare_workersai.js +1 -0
- package/llms/fake.cjs +1 -0
- package/llms/fake.d.ts +1 -0
- package/llms/fake.js +1 -0
- package/llms/yandex.cjs +1 -0
- package/llms/yandex.d.ts +1 -0
- package/llms/yandex.js +1 -0
- package/package.json +105 -5
- package/storage/encoder_backed.cjs +1 -0
- package/storage/encoder_backed.d.ts +1 -0
- package/storage/encoder_backed.js +1 -0
- package/storage/vercel_kv.cjs +1 -0
- package/storage/vercel_kv.d.ts +1 -0
- package/storage/vercel_kv.js +1 -0
- package/vectorstores/closevector/node.cjs +1 -0
- package/vectorstores/closevector/node.d.ts +1 -0
- package/vectorstores/closevector/node.js +1 -0
- package/vectorstores/closevector/web.cjs +1 -0
- package/vectorstores/closevector/web.d.ts +1 -0
- package/vectorstores/closevector/web.js +1 -0
- package/vectorstores/neo4j_vector.cjs +1 -0
- package/vectorstores/neo4j_vector.d.ts +1 -0
- package/vectorstores/neo4j_vector.js +1 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as uuid from "uuid";
|
|
2
|
-
import { BaseRetriever } from "../schema/retriever.js";
|
|
3
2
|
import { Document } from "../document.js";
|
|
4
|
-
|
|
3
|
+
import { MultiVectorRetriever, } from "./multi_vector.js";
|
|
5
4
|
/**
|
|
6
5
|
* A type of document retriever that splits input documents into smaller chunks
|
|
7
6
|
* while separately storing and preserving the original documents.
|
|
@@ -11,7 +10,7 @@ import { Document } from "../document.js";
|
|
|
11
10
|
* This strikes a balance between better targeted retrieval with small documents
|
|
12
11
|
* and the more context-rich larger documents.
|
|
13
12
|
*/
|
|
14
|
-
export class ParentDocumentRetriever extends
|
|
13
|
+
export class ParentDocumentRetriever extends MultiVectorRetriever {
|
|
15
14
|
static lc_name() {
|
|
16
15
|
return "ParentDocumentRetriever";
|
|
17
16
|
}
|
|
@@ -29,12 +28,6 @@ export class ParentDocumentRetriever extends BaseRetriever {
|
|
|
29
28
|
writable: true,
|
|
30
29
|
value: void 0
|
|
31
30
|
});
|
|
32
|
-
Object.defineProperty(this, "docstore", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
configurable: true,
|
|
35
|
-
writable: true,
|
|
36
|
-
value: void 0
|
|
37
|
-
});
|
|
38
31
|
Object.defineProperty(this, "childSplitter", {
|
|
39
32
|
enumerable: true,
|
|
40
33
|
configurable: true,
|
|
@@ -83,12 +76,9 @@ export class ParentDocumentRetriever extends BaseRetriever {
|
|
|
83
76
|
}
|
|
84
77
|
}
|
|
85
78
|
const parentDocs = [];
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
parentDocs.push(parentDoc);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
79
|
+
const storedParentDocs = await this.docstore.mget(parentDocIds);
|
|
80
|
+
const retrievedDocs = storedParentDocs.filter((doc) => doc !== undefined);
|
|
81
|
+
parentDocs.push(...retrievedDocs);
|
|
92
82
|
return parentDocs.slice(0, this.parentK);
|
|
93
83
|
}
|
|
94
84
|
/**
|
|
@@ -136,7 +126,7 @@ export class ParentDocumentRetriever extends BaseRetriever {
|
|
|
136
126
|
}
|
|
137
127
|
await this.vectorstore.addDocuments(embeddedDocs);
|
|
138
128
|
if (addToDocstore) {
|
|
139
|
-
await this.docstore.
|
|
129
|
+
await this.docstore.mset(Object.entries(fullDocs));
|
|
140
130
|
}
|
|
141
131
|
}
|
|
142
132
|
}
|
package/dist/schema/storage.d.ts
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
1
|
import { Serializable } from "../load/serializable.js";
|
|
2
|
+
/** @deprecated For backwards compatibility only. Remove on next minor version upgrade. */
|
|
3
|
+
export interface BaseStoreInterface<K, V> {
|
|
4
|
+
/**
|
|
5
|
+
* Method to get multiple values for a set of keys.
|
|
6
|
+
* @param {K[]} keys - An array of keys.
|
|
7
|
+
* @returns {Promise<(V | undefined)[]>} - A Promise that resolves with array of values or undefined if key not found.
|
|
8
|
+
*/
|
|
9
|
+
mget(keys: K[]): Promise<(V | undefined)[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Method to set a value for multiple keys.
|
|
12
|
+
* @param {[K, V][]} keyValuePairs - An array of key-value pairs.
|
|
13
|
+
* @returns {Promise<void>} - A Promise that resolves when the operation is complete.
|
|
14
|
+
*/
|
|
15
|
+
mset(keyValuePairs: [K, V][]): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Method to delete multiple keys.
|
|
18
|
+
* @param {K[]} keys - An array of keys to delete.
|
|
19
|
+
* @returns {Promise<void>} - A Promise that resolves when the operation is complete.
|
|
20
|
+
*/
|
|
21
|
+
mdelete(keys: K[]): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Method to yield keys optionally based on a prefix.
|
|
24
|
+
* @param {string} prefix - Optional prefix to filter keys.
|
|
25
|
+
* @returns {AsyncGenerator<K | string>} - An asynchronous generator that yields keys on iteration.
|
|
26
|
+
*/
|
|
27
|
+
yieldKeys(prefix?: string): AsyncGenerator<K | string>;
|
|
28
|
+
}
|
|
2
29
|
/**
|
|
3
30
|
* Abstract interface for a key-value store.
|
|
4
31
|
*/
|
|
5
|
-
export declare abstract class BaseStore<K, V> extends Serializable {
|
|
32
|
+
export declare abstract class BaseStore<K, V> extends Serializable implements BaseStoreInterface<K, V> {
|
|
6
33
|
/**
|
|
7
34
|
* Abstract method to get multiple values for a set of keys.
|
|
8
35
|
* @param {K[]} keys - An array of keys.
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EncoderBackedStore = void 0;
|
|
3
|
+
exports.createDocumentStoreFromByteStore = exports.EncoderBackedStore = void 0;
|
|
4
4
|
const storage_js_1 = require("../schema/storage.cjs");
|
|
5
|
+
const document_js_1 = require("../document.cjs");
|
|
5
6
|
/**
|
|
6
7
|
* Class that provides a layer of abstraction over the base storage,
|
|
7
8
|
* allowing for the encoding and decoding of keys and values. It extends
|
|
@@ -15,7 +16,7 @@ class EncoderBackedStore extends storage_js_1.BaseStore {
|
|
|
15
16
|
enumerable: true,
|
|
16
17
|
configurable: true,
|
|
17
18
|
writable: true,
|
|
18
|
-
value: ["langchain", "storage"
|
|
19
|
+
value: ["langchain", "storage"]
|
|
19
20
|
});
|
|
20
21
|
Object.defineProperty(this, "store", {
|
|
21
22
|
enumerable: true,
|
|
@@ -91,3 +92,14 @@ class EncoderBackedStore extends storage_js_1.BaseStore {
|
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
exports.EncoderBackedStore = EncoderBackedStore;
|
|
95
|
+
function createDocumentStoreFromByteStore(store) {
|
|
96
|
+
const encoder = new TextEncoder();
|
|
97
|
+
const decoder = new TextDecoder();
|
|
98
|
+
return new EncoderBackedStore({
|
|
99
|
+
store,
|
|
100
|
+
keyEncoder: (key) => key,
|
|
101
|
+
valueSerializer: (doc) => encoder.encode(JSON.stringify({ pageContent: doc.pageContent, metadata: doc.metadata })),
|
|
102
|
+
valueDeserializer: (bytes) => new document_js_1.Document(JSON.parse(decoder.decode(bytes))),
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
exports.createDocumentStoreFromByteStore = createDocumentStoreFromByteStore;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BaseStore } from "../schema/storage.js";
|
|
2
|
+
import { Document } from "../document.js";
|
|
2
3
|
/**
|
|
3
4
|
* Class that provides a layer of abstraction over the base storage,
|
|
4
5
|
* allowing for the encoding and decoding of keys and values. It extends
|
|
@@ -43,3 +44,4 @@ export declare class EncoderBackedStore<K, V, SerializedType = any> extends Base
|
|
|
43
44
|
*/
|
|
44
45
|
yieldKeys(prefix?: string | undefined): AsyncGenerator<string | K>;
|
|
45
46
|
}
|
|
47
|
+
export declare function createDocumentStoreFromByteStore(store: BaseStore<string, Uint8Array>): EncoderBackedStore<string, Document<Record<string, any>>, Uint8Array>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BaseStore } from "../schema/storage.js";
|
|
2
|
+
import { Document } from "../document.js";
|
|
2
3
|
/**
|
|
3
4
|
* Class that provides a layer of abstraction over the base storage,
|
|
4
5
|
* allowing for the encoding and decoding of keys and values. It extends
|
|
@@ -12,7 +13,7 @@ export class EncoderBackedStore extends BaseStore {
|
|
|
12
13
|
enumerable: true,
|
|
13
14
|
configurable: true,
|
|
14
15
|
writable: true,
|
|
15
|
-
value: ["langchain", "storage"
|
|
16
|
+
value: ["langchain", "storage"]
|
|
16
17
|
});
|
|
17
18
|
Object.defineProperty(this, "store", {
|
|
18
19
|
enumerable: true,
|
|
@@ -87,3 +88,13 @@ export class EncoderBackedStore extends BaseStore {
|
|
|
87
88
|
yield* this.store.yieldKeys(prefix);
|
|
88
89
|
}
|
|
89
90
|
}
|
|
91
|
+
export function createDocumentStoreFromByteStore(store) {
|
|
92
|
+
const encoder = new TextEncoder();
|
|
93
|
+
const decoder = new TextDecoder();
|
|
94
|
+
return new EncoderBackedStore({
|
|
95
|
+
store,
|
|
96
|
+
keyEncoder: (key) => key,
|
|
97
|
+
valueSerializer: (doc) => encoder.encode(JSON.stringify({ pageContent: doc.pageContent, metadata: doc.metadata })),
|
|
98
|
+
valueDeserializer: (bytes) => new Document(JSON.parse(decoder.decode(bytes))),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
@@ -14,7 +14,7 @@ class InMemoryStore extends storage_js_1.BaseStore {
|
|
|
14
14
|
enumerable: true,
|
|
15
15
|
configurable: true,
|
|
16
16
|
writable: true,
|
|
17
|
-
value: ["langchain", "storage"
|
|
17
|
+
value: ["langchain", "storage"]
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(this, "store", {
|
|
20
20
|
enumerable: true,
|
|
@@ -11,7 +11,7 @@ export class InMemoryStore extends BaseStore {
|
|
|
11
11
|
enumerable: true,
|
|
12
12
|
configurable: true,
|
|
13
13
|
writable: true,
|
|
14
|
-
value: ["langchain", "storage"
|
|
14
|
+
value: ["langchain", "storage"]
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(this, "store", {
|
|
17
17
|
enumerable: true,
|
package/dist/storage/ioredis.cjs
CHANGED
|
@@ -14,7 +14,7 @@ class RedisByteStore extends storage_js_1.BaseStore {
|
|
|
14
14
|
enumerable: true,
|
|
15
15
|
configurable: true,
|
|
16
16
|
writable: true,
|
|
17
|
-
value: ["langchain", "storage"
|
|
17
|
+
value: ["langchain", "storage"]
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(this, "client", {
|
|
20
20
|
enumerable: true,
|
|
@@ -68,12 +68,12 @@ class RedisByteStore extends storage_js_1.BaseStore {
|
|
|
68
68
|
async mget(keys) {
|
|
69
69
|
const prefixedKeys = keys.map(this._getPrefixedKey.bind(this));
|
|
70
70
|
const retrievedValues = await this.client.mgetBuffer(prefixedKeys);
|
|
71
|
-
return retrievedValues.map((
|
|
72
|
-
if (!
|
|
71
|
+
return retrievedValues.map((value) => {
|
|
72
|
+
if (!value) {
|
|
73
73
|
return undefined;
|
|
74
74
|
}
|
|
75
75
|
else {
|
|
76
|
-
return
|
|
76
|
+
return value;
|
|
77
77
|
}
|
|
78
78
|
});
|
|
79
79
|
}
|
package/dist/storage/ioredis.js
CHANGED
|
@@ -11,7 +11,7 @@ export class RedisByteStore extends BaseStore {
|
|
|
11
11
|
enumerable: true,
|
|
12
12
|
configurable: true,
|
|
13
13
|
writable: true,
|
|
14
|
-
value: ["langchain", "storage"
|
|
14
|
+
value: ["langchain", "storage"]
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(this, "client", {
|
|
17
17
|
enumerable: true,
|
|
@@ -65,12 +65,12 @@ export class RedisByteStore extends BaseStore {
|
|
|
65
65
|
async mget(keys) {
|
|
66
66
|
const prefixedKeys = keys.map(this._getPrefixedKey.bind(this));
|
|
67
67
|
const retrievedValues = await this.client.mgetBuffer(prefixedKeys);
|
|
68
|
-
return retrievedValues.map((
|
|
69
|
-
if (!
|
|
68
|
+
return retrievedValues.map((value) => {
|
|
69
|
+
if (!value) {
|
|
70
70
|
return undefined;
|
|
71
71
|
}
|
|
72
72
|
else {
|
|
73
|
-
return
|
|
73
|
+
return value;
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
76
|
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VercelKVStore = void 0;
|
|
4
|
+
const kv_1 = require("@vercel/kv");
|
|
5
|
+
const storage_js_1 = require("../schema/storage.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Class that extends the BaseStore class to interact with a Vercel KV
|
|
8
|
+
* database. It provides methods for getting, setting, and deleting data,
|
|
9
|
+
* as well as yielding keys from the database.
|
|
10
|
+
*/
|
|
11
|
+
class VercelKVStore extends storage_js_1.BaseStore {
|
|
12
|
+
constructor(fields) {
|
|
13
|
+
super(fields);
|
|
14
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: ["langchain", "storage"]
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "client", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(this, "ttl", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "namespace", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "yieldKeysScanBatchSize", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: 1000
|
|
43
|
+
});
|
|
44
|
+
this.client = fields?.client ?? kv_1.kv;
|
|
45
|
+
this.ttl = fields?.ttl;
|
|
46
|
+
this.namespace = fields?.namespace;
|
|
47
|
+
this.yieldKeysScanBatchSize =
|
|
48
|
+
fields?.yieldKeysScanBatchSize ?? this.yieldKeysScanBatchSize;
|
|
49
|
+
}
|
|
50
|
+
_getPrefixedKey(key) {
|
|
51
|
+
if (this.namespace) {
|
|
52
|
+
const delimiter = "/";
|
|
53
|
+
return `${this.namespace}${delimiter}${key}`;
|
|
54
|
+
}
|
|
55
|
+
return key;
|
|
56
|
+
}
|
|
57
|
+
_getDeprefixedKey(key) {
|
|
58
|
+
if (this.namespace) {
|
|
59
|
+
const delimiter = "/";
|
|
60
|
+
return key.slice(this.namespace.length + delimiter.length);
|
|
61
|
+
}
|
|
62
|
+
return key;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Gets multiple keys from the Redis database.
|
|
66
|
+
* @param keys Array of keys to be retrieved.
|
|
67
|
+
* @returns An array of retrieved values.
|
|
68
|
+
*/
|
|
69
|
+
async mget(keys) {
|
|
70
|
+
const prefixedKeys = keys.map(this._getPrefixedKey.bind(this));
|
|
71
|
+
const retrievedValues = await this.client.mget(...prefixedKeys);
|
|
72
|
+
const encoder = new TextEncoder();
|
|
73
|
+
return retrievedValues.map((value) => {
|
|
74
|
+
if (value === undefined || value === null) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
else if (typeof value === "object") {
|
|
78
|
+
return encoder.encode(JSON.stringify(value));
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return encoder.encode(value);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sets multiple keys in the Redis database.
|
|
87
|
+
* @param keyValuePairs Array of key-value pairs to be set.
|
|
88
|
+
* @returns Promise that resolves when all keys have been set.
|
|
89
|
+
*/
|
|
90
|
+
async mset(keyValuePairs) {
|
|
91
|
+
const decoder = new TextDecoder();
|
|
92
|
+
const decodedKeyValuePairs = keyValuePairs.map(([key, value]) => [
|
|
93
|
+
this._getPrefixedKey(key),
|
|
94
|
+
decoder.decode(value),
|
|
95
|
+
]);
|
|
96
|
+
const pipeline = this.client.pipeline();
|
|
97
|
+
for (const [key, value] of decodedKeyValuePairs) {
|
|
98
|
+
if (this.ttl) {
|
|
99
|
+
pipeline.setex(key, this.ttl, value);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
pipeline.set(key, value);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
await pipeline.exec();
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Deletes multiple keys from the Redis database.
|
|
109
|
+
* @param keys Array of keys to be deleted.
|
|
110
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
111
|
+
*/
|
|
112
|
+
async mdelete(keys) {
|
|
113
|
+
await this.client.del(...keys.map(this._getPrefixedKey.bind(this)));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Yields keys from the Redis database.
|
|
117
|
+
* @param prefix Optional prefix to filter the keys.
|
|
118
|
+
* @returns An AsyncGenerator that yields keys from the Redis database.
|
|
119
|
+
*/
|
|
120
|
+
async *yieldKeys(prefix) {
|
|
121
|
+
let pattern;
|
|
122
|
+
if (prefix) {
|
|
123
|
+
pattern = this._getPrefixedKey(prefix);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
pattern = this._getPrefixedKey("*");
|
|
127
|
+
}
|
|
128
|
+
let [cursor, batch] = await this.client.scan(0, {
|
|
129
|
+
match: pattern,
|
|
130
|
+
count: this.yieldKeysScanBatchSize,
|
|
131
|
+
});
|
|
132
|
+
for (const key of batch) {
|
|
133
|
+
yield this._getDeprefixedKey(key);
|
|
134
|
+
}
|
|
135
|
+
while (cursor !== 0) {
|
|
136
|
+
[cursor, batch] = await this.client.scan(cursor, {
|
|
137
|
+
match: pattern,
|
|
138
|
+
count: this.yieldKeysScanBatchSize,
|
|
139
|
+
});
|
|
140
|
+
for (const key of batch) {
|
|
141
|
+
yield this._getDeprefixedKey(key);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.VercelKVStore = VercelKVStore;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type VercelKV } from "@vercel/kv";
|
|
2
|
+
import { BaseStore } from "../schema/storage.js";
|
|
3
|
+
/**
|
|
4
|
+
* Class that extends the BaseStore class to interact with a Vercel KV
|
|
5
|
+
* database. It provides methods for getting, setting, and deleting data,
|
|
6
|
+
* as well as yielding keys from the database.
|
|
7
|
+
*/
|
|
8
|
+
export declare class VercelKVStore extends BaseStore<string, Uint8Array> {
|
|
9
|
+
lc_namespace: string[];
|
|
10
|
+
protected client: VercelKV;
|
|
11
|
+
protected ttl?: number;
|
|
12
|
+
protected namespace?: string;
|
|
13
|
+
protected yieldKeysScanBatchSize: number;
|
|
14
|
+
constructor(fields?: {
|
|
15
|
+
client?: VercelKV;
|
|
16
|
+
ttl?: number;
|
|
17
|
+
namespace?: string;
|
|
18
|
+
yieldKeysScanBatchSize?: number;
|
|
19
|
+
});
|
|
20
|
+
_getPrefixedKey(key: string): string;
|
|
21
|
+
_getDeprefixedKey(key: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Gets multiple keys from the Redis database.
|
|
24
|
+
* @param keys Array of keys to be retrieved.
|
|
25
|
+
* @returns An array of retrieved values.
|
|
26
|
+
*/
|
|
27
|
+
mget(keys: string[]): Promise<(Uint8Array | undefined)[]>;
|
|
28
|
+
/**
|
|
29
|
+
* Sets multiple keys in the Redis database.
|
|
30
|
+
* @param keyValuePairs Array of key-value pairs to be set.
|
|
31
|
+
* @returns Promise that resolves when all keys have been set.
|
|
32
|
+
*/
|
|
33
|
+
mset(keyValuePairs: [string, Uint8Array][]): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Deletes multiple keys from the Redis database.
|
|
36
|
+
* @param keys Array of keys to be deleted.
|
|
37
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
38
|
+
*/
|
|
39
|
+
mdelete(keys: string[]): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Yields keys from the Redis database.
|
|
42
|
+
* @param prefix Optional prefix to filter the keys.
|
|
43
|
+
* @returns An AsyncGenerator that yields keys from the Redis database.
|
|
44
|
+
*/
|
|
45
|
+
yieldKeys(prefix?: string): AsyncGenerator<string>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { kv } from "@vercel/kv";
|
|
2
|
+
import { BaseStore } from "../schema/storage.js";
|
|
3
|
+
/**
|
|
4
|
+
* Class that extends the BaseStore class to interact with a Vercel KV
|
|
5
|
+
* database. It provides methods for getting, setting, and deleting data,
|
|
6
|
+
* as well as yielding keys from the database.
|
|
7
|
+
*/
|
|
8
|
+
export class VercelKVStore extends BaseStore {
|
|
9
|
+
constructor(fields) {
|
|
10
|
+
super(fields);
|
|
11
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: ["langchain", "storage"]
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "client", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: void 0
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "ttl", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "namespace", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: void 0
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "yieldKeysScanBatchSize", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: 1000
|
|
40
|
+
});
|
|
41
|
+
this.client = fields?.client ?? kv;
|
|
42
|
+
this.ttl = fields?.ttl;
|
|
43
|
+
this.namespace = fields?.namespace;
|
|
44
|
+
this.yieldKeysScanBatchSize =
|
|
45
|
+
fields?.yieldKeysScanBatchSize ?? this.yieldKeysScanBatchSize;
|
|
46
|
+
}
|
|
47
|
+
_getPrefixedKey(key) {
|
|
48
|
+
if (this.namespace) {
|
|
49
|
+
const delimiter = "/";
|
|
50
|
+
return `${this.namespace}${delimiter}${key}`;
|
|
51
|
+
}
|
|
52
|
+
return key;
|
|
53
|
+
}
|
|
54
|
+
_getDeprefixedKey(key) {
|
|
55
|
+
if (this.namespace) {
|
|
56
|
+
const delimiter = "/";
|
|
57
|
+
return key.slice(this.namespace.length + delimiter.length);
|
|
58
|
+
}
|
|
59
|
+
return key;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Gets multiple keys from the Redis database.
|
|
63
|
+
* @param keys Array of keys to be retrieved.
|
|
64
|
+
* @returns An array of retrieved values.
|
|
65
|
+
*/
|
|
66
|
+
async mget(keys) {
|
|
67
|
+
const prefixedKeys = keys.map(this._getPrefixedKey.bind(this));
|
|
68
|
+
const retrievedValues = await this.client.mget(...prefixedKeys);
|
|
69
|
+
const encoder = new TextEncoder();
|
|
70
|
+
return retrievedValues.map((value) => {
|
|
71
|
+
if (value === undefined || value === null) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
else if (typeof value === "object") {
|
|
75
|
+
return encoder.encode(JSON.stringify(value));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return encoder.encode(value);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Sets multiple keys in the Redis database.
|
|
84
|
+
* @param keyValuePairs Array of key-value pairs to be set.
|
|
85
|
+
* @returns Promise that resolves when all keys have been set.
|
|
86
|
+
*/
|
|
87
|
+
async mset(keyValuePairs) {
|
|
88
|
+
const decoder = new TextDecoder();
|
|
89
|
+
const decodedKeyValuePairs = keyValuePairs.map(([key, value]) => [
|
|
90
|
+
this._getPrefixedKey(key),
|
|
91
|
+
decoder.decode(value),
|
|
92
|
+
]);
|
|
93
|
+
const pipeline = this.client.pipeline();
|
|
94
|
+
for (const [key, value] of decodedKeyValuePairs) {
|
|
95
|
+
if (this.ttl) {
|
|
96
|
+
pipeline.setex(key, this.ttl, value);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
pipeline.set(key, value);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
await pipeline.exec();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Deletes multiple keys from the Redis database.
|
|
106
|
+
* @param keys Array of keys to be deleted.
|
|
107
|
+
* @returns Promise that resolves when all keys have been deleted.
|
|
108
|
+
*/
|
|
109
|
+
async mdelete(keys) {
|
|
110
|
+
await this.client.del(...keys.map(this._getPrefixedKey.bind(this)));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Yields keys from the Redis database.
|
|
114
|
+
* @param prefix Optional prefix to filter the keys.
|
|
115
|
+
* @returns An AsyncGenerator that yields keys from the Redis database.
|
|
116
|
+
*/
|
|
117
|
+
async *yieldKeys(prefix) {
|
|
118
|
+
let pattern;
|
|
119
|
+
if (prefix) {
|
|
120
|
+
pattern = this._getPrefixedKey(prefix);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
pattern = this._getPrefixedKey("*");
|
|
124
|
+
}
|
|
125
|
+
let [cursor, batch] = await this.client.scan(0, {
|
|
126
|
+
match: pattern,
|
|
127
|
+
count: this.yieldKeysScanBatchSize,
|
|
128
|
+
});
|
|
129
|
+
for (const key of batch) {
|
|
130
|
+
yield this._getDeprefixedKey(key);
|
|
131
|
+
}
|
|
132
|
+
while (cursor !== 0) {
|
|
133
|
+
[cursor, batch] = await this.client.scan(cursor, {
|
|
134
|
+
match: pattern,
|
|
135
|
+
count: this.yieldKeysScanBatchSize,
|
|
136
|
+
});
|
|
137
|
+
for (const key of batch) {
|
|
138
|
+
yield this._getDeprefixedKey(key);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -46,6 +46,19 @@ class InMemoryDocstore extends index_js_1.Docstore {
|
|
|
46
46
|
this._docs.set(key, value);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
+
async mget(keys) {
|
|
50
|
+
return Promise.all(keys.map((key) => this.search(key)));
|
|
51
|
+
}
|
|
52
|
+
async mset(keyValuePairs) {
|
|
53
|
+
await Promise.all(keyValuePairs.map(([key, value]) => this.add({ [key]: value })));
|
|
54
|
+
}
|
|
55
|
+
async mdelete(_keys) {
|
|
56
|
+
throw new Error("Not implemented.");
|
|
57
|
+
}
|
|
58
|
+
// eslint-disable-next-line require-yield
|
|
59
|
+
async *yieldKeys(_prefix) {
|
|
60
|
+
throw new Error("Not implemented");
|
|
61
|
+
}
|
|
49
62
|
}
|
|
50
63
|
exports.InMemoryDocstore = InMemoryDocstore;
|
|
51
64
|
/**
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Document } from "../../document.js";
|
|
2
2
|
import { Docstore } from "../../schema/index.js";
|
|
3
|
+
import { BaseStoreInterface } from "../../schema/storage.js";
|
|
3
4
|
/**
|
|
4
5
|
* Class for storing and retrieving documents in memory asynchronously.
|
|
5
6
|
* Extends the Docstore class.
|
|
6
7
|
*/
|
|
7
|
-
export declare class InMemoryDocstore extends Docstore {
|
|
8
|
+
export declare class InMemoryDocstore extends Docstore implements BaseStoreInterface<string, Document> {
|
|
8
9
|
_docs: Map<string, Document>;
|
|
9
10
|
constructor(docs?: Map<string, Document>);
|
|
10
11
|
/**
|
|
@@ -19,6 +20,10 @@ export declare class InMemoryDocstore extends Docstore {
|
|
|
19
20
|
* @returns Void
|
|
20
21
|
*/
|
|
21
22
|
add(texts: Record<string, Document>): Promise<void>;
|
|
23
|
+
mget(keys: string[]): Promise<Document[]>;
|
|
24
|
+
mset(keyValuePairs: [string, Document][]): Promise<void>;
|
|
25
|
+
mdelete(_keys: string[]): Promise<void>;
|
|
26
|
+
yieldKeys(_prefix?: string): AsyncGenerator<string>;
|
|
22
27
|
}
|
|
23
28
|
/**
|
|
24
29
|
* Class for storing and retrieving documents in memory synchronously.
|
|
@@ -43,6 +43,19 @@ export class InMemoryDocstore extends Docstore {
|
|
|
43
43
|
this._docs.set(key, value);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
+
async mget(keys) {
|
|
47
|
+
return Promise.all(keys.map((key) => this.search(key)));
|
|
48
|
+
}
|
|
49
|
+
async mset(keyValuePairs) {
|
|
50
|
+
await Promise.all(keyValuePairs.map(([key, value]) => this.add({ [key]: value })));
|
|
51
|
+
}
|
|
52
|
+
async mdelete(_keys) {
|
|
53
|
+
throw new Error("Not implemented.");
|
|
54
|
+
}
|
|
55
|
+
// eslint-disable-next-line require-yield
|
|
56
|
+
async *yieldKeys(_prefix) {
|
|
57
|
+
throw new Error("Not implemented");
|
|
58
|
+
}
|
|
46
59
|
}
|
|
47
60
|
/**
|
|
48
61
|
* Class for storing and retrieving documents in memory synchronously.
|
|
@@ -293,7 +293,7 @@ function createRequest(config) {
|
|
|
293
293
|
}
|
|
294
294
|
// Some `fetch` implementations will override the Content-Type to text/plain
|
|
295
295
|
// when body is a string.
|
|
296
|
-
// See https://github.com/
|
|
296
|
+
// See https://github.com/langchain-ai/langchainjs/issues/1010
|
|
297
297
|
if (typeof options.body === "string") {
|
|
298
298
|
options.body = new TextEncoder().encode(options.body);
|
|
299
299
|
}
|
|
@@ -287,7 +287,7 @@ function createRequest(config) {
|
|
|
287
287
|
}
|
|
288
288
|
// Some `fetch` implementations will override the Content-Type to text/plain
|
|
289
289
|
// when body is a string.
|
|
290
|
-
// See https://github.com/
|
|
290
|
+
// See https://github.com/langchain-ai/langchainjs/issues/1010
|
|
291
291
|
if (typeof options.body === "string") {
|
|
292
292
|
options.body = new TextEncoder().encode(options.body);
|
|
293
293
|
}
|
package/dist/util/env.cjs
CHANGED
|
@@ -59,7 +59,7 @@ async function getRuntimeEnvironment() {
|
|
|
59
59
|
exports.getRuntimeEnvironment = getRuntimeEnvironment;
|
|
60
60
|
function getEnvironmentVariable(name) {
|
|
61
61
|
// Certain Deno setups will throw an error if you try to access environment variables
|
|
62
|
-
// https://github.com/
|
|
62
|
+
// https://github.com/langchain-ai/langchainjs/issues/1412
|
|
63
63
|
try {
|
|
64
64
|
return typeof process !== "undefined"
|
|
65
65
|
? // eslint-disable-next-line no-process-env
|