langchain 0.0.204-rc.0 → 0.0.204-rc.2
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/toolkits/aws_sfn.cjs +3 -75
- package/dist/agents/toolkits/aws_sfn.d.ts +2 -45
- package/dist/agents/toolkits/aws_sfn.js +2 -73
- package/dist/agents/toolkits/base.cjs +15 -9
- package/dist/agents/toolkits/base.d.ts +1 -9
- package/dist/agents/toolkits/base.js +1 -7
- package/dist/agents/toolkits/connery/index.cjs +15 -37
- package/dist/agents/toolkits/connery/index.d.ts +1 -23
- package/dist/agents/toolkits/connery/index.js +1 -35
- package/dist/cache/ioredis.cjs +15 -77
- package/dist/cache/ioredis.d.ts +1 -40
- package/dist/cache/ioredis.js +1 -75
- package/dist/memory/chat_memory.cjs +15 -61
- package/dist/memory/chat_memory.d.ts +1 -36
- package/dist/memory/chat_memory.js +1 -59
- package/dist/memory/motorhead_memory.cjs +15 -161
- package/dist/memory/motorhead_memory.d.ts +1 -63
- package/dist/memory/motorhead_memory.js +1 -159
- package/dist/memory/zep.cjs +15 -222
- package/dist/memory/zep.d.ts +1 -86
- package/dist/memory/zep.js +1 -220
- package/dist/schema/runnable/base.cjs +2 -1
- package/dist/schema/runnable/base.d.ts +1 -1
- package/dist/schema/runnable/base.js +1 -1
- package/dist/stores/message/in_memory.cjs +15 -49
- package/dist/stores/message/in_memory.d.ts +1 -28
- package/dist/stores/message/in_memory.js +1 -47
- package/dist/tools/aws_lambda.cjs +15 -83
- package/dist/tools/aws_lambda.d.ts +1 -25
- package/dist/tools/aws_lambda.js +1 -82
- package/dist/tools/dynamic.cjs +15 -87
- package/dist/tools/dynamic.d.ts +1 -48
- package/dist/tools/dynamic.js +1 -84
- package/dist/util/convex.cjs +15 -75
- package/dist/util/convex.d.ts +1 -26
- package/dist/util/convex.js +1 -74
- package/dist/vectorstores/memory.cjs +143 -15
- package/dist/vectorstores/memory.d.ts +92 -1
- package/dist/vectorstores/memory.js +141 -1
- package/package.json +4 -243
- package/dist/types/openai-types.cjs +0 -2
- package/dist/types/openai-types.d.ts +0 -135
- package/dist/types/openai-types.js +0 -1
- package/dist/util/chunk.cjs +0 -11
- package/dist/util/chunk.d.ts +0 -1
- package/dist/util/chunk.js +0 -7
- package/dist/util/googlevertexai-gauth.cjs +0 -36
- package/dist/util/googlevertexai-gauth.d.ts +0 -8
- package/dist/util/googlevertexai-gauth.js +0 -32
- package/dist/util/googlevertexai-webauth.cjs +0 -96
- package/dist/util/googlevertexai-webauth.d.ts +0 -22
- package/dist/util/googlevertexai-webauth.js +0 -92
- package/dist/util/iflytek_websocket_stream.cjs +0 -81
- package/dist/util/iflytek_websocket_stream.d.ts +0 -27
- package/dist/util/iflytek_websocket_stream.js +0 -77
- package/dist/util/llama_cpp.cjs +0 -34
- package/dist/util/llama_cpp.d.ts +0 -46
- package/dist/util/llama_cpp.js +0 -28
- package/dist/util/momento.cjs +0 -26
- package/dist/util/momento.d.ts +0 -9
- package/dist/util/momento.js +0 -22
package/dist/util/convex.js
CHANGED
|
@@ -1,74 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
3
|
-
import { internalQueryGeneric as internalQuery, internalMutationGeneric as internalMutation, } from "convex/server";
|
|
4
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
5
|
-
import { v } from "convex/values";
|
|
6
|
-
export const get = /*#__PURE__*/ internalQuery({
|
|
7
|
-
args: {
|
|
8
|
-
id: /*#__PURE__*/ v.string(),
|
|
9
|
-
},
|
|
10
|
-
handler: async (ctx, args) => {
|
|
11
|
-
const result = await ctx.db.get(args.id);
|
|
12
|
-
return result;
|
|
13
|
-
},
|
|
14
|
-
});
|
|
15
|
-
export const insert = /*#__PURE__*/ internalMutation({
|
|
16
|
-
args: {
|
|
17
|
-
table: /*#__PURE__*/ v.string(),
|
|
18
|
-
document: /*#__PURE__*/ v.any(),
|
|
19
|
-
},
|
|
20
|
-
handler: async (ctx, args) => {
|
|
21
|
-
await ctx.db.insert(args.table, args.document);
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
export const lookup = /*#__PURE__*/ internalQuery({
|
|
25
|
-
args: {
|
|
26
|
-
table: /*#__PURE__*/ v.string(),
|
|
27
|
-
index: /*#__PURE__*/ v.string(),
|
|
28
|
-
keyField: /*#__PURE__*/ v.string(),
|
|
29
|
-
key: /*#__PURE__*/ v.string(),
|
|
30
|
-
},
|
|
31
|
-
handler: async (ctx, args) => {
|
|
32
|
-
const result = await ctx.db
|
|
33
|
-
.query(args.table)
|
|
34
|
-
.withIndex(args.index, (q) => q.eq(args.keyField, args.key))
|
|
35
|
-
.collect();
|
|
36
|
-
return result;
|
|
37
|
-
},
|
|
38
|
-
});
|
|
39
|
-
export const upsert = /*#__PURE__*/ internalMutation({
|
|
40
|
-
args: {
|
|
41
|
-
table: /*#__PURE__*/ v.string(),
|
|
42
|
-
index: /*#__PURE__*/ v.string(),
|
|
43
|
-
keyField: /*#__PURE__*/ v.string(),
|
|
44
|
-
key: /*#__PURE__*/ v.string(),
|
|
45
|
-
document: /*#__PURE__*/ v.any(),
|
|
46
|
-
},
|
|
47
|
-
handler: async (ctx, args) => {
|
|
48
|
-
const existing = await ctx.db
|
|
49
|
-
.query(args.table)
|
|
50
|
-
.withIndex(args.index, (q) => q.eq(args.keyField, args.key))
|
|
51
|
-
.unique();
|
|
52
|
-
if (existing !== null) {
|
|
53
|
-
await ctx.db.replace(existing._id, args.document);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
await ctx.db.insert(args.table, args.document);
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
export const deleteMany = /*#__PURE__*/ internalMutation({
|
|
61
|
-
args: {
|
|
62
|
-
table: /*#__PURE__*/ v.string(),
|
|
63
|
-
index: /*#__PURE__*/ v.string(),
|
|
64
|
-
keyField: /*#__PURE__*/ v.string(),
|
|
65
|
-
key: /*#__PURE__*/ v.string(),
|
|
66
|
-
},
|
|
67
|
-
handler: async (ctx, args) => {
|
|
68
|
-
const existing = await ctx.db
|
|
69
|
-
.query(args.table)
|
|
70
|
-
.withIndex(args.index, (q) => q.eq(args.keyField, args.key))
|
|
71
|
-
.collect();
|
|
72
|
-
await Promise.all(existing.map((doc) => ctx.db.delete(doc._id)));
|
|
73
|
-
},
|
|
74
|
-
});
|
|
1
|
+
export * from "@langchain/community/util/convex";
|
|
@@ -1,17 +1,145 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
3
|
+
exports.MemoryVectorStore = void 0;
|
|
4
|
+
const ml_distance_1 = require("ml-distance");
|
|
5
|
+
const vectorstores_1 = require("@langchain/core/vectorstores");
|
|
6
|
+
const documents_1 = require("@langchain/core/documents");
|
|
7
|
+
/**
|
|
8
|
+
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
9
|
+
* methods for adding documents, performing similarity searches, and
|
|
10
|
+
* creating instances from texts, documents, or an existing index.
|
|
11
|
+
*/
|
|
12
|
+
class MemoryVectorStore extends vectorstores_1.VectorStore {
|
|
13
|
+
_vectorstoreType() {
|
|
14
|
+
return "memory";
|
|
15
|
+
}
|
|
16
|
+
constructor(embeddings, { similarity, ...rest } = {}) {
|
|
17
|
+
super(embeddings, rest);
|
|
18
|
+
Object.defineProperty(this, "memoryVectors", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: []
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "similarity", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: void 0
|
|
29
|
+
});
|
|
30
|
+
this.similarity = similarity ?? ml_distance_1.similarity.cosine;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Method to add documents to the memory vector store. It extracts the
|
|
34
|
+
* text from each document, generates embeddings for them, and adds the
|
|
35
|
+
* resulting vectors to the store.
|
|
36
|
+
* @param documents Array of `Document` instances to be added to the store.
|
|
37
|
+
* @returns Promise that resolves when all documents have been added.
|
|
38
|
+
*/
|
|
39
|
+
async addDocuments(documents) {
|
|
40
|
+
const texts = documents.map(({ pageContent }) => pageContent);
|
|
41
|
+
return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Method to add vectors to the memory vector store. It creates
|
|
45
|
+
* `MemoryVector` instances for each vector and document pair and adds
|
|
46
|
+
* them to the store.
|
|
47
|
+
* @param vectors Array of vectors to be added to the store.
|
|
48
|
+
* @param documents Array of `Document` instances corresponding to the vectors.
|
|
49
|
+
* @returns Promise that resolves when all vectors have been added.
|
|
50
|
+
*/
|
|
51
|
+
async addVectors(vectors, documents) {
|
|
52
|
+
const memoryVectors = vectors.map((embedding, idx) => ({
|
|
53
|
+
content: documents[idx].pageContent,
|
|
54
|
+
embedding,
|
|
55
|
+
metadata: documents[idx].metadata,
|
|
56
|
+
}));
|
|
57
|
+
this.memoryVectors = this.memoryVectors.concat(memoryVectors);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Method to perform a similarity search in the memory vector store. It
|
|
61
|
+
* calculates the similarity between the query vector and each vector in
|
|
62
|
+
* the store, sorts the results by similarity, and returns the top `k`
|
|
63
|
+
* results along with their scores.
|
|
64
|
+
* @param query Query vector to compare against the vectors in the store.
|
|
65
|
+
* @param k Number of top results to return.
|
|
66
|
+
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
67
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
68
|
+
*/
|
|
69
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
70
|
+
const filterFunction = (memoryVector) => {
|
|
71
|
+
if (!filter) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
const doc = new documents_1.Document({
|
|
75
|
+
metadata: memoryVector.metadata,
|
|
76
|
+
pageContent: memoryVector.content,
|
|
77
|
+
});
|
|
78
|
+
return filter(doc);
|
|
79
|
+
};
|
|
80
|
+
const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
|
|
81
|
+
const searches = filteredMemoryVectors
|
|
82
|
+
.map((vector, index) => ({
|
|
83
|
+
similarity: this.similarity(query, vector.embedding),
|
|
84
|
+
index,
|
|
85
|
+
}))
|
|
86
|
+
.sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
|
|
87
|
+
.slice(0, k);
|
|
88
|
+
const result = searches.map((search) => [
|
|
89
|
+
new documents_1.Document({
|
|
90
|
+
metadata: filteredMemoryVectors[search.index].metadata,
|
|
91
|
+
pageContent: filteredMemoryVectors[search.index].content,
|
|
92
|
+
}),
|
|
93
|
+
search.similarity,
|
|
94
|
+
]);
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
99
|
+
* texts. It creates a `Document` for each text and metadata pair, and
|
|
100
|
+
* adds them to the store.
|
|
101
|
+
* @param texts Array of texts to be added to the store.
|
|
102
|
+
* @param metadatas Array or single object of metadata corresponding to the texts.
|
|
103
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the texts.
|
|
104
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
105
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
106
|
+
*/
|
|
107
|
+
static async fromTexts(texts, metadatas, embeddings, dbConfig) {
|
|
108
|
+
const docs = [];
|
|
109
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
110
|
+
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
|
|
111
|
+
const newDoc = new documents_1.Document({
|
|
112
|
+
pageContent: texts[i],
|
|
113
|
+
metadata,
|
|
114
|
+
});
|
|
115
|
+
docs.push(newDoc);
|
|
116
|
+
}
|
|
117
|
+
return MemoryVectorStore.fromDocuments(docs, embeddings, dbConfig);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
121
|
+
* `Document` instances. It adds the documents to the store.
|
|
122
|
+
* @param docs Array of `Document` instances to be added to the store.
|
|
123
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
124
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
125
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
126
|
+
*/
|
|
127
|
+
static async fromDocuments(docs, embeddings, dbConfig) {
|
|
128
|
+
const instance = new this(embeddings, dbConfig);
|
|
129
|
+
await instance.addDocuments(docs);
|
|
130
|
+
return instance;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Static method to create a `MemoryVectorStore` instance from an existing
|
|
134
|
+
* index. It creates a new `MemoryVectorStore` instance without adding any
|
|
135
|
+
* documents or vectors.
|
|
136
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
137
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
138
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
139
|
+
*/
|
|
140
|
+
static async fromExistingIndex(embeddings, dbConfig) {
|
|
141
|
+
const instance = new this(embeddings, dbConfig);
|
|
142
|
+
return instance;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.MemoryVectorStore = MemoryVectorStore;
|
|
@@ -1 +1,92 @@
|
|
|
1
|
-
|
|
1
|
+
import { similarity as ml_distance_similarity } from "ml-distance";
|
|
2
|
+
import { VectorStore } from "@langchain/core/vectorstores";
|
|
3
|
+
import { Embeddings } from "@langchain/core/embeddings";
|
|
4
|
+
import { Document } from "@langchain/core/documents";
|
|
5
|
+
/**
|
|
6
|
+
* Interface representing a vector in memory. It includes the content
|
|
7
|
+
* (text), the corresponding embedding (vector), and any associated
|
|
8
|
+
* metadata.
|
|
9
|
+
*/
|
|
10
|
+
interface MemoryVector {
|
|
11
|
+
content: string;
|
|
12
|
+
embedding: number[];
|
|
13
|
+
metadata: Record<string, any>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface for the arguments that can be passed to the
|
|
17
|
+
* `MemoryVectorStore` constructor. It includes an optional `similarity`
|
|
18
|
+
* function.
|
|
19
|
+
*/
|
|
20
|
+
export interface MemoryVectorStoreArgs {
|
|
21
|
+
similarity?: typeof ml_distance_similarity.cosine;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
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.
|
|
27
|
+
*/
|
|
28
|
+
export declare class MemoryVectorStore extends VectorStore {
|
|
29
|
+
FilterType: (doc: Document) => boolean;
|
|
30
|
+
memoryVectors: MemoryVector[];
|
|
31
|
+
similarity: typeof ml_distance_similarity.cosine;
|
|
32
|
+
_vectorstoreType(): string;
|
|
33
|
+
constructor(embeddings: Embeddings, { similarity, ...rest }?: MemoryVectorStoreArgs);
|
|
34
|
+
/**
|
|
35
|
+
* Method to add documents to the memory vector store. It extracts the
|
|
36
|
+
* text from each document, generates embeddings for them, and adds the
|
|
37
|
+
* resulting vectors to the store.
|
|
38
|
+
* @param documents Array of `Document` instances to be added to the store.
|
|
39
|
+
* @returns Promise that resolves when all documents have been added.
|
|
40
|
+
*/
|
|
41
|
+
addDocuments(documents: Document[]): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Method to add vectors to the memory vector store. It creates
|
|
44
|
+
* `MemoryVector` instances for each vector and document pair and adds
|
|
45
|
+
* them to the store.
|
|
46
|
+
* @param vectors Array of vectors to be added to the store.
|
|
47
|
+
* @param documents Array of `Document` instances corresponding to the vectors.
|
|
48
|
+
* @returns Promise that resolves when all vectors have been added.
|
|
49
|
+
*/
|
|
50
|
+
addVectors(vectors: number[][], documents: Document[]): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Method to perform a similarity search in the memory vector store. It
|
|
53
|
+
* calculates the similarity between the query vector and each vector in
|
|
54
|
+
* the store, sorts the results by similarity, and returns the top `k`
|
|
55
|
+
* results along with their scores.
|
|
56
|
+
* @param query Query vector to compare against the vectors in the store.
|
|
57
|
+
* @param k Number of top results to return.
|
|
58
|
+
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
59
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
60
|
+
*/
|
|
61
|
+
similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[Document, number][]>;
|
|
62
|
+
/**
|
|
63
|
+
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
64
|
+
* texts. It creates a `Document` for each text and metadata pair, and
|
|
65
|
+
* adds them to the store.
|
|
66
|
+
* @param texts Array of texts to be added to the store.
|
|
67
|
+
* @param metadatas Array or single object of metadata corresponding to the texts.
|
|
68
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the texts.
|
|
69
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
70
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
71
|
+
*/
|
|
72
|
+
static fromTexts(texts: string[], metadatas: object[] | object, embeddings: Embeddings, dbConfig?: MemoryVectorStoreArgs): Promise<MemoryVectorStore>;
|
|
73
|
+
/**
|
|
74
|
+
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
75
|
+
* `Document` instances. It adds the documents to the store.
|
|
76
|
+
* @param docs Array of `Document` instances to be added to the store.
|
|
77
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
78
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
79
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
80
|
+
*/
|
|
81
|
+
static fromDocuments(docs: Document[], embeddings: Embeddings, dbConfig?: MemoryVectorStoreArgs): Promise<MemoryVectorStore>;
|
|
82
|
+
/**
|
|
83
|
+
* Static method to create a `MemoryVectorStore` instance from an existing
|
|
84
|
+
* index. It creates a new `MemoryVectorStore` instance without adding any
|
|
85
|
+
* documents or vectors.
|
|
86
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
87
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
88
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
89
|
+
*/
|
|
90
|
+
static fromExistingIndex(embeddings: Embeddings, dbConfig?: MemoryVectorStoreArgs): Promise<MemoryVectorStore>;
|
|
91
|
+
}
|
|
92
|
+
export {};
|
|
@@ -1 +1,141 @@
|
|
|
1
|
-
|
|
1
|
+
import { similarity as ml_distance_similarity } from "ml-distance";
|
|
2
|
+
import { VectorStore } from "@langchain/core/vectorstores";
|
|
3
|
+
import { Document } from "@langchain/core/documents";
|
|
4
|
+
/**
|
|
5
|
+
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
6
|
+
* methods for adding documents, performing similarity searches, and
|
|
7
|
+
* creating instances from texts, documents, or an existing index.
|
|
8
|
+
*/
|
|
9
|
+
export class MemoryVectorStore extends VectorStore {
|
|
10
|
+
_vectorstoreType() {
|
|
11
|
+
return "memory";
|
|
12
|
+
}
|
|
13
|
+
constructor(embeddings, { similarity, ...rest } = {}) {
|
|
14
|
+
super(embeddings, rest);
|
|
15
|
+
Object.defineProperty(this, "memoryVectors", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: []
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "similarity", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
this.similarity = similarity ?? ml_distance_similarity.cosine;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Method to add documents to the memory vector store. It extracts the
|
|
31
|
+
* text from each document, generates embeddings for them, and adds the
|
|
32
|
+
* resulting vectors to the store.
|
|
33
|
+
* @param documents Array of `Document` instances to be added to the store.
|
|
34
|
+
* @returns Promise that resolves when all documents have been added.
|
|
35
|
+
*/
|
|
36
|
+
async addDocuments(documents) {
|
|
37
|
+
const texts = documents.map(({ pageContent }) => pageContent);
|
|
38
|
+
return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Method to add vectors to the memory vector store. It creates
|
|
42
|
+
* `MemoryVector` instances for each vector and document pair and adds
|
|
43
|
+
* them to the store.
|
|
44
|
+
* @param vectors Array of vectors to be added to the store.
|
|
45
|
+
* @param documents Array of `Document` instances corresponding to the vectors.
|
|
46
|
+
* @returns Promise that resolves when all vectors have been added.
|
|
47
|
+
*/
|
|
48
|
+
async addVectors(vectors, documents) {
|
|
49
|
+
const memoryVectors = vectors.map((embedding, idx) => ({
|
|
50
|
+
content: documents[idx].pageContent,
|
|
51
|
+
embedding,
|
|
52
|
+
metadata: documents[idx].metadata,
|
|
53
|
+
}));
|
|
54
|
+
this.memoryVectors = this.memoryVectors.concat(memoryVectors);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Method to perform a similarity search in the memory vector store. It
|
|
58
|
+
* calculates the similarity between the query vector and each vector in
|
|
59
|
+
* the store, sorts the results by similarity, and returns the top `k`
|
|
60
|
+
* results along with their scores.
|
|
61
|
+
* @param query Query vector to compare against the vectors in the store.
|
|
62
|
+
* @param k Number of top results to return.
|
|
63
|
+
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
64
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
65
|
+
*/
|
|
66
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
67
|
+
const filterFunction = (memoryVector) => {
|
|
68
|
+
if (!filter) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
const doc = new Document({
|
|
72
|
+
metadata: memoryVector.metadata,
|
|
73
|
+
pageContent: memoryVector.content,
|
|
74
|
+
});
|
|
75
|
+
return filter(doc);
|
|
76
|
+
};
|
|
77
|
+
const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
|
|
78
|
+
const searches = filteredMemoryVectors
|
|
79
|
+
.map((vector, index) => ({
|
|
80
|
+
similarity: this.similarity(query, vector.embedding),
|
|
81
|
+
index,
|
|
82
|
+
}))
|
|
83
|
+
.sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
|
|
84
|
+
.slice(0, k);
|
|
85
|
+
const result = searches.map((search) => [
|
|
86
|
+
new Document({
|
|
87
|
+
metadata: filteredMemoryVectors[search.index].metadata,
|
|
88
|
+
pageContent: filteredMemoryVectors[search.index].content,
|
|
89
|
+
}),
|
|
90
|
+
search.similarity,
|
|
91
|
+
]);
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
96
|
+
* texts. It creates a `Document` for each text and metadata pair, and
|
|
97
|
+
* adds them to the store.
|
|
98
|
+
* @param texts Array of texts to be added to the store.
|
|
99
|
+
* @param metadatas Array or single object of metadata corresponding to the texts.
|
|
100
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the texts.
|
|
101
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
102
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
103
|
+
*/
|
|
104
|
+
static async fromTexts(texts, metadatas, embeddings, dbConfig) {
|
|
105
|
+
const docs = [];
|
|
106
|
+
for (let i = 0; i < texts.length; i += 1) {
|
|
107
|
+
const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
|
|
108
|
+
const newDoc = new Document({
|
|
109
|
+
pageContent: texts[i],
|
|
110
|
+
metadata,
|
|
111
|
+
});
|
|
112
|
+
docs.push(newDoc);
|
|
113
|
+
}
|
|
114
|
+
return MemoryVectorStore.fromDocuments(docs, embeddings, dbConfig);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
118
|
+
* `Document` instances. It adds the documents to the store.
|
|
119
|
+
* @param docs Array of `Document` instances to be added to the store.
|
|
120
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
121
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
122
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
123
|
+
*/
|
|
124
|
+
static async fromDocuments(docs, embeddings, dbConfig) {
|
|
125
|
+
const instance = new this(embeddings, dbConfig);
|
|
126
|
+
await instance.addDocuments(docs);
|
|
127
|
+
return instance;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Static method to create a `MemoryVectorStore` instance from an existing
|
|
131
|
+
* index. It creates a new `MemoryVectorStore` instance without adding any
|
|
132
|
+
* documents or vectors.
|
|
133
|
+
* @param embeddings `Embeddings` instance used to generate embeddings for the documents.
|
|
134
|
+
* @param dbConfig Optional `MemoryVectorStoreArgs` to configure the `MemoryVectorStore` instance.
|
|
135
|
+
* @returns Promise that resolves with a new `MemoryVectorStore` instance.
|
|
136
|
+
*/
|
|
137
|
+
static async fromExistingIndex(embeddings, dbConfig) {
|
|
138
|
+
const instance = new this(embeddings, dbConfig);
|
|
139
|
+
return instance;
|
|
140
|
+
}
|
|
141
|
+
}
|