langchain 0.0.167 → 0.0.169

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.
Files changed (90) hide show
  1. package/README.md +4 -4
  2. package/chat_models/cloudflare_workersai.cjs +1 -0
  3. package/chat_models/cloudflare_workersai.d.ts +1 -0
  4. package/chat_models/cloudflare_workersai.js +1 -0
  5. package/chat_models/fake.cjs +1 -0
  6. package/chat_models/fake.d.ts +1 -0
  7. package/chat_models/fake.js +1 -0
  8. package/chat_models/yandex.cjs +1 -0
  9. package/chat_models/yandex.d.ts +1 -0
  10. package/chat_models/yandex.js +1 -0
  11. package/dist/agents/chat/index.cjs +3 -2
  12. package/dist/agents/chat/index.d.ts +3 -0
  13. package/dist/agents/chat/index.js +3 -2
  14. package/dist/callbacks/handlers/llmonitor.cjs +21 -17
  15. package/dist/callbacks/handlers/llmonitor.js +21 -17
  16. package/dist/chat_models/cloudflare_workersai.cjs +145 -0
  17. package/dist/chat_models/cloudflare_workersai.d.ts +46 -0
  18. package/dist/chat_models/cloudflare_workersai.js +141 -0
  19. package/dist/chat_models/fake.cjs +101 -0
  20. package/dist/chat_models/fake.d.ts +36 -0
  21. package/dist/chat_models/fake.js +97 -0
  22. package/dist/chat_models/yandex.cjs +117 -0
  23. package/dist/chat_models/yandex.d.ts +16 -0
  24. package/dist/chat_models/yandex.js +113 -0
  25. package/dist/evaluation/comparison/prompt.d.ts +2 -2
  26. package/dist/experimental/chains/violation_of_expectations/index.cjs +5 -0
  27. package/dist/experimental/chains/violation_of_expectations/index.d.ts +1 -0
  28. package/dist/experimental/chains/violation_of_expectations/index.js +1 -0
  29. package/dist/experimental/chains/violation_of_expectations/types.cjs +49 -0
  30. package/dist/experimental/chains/violation_of_expectations/types.d.ts +69 -0
  31. package/dist/experimental/chains/violation_of_expectations/types.js +46 -0
  32. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.cjs +328 -0
  33. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.d.ts +148 -0
  34. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_chain.js +324 -0
  35. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.cjs +49 -0
  36. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.d.ts +5 -0
  37. package/dist/experimental/chains/violation_of_expectations/violation_of_expectations_prompt.js +46 -0
  38. package/dist/llms/cloudflare_workersai.cjs +124 -0
  39. package/dist/llms/cloudflare_workersai.d.ts +49 -0
  40. package/dist/llms/cloudflare_workersai.js +120 -0
  41. package/dist/llms/fake.cjs +82 -0
  42. package/dist/llms/fake.d.ts +31 -0
  43. package/dist/llms/fake.js +78 -0
  44. package/dist/llms/sagemaker_endpoint.cjs +9 -7
  45. package/dist/llms/sagemaker_endpoint.d.ts +3 -3
  46. package/dist/llms/sagemaker_endpoint.js +9 -7
  47. package/dist/load/import_constants.cjs +2 -0
  48. package/dist/load/import_constants.js +2 -0
  49. package/dist/load/import_map.cjs +8 -2
  50. package/dist/load/import_map.d.ts +6 -0
  51. package/dist/load/import_map.js +6 -0
  52. package/dist/retrievers/zep.cjs +29 -3
  53. package/dist/retrievers/zep.d.ts +14 -0
  54. package/dist/retrievers/zep.js +29 -3
  55. package/dist/util/axios-fetch-adapter.cjs +1 -1
  56. package/dist/util/axios-fetch-adapter.js +1 -1
  57. package/dist/util/env.cjs +1 -1
  58. package/dist/util/env.js +1 -1
  59. package/dist/util/event-source-parse.cjs +1 -1
  60. package/dist/util/event-source-parse.js +1 -1
  61. package/dist/vectorstores/closevector/common.cjs +128 -0
  62. package/dist/vectorstores/closevector/common.d.ts +82 -0
  63. package/dist/vectorstores/closevector/common.js +124 -0
  64. package/dist/vectorstores/closevector/node.cjs +109 -0
  65. package/dist/vectorstores/closevector/node.d.ts +83 -0
  66. package/dist/vectorstores/closevector/node.js +105 -0
  67. package/dist/vectorstores/closevector/web.cjs +109 -0
  68. package/dist/vectorstores/closevector/web.d.ts +80 -0
  69. package/dist/vectorstores/closevector/web.js +105 -0
  70. package/dist/vectorstores/faiss.cjs +38 -6
  71. package/dist/vectorstores/faiss.d.ts +14 -2
  72. package/dist/vectorstores/faiss.js +38 -6
  73. package/dist/vectorstores/weaviate.cjs +13 -2
  74. package/dist/vectorstores/weaviate.js +13 -2
  75. package/experimental/chains/violation_of_expectations.cjs +1 -0
  76. package/experimental/chains/violation_of_expectations.d.ts +1 -0
  77. package/experimental/chains/violation_of_expectations.js +1 -0
  78. package/llms/cloudflare_workersai.cjs +1 -0
  79. package/llms/cloudflare_workersai.d.ts +1 -0
  80. package/llms/cloudflare_workersai.js +1 -0
  81. package/llms/fake.cjs +1 -0
  82. package/llms/fake.d.ts +1 -0
  83. package/llms/fake.js +1 -0
  84. package/package.json +92 -13
  85. package/vectorstores/closevector/node.cjs +1 -0
  86. package/vectorstores/closevector/node.d.ts +1 -0
  87. package/vectorstores/closevector/node.js +1 -0
  88. package/vectorstores/closevector/web.cjs +1 -0
  89. package/vectorstores/closevector/web.d.ts +1 -0
  90. package/vectorstores/closevector/web.js +1 -0
@@ -0,0 +1,83 @@
1
+ import { CloseVectorHNSWNode, HierarchicalNSWT, CloseVectorHNSWLibArgs, CloseVectorCredentials } from "closevector-node";
2
+ import { CloseVector } from "./common.js";
3
+ import { Embeddings } from "../../embeddings/base.js";
4
+ import { Document } from "../../document.js";
5
+ /**
6
+ * package closevector-node is largely based on hnswlib.ts in the current folder with the following exceptions:
7
+ * 1. It uses a modified version of hnswlib-node to ensure the generated index can be loaded by closevector_web.ts.
8
+ * 2. It adds features to upload and download the index to/from the CDN provided by CloseVector.
9
+ *
10
+ * For more information, check out https://closevector-docs.getmegaportal.com/
11
+ */
12
+ /**
13
+ * Arguments for creating a CloseVectorNode instance, extending CloseVectorHNSWLibArgs.
14
+ */
15
+ export interface CloseVectorNodeArgs extends CloseVectorHNSWLibArgs<HierarchicalNSWT> {
16
+ instance?: CloseVectorHNSWNode;
17
+ }
18
+ /**
19
+ * Class that implements a vector store using Hierarchical Navigable Small
20
+ * World (HNSW) graphs. It extends the SaveableVectorStore class and
21
+ * provides methods for adding documents and vectors, performing
22
+ * similarity searches, and saving and loading the vector store.
23
+ */
24
+ export declare class CloseVectorNode extends CloseVector<CloseVectorHNSWNode> {
25
+ FilterType: (doc: Document) => boolean;
26
+ constructor(embeddings: Embeddings, args: CloseVectorNodeArgs, credentials?: CloseVectorCredentials);
27
+ /**
28
+ * Method to save the index to the CloseVector CDN.
29
+ * @param options
30
+ * @param options.description A description of the index.
31
+ * @param options.public Whether the index should be public or private. Defaults to false.
32
+ * @param options.uuid A UUID for the index. If not provided, a new index will be created.
33
+ * @param options.onProgress A callback function that will be called with the progress of the upload.
34
+ */
35
+ saveToCloud(options: Parameters<CloseVectorHNSWNode["saveToCloud"]>[0]): Promise<void>;
36
+ /**
37
+ * Method to load the index from the CloseVector CDN.
38
+ * @param options
39
+ * @param options.uuid The UUID of the index to be downloaded.
40
+ * @param options.credentials The credentials to be used by the CloseVectorNode instance.
41
+ * @param options.embeddings The embeddings to be used by the CloseVectorNode instance.
42
+ * @param options.onProgress A callback function that will be called with the progress of the download.
43
+ */
44
+ static loadFromCloud(options: Omit<Parameters<(typeof CloseVectorHNSWNode)["loadFromCloud"]>[0] & {
45
+ embeddings: Embeddings;
46
+ credentials: CloseVectorCredentials;
47
+ }, "accessKey" | "secret">): Promise<CloseVectorNode>;
48
+ /**
49
+ * Static method to load a vector store from a directory. It reads the
50
+ * HNSW index, the arguments, and the document store from the directory,
51
+ * then creates a new HNSWLib instance with these values.
52
+ * @param directory The directory from which to load the vector store.
53
+ * @param embeddings The embeddings to be used by the CloseVectorNode instance.
54
+ * @returns A Promise that resolves to a new CloseVectorNode instance.
55
+ */
56
+ static load(directory: string, embeddings: Embeddings, credentials?: CloseVectorCredentials): Promise<CloseVectorNode>;
57
+ /**
58
+ * Static method to create a new CloseVectorWeb instance from texts and metadata.
59
+ * It creates a new Document instance for each text and metadata, then
60
+ * calls the fromDocuments method to create the CloseVectorWeb instance.
61
+ * @param texts The texts to be used to create the documents.
62
+ * @param metadatas The metadata to be used to create the documents.
63
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
64
+ * @param args An optional configuration object for the CloseVectorWeb instance.
65
+ * @param credential An optional credential object for the CloseVector API.
66
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
67
+ */
68
+ static fromTexts(texts: string[], metadatas: object[] | object, embeddings: Embeddings, args?: Record<string, unknown>, credential?: CloseVectorCredentials): Promise<CloseVectorNode>;
69
+ /**
70
+ * Static method to create a new CloseVectorNode instance from documents. It
71
+ * creates a new CloseVectorNode instance, adds the documents to it, then returns
72
+ * the instance.
73
+ * @param docs The documents to be added to the HNSWLib instance.
74
+ * @param embeddings The embeddings to be used by the HNSWLib instance.
75
+ * @param args An optional configuration object for the HNSWLib instance.
76
+ * @param credentials An optional credential object for the CloseVector API.
77
+ * @returns A Promise that resolves to a new CloseVectorNode instance.
78
+ */
79
+ static fromDocuments(docs: Document[], embeddings: Embeddings, args?: Record<string, unknown>, credentials?: CloseVectorCredentials): Promise<CloseVectorNode>;
80
+ static imports(): Promise<{
81
+ HierarchicalNSW: typeof HierarchicalNSWT;
82
+ }>;
83
+ }
@@ -0,0 +1,105 @@
1
+ import { CloseVectorHNSWNode, } from "closevector-node";
2
+ import { CloseVector } from "./common.js";
3
+ /**
4
+ * Class that implements a vector store using Hierarchical Navigable Small
5
+ * World (HNSW) graphs. It extends the SaveableVectorStore class and
6
+ * provides methods for adding documents and vectors, performing
7
+ * similarity searches, and saving and loading the vector store.
8
+ */
9
+ export class CloseVectorNode extends CloseVector {
10
+ constructor(embeddings, args, credentials) {
11
+ super(embeddings, args, credentials);
12
+ if (args.instance) {
13
+ this.instance = args.instance;
14
+ }
15
+ else {
16
+ this.instance = new CloseVectorHNSWNode(embeddings, args);
17
+ }
18
+ if (this.credentials?.key) {
19
+ this.instance.accessKey = this.credentials.key;
20
+ }
21
+ if (this.credentials?.secret) {
22
+ this.instance.secret = this.credentials.secret;
23
+ }
24
+ }
25
+ /**
26
+ * Method to save the index to the CloseVector CDN.
27
+ * @param options
28
+ * @param options.description A description of the index.
29
+ * @param options.public Whether the index should be public or private. Defaults to false.
30
+ * @param options.uuid A UUID for the index. If not provided, a new index will be created.
31
+ * @param options.onProgress A callback function that will be called with the progress of the upload.
32
+ */
33
+ async saveToCloud(options) {
34
+ await this.instance.saveToCloud(options);
35
+ }
36
+ /**
37
+ * Method to load the index from the CloseVector CDN.
38
+ * @param options
39
+ * @param options.uuid The UUID of the index to be downloaded.
40
+ * @param options.credentials The credentials to be used by the CloseVectorNode instance.
41
+ * @param options.embeddings The embeddings to be used by the CloseVectorNode instance.
42
+ * @param options.onProgress A callback function that will be called with the progress of the download.
43
+ */
44
+ static async loadFromCloud(options) {
45
+ if (!options.credentials.key || !options.credentials.secret) {
46
+ throw new Error("key and secret must be provided");
47
+ }
48
+ const instance = await CloseVectorHNSWNode.loadFromCloud({
49
+ ...options,
50
+ accessKey: options.credentials.key,
51
+ secret: options.credentials.secret,
52
+ });
53
+ const vectorstore = new this(options.embeddings, instance.args, options.credentials);
54
+ return vectorstore;
55
+ }
56
+ /**
57
+ * Static method to load a vector store from a directory. It reads the
58
+ * HNSW index, the arguments, and the document store from the directory,
59
+ * then creates a new HNSWLib instance with these values.
60
+ * @param directory The directory from which to load the vector store.
61
+ * @param embeddings The embeddings to be used by the CloseVectorNode instance.
62
+ * @returns A Promise that resolves to a new CloseVectorNode instance.
63
+ */
64
+ static async load(directory, embeddings, credentials) {
65
+ const instance = await CloseVectorHNSWNode.load(directory, embeddings);
66
+ const vectorstore = new this(embeddings, instance.args, credentials);
67
+ return vectorstore;
68
+ }
69
+ /**
70
+ * Static method to create a new CloseVectorWeb instance from texts and metadata.
71
+ * It creates a new Document instance for each text and metadata, then
72
+ * calls the fromDocuments method to create the CloseVectorWeb instance.
73
+ * @param texts The texts to be used to create the documents.
74
+ * @param metadatas The metadata to be used to create the documents.
75
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
76
+ * @param args An optional configuration object for the CloseVectorWeb instance.
77
+ * @param credential An optional credential object for the CloseVector API.
78
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
79
+ */
80
+ static async fromTexts(texts, metadatas, embeddings, args, credential) {
81
+ const docs = CloseVector.textsToDocuments(texts, metadatas);
82
+ return await CloseVectorNode.fromDocuments(docs, embeddings, args, credential);
83
+ }
84
+ /**
85
+ * Static method to create a new CloseVectorNode instance from documents. It
86
+ * creates a new CloseVectorNode instance, adds the documents to it, then returns
87
+ * the instance.
88
+ * @param docs The documents to be added to the HNSWLib instance.
89
+ * @param embeddings The embeddings to be used by the HNSWLib instance.
90
+ * @param args An optional configuration object for the HNSWLib instance.
91
+ * @param credentials An optional credential object for the CloseVector API.
92
+ * @returns A Promise that resolves to a new CloseVectorNode instance.
93
+ */
94
+ static async fromDocuments(docs, embeddings, args, credentials) {
95
+ const _args = args || {
96
+ space: "cosine",
97
+ };
98
+ const instance = new this(embeddings, _args, credentials);
99
+ await instance.addDocuments(docs);
100
+ return instance;
101
+ }
102
+ static async imports() {
103
+ return CloseVectorHNSWNode.imports();
104
+ }
105
+ }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloseVectorWeb = void 0;
4
+ const closevector_web_1 = require("closevector-web");
5
+ const common_js_1 = require("./common.cjs");
6
+ const document_js_1 = require("../../document.cjs");
7
+ /**
8
+ * Class that implements a vector store using CloseVector, It extends the SaveableVectorStore class and
9
+ * provides methods for adding documents and vectors, performing
10
+ * similarity searches, and saving and loading the vector store.
11
+ */
12
+ class CloseVectorWeb extends common_js_1.CloseVector {
13
+ constructor(embeddings, args, credentials) {
14
+ super(embeddings, args, credentials);
15
+ if (args.instance) {
16
+ this.instance = args.instance;
17
+ }
18
+ else {
19
+ this.instance = new closevector_web_1.CloseVectorHNSWWeb(embeddings, args);
20
+ }
21
+ }
22
+ /**
23
+ * Method to save the index to the CloseVector CDN.
24
+ * @param options
25
+ * @param options.url the upload url generated by the CloseVector API: https://closevector-docs.getmegaportal.com/docs/api/http-api/file-url
26
+ * @param options.onProgress a callback function to track the upload progress
27
+ */
28
+ async saveToCloud(options) {
29
+ if (!this.instance.uuid && !options.uuid) {
30
+ throw new Error("No uuid provided");
31
+ }
32
+ if (!this.instance.uuid) {
33
+ this.instance._uuid = options.uuid;
34
+ }
35
+ await this.save(this.instance.uuid);
36
+ await this.instance.saveToCloud(options);
37
+ }
38
+ /**
39
+ * Method to load the index from the CloseVector CDN.
40
+ * @param options
41
+ * @param options.url the upload url generated by the CloseVector API: https://closevector-docs.getmegaportal.com/docs/api/http-api/file-url
42
+ * @param options.onProgress a callback function to track the upload progress
43
+ * @param options.uuid the uuid of the index to be downloaded
44
+ * @param options.embeddings the embeddings to be used by the CloseVectorWeb instance
45
+ */
46
+ static async loadFromCloud(options) {
47
+ const instance = await closevector_web_1.CloseVectorHNSWWeb.loadFromCloud(options);
48
+ const vectorstore = new this(options.embeddings, instance.args, options.credentials);
49
+ return vectorstore;
50
+ }
51
+ /**
52
+ * Static method to load a vector store from a directory. It reads the
53
+ * HNSW index, the arguments, and the document store from the directory,
54
+ * then creates a new CloseVectorWeb instance with these values.
55
+ * @param directory The directory from which to load the vector store.
56
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
57
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
58
+ */
59
+ static async load(directory, embeddings, credentials) {
60
+ const instance = await closevector_web_1.CloseVectorHNSWWeb.load(directory, embeddings);
61
+ const vectorstore = new this(embeddings, instance.args, credentials);
62
+ return vectorstore;
63
+ }
64
+ /**
65
+ * Static method to create a new CloseVectorWeb instance from texts and metadata.
66
+ * It creates a new Document instance for each text and metadata, then
67
+ * calls the fromDocuments method to create the CloseVectorWeb instance.
68
+ * @param texts The texts to be used to create the documents.
69
+ * @param metadatas The metadata to be used to create the documents.
70
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
71
+ * @param args An optional configuration object for the CloseVectorWeb instance.
72
+ * @param credential An optional credential object for the CloseVector API.
73
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
74
+ */
75
+ static async fromTexts(texts, metadatas, embeddings, args, credential) {
76
+ const docs = [];
77
+ for (let i = 0; i < texts.length; i += 1) {
78
+ const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
79
+ const newDoc = new document_js_1.Document({
80
+ pageContent: texts[i],
81
+ metadata,
82
+ });
83
+ docs.push(newDoc);
84
+ }
85
+ return await CloseVectorWeb.fromDocuments(docs, embeddings, args, credential);
86
+ }
87
+ /**
88
+ * Static method to create a new CloseVectorWeb instance from documents. It
89
+ * creates a new CloseVectorWeb instance, adds the documents to it, then returns
90
+ * the instance.
91
+ * @param docs The documents to be added to the CloseVectorWeb instance.
92
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
93
+ * @param args An optional configuration object for the CloseVectorWeb instance.
94
+ * @param credentials An optional credential object for the CloseVector API.
95
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
96
+ */
97
+ static async fromDocuments(docs, embeddings, args, credentials) {
98
+ const _args = args || {
99
+ space: "cosine",
100
+ };
101
+ const instance = new this(embeddings, _args, credentials);
102
+ await instance.addDocuments(docs);
103
+ return instance;
104
+ }
105
+ static async imports() {
106
+ return closevector_web_1.CloseVectorHNSWWeb.imports();
107
+ }
108
+ }
109
+ exports.CloseVectorWeb = CloseVectorWeb;
@@ -0,0 +1,80 @@
1
+ import { CloseVectorHNSWWeb, HierarchicalNSWT, CloseVectorHNSWLibArgs, CloseVectorCredentials, HnswlibModule } from "closevector-web";
2
+ import { CloseVector } from "./common.js";
3
+ import { Embeddings } from "../../embeddings/base.js";
4
+ import { Document } from "../../document.js";
5
+ /**
6
+ * package closevector-node is largely based on hnswlib.ts in the current folder with the following exceptions:
7
+ * 1. It uses a modified version of hnswlib-node to ensure the generated index can be loaded by closevector_web.ts.
8
+ * 2. It adds features to upload and download the index to/from the CDN provided by CloseVector.
9
+ *
10
+ * For more information, check out https://closevector-docs.getmegaportal.com/
11
+ */
12
+ /**
13
+ * Arguments for creating a CloseVectorWeb instance, extending CloseVectorHNSWLibArgs.
14
+ */
15
+ export interface CloseVectorWebArgs extends CloseVectorHNSWLibArgs<HierarchicalNSWT> {
16
+ instance?: CloseVectorHNSWWeb;
17
+ }
18
+ /**
19
+ * Class that implements a vector store using CloseVector, It extends the SaveableVectorStore class and
20
+ * provides methods for adding documents and vectors, performing
21
+ * similarity searches, and saving and loading the vector store.
22
+ */
23
+ export declare class CloseVectorWeb extends CloseVector<CloseVectorHNSWWeb> {
24
+ FilterType: (doc: Document) => boolean;
25
+ constructor(embeddings: Embeddings, args: CloseVectorWebArgs, credentials?: CloseVectorCredentials);
26
+ /**
27
+ * Method to save the index to the CloseVector CDN.
28
+ * @param options
29
+ * @param options.url the upload url generated by the CloseVector API: https://closevector-docs.getmegaportal.com/docs/api/http-api/file-url
30
+ * @param options.onProgress a callback function to track the upload progress
31
+ */
32
+ saveToCloud(options: Parameters<typeof this.instance.saveToCloud>[0] & {
33
+ uuid?: string;
34
+ }): Promise<void>;
35
+ /**
36
+ * Method to load the index from the CloseVector CDN.
37
+ * @param options
38
+ * @param options.url the upload url generated by the CloseVector API: https://closevector-docs.getmegaportal.com/docs/api/http-api/file-url
39
+ * @param options.onProgress a callback function to track the upload progress
40
+ * @param options.uuid the uuid of the index to be downloaded
41
+ * @param options.embeddings the embeddings to be used by the CloseVectorWeb instance
42
+ */
43
+ static loadFromCloud(options: Parameters<typeof CloseVectorHNSWWeb.loadFromCloud>[0] & {
44
+ embeddings: Embeddings;
45
+ credentials?: CloseVectorCredentials;
46
+ }): Promise<CloseVectorWeb>;
47
+ /**
48
+ * Static method to load a vector store from a directory. It reads the
49
+ * HNSW index, the arguments, and the document store from the directory,
50
+ * then creates a new CloseVectorWeb instance with these values.
51
+ * @param directory The directory from which to load the vector store.
52
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
53
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
54
+ */
55
+ static load(directory: string, embeddings: Embeddings, credentials?: CloseVectorCredentials): Promise<CloseVectorWeb>;
56
+ /**
57
+ * Static method to create a new CloseVectorWeb instance from texts and metadata.
58
+ * It creates a new Document instance for each text and metadata, then
59
+ * calls the fromDocuments method to create the CloseVectorWeb instance.
60
+ * @param texts The texts to be used to create the documents.
61
+ * @param metadatas The metadata to be used to create the documents.
62
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
63
+ * @param args An optional configuration object for the CloseVectorWeb instance.
64
+ * @param credential An optional credential object for the CloseVector API.
65
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
66
+ */
67
+ static fromTexts(texts: string[], metadatas: object[] | object, embeddings: Embeddings, args?: Record<string, unknown>, credential?: CloseVectorCredentials): Promise<CloseVectorWeb>;
68
+ /**
69
+ * Static method to create a new CloseVectorWeb instance from documents. It
70
+ * creates a new CloseVectorWeb instance, adds the documents to it, then returns
71
+ * the instance.
72
+ * @param docs The documents to be added to the CloseVectorWeb instance.
73
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
74
+ * @param args An optional configuration object for the CloseVectorWeb instance.
75
+ * @param credentials An optional credential object for the CloseVector API.
76
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
77
+ */
78
+ static fromDocuments(docs: Document[], embeddings: Embeddings, args?: Record<string, unknown>, credentials?: CloseVectorCredentials): Promise<CloseVectorWeb>;
79
+ static imports(): Promise<HnswlibModule>;
80
+ }
@@ -0,0 +1,105 @@
1
+ import { CloseVectorHNSWWeb, } from "closevector-web";
2
+ import { CloseVector } from "./common.js";
3
+ import { Document } from "../../document.js";
4
+ /**
5
+ * Class that implements a vector store using CloseVector, It extends the SaveableVectorStore class and
6
+ * provides methods for adding documents and vectors, performing
7
+ * similarity searches, and saving and loading the vector store.
8
+ */
9
+ export class CloseVectorWeb extends CloseVector {
10
+ constructor(embeddings, args, credentials) {
11
+ super(embeddings, args, credentials);
12
+ if (args.instance) {
13
+ this.instance = args.instance;
14
+ }
15
+ else {
16
+ this.instance = new CloseVectorHNSWWeb(embeddings, args);
17
+ }
18
+ }
19
+ /**
20
+ * Method to save the index to the CloseVector CDN.
21
+ * @param options
22
+ * @param options.url the upload url generated by the CloseVector API: https://closevector-docs.getmegaportal.com/docs/api/http-api/file-url
23
+ * @param options.onProgress a callback function to track the upload progress
24
+ */
25
+ async saveToCloud(options) {
26
+ if (!this.instance.uuid && !options.uuid) {
27
+ throw new Error("No uuid provided");
28
+ }
29
+ if (!this.instance.uuid) {
30
+ this.instance._uuid = options.uuid;
31
+ }
32
+ await this.save(this.instance.uuid);
33
+ await this.instance.saveToCloud(options);
34
+ }
35
+ /**
36
+ * Method to load the index from the CloseVector CDN.
37
+ * @param options
38
+ * @param options.url the upload url generated by the CloseVector API: https://closevector-docs.getmegaportal.com/docs/api/http-api/file-url
39
+ * @param options.onProgress a callback function to track the upload progress
40
+ * @param options.uuid the uuid of the index to be downloaded
41
+ * @param options.embeddings the embeddings to be used by the CloseVectorWeb instance
42
+ */
43
+ static async loadFromCloud(options) {
44
+ const instance = await CloseVectorHNSWWeb.loadFromCloud(options);
45
+ const vectorstore = new this(options.embeddings, instance.args, options.credentials);
46
+ return vectorstore;
47
+ }
48
+ /**
49
+ * Static method to load a vector store from a directory. It reads the
50
+ * HNSW index, the arguments, and the document store from the directory,
51
+ * then creates a new CloseVectorWeb instance with these values.
52
+ * @param directory The directory from which to load the vector store.
53
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
54
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
55
+ */
56
+ static async load(directory, embeddings, credentials) {
57
+ const instance = await CloseVectorHNSWWeb.load(directory, embeddings);
58
+ const vectorstore = new this(embeddings, instance.args, credentials);
59
+ return vectorstore;
60
+ }
61
+ /**
62
+ * Static method to create a new CloseVectorWeb instance from texts and metadata.
63
+ * It creates a new Document instance for each text and metadata, then
64
+ * calls the fromDocuments method to create the CloseVectorWeb instance.
65
+ * @param texts The texts to be used to create the documents.
66
+ * @param metadatas The metadata to be used to create the documents.
67
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
68
+ * @param args An optional configuration object for the CloseVectorWeb instance.
69
+ * @param credential An optional credential object for the CloseVector API.
70
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
71
+ */
72
+ static async fromTexts(texts, metadatas, embeddings, args, credential) {
73
+ const docs = [];
74
+ for (let i = 0; i < texts.length; i += 1) {
75
+ const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
76
+ const newDoc = new Document({
77
+ pageContent: texts[i],
78
+ metadata,
79
+ });
80
+ docs.push(newDoc);
81
+ }
82
+ return await CloseVectorWeb.fromDocuments(docs, embeddings, args, credential);
83
+ }
84
+ /**
85
+ * Static method to create a new CloseVectorWeb instance from documents. It
86
+ * creates a new CloseVectorWeb instance, adds the documents to it, then returns
87
+ * the instance.
88
+ * @param docs The documents to be added to the CloseVectorWeb instance.
89
+ * @param embeddings The embeddings to be used by the CloseVectorWeb instance.
90
+ * @param args An optional configuration object for the CloseVectorWeb instance.
91
+ * @param credentials An optional credential object for the CloseVector API.
92
+ * @returns A Promise that resolves to a new CloseVectorWeb instance.
93
+ */
94
+ static async fromDocuments(docs, embeddings, args, credentials) {
95
+ const _args = args || {
96
+ space: "cosine",
97
+ };
98
+ const instance = new this(embeddings, _args, credentials);
99
+ await instance.addDocuments(docs);
100
+ return instance;
101
+ }
102
+ static async imports() {
103
+ return CloseVectorHNSWWeb.imports();
104
+ }
105
+ }
@@ -80,9 +80,9 @@ class FaissStore extends base_js_1.SaveableVectorStore {
80
80
  * @param documents An array of Document objects.
81
81
  * @returns A Promise that resolves when the documents have been added.
82
82
  */
83
- async addDocuments(documents) {
83
+ async addDocuments(documents, options) {
84
84
  const texts = documents.map(({ pageContent }) => pageContent);
85
- return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
85
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents, options);
86
86
  }
87
87
  get index() {
88
88
  if (!this._index) {
@@ -100,7 +100,7 @@ class FaissStore extends base_js_1.SaveableVectorStore {
100
100
  * @param documents An array of Document objects corresponding to the vectors.
101
101
  * @returns A Promise that resolves with an array of document IDs when the vectors and documents have been added.
102
102
  */
103
- async addVectors(vectors, documents) {
103
+ async addVectors(vectors, documents, options) {
104
104
  if (vectors.length === 0) {
105
105
  return [];
106
106
  }
@@ -117,10 +117,9 @@ class FaissStore extends base_js_1.SaveableVectorStore {
117
117
  throw new Error(`Vectors must have the same length as the number of dimensions (${d})`);
118
118
  }
119
119
  const docstoreSize = this.index.ntotal();
120
- const documentIds = [];
120
+ const documentIds = options?.ids ?? documents.map(() => uuid.v4());
121
121
  for (let i = 0; i < vectors.length; i += 1) {
122
- const documentId = uuid.v4();
123
- documentIds.push(documentId);
122
+ const documentId = documentIds[i];
124
123
  const id = docstoreSize + i;
125
124
  this.index.add(vectors[i]);
126
125
  this._mapping[id] = documentId;
@@ -169,6 +168,39 @@ class FaissStore extends base_js_1.SaveableVectorStore {
169
168
  ])),
170
169
  ]);
171
170
  }
171
+ /**
172
+ * Method to delete documents.
173
+ * @param params Object containing the IDs of the documents to delete.
174
+ * @returns A promise that resolves when the deletion is complete.
175
+ */
176
+ async delete(params) {
177
+ const documentIds = params.ids;
178
+ if (documentIds == null) {
179
+ throw new Error("No documentIds provided to delete.");
180
+ }
181
+ const mappings = new Map(Object.entries(this._mapping).map(([key, value]) => [
182
+ parseInt(key, 10),
183
+ value,
184
+ ]));
185
+ const reversedMappings = new Map(Array.from(mappings, (entry) => [entry[1], entry[0]]));
186
+ const missingIds = new Set(documentIds.filter((id) => !reversedMappings.has(id)));
187
+ if (missingIds.size > 0) {
188
+ throw new Error(`Some specified documentIds do not exist in the current store. DocumentIds not found: ${Array.from(missingIds).join(", ")}`);
189
+ }
190
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
191
+ const indexIdToDelete = documentIds.map((id) => reversedMappings.get(id));
192
+ // remove from index
193
+ this.index.removeIds(indexIdToDelete);
194
+ // remove from docstore
195
+ documentIds.forEach((id) => {
196
+ this.docstore._docs.delete(id);
197
+ });
198
+ // remove from mappings
199
+ indexIdToDelete.forEach((id) => {
200
+ mappings.delete(id);
201
+ });
202
+ this._mapping = { ...Array.from(mappings.values()) };
203
+ }
172
204
  /**
173
205
  * Merges the current FaissStore with another FaissStore.
174
206
  * @param targetIndex The FaissStore to merge with.
@@ -32,7 +32,9 @@ export declare class FaissStore extends SaveableVectorStore {
32
32
  * @param documents An array of Document objects.
33
33
  * @returns A Promise that resolves when the documents have been added.
34
34
  */
35
- addDocuments(documents: Document[]): Promise<string[]>;
35
+ addDocuments(documents: Document[], options?: {
36
+ ids?: string[];
37
+ }): Promise<string[]>;
36
38
  get index(): IndexFlatL2;
37
39
  private set index(value);
38
40
  /**
@@ -42,7 +44,9 @@ export declare class FaissStore extends SaveableVectorStore {
42
44
  * @param documents An array of Document objects corresponding to the vectors.
43
45
  * @returns A Promise that resolves with an array of document IDs when the vectors and documents have been added.
44
46
  */
45
- addVectors(vectors: number[][], documents: Document[]): Promise<string[]>;
47
+ addVectors(vectors: number[][], documents: Document[], options?: {
48
+ ids?: string[];
49
+ }): Promise<string[]>;
46
50
  /**
47
51
  * Performs a similarity search in the vector store using a query vector
48
52
  * and returns the top k results along with their scores.
@@ -57,6 +61,14 @@ export declare class FaissStore extends SaveableVectorStore {
57
61
  * @returns A Promise that resolves when the state has been saved.
58
62
  */
59
63
  save(directory: string): Promise<void>;
64
+ /**
65
+ * Method to delete documents.
66
+ * @param params Object containing the IDs of the documents to delete.
67
+ * @returns A promise that resolves when the deletion is complete.
68
+ */
69
+ delete(params: {
70
+ ids: string[];
71
+ }): Promise<void>;
60
72
  /**
61
73
  * Merges the current FaissStore with another FaissStore.
62
74
  * @param targetIndex The FaissStore to merge with.
@@ -54,9 +54,9 @@ export class FaissStore extends SaveableVectorStore {
54
54
  * @param documents An array of Document objects.
55
55
  * @returns A Promise that resolves when the documents have been added.
56
56
  */
57
- async addDocuments(documents) {
57
+ async addDocuments(documents, options) {
58
58
  const texts = documents.map(({ pageContent }) => pageContent);
59
- return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
59
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents, options);
60
60
  }
61
61
  get index() {
62
62
  if (!this._index) {
@@ -74,7 +74,7 @@ export class FaissStore extends SaveableVectorStore {
74
74
  * @param documents An array of Document objects corresponding to the vectors.
75
75
  * @returns A Promise that resolves with an array of document IDs when the vectors and documents have been added.
76
76
  */
77
- async addVectors(vectors, documents) {
77
+ async addVectors(vectors, documents, options) {
78
78
  if (vectors.length === 0) {
79
79
  return [];
80
80
  }
@@ -91,10 +91,9 @@ export class FaissStore extends SaveableVectorStore {
91
91
  throw new Error(`Vectors must have the same length as the number of dimensions (${d})`);
92
92
  }
93
93
  const docstoreSize = this.index.ntotal();
94
- const documentIds = [];
94
+ const documentIds = options?.ids ?? documents.map(() => uuid.v4());
95
95
  for (let i = 0; i < vectors.length; i += 1) {
96
- const documentId = uuid.v4();
97
- documentIds.push(documentId);
96
+ const documentId = documentIds[i];
98
97
  const id = docstoreSize + i;
99
98
  this.index.add(vectors[i]);
100
99
  this._mapping[id] = documentId;
@@ -143,6 +142,39 @@ export class FaissStore extends SaveableVectorStore {
143
142
  ])),
144
143
  ]);
145
144
  }
145
+ /**
146
+ * Method to delete documents.
147
+ * @param params Object containing the IDs of the documents to delete.
148
+ * @returns A promise that resolves when the deletion is complete.
149
+ */
150
+ async delete(params) {
151
+ const documentIds = params.ids;
152
+ if (documentIds == null) {
153
+ throw new Error("No documentIds provided to delete.");
154
+ }
155
+ const mappings = new Map(Object.entries(this._mapping).map(([key, value]) => [
156
+ parseInt(key, 10),
157
+ value,
158
+ ]));
159
+ const reversedMappings = new Map(Array.from(mappings, (entry) => [entry[1], entry[0]]));
160
+ const missingIds = new Set(documentIds.filter((id) => !reversedMappings.has(id)));
161
+ if (missingIds.size > 0) {
162
+ throw new Error(`Some specified documentIds do not exist in the current store. DocumentIds not found: ${Array.from(missingIds).join(", ")}`);
163
+ }
164
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165
+ const indexIdToDelete = documentIds.map((id) => reversedMappings.get(id));
166
+ // remove from index
167
+ this.index.removeIds(indexIdToDelete);
168
+ // remove from docstore
169
+ documentIds.forEach((id) => {
170
+ this.docstore._docs.delete(id);
171
+ });
172
+ // remove from mappings
173
+ indexIdToDelete.forEach((id) => {
174
+ mappings.delete(id);
175
+ });
176
+ this._mapping = { ...Array.from(mappings.values()) };
177
+ }
146
178
  /**
147
179
  * Merges the current FaissStore with another FaissStore.
148
180
  * @param targetIndex The FaissStore to merge with.