langchain 0.0.197 → 0.0.199
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/index.d.ts +1 -1
- package/dist/chains/conversational_retrieval_chain.cjs +16 -2
- package/dist/chains/conversational_retrieval_chain.d.ts +2 -0
- package/dist/chains/conversational_retrieval_chain.js +16 -2
- package/dist/chat_models/llama_cpp.cjs +45 -0
- package/dist/chat_models/llama_cpp.d.ts +4 -1
- package/dist/chat_models/llama_cpp.js +45 -0
- package/dist/document_loaders/fs/chatgpt.cjs +85 -0
- package/dist/document_loaders/fs/chatgpt.d.ts +8 -0
- package/dist/document_loaders/fs/chatgpt.js +81 -0
- package/dist/document_loaders/fs/pptx.cjs +39 -0
- package/dist/document_loaders/fs/pptx.d.ts +23 -0
- package/dist/document_loaders/fs/pptx.js +35 -0
- package/dist/document_loaders/web/confluence.cjs +31 -7
- package/dist/document_loaders/web/confluence.d.ts +12 -5
- package/dist/document_loaders/web/confluence.js +31 -7
- package/dist/experimental/openai_assistant/index.cjs +32 -0
- package/dist/experimental/openai_assistant/index.d.ts +26 -0
- package/dist/experimental/openai_assistant/index.js +32 -0
- package/dist/experimental/tools/pyinterpreter.cjs +248 -0
- package/dist/experimental/tools/pyinterpreter.d.ts +18 -0
- package/dist/experimental/tools/pyinterpreter.js +244 -0
- package/dist/graphs/neo4j_graph.cjs +49 -14
- package/dist/graphs/neo4j_graph.d.ts +30 -0
- package/dist/graphs/neo4j_graph.js +49 -14
- package/dist/llms/gradient_ai.cjs +98 -0
- package/dist/llms/gradient_ai.d.ts +50 -0
- package/dist/llms/gradient_ai.js +94 -0
- package/dist/llms/hf.cjs +13 -2
- package/dist/llms/hf.d.ts +5 -0
- package/dist/llms/hf.js +13 -2
- package/dist/llms/llama_cpp.cjs +17 -3
- package/dist/llms/llama_cpp.d.ts +4 -1
- package/dist/llms/llama_cpp.js +17 -3
- package/dist/llms/watsonx_ai.cjs +154 -0
- package/dist/llms/watsonx_ai.d.ts +72 -0
- package/dist/llms/watsonx_ai.js +150 -0
- package/dist/load/import_constants.cjs +6 -0
- package/dist/load/import_constants.js +6 -0
- package/dist/load/import_map.cjs +4 -3
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/output_parsers/json.cjs +4 -0
- package/dist/output_parsers/json.js +4 -0
- package/dist/tools/google_places.cjs +81 -0
- package/dist/tools/google_places.d.ts +21 -0
- package/dist/tools/google_places.js +77 -0
- package/dist/vectorstores/clickhouse.cjs +286 -0
- package/dist/vectorstores/clickhouse.d.ts +126 -0
- package/dist/vectorstores/clickhouse.js +259 -0
- package/dist/vectorstores/elasticsearch.cjs +16 -3
- package/dist/vectorstores/elasticsearch.d.ts +6 -2
- package/dist/vectorstores/elasticsearch.js +16 -3
- package/dist/vectorstores/pgvector.cjs +142 -18
- package/dist/vectorstores/pgvector.d.ts +21 -0
- package/dist/vectorstores/pgvector.js +142 -18
- package/dist/vectorstores/prisma.cjs +1 -1
- package/dist/vectorstores/prisma.js +1 -1
- package/dist/vectorstores/weaviate.cjs +45 -2
- package/dist/vectorstores/weaviate.d.ts +27 -1
- package/dist/vectorstores/weaviate.js +45 -2
- package/dist/vectorstores/xata.cjs +3 -2
- package/dist/vectorstores/xata.js +3 -2
- package/document_loaders/fs/chatgpt.cjs +1 -0
- package/document_loaders/fs/chatgpt.d.ts +1 -0
- package/document_loaders/fs/chatgpt.js +1 -0
- package/document_loaders/fs/pptx.cjs +1 -0
- package/document_loaders/fs/pptx.d.ts +1 -0
- package/document_loaders/fs/pptx.js +1 -0
- package/experimental/tools/pyinterpreter.cjs +1 -0
- package/experimental/tools/pyinterpreter.d.ts +1 -0
- package/experimental/tools/pyinterpreter.js +1 -0
- package/llms/gradient_ai.cjs +1 -0
- package/llms/gradient_ai.d.ts +1 -0
- package/llms/gradient_ai.js +1 -0
- package/llms/watsonx_ai.cjs +1 -0
- package/llms/watsonx_ai.d.ts +1 -0
- package/llms/watsonx_ai.js +1 -0
- package/package.json +87 -13
- package/tools/google_places.cjs +1 -0
- package/tools/google_places.d.ts +1 -0
- package/tools/google_places.js +1 -0
- package/vectorstores/clickhouse.cjs +1 -0
- package/vectorstores/clickhouse.d.ts +1 -0
- package/vectorstores/clickhouse.js +1 -0
|
@@ -20,6 +20,24 @@ export class PGVectorStore extends VectorStore {
|
|
|
20
20
|
writable: true,
|
|
21
21
|
value: void 0
|
|
22
22
|
});
|
|
23
|
+
Object.defineProperty(this, "collectionTableName", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "collectionName", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: "langchain"
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "collectionMetadata", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: void 0
|
|
40
|
+
});
|
|
23
41
|
Object.defineProperty(this, "idColumnName", {
|
|
24
42
|
enumerable: true,
|
|
25
43
|
configurable: true,
|
|
@@ -75,6 +93,9 @@ export class PGVectorStore extends VectorStore {
|
|
|
75
93
|
value: 500
|
|
76
94
|
});
|
|
77
95
|
this.tableName = config.tableName;
|
|
96
|
+
this.collectionTableName = config.collectionTableName;
|
|
97
|
+
this.collectionName = config.collectionName ?? "langchain";
|
|
98
|
+
this.collectionMetadata = config.collectionMetadata ?? null;
|
|
78
99
|
this.filter = config.filter;
|
|
79
100
|
this.vectorColumnName = config.columns?.vectorColumnName ?? "embedding";
|
|
80
101
|
this.contentColumnName = config.columns?.contentColumnName ?? "text";
|
|
@@ -100,6 +121,9 @@ export class PGVectorStore extends VectorStore {
|
|
|
100
121
|
const postgresqlVectorStore = new PGVectorStore(embeddings, config);
|
|
101
122
|
await postgresqlVectorStore._initializeClient();
|
|
102
123
|
await postgresqlVectorStore.ensureTableInDatabase();
|
|
124
|
+
if (postgresqlVectorStore.collectionTableName) {
|
|
125
|
+
await postgresqlVectorStore.ensureCollectionTableInDatabase();
|
|
126
|
+
}
|
|
103
127
|
return postgresqlVectorStore;
|
|
104
128
|
}
|
|
105
129
|
async _initializeClient() {
|
|
@@ -116,15 +140,56 @@ export class PGVectorStore extends VectorStore {
|
|
|
116
140
|
const texts = documents.map(({ pageContent }) => pageContent);
|
|
117
141
|
return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
|
|
118
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Inserts a row for the collectionName provided at initialization if it does not
|
|
145
|
+
* exist and returns the collectionId.
|
|
146
|
+
*
|
|
147
|
+
* @returns The collectionId for the given collectionName.
|
|
148
|
+
*/
|
|
149
|
+
async getOrCreateCollection() {
|
|
150
|
+
const queryString = `
|
|
151
|
+
SELECT uuid from ${this.collectionTableName}
|
|
152
|
+
WHERE name = $1;
|
|
153
|
+
`;
|
|
154
|
+
const queryResult = await this.pool.query(queryString, [
|
|
155
|
+
this.collectionName,
|
|
156
|
+
]);
|
|
157
|
+
let collectionId = queryResult.rows[0]?.uuid;
|
|
158
|
+
if (!collectionId) {
|
|
159
|
+
const insertString = `
|
|
160
|
+
INSERT INTO ${this.collectionTableName}(
|
|
161
|
+
uuid,
|
|
162
|
+
name,
|
|
163
|
+
cmetadata
|
|
164
|
+
)
|
|
165
|
+
VALUES (
|
|
166
|
+
uuid_generate_v4(),
|
|
167
|
+
$1,
|
|
168
|
+
$2
|
|
169
|
+
)
|
|
170
|
+
RETURNING uuid;
|
|
171
|
+
`;
|
|
172
|
+
const insertResult = await this.pool.query(insertString, [
|
|
173
|
+
this.collectionName,
|
|
174
|
+
this.collectionMetadata,
|
|
175
|
+
]);
|
|
176
|
+
collectionId = insertResult.rows[0]?.uuid;
|
|
177
|
+
}
|
|
178
|
+
return collectionId;
|
|
179
|
+
}
|
|
119
180
|
/**
|
|
120
181
|
* Generates the SQL placeholders for a specific row at the provided index.
|
|
121
182
|
*
|
|
122
183
|
* @param index - The index of the row for which placeholders need to be generated.
|
|
184
|
+
* @param numOfColumns - The number of columns we are inserting data into.
|
|
123
185
|
* @returns The SQL placeholders for the row values.
|
|
124
186
|
*/
|
|
125
|
-
generatePlaceholderForRowAt(index) {
|
|
126
|
-
const
|
|
127
|
-
|
|
187
|
+
generatePlaceholderForRowAt(index, numOfColumns) {
|
|
188
|
+
const placeholders = [];
|
|
189
|
+
for (let i = 0; i < numOfColumns; i += 1) {
|
|
190
|
+
placeholders.push(`$${index * numOfColumns + i + 1}`);
|
|
191
|
+
}
|
|
192
|
+
return `(${placeholders.join(", ")})`;
|
|
128
193
|
}
|
|
129
194
|
/**
|
|
130
195
|
* Constructs the SQL query for inserting rows into the specified table.
|
|
@@ -133,15 +198,25 @@ export class PGVectorStore extends VectorStore {
|
|
|
133
198
|
* @param chunkIndex - The starting index for generating query placeholders based on chunk positioning.
|
|
134
199
|
* @returns The complete SQL INSERT INTO query string.
|
|
135
200
|
*/
|
|
136
|
-
buildInsertQuery(rows) {
|
|
201
|
+
async buildInsertQuery(rows) {
|
|
202
|
+
let collectionId;
|
|
203
|
+
if (this.collectionTableName) {
|
|
204
|
+
collectionId = await this.getOrCreateCollection();
|
|
205
|
+
}
|
|
206
|
+
const columns = [
|
|
207
|
+
this.contentColumnName,
|
|
208
|
+
this.vectorColumnName,
|
|
209
|
+
this.metadataColumnName,
|
|
210
|
+
];
|
|
211
|
+
if (collectionId) {
|
|
212
|
+
columns.push("collection_id");
|
|
213
|
+
}
|
|
137
214
|
const valuesPlaceholders = rows
|
|
138
|
-
.map((_, j) => this.generatePlaceholderForRowAt(j))
|
|
215
|
+
.map((_, j) => this.generatePlaceholderForRowAt(j, columns.length))
|
|
139
216
|
.join(", ");
|
|
140
217
|
const text = `
|
|
141
218
|
INSERT INTO ${this.tableName}(
|
|
142
|
-
${
|
|
143
|
-
${this.vectorColumnName},
|
|
144
|
-
${this.metadataColumnName}
|
|
219
|
+
${columns}
|
|
145
220
|
)
|
|
146
221
|
VALUES ${valuesPlaceholders}
|
|
147
222
|
`;
|
|
@@ -156,17 +231,24 @@ export class PGVectorStore extends VectorStore {
|
|
|
156
231
|
* @returns Promise that resolves when the vectors have been added.
|
|
157
232
|
*/
|
|
158
233
|
async addVectors(vectors, documents) {
|
|
159
|
-
const rows =
|
|
234
|
+
const rows = [];
|
|
235
|
+
let collectionId;
|
|
236
|
+
if (this.collectionTableName) {
|
|
237
|
+
collectionId = await this.getOrCreateCollection();
|
|
238
|
+
}
|
|
239
|
+
for (let i = 0; i < vectors.length; i += 1) {
|
|
240
|
+
const values = [];
|
|
241
|
+
const embedding = vectors[i];
|
|
160
242
|
const embeddingString = `[${embedding.join(",")}]`;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
243
|
+
values.push(documents[i].pageContent, embeddingString, documents[i].metadata);
|
|
244
|
+
if (collectionId) {
|
|
245
|
+
values.push(collectionId);
|
|
246
|
+
}
|
|
247
|
+
rows.push(values);
|
|
248
|
+
}
|
|
167
249
|
for (let i = 0; i < rows.length; i += this.chunkSize) {
|
|
168
250
|
const chunk = rows.slice(i, i + this.chunkSize);
|
|
169
|
-
const insertQuery = this.buildInsertQuery(chunk);
|
|
251
|
+
const insertQuery = await this.buildInsertQuery(chunk);
|
|
170
252
|
const flatValues = chunk.flat();
|
|
171
253
|
try {
|
|
172
254
|
await this.pool.query(insertQuery, flatValues);
|
|
@@ -190,13 +272,23 @@ export class PGVectorStore extends VectorStore {
|
|
|
190
272
|
async similaritySearchVectorWithScore(query, k, filter) {
|
|
191
273
|
const embeddingString = `[${query.join(",")}]`;
|
|
192
274
|
const _filter = filter ?? "{}";
|
|
275
|
+
let collectionId;
|
|
276
|
+
if (this.collectionTableName) {
|
|
277
|
+
collectionId = await this.getOrCreateCollection();
|
|
278
|
+
}
|
|
279
|
+
const parameters = [embeddingString, _filter, k];
|
|
280
|
+
if (collectionId) {
|
|
281
|
+
parameters.push(collectionId);
|
|
282
|
+
}
|
|
193
283
|
const queryString = `
|
|
194
284
|
SELECT *, ${this.vectorColumnName} <=> $1 as "_distance"
|
|
195
285
|
FROM ${this.tableName}
|
|
196
286
|
WHERE ${this.metadataColumnName}::jsonb @> $2
|
|
287
|
+
${collectionId ? "AND collection_id = $4" : ""}
|
|
197
288
|
ORDER BY "_distance" ASC
|
|
198
|
-
LIMIT $3
|
|
199
|
-
|
|
289
|
+
LIMIT $3;
|
|
290
|
+
`;
|
|
291
|
+
const documents = (await this.pool.query(queryString, parameters)).rows;
|
|
200
292
|
const results = [];
|
|
201
293
|
for (const doc of documents) {
|
|
202
294
|
if (doc._distance != null && doc[this.contentColumnName] != null) {
|
|
@@ -227,6 +319,38 @@ export class PGVectorStore extends VectorStore {
|
|
|
227
319
|
);
|
|
228
320
|
`);
|
|
229
321
|
}
|
|
322
|
+
/**
|
|
323
|
+
* Method to ensure the existence of the collection table in the database.
|
|
324
|
+
* It creates the table if it does not already exist.
|
|
325
|
+
*
|
|
326
|
+
* @returns Promise that resolves when the collection table has been ensured.
|
|
327
|
+
*/
|
|
328
|
+
async ensureCollectionTableInDatabase() {
|
|
329
|
+
try {
|
|
330
|
+
await this.pool.query(`
|
|
331
|
+
CREATE TABLE IF NOT EXISTS ${this.collectionTableName} (
|
|
332
|
+
uuid uuid NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY,
|
|
333
|
+
name character varying,
|
|
334
|
+
cmetadata jsonb
|
|
335
|
+
);
|
|
336
|
+
|
|
337
|
+
ALTER TABLE ${this.tableName}
|
|
338
|
+
ADD COLUMN collection_id uuid;
|
|
339
|
+
|
|
340
|
+
ALTER TABLE ${this.tableName}
|
|
341
|
+
ADD CONSTRAINT ${this.tableName}_collection_id_fkey
|
|
342
|
+
FOREIGN KEY (collection_id)
|
|
343
|
+
REFERENCES ${this.collectionTableName}(uuid)
|
|
344
|
+
ON DELETE CASCADE;
|
|
345
|
+
`);
|
|
346
|
+
}
|
|
347
|
+
catch (e) {
|
|
348
|
+
if (!e.message.includes("already exists")) {
|
|
349
|
+
console.error(e);
|
|
350
|
+
throw new Error(`Error adding column: ${e.message}`);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
230
354
|
/**
|
|
231
355
|
* Static method to create a new `PGVectorStore` instance from an
|
|
232
356
|
* array of texts and their metadata. It converts the texts into
|
|
@@ -249,7 +249,7 @@ class PrismaVectorStore extends base_js_1.VectorStore {
|
|
|
249
249
|
!value.every((v) => typeof v === "string")) {
|
|
250
250
|
throw new Error(`Invalid filter: IN operator requires an array of strings. Received: ${JSON.stringify(value, null, 2)}`);
|
|
251
251
|
}
|
|
252
|
-
return this.Prisma.sql `${colRaw} ${opRaw} (${
|
|
252
|
+
return this.Prisma.sql `${colRaw} ${opRaw} (${this.Prisma.join(value)})`;
|
|
253
253
|
}
|
|
254
254
|
case OpMap.isNull:
|
|
255
255
|
case OpMap.isNotNull:
|
|
@@ -246,7 +246,7 @@ class PrismaVectorStore extends VectorStore {
|
|
|
246
246
|
!value.every((v) => typeof v === "string")) {
|
|
247
247
|
throw new Error(`Invalid filter: IN operator requires an array of strings. Received: ${JSON.stringify(value, null, 2)}`);
|
|
248
248
|
}
|
|
249
|
-
return this.Prisma.sql `${colRaw} ${opRaw} (${
|
|
249
|
+
return this.Prisma.sql `${colRaw} ${opRaw} (${this.Prisma.join(value)})`;
|
|
250
250
|
}
|
|
251
251
|
case OpMap.isNull:
|
|
252
252
|
case OpMap.isNotNull:
|
|
@@ -27,6 +27,7 @@ exports.WeaviateStore = exports.flattenObjectForWeaviate = void 0;
|
|
|
27
27
|
const uuid = __importStar(require("uuid"));
|
|
28
28
|
const base_js_1 = require("./base.cjs");
|
|
29
29
|
const document_js_1 = require("../document.cjs");
|
|
30
|
+
const math_js_1 = require("../util/math.cjs");
|
|
30
31
|
// Note this function is not generic, it is designed specifically for Weaviate
|
|
31
32
|
// https://weaviate.io/developers/weaviate/config-refs/datatypes#introduction
|
|
32
33
|
const flattenObjectForWeaviate = (
|
|
@@ -235,11 +236,27 @@ class WeaviateStore extends base_js_1.VectorStore {
|
|
|
235
236
|
* @returns An array of tuples, where each tuple contains a document and its similarity score.
|
|
236
237
|
*/
|
|
237
238
|
async similaritySearchVectorWithScore(query, k, filter) {
|
|
239
|
+
const resultsWithEmbedding = await this.similaritySearchVectorWithScoreAndEmbedding(query, k, filter);
|
|
240
|
+
return resultsWithEmbedding.map(([document, score, _embedding]) => [
|
|
241
|
+
document,
|
|
242
|
+
score,
|
|
243
|
+
]);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Method to perform a similarity search on the stored vectors in the
|
|
247
|
+
* Weaviate index. It returns the top k most similar documents, their
|
|
248
|
+
* similarity scores and embedding vectors.
|
|
249
|
+
* @param query The query vector.
|
|
250
|
+
* @param k The number of most similar documents to return.
|
|
251
|
+
* @param filter Optional filter to apply to the search.
|
|
252
|
+
* @returns An array of tuples, where each tuple contains a document, its similarity score and its embedding vector.
|
|
253
|
+
*/
|
|
254
|
+
async similaritySearchVectorWithScoreAndEmbedding(query, k, filter) {
|
|
238
255
|
try {
|
|
239
|
-
let builder =
|
|
256
|
+
let builder = this.client.graphql
|
|
240
257
|
.get()
|
|
241
258
|
.withClassName(this.indexName)
|
|
242
|
-
.withFields(`${this.queryAttrs.join(" ")} _additional { distance }`)
|
|
259
|
+
.withFields(`${this.queryAttrs.join(" ")} _additional { distance vector }`)
|
|
243
260
|
.withNearVector({
|
|
244
261
|
vector: query,
|
|
245
262
|
distance: filter?.distance,
|
|
@@ -261,6 +278,7 @@ class WeaviateStore extends base_js_1.VectorStore {
|
|
|
261
278
|
metadata: rest,
|
|
262
279
|
}),
|
|
263
280
|
_additional.distance,
|
|
281
|
+
_additional.vector,
|
|
264
282
|
]);
|
|
265
283
|
}
|
|
266
284
|
return documents;
|
|
@@ -269,6 +287,31 @@ class WeaviateStore extends base_js_1.VectorStore {
|
|
|
269
287
|
throw Error(`'Error in similaritySearch' ${e}`);
|
|
270
288
|
}
|
|
271
289
|
}
|
|
290
|
+
/**
|
|
291
|
+
* Return documents selected using the maximal marginal relevance.
|
|
292
|
+
* Maximal marginal relevance optimizes for similarity to the query AND diversity
|
|
293
|
+
* among selected documents.
|
|
294
|
+
*
|
|
295
|
+
* @param {string} query - Text to look up documents similar to.
|
|
296
|
+
* @param {number} options.k - Number of documents to return.
|
|
297
|
+
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
|
|
298
|
+
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
|
|
299
|
+
* where 0 corresponds to maximum diversity and 1 to minimum diversity.
|
|
300
|
+
* @param {this["FilterType"]} options.filter - Optional filter
|
|
301
|
+
* @param _callbacks
|
|
302
|
+
*
|
|
303
|
+
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
|
|
304
|
+
*/
|
|
305
|
+
async maxMarginalRelevanceSearch(query, options, _callbacks) {
|
|
306
|
+
const { k, fetchK = 20, lambda = 0.5, filter } = options;
|
|
307
|
+
const queryEmbedding = await this.embeddings.embedQuery(query);
|
|
308
|
+
const allResults = await this.similaritySearchVectorWithScoreAndEmbedding(queryEmbedding, fetchK, filter);
|
|
309
|
+
const embeddingList = allResults.map(([_doc, _score, embedding]) => embedding);
|
|
310
|
+
const mmrIndexes = (0, math_js_1.maximalMarginalRelevance)(queryEmbedding, embeddingList, lambda, k);
|
|
311
|
+
return mmrIndexes
|
|
312
|
+
.filter((idx) => idx !== -1)
|
|
313
|
+
.map((idx) => allResults[idx][0]);
|
|
314
|
+
}
|
|
272
315
|
/**
|
|
273
316
|
* Static method to create a new `WeaviateStore` instance from a list of
|
|
274
317
|
* texts. It first creates documents from the texts and metadata, then
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { WeaviateClient, WhereFilter } from "weaviate-ts-client";
|
|
2
|
-
import { VectorStore } from "./base.js";
|
|
2
|
+
import { MaxMarginalRelevanceSearchOptions, VectorStore } from "./base.js";
|
|
3
3
|
import { Embeddings } from "../embeddings/base.js";
|
|
4
4
|
import { Document } from "../document.js";
|
|
5
5
|
export declare const flattenObjectForWeaviate: (obj: Record<string, any>) => Record<string, any>;
|
|
@@ -83,6 +83,32 @@ export declare class WeaviateStore extends VectorStore {
|
|
|
83
83
|
* @returns An array of tuples, where each tuple contains a document and its similarity score.
|
|
84
84
|
*/
|
|
85
85
|
similaritySearchVectorWithScore(query: number[], k: number, filter?: WeaviateFilter): Promise<[Document, number][]>;
|
|
86
|
+
/**
|
|
87
|
+
* Method to perform a similarity search on the stored vectors in the
|
|
88
|
+
* Weaviate index. It returns the top k most similar documents, their
|
|
89
|
+
* similarity scores and embedding vectors.
|
|
90
|
+
* @param query The query vector.
|
|
91
|
+
* @param k The number of most similar documents to return.
|
|
92
|
+
* @param filter Optional filter to apply to the search.
|
|
93
|
+
* @returns An array of tuples, where each tuple contains a document, its similarity score and its embedding vector.
|
|
94
|
+
*/
|
|
95
|
+
similaritySearchVectorWithScoreAndEmbedding(query: number[], k: number, filter?: WeaviateFilter): Promise<[Document, number, number[]][]>;
|
|
96
|
+
/**
|
|
97
|
+
* Return documents selected using the maximal marginal relevance.
|
|
98
|
+
* Maximal marginal relevance optimizes for similarity to the query AND diversity
|
|
99
|
+
* among selected documents.
|
|
100
|
+
*
|
|
101
|
+
* @param {string} query - Text to look up documents similar to.
|
|
102
|
+
* @param {number} options.k - Number of documents to return.
|
|
103
|
+
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
|
|
104
|
+
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
|
|
105
|
+
* where 0 corresponds to maximum diversity and 1 to minimum diversity.
|
|
106
|
+
* @param {this["FilterType"]} options.filter - Optional filter
|
|
107
|
+
* @param _callbacks
|
|
108
|
+
*
|
|
109
|
+
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
|
|
110
|
+
*/
|
|
111
|
+
maxMarginalRelevanceSearch(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>, _callbacks?: undefined): Promise<Document[]>;
|
|
86
112
|
/**
|
|
87
113
|
* Static method to create a new `WeaviateStore` instance from a list of
|
|
88
114
|
* texts. It first creates documents from the texts and metadata, then
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as uuid from "uuid";
|
|
2
2
|
import { VectorStore } from "./base.js";
|
|
3
3
|
import { Document } from "../document.js";
|
|
4
|
+
import { maximalMarginalRelevance } from "../util/math.js";
|
|
4
5
|
// Note this function is not generic, it is designed specifically for Weaviate
|
|
5
6
|
// https://weaviate.io/developers/weaviate/config-refs/datatypes#introduction
|
|
6
7
|
export const flattenObjectForWeaviate = (
|
|
@@ -208,11 +209,27 @@ export class WeaviateStore extends VectorStore {
|
|
|
208
209
|
* @returns An array of tuples, where each tuple contains a document and its similarity score.
|
|
209
210
|
*/
|
|
210
211
|
async similaritySearchVectorWithScore(query, k, filter) {
|
|
212
|
+
const resultsWithEmbedding = await this.similaritySearchVectorWithScoreAndEmbedding(query, k, filter);
|
|
213
|
+
return resultsWithEmbedding.map(([document, score, _embedding]) => [
|
|
214
|
+
document,
|
|
215
|
+
score,
|
|
216
|
+
]);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Method to perform a similarity search on the stored vectors in the
|
|
220
|
+
* Weaviate index. It returns the top k most similar documents, their
|
|
221
|
+
* similarity scores and embedding vectors.
|
|
222
|
+
* @param query The query vector.
|
|
223
|
+
* @param k The number of most similar documents to return.
|
|
224
|
+
* @param filter Optional filter to apply to the search.
|
|
225
|
+
* @returns An array of tuples, where each tuple contains a document, its similarity score and its embedding vector.
|
|
226
|
+
*/
|
|
227
|
+
async similaritySearchVectorWithScoreAndEmbedding(query, k, filter) {
|
|
211
228
|
try {
|
|
212
|
-
let builder =
|
|
229
|
+
let builder = this.client.graphql
|
|
213
230
|
.get()
|
|
214
231
|
.withClassName(this.indexName)
|
|
215
|
-
.withFields(`${this.queryAttrs.join(" ")} _additional { distance }`)
|
|
232
|
+
.withFields(`${this.queryAttrs.join(" ")} _additional { distance vector }`)
|
|
216
233
|
.withNearVector({
|
|
217
234
|
vector: query,
|
|
218
235
|
distance: filter?.distance,
|
|
@@ -234,6 +251,7 @@ export class WeaviateStore extends VectorStore {
|
|
|
234
251
|
metadata: rest,
|
|
235
252
|
}),
|
|
236
253
|
_additional.distance,
|
|
254
|
+
_additional.vector,
|
|
237
255
|
]);
|
|
238
256
|
}
|
|
239
257
|
return documents;
|
|
@@ -242,6 +260,31 @@ export class WeaviateStore extends VectorStore {
|
|
|
242
260
|
throw Error(`'Error in similaritySearch' ${e}`);
|
|
243
261
|
}
|
|
244
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Return documents selected using the maximal marginal relevance.
|
|
265
|
+
* Maximal marginal relevance optimizes for similarity to the query AND diversity
|
|
266
|
+
* among selected documents.
|
|
267
|
+
*
|
|
268
|
+
* @param {string} query - Text to look up documents similar to.
|
|
269
|
+
* @param {number} options.k - Number of documents to return.
|
|
270
|
+
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
|
|
271
|
+
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
|
|
272
|
+
* where 0 corresponds to maximum diversity and 1 to minimum diversity.
|
|
273
|
+
* @param {this["FilterType"]} options.filter - Optional filter
|
|
274
|
+
* @param _callbacks
|
|
275
|
+
*
|
|
276
|
+
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
|
|
277
|
+
*/
|
|
278
|
+
async maxMarginalRelevanceSearch(query, options, _callbacks) {
|
|
279
|
+
const { k, fetchK = 20, lambda = 0.5, filter } = options;
|
|
280
|
+
const queryEmbedding = await this.embeddings.embedQuery(query);
|
|
281
|
+
const allResults = await this.similaritySearchVectorWithScoreAndEmbedding(queryEmbedding, fetchK, filter);
|
|
282
|
+
const embeddingList = allResults.map(([_doc, _score, embedding]) => embedding);
|
|
283
|
+
const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, lambda, k);
|
|
284
|
+
return mmrIndexes
|
|
285
|
+
.filter((idx) => idx !== -1)
|
|
286
|
+
.map((idx) => allResults[idx][0]);
|
|
287
|
+
}
|
|
245
288
|
/**
|
|
246
289
|
* Static method to create a new `WeaviateStore` instance from a list of
|
|
247
290
|
* texts. It first creates documents from the texts and metadata, then
|
|
@@ -93,8 +93,9 @@ class XataVectorSearch extends base_js_1.VectorStore {
|
|
|
93
93
|
size: k,
|
|
94
94
|
filter,
|
|
95
95
|
});
|
|
96
|
+
return (
|
|
96
97
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
97
|
-
|
|
98
|
+
records?.map((record) => [
|
|
98
99
|
new document_js_1.Document({
|
|
99
100
|
pageContent: record.content,
|
|
100
101
|
metadata: Object.fromEntries(Object.entries(record).filter(([key]) => key !== "content" &&
|
|
@@ -103,7 +104,7 @@ class XataVectorSearch extends base_js_1.VectorStore {
|
|
|
103
104
|
key !== "id")),
|
|
104
105
|
}),
|
|
105
106
|
record.xata.score,
|
|
106
|
-
]);
|
|
107
|
+
]) ?? []);
|
|
107
108
|
}
|
|
108
109
|
}
|
|
109
110
|
exports.XataVectorSearch = XataVectorSearch;
|
|
@@ -90,8 +90,9 @@ export class XataVectorSearch extends VectorStore {
|
|
|
90
90
|
size: k,
|
|
91
91
|
filter,
|
|
92
92
|
});
|
|
93
|
+
return (
|
|
93
94
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
-
|
|
95
|
+
records?.map((record) => [
|
|
95
96
|
new Document({
|
|
96
97
|
pageContent: record.content,
|
|
97
98
|
metadata: Object.fromEntries(Object.entries(record).filter(([key]) => key !== "content" &&
|
|
@@ -100,6 +101,6 @@ export class XataVectorSearch extends VectorStore {
|
|
|
100
101
|
key !== "id")),
|
|
101
102
|
}),
|
|
102
103
|
record.xata.score,
|
|
103
|
-
]);
|
|
104
|
+
]) ?? []);
|
|
104
105
|
}
|
|
105
106
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../dist/document_loaders/fs/chatgpt.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/document_loaders/fs/chatgpt.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/document_loaders/fs/chatgpt.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../dist/document_loaders/fs/pptx.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/document_loaders/fs/pptx.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/document_loaders/fs/pptx.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../dist/experimental/tools/pyinterpreter.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/experimental/tools/pyinterpreter.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../../dist/experimental/tools/pyinterpreter.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/llms/gradient_ai.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/llms/gradient_ai.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/llms/gradient_ai.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/llms/watsonx_ai.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/llms/watsonx_ai.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/llms/watsonx_ai.js'
|