-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
in_memory.ts
113 lines (97 loc) · 3.09 KB
/
in_memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Document } from "@langchain/core/documents";
import { BaseStoreInterface } from "@langchain/core/stores";
import { Docstore } from "./base.js";
/**
* Class for storing and retrieving documents in memory asynchronously.
* Extends the Docstore class.
*/
export class InMemoryDocstore
extends Docstore
implements BaseStoreInterface<string, Document>
{
_docs: Map<string, Document>;
constructor(docs?: Map<string, Document>) {
super();
this._docs = docs ?? new Map();
}
/**
* Searches for a document in the store based on its ID.
* @param search The ID of the document to search for.
* @returns The document with the given ID.
*/
async search(search: string): Promise<Document> {
const result = this._docs.get(search);
if (!result) {
throw new Error(`ID ${search} not found.`);
} else {
return result;
}
}
/**
* Adds new documents to the store.
* @param texts An object where the keys are document IDs and the values are the documents themselves.
* @returns Void
*/
async add(texts: Record<string, Document>): Promise<void> {
const keys = [...this._docs.keys()];
const overlapping = Object.keys(texts).filter((x) => keys.includes(x));
if (overlapping.length > 0) {
throw new Error(`Tried to add ids that already exist: ${overlapping}`);
}
for (const [key, value] of Object.entries(texts)) {
this._docs.set(key, value);
}
}
async mget(keys: string[]): Promise<Document[]> {
return Promise.all(keys.map((key) => this.search(key)));
}
async mset(keyValuePairs: [string, Document][]): Promise<void> {
await Promise.all(
keyValuePairs.map(([key, value]) => this.add({ [key]: value }))
);
}
async mdelete(_keys: string[]): Promise<void> {
throw new Error("Not implemented.");
}
// eslint-disable-next-line require-yield
async *yieldKeys(_prefix?: string): AsyncGenerator<string> {
throw new Error("Not implemented");
}
}
/**
* Class for storing and retrieving documents in memory synchronously.
*/
export class SynchronousInMemoryDocstore {
_docs: Map<string, Document>;
constructor(docs?: Map<string, Document>) {
this._docs = docs ?? new Map();
}
/**
* Searches for a document in the store based on its ID.
* @param search The ID of the document to search for.
* @returns The document with the given ID.
*/
search(search: string): Document {
const result = this._docs.get(search);
if (!result) {
throw new Error(`ID ${search} not found.`);
} else {
return result;
}
}
/**
* Adds new documents to the store.
* @param texts An object where the keys are document IDs and the values are the documents themselves.
* @returns Void
*/
add(texts: Record<string, Document>): void {
const keys = [...this._docs.keys()];
const overlapping = Object.keys(texts).filter((x) => keys.includes(x));
if (overlapping.length > 0) {
throw new Error(`Tried to add ids that already exist: ${overlapping}`);
}
for (const [key, value] of Object.entries(texts)) {
this._docs.set(key, value);
}
}
}