langchain 0.2.16 → 0.2.17

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.
@@ -6,9 +6,114 @@ const documents_1 = require("@langchain/core/documents");
6
6
  const similarities_js_1 = require("../util/ml-distance/similarities.cjs");
7
7
  const math_js_1 = require("../util/math.cjs");
8
8
  /**
9
- * Class that extends `VectorStore` to store vectors in memory. Provides
10
- * methods for adding documents, performing similarity searches, and
11
- * creating instances from texts, documents, or an existing index.
9
+ * In-memory, ephemeral vector store.
10
+ *
11
+ * Setup:
12
+ * Install `langchain`:
13
+ *
14
+ * ```bash
15
+ * npm install langchain
16
+ * ```
17
+ *
18
+ * ## [Constructor args](https://api.js.langchain.com/classes/langchain.vectorstores_memory.MemoryVectorStore.html#constructor)
19
+ *
20
+ * <details open>
21
+ * <summary><strong>Instantiate</strong></summary>
22
+ *
23
+ * ```typescript
24
+ * import { MemoryVectorStore } from 'langchain/vectorstores/memory';
25
+ * // Or other embeddings
26
+ * import { OpenAIEmbeddings } from '@langchain/openai';
27
+ *
28
+ * const embeddings = new OpenAIEmbeddings({
29
+ * model: "text-embedding-3-small",
30
+ * });
31
+ *
32
+ * const vectorStore = new MemoryVectorStore(embeddings);
33
+ * ```
34
+ * </details>
35
+ *
36
+ * <br />
37
+ *
38
+ * <details>
39
+ * <summary><strong>Add documents</strong></summary>
40
+ *
41
+ * ```typescript
42
+ * import type { Document } from '@langchain/core/documents';
43
+ *
44
+ * const document1 = { pageContent: "foo", metadata: { baz: "bar" } };
45
+ * const document2 = { pageContent: "thud", metadata: { bar: "baz" } };
46
+ * const document3 = { pageContent: "i will be deleted :(", metadata: {} };
47
+ *
48
+ * const documents: Document[] = [document1, document2, document3];
49
+ *
50
+ * await vectorStore.addDocuments(documents);
51
+ * ```
52
+ * </details>
53
+ *
54
+ * <br />
55
+ *
56
+ * <details>
57
+ * <summary><strong>Similarity search</strong></summary>
58
+ *
59
+ * ```typescript
60
+ * const results = await vectorStore.similaritySearch("thud", 1);
61
+ * for (const doc of results) {
62
+ * console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
63
+ * }
64
+ * // Output: * thud [{"baz":"bar"}]
65
+ * ```
66
+ * </details>
67
+ *
68
+ * <br />
69
+ *
70
+ *
71
+ * <details>
72
+ * <summary><strong>Similarity search with filter</strong></summary>
73
+ *
74
+ * ```typescript
75
+ * const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, { baz: "bar" });
76
+ *
77
+ * for (const doc of resultsWithFilter) {
78
+ * console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
79
+ * }
80
+ * // Output: * foo [{"baz":"bar"}]
81
+ * ```
82
+ * </details>
83
+ *
84
+ * <br />
85
+ *
86
+ *
87
+ * <details>
88
+ * <summary><strong>Similarity search with score</strong></summary>
89
+ *
90
+ * ```typescript
91
+ * const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1);
92
+ * for (const [doc, score] of resultsWithScore) {
93
+ * console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
94
+ * }
95
+ * // Output: * [SIM=0.000000] qux [{"bar":"baz","baz":"bar"}]
96
+ * ```
97
+ * </details>
98
+ *
99
+ * <br />
100
+ *
101
+ * <details>
102
+ * <summary><strong>As a retriever</strong></summary>
103
+ *
104
+ * ```typescript
105
+ * const retriever = vectorStore.asRetriever({
106
+ * searchType: "mmr", // Leave blank for standard similarity search
107
+ * k: 1,
108
+ * });
109
+ * const resultAsRetriever = await retriever.invoke("thud");
110
+ * console.log(resultAsRetriever);
111
+ *
112
+ * // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })]
113
+ * ```
114
+ * </details>
115
+ *
116
+ * <br />
12
117
  */
13
118
  class MemoryVectorStore extends vectorstores_1.VectorStore {
14
119
  _vectorstoreType() {
@@ -54,6 +159,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
54
159
  content: documents[idx].pageContent,
55
160
  embedding,
56
161
  metadata: documents[idx].metadata,
162
+ id: documents[idx].id,
57
163
  }));
58
164
  this.memoryVectors = this.memoryVectors.concat(memoryVectors);
59
165
  }
@@ -65,6 +171,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
65
171
  const doc = new documents_1.Document({
66
172
  metadata: memoryVector.metadata,
67
173
  pageContent: memoryVector.content,
174
+ id: memoryVector.id,
68
175
  });
69
176
  return filter(doc);
70
177
  };
@@ -76,6 +183,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
76
183
  metadata: vector.metadata,
77
184
  content: vector.content,
78
185
  embedding: vector.embedding,
186
+ id: vector.id,
79
187
  }))
80
188
  .sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
81
189
  .slice(0, k);
@@ -96,6 +204,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
96
204
  new documents_1.Document({
97
205
  metadata: search.metadata,
98
206
  pageContent: search.content,
207
+ id: search.id,
99
208
  }),
100
209
  search.similarity,
101
210
  ]);
@@ -109,6 +218,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
109
218
  return mmrIndexes.map((idx) => new documents_1.Document({
110
219
  metadata: searches[idx].metadata,
111
220
  pageContent: searches[idx].content,
221
+ id: searches[idx].id,
112
222
  }));
113
223
  }
114
224
  /**
@@ -11,6 +11,7 @@ interface MemoryVector {
11
11
  content: string;
12
12
  embedding: number[];
13
13
  metadata: Record<string, any>;
14
+ id?: string;
14
15
  }
15
16
  /**
16
17
  * Interface for the arguments that can be passed to the
@@ -21,9 +22,114 @@ export interface MemoryVectorStoreArgs {
21
22
  similarity?: typeof cosine;
22
23
  }
23
24
  /**
24
- * Class that extends `VectorStore` to store vectors in memory. Provides
25
- * methods for adding documents, performing similarity searches, and
26
- * creating instances from texts, documents, or an existing index.
25
+ * In-memory, ephemeral vector store.
26
+ *
27
+ * Setup:
28
+ * Install `langchain`:
29
+ *
30
+ * ```bash
31
+ * npm install langchain
32
+ * ```
33
+ *
34
+ * ## [Constructor args](https://api.js.langchain.com/classes/langchain.vectorstores_memory.MemoryVectorStore.html#constructor)
35
+ *
36
+ * <details open>
37
+ * <summary><strong>Instantiate</strong></summary>
38
+ *
39
+ * ```typescript
40
+ * import { MemoryVectorStore } from 'langchain/vectorstores/memory';
41
+ * // Or other embeddings
42
+ * import { OpenAIEmbeddings } from '@langchain/openai';
43
+ *
44
+ * const embeddings = new OpenAIEmbeddings({
45
+ * model: "text-embedding-3-small",
46
+ * });
47
+ *
48
+ * const vectorStore = new MemoryVectorStore(embeddings);
49
+ * ```
50
+ * </details>
51
+ *
52
+ * <br />
53
+ *
54
+ * <details>
55
+ * <summary><strong>Add documents</strong></summary>
56
+ *
57
+ * ```typescript
58
+ * import type { Document } from '@langchain/core/documents';
59
+ *
60
+ * const document1 = { pageContent: "foo", metadata: { baz: "bar" } };
61
+ * const document2 = { pageContent: "thud", metadata: { bar: "baz" } };
62
+ * const document3 = { pageContent: "i will be deleted :(", metadata: {} };
63
+ *
64
+ * const documents: Document[] = [document1, document2, document3];
65
+ *
66
+ * await vectorStore.addDocuments(documents);
67
+ * ```
68
+ * </details>
69
+ *
70
+ * <br />
71
+ *
72
+ * <details>
73
+ * <summary><strong>Similarity search</strong></summary>
74
+ *
75
+ * ```typescript
76
+ * const results = await vectorStore.similaritySearch("thud", 1);
77
+ * for (const doc of results) {
78
+ * console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
79
+ * }
80
+ * // Output: * thud [{"baz":"bar"}]
81
+ * ```
82
+ * </details>
83
+ *
84
+ * <br />
85
+ *
86
+ *
87
+ * <details>
88
+ * <summary><strong>Similarity search with filter</strong></summary>
89
+ *
90
+ * ```typescript
91
+ * const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, { baz: "bar" });
92
+ *
93
+ * for (const doc of resultsWithFilter) {
94
+ * console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
95
+ * }
96
+ * // Output: * foo [{"baz":"bar"}]
97
+ * ```
98
+ * </details>
99
+ *
100
+ * <br />
101
+ *
102
+ *
103
+ * <details>
104
+ * <summary><strong>Similarity search with score</strong></summary>
105
+ *
106
+ * ```typescript
107
+ * const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1);
108
+ * for (const [doc, score] of resultsWithScore) {
109
+ * console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
110
+ * }
111
+ * // Output: * [SIM=0.000000] qux [{"bar":"baz","baz":"bar"}]
112
+ * ```
113
+ * </details>
114
+ *
115
+ * <br />
116
+ *
117
+ * <details>
118
+ * <summary><strong>As a retriever</strong></summary>
119
+ *
120
+ * ```typescript
121
+ * const retriever = vectorStore.asRetriever({
122
+ * searchType: "mmr", // Leave blank for standard similarity search
123
+ * k: 1,
124
+ * });
125
+ * const resultAsRetriever = await retriever.invoke("thud");
126
+ * console.log(resultAsRetriever);
127
+ *
128
+ * // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })]
129
+ * ```
130
+ * </details>
131
+ *
132
+ * <br />
27
133
  */
28
134
  export declare class MemoryVectorStore extends VectorStore {
29
135
  FilterType: (doc: Document) => boolean;
@@ -54,6 +160,7 @@ export declare class MemoryVectorStore extends VectorStore {
54
160
  metadata: Record<string, any>;
55
161
  content: string;
56
162
  embedding: number[];
163
+ id: string | undefined;
57
164
  }[]>;
58
165
  /**
59
166
  * Method to perform a similarity search in the memory vector store. It
@@ -3,9 +3,114 @@ import { Document } from "@langchain/core/documents";
3
3
  import { cosine } from "../util/ml-distance/similarities.js";
4
4
  import { maximalMarginalRelevance } from "../util/math.js";
5
5
  /**
6
- * Class that extends `VectorStore` to store vectors in memory. Provides
7
- * methods for adding documents, performing similarity searches, and
8
- * creating instances from texts, documents, or an existing index.
6
+ * In-memory, ephemeral vector store.
7
+ *
8
+ * Setup:
9
+ * Install `langchain`:
10
+ *
11
+ * ```bash
12
+ * npm install langchain
13
+ * ```
14
+ *
15
+ * ## [Constructor args](https://api.js.langchain.com/classes/langchain.vectorstores_memory.MemoryVectorStore.html#constructor)
16
+ *
17
+ * <details open>
18
+ * <summary><strong>Instantiate</strong></summary>
19
+ *
20
+ * ```typescript
21
+ * import { MemoryVectorStore } from 'langchain/vectorstores/memory';
22
+ * // Or other embeddings
23
+ * import { OpenAIEmbeddings } from '@langchain/openai';
24
+ *
25
+ * const embeddings = new OpenAIEmbeddings({
26
+ * model: "text-embedding-3-small",
27
+ * });
28
+ *
29
+ * const vectorStore = new MemoryVectorStore(embeddings);
30
+ * ```
31
+ * </details>
32
+ *
33
+ * <br />
34
+ *
35
+ * <details>
36
+ * <summary><strong>Add documents</strong></summary>
37
+ *
38
+ * ```typescript
39
+ * import type { Document } from '@langchain/core/documents';
40
+ *
41
+ * const document1 = { pageContent: "foo", metadata: { baz: "bar" } };
42
+ * const document2 = { pageContent: "thud", metadata: { bar: "baz" } };
43
+ * const document3 = { pageContent: "i will be deleted :(", metadata: {} };
44
+ *
45
+ * const documents: Document[] = [document1, document2, document3];
46
+ *
47
+ * await vectorStore.addDocuments(documents);
48
+ * ```
49
+ * </details>
50
+ *
51
+ * <br />
52
+ *
53
+ * <details>
54
+ * <summary><strong>Similarity search</strong></summary>
55
+ *
56
+ * ```typescript
57
+ * const results = await vectorStore.similaritySearch("thud", 1);
58
+ * for (const doc of results) {
59
+ * console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
60
+ * }
61
+ * // Output: * thud [{"baz":"bar"}]
62
+ * ```
63
+ * </details>
64
+ *
65
+ * <br />
66
+ *
67
+ *
68
+ * <details>
69
+ * <summary><strong>Similarity search with filter</strong></summary>
70
+ *
71
+ * ```typescript
72
+ * const resultsWithFilter = await vectorStore.similaritySearch("thud", 1, { baz: "bar" });
73
+ *
74
+ * for (const doc of resultsWithFilter) {
75
+ * console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
76
+ * }
77
+ * // Output: * foo [{"baz":"bar"}]
78
+ * ```
79
+ * </details>
80
+ *
81
+ * <br />
82
+ *
83
+ *
84
+ * <details>
85
+ * <summary><strong>Similarity search with score</strong></summary>
86
+ *
87
+ * ```typescript
88
+ * const resultsWithScore = await vectorStore.similaritySearchWithScore("qux", 1);
89
+ * for (const [doc, score] of resultsWithScore) {
90
+ * console.log(`* [SIM=${score.toFixed(6)}] ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
91
+ * }
92
+ * // Output: * [SIM=0.000000] qux [{"bar":"baz","baz":"bar"}]
93
+ * ```
94
+ * </details>
95
+ *
96
+ * <br />
97
+ *
98
+ * <details>
99
+ * <summary><strong>As a retriever</strong></summary>
100
+ *
101
+ * ```typescript
102
+ * const retriever = vectorStore.asRetriever({
103
+ * searchType: "mmr", // Leave blank for standard similarity search
104
+ * k: 1,
105
+ * });
106
+ * const resultAsRetriever = await retriever.invoke("thud");
107
+ * console.log(resultAsRetriever);
108
+ *
109
+ * // Output: [Document({ metadata: { "baz":"bar" }, pageContent: "thud" })]
110
+ * ```
111
+ * </details>
112
+ *
113
+ * <br />
9
114
  */
10
115
  export class MemoryVectorStore extends VectorStore {
11
116
  _vectorstoreType() {
@@ -51,6 +156,7 @@ export class MemoryVectorStore extends VectorStore {
51
156
  content: documents[idx].pageContent,
52
157
  embedding,
53
158
  metadata: documents[idx].metadata,
159
+ id: documents[idx].id,
54
160
  }));
55
161
  this.memoryVectors = this.memoryVectors.concat(memoryVectors);
56
162
  }
@@ -62,6 +168,7 @@ export class MemoryVectorStore extends VectorStore {
62
168
  const doc = new Document({
63
169
  metadata: memoryVector.metadata,
64
170
  pageContent: memoryVector.content,
171
+ id: memoryVector.id,
65
172
  });
66
173
  return filter(doc);
67
174
  };
@@ -73,6 +180,7 @@ export class MemoryVectorStore extends VectorStore {
73
180
  metadata: vector.metadata,
74
181
  content: vector.content,
75
182
  embedding: vector.embedding,
183
+ id: vector.id,
76
184
  }))
77
185
  .sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
78
186
  .slice(0, k);
@@ -93,6 +201,7 @@ export class MemoryVectorStore extends VectorStore {
93
201
  new Document({
94
202
  metadata: search.metadata,
95
203
  pageContent: search.content,
204
+ id: search.id,
96
205
  }),
97
206
  search.similarity,
98
207
  ]);
@@ -106,6 +215,7 @@ export class MemoryVectorStore extends VectorStore {
106
215
  return mmrIndexes.map((idx) => new Document({
107
216
  metadata: searches[idx].metadata,
108
217
  pageContent: searches[idx].content,
218
+ id: searches[idx].id,
109
219
  }));
110
220
  }
111
221
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.2.16",
3
+ "version": "0.2.17",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -574,12 +574,7 @@
574
574
  "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/langchain/",
575
575
  "scripts": {
576
576
  "build": "yarn turbo:command build:internal --filter=langchain",
577
- "build:internal": "yarn lc_build_v2 --create-entrypoints --pre --tree-shaking --gen-maps",
578
- "build:deps": "yarn run turbo:command build --filter=@langchain/openai --filter=@langchain/textsplitters --filter=@langchain/cohere --concurrency=1",
579
- "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests",
580
- "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs",
581
- "build:watch": "yarn create-entrypoints && tsc --outDir dist/ --watch",
582
- "build:scripts": "yarn create-entrypoints && yarn check-tree-shaking",
577
+ "build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking --gen-maps",
583
578
  "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
584
579
  "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
585
580
  "lint": "yarn lint:eslint && yarn lint:dpdm",
@@ -593,10 +588,7 @@
593
588
  "test:integration": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
594
589
  "test:single": "yarn run build:deps && NODE_OPTIONS=--experimental-vm-modules yarn run jest --config jest.config.cjs --testTimeout 100000",
595
590
  "format": "prettier --config .prettierrc --write \"src\"",
596
- "format:check": "prettier --config .prettierrc --check \"src\"",
597
- "move-cjs-to-dist": "yarn lc-build --config ./langchain.config.js --move-cjs-dist",
598
- "create-entrypoints": "yarn lc-build --config ./langchain.config.js --create-entrypoints",
599
- "check-tree-shaking": "yarn lc-build --config ./langchain.config.js --tree-shaking"
591
+ "format:check": "prettier --config .prettierrc --check \"src\""
600
592
  },
601
593
  "author": "LangChain",
602
594
  "license": "MIT",
@@ -616,12 +608,12 @@
616
608
  "@langchain/anthropic": "^0.2.8",
617
609
  "@langchain/aws": "^0.0.5",
618
610
  "@langchain/cohere": "^0.2.1",
619
- "@langchain/google-genai": "^0.0.25",
620
- "@langchain/google-vertexai": "^0.0.25",
611
+ "@langchain/google-genai": "^0.0.26",
612
+ "@langchain/google-vertexai": "^0.0.26",
621
613
  "@langchain/groq": "^0.0.15",
622
614
  "@langchain/mistralai": "^0.0.26",
623
615
  "@langchain/ollama": "^0.0.2",
624
- "@langchain/scripts": "~0.0.20",
616
+ "@langchain/scripts": ">=0.1.0 <0.2.0",
625
617
  "@mendable/firecrawl-js": "^0.0.13",
626
618
  "@notionhq/client": "^2.2.10",
627
619
  "@pinecone-database/pinecone": "^1.1.0",