langchain
Version:
Typescript bindings for langchain
103 lines (102 loc) • 3.22 kB
JavaScript
import { Docstore } from "./base.js";
/**
* Class for storing and retrieving documents in memory asynchronously.
* Extends the Docstore class.
*/
export class InMemoryDocstore extends Docstore {
constructor(docs) {
super();
Object.defineProperty(this, "_docs", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
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) {
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) {
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) {
return Promise.all(keys.map((key) => this.search(key)));
}
async mset(keyValuePairs) {
await Promise.all(keyValuePairs.map(([key, value]) => this.add({ [key]: value })));
}
async mdelete(_keys) {
throw new Error("Not implemented.");
}
// eslint-disable-next-line require-yield
async *yieldKeys(_prefix) {
throw new Error("Not implemented");
}
}
/**
* Class for storing and retrieving documents in memory synchronously.
*/
export class SynchronousInMemoryDocstore {
constructor(docs) {
Object.defineProperty(this, "_docs", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
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) {
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) {
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);
}
}
}