langchain 0.2.16 → 0.2.18
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/dist/agents/executor.cjs +11 -0
- package/dist/agents/executor.d.ts +2 -0
- package/dist/agents/executor.js +11 -0
- package/dist/agents/react/output_parser.cjs +3 -2
- package/dist/agents/react/output_parser.js +3 -2
- package/dist/chains/conversational_retrieval_chain.cjs +1 -1
- package/dist/chains/conversational_retrieval_chain.d.ts +1 -1
- package/dist/chains/conversational_retrieval_chain.js +1 -1
- package/dist/chains/llm_chain.cjs +1 -1
- package/dist/chains/llm_chain.d.ts +1 -1
- package/dist/chains/llm_chain.js +1 -1
- package/dist/chains/retrieval_qa.cjs +1 -1
- package/dist/chains/retrieval_qa.d.ts +1 -1
- package/dist/chains/retrieval_qa.js +1 -1
- package/dist/vectorstores/memory.cjs +113 -3
- package/dist/vectorstores/memory.d.ts +110 -3
- package/dist/vectorstores/memory.js +113 -3
- package/package.json +11 -19
package/dist/agents/executor.cjs
CHANGED
|
@@ -364,10 +364,17 @@ class AgentExecutor extends base_js_1.BaseChain {
|
|
|
364
364
|
writable: true,
|
|
365
365
|
value: false
|
|
366
366
|
});
|
|
367
|
+
Object.defineProperty(this, "handleToolRuntimeErrors", {
|
|
368
|
+
enumerable: true,
|
|
369
|
+
configurable: true,
|
|
370
|
+
writable: true,
|
|
371
|
+
value: void 0
|
|
372
|
+
});
|
|
367
373
|
this.agent = agent;
|
|
368
374
|
this.tools = input.tools;
|
|
369
375
|
this.handleParsingErrors =
|
|
370
376
|
input.handleParsingErrors ?? this.handleParsingErrors;
|
|
377
|
+
this.handleToolRuntimeErrors = input.handleToolRuntimeErrors;
|
|
371
378
|
this.returnOnlyOutputs = returnOnlyOutputs;
|
|
372
379
|
if (this.agent._agentActionType() === "multi") {
|
|
373
380
|
for (const tool of this.tools) {
|
|
@@ -481,6 +488,7 @@ class AgentExecutor extends base_js_1.BaseChain {
|
|
|
481
488
|
if (typeof observation !== "string") {
|
|
482
489
|
throw new Error("Received unsupported non-string response from tool call.");
|
|
483
490
|
}
|
|
491
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
484
492
|
}
|
|
485
493
|
catch (e) {
|
|
486
494
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
@@ -501,6 +509,9 @@ class AgentExecutor extends base_js_1.BaseChain {
|
|
|
501
509
|
observation = await new ExceptionTool().call(observation, runManager?.getChild());
|
|
502
510
|
return { action, observation: observation ?? "" };
|
|
503
511
|
}
|
|
512
|
+
else if (this.handleToolRuntimeErrors !== undefined) {
|
|
513
|
+
observation = this.handleToolRuntimeErrors(e);
|
|
514
|
+
}
|
|
504
515
|
}
|
|
505
516
|
return { action, observation: observation ?? "" };
|
|
506
517
|
}));
|
|
@@ -86,6 +86,7 @@ export interface AgentExecutorInput extends ChainInputs {
|
|
|
86
86
|
maxIterations?: number;
|
|
87
87
|
earlyStoppingMethod?: StoppingMethod;
|
|
88
88
|
handleParsingErrors?: boolean | string | ((e: OutputParserException | ToolInputParsingException) => string);
|
|
89
|
+
handleToolRuntimeErrors?: (e: Error) => string;
|
|
89
90
|
}
|
|
90
91
|
export type AgentExecutorOutput = ChainValues;
|
|
91
92
|
/**
|
|
@@ -135,6 +136,7 @@ export declare class AgentExecutor extends BaseChain<ChainValues, AgentExecutorO
|
|
|
135
136
|
as an observation.
|
|
136
137
|
*/
|
|
137
138
|
handleParsingErrors: boolean | string | ((e: OutputParserException | ToolInputParsingException) => string);
|
|
139
|
+
handleToolRuntimeErrors?: (e: Error) => string;
|
|
138
140
|
get inputKeys(): string[];
|
|
139
141
|
get outputKeys(): string[];
|
|
140
142
|
constructor(input: AgentExecutorInput);
|
package/dist/agents/executor.js
CHANGED
|
@@ -359,10 +359,17 @@ export class AgentExecutor extends BaseChain {
|
|
|
359
359
|
writable: true,
|
|
360
360
|
value: false
|
|
361
361
|
});
|
|
362
|
+
Object.defineProperty(this, "handleToolRuntimeErrors", {
|
|
363
|
+
enumerable: true,
|
|
364
|
+
configurable: true,
|
|
365
|
+
writable: true,
|
|
366
|
+
value: void 0
|
|
367
|
+
});
|
|
362
368
|
this.agent = agent;
|
|
363
369
|
this.tools = input.tools;
|
|
364
370
|
this.handleParsingErrors =
|
|
365
371
|
input.handleParsingErrors ?? this.handleParsingErrors;
|
|
372
|
+
this.handleToolRuntimeErrors = input.handleToolRuntimeErrors;
|
|
366
373
|
this.returnOnlyOutputs = returnOnlyOutputs;
|
|
367
374
|
if (this.agent._agentActionType() === "multi") {
|
|
368
375
|
for (const tool of this.tools) {
|
|
@@ -476,6 +483,7 @@ export class AgentExecutor extends BaseChain {
|
|
|
476
483
|
if (typeof observation !== "string") {
|
|
477
484
|
throw new Error("Received unsupported non-string response from tool call.");
|
|
478
485
|
}
|
|
486
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
479
487
|
}
|
|
480
488
|
catch (e) {
|
|
481
489
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
@@ -496,6 +504,9 @@ export class AgentExecutor extends BaseChain {
|
|
|
496
504
|
observation = await new ExceptionTool().call(observation, runManager?.getChild());
|
|
497
505
|
return { action, observation: observation ?? "" };
|
|
498
506
|
}
|
|
507
|
+
else if (this.handleToolRuntimeErrors !== undefined) {
|
|
508
|
+
observation = this.handleToolRuntimeErrors(e);
|
|
509
|
+
}
|
|
499
510
|
}
|
|
500
511
|
return { action, observation: observation ?? "" };
|
|
501
512
|
}));
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ReActSingleInputOutputParser = void 0;
|
|
4
4
|
const prompts_1 = require("@langchain/core/prompts");
|
|
5
|
+
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
6
|
const types_js_1 = require("../types.cjs");
|
|
6
7
|
const prompt_js_1 = require("./prompt.cjs");
|
|
7
8
|
const FINAL_ANSWER_ACTION = "Final Answer:";
|
|
@@ -74,7 +75,7 @@ class ReActSingleInputOutputParser extends types_js_1.AgentActionOutputParser {
|
|
|
74
75
|
const actionMatch = text.match(regex);
|
|
75
76
|
if (actionMatch) {
|
|
76
77
|
if (includesAnswer) {
|
|
77
|
-
throw new
|
|
78
|
+
throw new output_parsers_1.OutputParserException(`${FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: ${text}`);
|
|
78
79
|
}
|
|
79
80
|
const action = actionMatch[1];
|
|
80
81
|
const actionInput = actionMatch[2];
|
|
@@ -94,7 +95,7 @@ class ReActSingleInputOutputParser extends types_js_1.AgentActionOutputParser {
|
|
|
94
95
|
log: text,
|
|
95
96
|
};
|
|
96
97
|
}
|
|
97
|
-
throw new
|
|
98
|
+
throw new output_parsers_1.OutputParserException(`Could not parse LLM output: ${text}`);
|
|
98
99
|
}
|
|
99
100
|
/**
|
|
100
101
|
* Returns the format instructions as a string. If the 'raw' option is
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { renderTemplate } from "@langchain/core/prompts";
|
|
2
|
+
import { OutputParserException } from "@langchain/core/output_parsers";
|
|
2
3
|
import { AgentActionOutputParser } from "../types.js";
|
|
3
4
|
import { FORMAT_INSTRUCTIONS } from "./prompt.js";
|
|
4
5
|
const FINAL_ANSWER_ACTION = "Final Answer:";
|
|
@@ -71,7 +72,7 @@ export class ReActSingleInputOutputParser extends AgentActionOutputParser {
|
|
|
71
72
|
const actionMatch = text.match(regex);
|
|
72
73
|
if (actionMatch) {
|
|
73
74
|
if (includesAnswer) {
|
|
74
|
-
throw new
|
|
75
|
+
throw new OutputParserException(`${FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: ${text}`);
|
|
75
76
|
}
|
|
76
77
|
const action = actionMatch[1];
|
|
77
78
|
const actionInput = actionMatch[2];
|
|
@@ -91,7 +92,7 @@ export class ReActSingleInputOutputParser extends AgentActionOutputParser {
|
|
|
91
92
|
log: text,
|
|
92
93
|
};
|
|
93
94
|
}
|
|
94
|
-
throw new
|
|
95
|
+
throw new OutputParserException(`Could not parse LLM output: ${text}`);
|
|
95
96
|
}
|
|
96
97
|
/**
|
|
97
98
|
* Returns the format instructions as a string. If the 'raw' option is
|
|
@@ -13,7 +13,7 @@ Chat History:
|
|
|
13
13
|
Follow Up Input: {question}
|
|
14
14
|
Standalone question:`;
|
|
15
15
|
/**
|
|
16
|
-
* @deprecated This class will be removed in 0.
|
|
16
|
+
* @deprecated This class will be removed in 1.0.0. See below for an example implementation using
|
|
17
17
|
* `createRetrievalChain`.
|
|
18
18
|
*
|
|
19
19
|
* Class for conducting conversational question-answering tasks with a
|
|
@@ -21,7 +21,7 @@ export interface ConversationalRetrievalQAChainInput extends ChainInputs {
|
|
|
21
21
|
inputKey?: string;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
|
-
* @deprecated This class will be removed in 0.
|
|
24
|
+
* @deprecated This class will be removed in 1.0.0. See below for an example implementation using
|
|
25
25
|
* `createRetrievalChain`.
|
|
26
26
|
*
|
|
27
27
|
* Class for conducting conversational question-answering tasks with a
|
|
@@ -10,7 +10,7 @@ Chat History:
|
|
|
10
10
|
Follow Up Input: {question}
|
|
11
11
|
Standalone question:`;
|
|
12
12
|
/**
|
|
13
|
-
* @deprecated This class will be removed in 0.
|
|
13
|
+
* @deprecated This class will be removed in 1.0.0. See below for an example implementation using
|
|
14
14
|
* `createRetrievalChain`.
|
|
15
15
|
*
|
|
16
16
|
* Class for conducting conversational question-answering tasks with a
|
|
@@ -29,7 +29,7 @@ function _getLanguageModel(llmLike) {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
|
-
* @deprecated This class will be removed in 0.
|
|
32
|
+
* @deprecated This class will be removed in 1.0.0. Use the LangChain Expression Language (LCEL) instead.
|
|
33
33
|
* See the example below for how to use LCEL with the LLMChain class:
|
|
34
34
|
*
|
|
35
35
|
* Chain to run queries against LLMs.
|
|
@@ -29,7 +29,7 @@ export interface LLMChainInput<T extends string | object = string, Model extends
|
|
|
29
29
|
outputKey?: string;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
|
-
* @deprecated This class will be removed in 0.
|
|
32
|
+
* @deprecated This class will be removed in 1.0.0. Use the LangChain Expression Language (LCEL) instead.
|
|
33
33
|
* See the example below for how to use LCEL with the LLMChain class:
|
|
34
34
|
*
|
|
35
35
|
* Chain to run queries against LLMs.
|
package/dist/chains/llm_chain.js
CHANGED
|
@@ -26,7 +26,7 @@ function _getLanguageModel(llmLike) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
* @deprecated This class will be removed in 0.
|
|
29
|
+
* @deprecated This class will be removed in 1.0.0. Use the LangChain Expression Language (LCEL) instead.
|
|
30
30
|
* See the example below for how to use LCEL with the LLMChain class:
|
|
31
31
|
*
|
|
32
32
|
* Chain to run queries against LLMs.
|
|
@@ -4,7 +4,7 @@ exports.RetrievalQAChain = void 0;
|
|
|
4
4
|
const base_js_1 = require("./base.cjs");
|
|
5
5
|
const load_js_1 = require("./question_answering/load.cjs");
|
|
6
6
|
/**
|
|
7
|
-
* @deprecated This class will be removed in 0.
|
|
7
|
+
* @deprecated This class will be removed in 1.0.0. See below for an example implementation using
|
|
8
8
|
* `createRetrievalChain`:
|
|
9
9
|
* Class representing a chain for performing question-answering tasks with
|
|
10
10
|
* a retrieval component.
|
|
@@ -16,7 +16,7 @@ export interface RetrievalQAChainInput extends Omit<ChainInputs, "memory"> {
|
|
|
16
16
|
returnSourceDocuments?: boolean;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* @deprecated This class will be removed in 0.
|
|
19
|
+
* @deprecated This class will be removed in 1.0.0. See below for an example implementation using
|
|
20
20
|
* `createRetrievalChain`:
|
|
21
21
|
* Class representing a chain for performing question-answering tasks with
|
|
22
22
|
* a retrieval component.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseChain } from "./base.js";
|
|
2
2
|
import { loadQAStuffChain, } from "./question_answering/load.js";
|
|
3
3
|
/**
|
|
4
|
-
* @deprecated This class will be removed in 0.
|
|
4
|
+
* @deprecated This class will be removed in 1.0.0. See below for an example implementation using
|
|
5
5
|
* `createRetrievalChain`:
|
|
6
6
|
* Class representing a chain for performing question-answering tasks with
|
|
7
7
|
* a retrieval component.
|
|
@@ -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
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
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
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
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
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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.
|
|
3
|
+
"version": "0.2.18",
|
|
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
|
|
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",
|
|
@@ -589,14 +584,11 @@
|
|
|
589
584
|
"prepack": "yarn build",
|
|
590
585
|
"release": "release-it --only-version --config .release-it.json",
|
|
591
586
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testPathIgnorePatterns=\\.int\\.test.ts --testTimeout 30000 --maxWorkers=50%",
|
|
592
|
-
"test:watch": "
|
|
593
|
-
"test:integration": "
|
|
594
|
-
"test:single": "
|
|
587
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch --testPathIgnorePatterns=\\.int\\.test.ts",
|
|
588
|
+
"test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --testPathPattern=\\.int\\.test.ts --testTimeout 100000 --maxWorkers=50%",
|
|
589
|
+
"test:single": "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.
|
|
620
|
-
"@langchain/google-vertexai": "
|
|
611
|
+
"@langchain/google-genai": "^0.0.26",
|
|
612
|
+
"@langchain/google-vertexai": "~0.0",
|
|
621
613
|
"@langchain/groq": "^0.0.15",
|
|
622
614
|
"@langchain/mistralai": "^0.0.26",
|
|
623
615
|
"@langchain/ollama": "^0.0.2",
|
|
624
|
-
"@langchain/scripts": "
|
|
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",
|
|
@@ -678,7 +670,7 @@
|
|
|
678
670
|
"playwright": "^1.32.1",
|
|
679
671
|
"prettier": "^2.8.3",
|
|
680
672
|
"puppeteer": "^22.0.0",
|
|
681
|
-
"pyodide": "^0.
|
|
673
|
+
"pyodide": "^0.26.2",
|
|
682
674
|
"redis": "^4.6.6",
|
|
683
675
|
"release-it": "^17.6.0",
|
|
684
676
|
"rimraf": "^5.0.1",
|
|
@@ -743,7 +735,7 @@
|
|
|
743
735
|
"peggy": "^3.0.2",
|
|
744
736
|
"playwright": "*",
|
|
745
737
|
"puppeteer": "*",
|
|
746
|
-
"pyodide": "
|
|
738
|
+
"pyodide": ">=0.24.1 <0.27.0",
|
|
747
739
|
"redis": "*",
|
|
748
740
|
"sonix-speech-recognition": "*",
|
|
749
741
|
"srt-parser-2": "*",
|