langchain 0.0.166 → 0.0.167

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/embeddings/bedrock.cjs +43 -22
  2. package/dist/embeddings/bedrock.d.ts +11 -4
  3. package/dist/embeddings/bedrock.js +43 -22
  4. package/dist/llms/yandex.cjs +100 -0
  5. package/dist/llms/yandex.d.ts +40 -0
  6. package/dist/llms/yandex.js +96 -0
  7. package/dist/load/import_constants.cjs +2 -0
  8. package/dist/load/import_constants.js +2 -0
  9. package/dist/load/import_map.cjs +4 -2
  10. package/dist/load/import_map.d.ts +2 -0
  11. package/dist/load/import_map.js +2 -0
  12. package/dist/retrievers/multi_vector.d.ts +3 -3
  13. package/dist/retrievers/parent_document.cjs +6 -16
  14. package/dist/retrievers/parent_document.d.ts +5 -12
  15. package/dist/retrievers/parent_document.js +6 -16
  16. package/dist/schema/storage.d.ts +28 -1
  17. package/dist/storage/encoder_backed.cjs +14 -2
  18. package/dist/storage/encoder_backed.d.ts +2 -0
  19. package/dist/storage/encoder_backed.js +12 -1
  20. package/dist/storage/in_memory.cjs +1 -1
  21. package/dist/storage/in_memory.js +1 -1
  22. package/dist/storage/ioredis.cjs +4 -4
  23. package/dist/storage/ioredis.js +4 -4
  24. package/dist/storage/vercel_kv.cjs +146 -0
  25. package/dist/storage/vercel_kv.d.ts +46 -0
  26. package/dist/storage/vercel_kv.js +142 -0
  27. package/dist/stores/doc/in_memory.cjs +13 -0
  28. package/dist/stores/doc/in_memory.d.ts +6 -1
  29. package/dist/stores/doc/in_memory.js +13 -0
  30. package/dist/vectorstores/cassandra.cjs +4 -2
  31. package/dist/vectorstores/cassandra.js +4 -2
  32. package/dist/vectorstores/elasticsearch.cjs +3 -1
  33. package/dist/vectorstores/elasticsearch.js +3 -1
  34. package/dist/vectorstores/neo4j_vector.cjs +578 -0
  35. package/dist/vectorstores/neo4j_vector.d.ts +61 -0
  36. package/dist/vectorstores/neo4j_vector.js +548 -0
  37. package/llms/yandex.cjs +1 -0
  38. package/llms/yandex.d.ts +1 -0
  39. package/llms/yandex.js +1 -0
  40. package/package.json +38 -1
  41. package/storage/encoder_backed.cjs +1 -0
  42. package/storage/encoder_backed.d.ts +1 -0
  43. package/storage/encoder_backed.js +1 -0
  44. package/storage/vercel_kv.cjs +1 -0
  45. package/storage/vercel_kv.d.ts +1 -0
  46. package/storage/vercel_kv.js +1 -0
  47. package/vectorstores/neo4j_vector.cjs +1 -0
  48. package/vectorstores/neo4j_vector.d.ts +1 -0
  49. package/vectorstores/neo4j_vector.js +1 -0
@@ -0,0 +1,61 @@
1
+ import { Document } from "../document.js";
2
+ import { Embeddings } from "../embeddings/base.js";
3
+ import { VectorStore } from "./base.js";
4
+ export type SearchType = "vector" | "hybrid";
5
+ export type DistanceStrategy = "euclidean" | "cosine";
6
+ interface Neo4jVectorStoreArgs {
7
+ url: string;
8
+ username: string;
9
+ password: string;
10
+ database?: string;
11
+ preDeleteCollection?: boolean;
12
+ textNodeProperty?: string;
13
+ textNodeProperties?: string[];
14
+ embeddingNodeProperty?: string;
15
+ keywordIndexName?: string;
16
+ indexName?: string;
17
+ searchType?: SearchType;
18
+ retrievalQuery?: string;
19
+ nodeLabel?: string;
20
+ createIdIndex?: boolean;
21
+ }
22
+ export declare class Neo4jVectorStore extends VectorStore {
23
+ private driver;
24
+ private database;
25
+ private preDeleteCollection;
26
+ private nodeLabel;
27
+ private embeddingNodeProperty;
28
+ private embeddingDimension;
29
+ private textNodeProperty;
30
+ private keywordIndexName;
31
+ private indexName;
32
+ private retrievalQuery;
33
+ private searchType;
34
+ private distanceStrategy;
35
+ _vectorstoreType(): string;
36
+ constructor(embeddings: Embeddings, config: Neo4jVectorStoreArgs);
37
+ static initialize(embeddings: Embeddings, config: Neo4jVectorStoreArgs): Promise<Neo4jVectorStore>;
38
+ _initializeDriver({ url, username, password, database, }: Neo4jVectorStoreArgs): Promise<void>;
39
+ _verifyConnectivity(): Promise<void>;
40
+ close(): Promise<void>;
41
+ _dropIndex(): Promise<void>;
42
+ query(query: string, params?: any): Promise<any[]>;
43
+ static fromTexts(texts: string[], metadatas: any, embeddings: Embeddings, config: Neo4jVectorStoreArgs): Promise<Neo4jVectorStore>;
44
+ static fromDocuments(docs: Document[], embeddings: Embeddings, config: Neo4jVectorStoreArgs): Promise<Neo4jVectorStore>;
45
+ static fromExistingIndex(embeddings: Embeddings, config: Neo4jVectorStoreArgs): Promise<Neo4jVectorStore>;
46
+ static fromExistingGraph(embeddings: Embeddings, config: Neo4jVectorStoreArgs): Promise<Neo4jVectorStore>;
47
+ createNewIndex(): Promise<void>;
48
+ retrieveExistingIndex(): Promise<number | null>;
49
+ retrieveExistingFtsIndex(textNodeProperties?: string[]): Promise<string | null>;
50
+ createNewKeywordIndex(textNodeProperties?: string[]): Promise<void>;
51
+ sortByIndexName(values: Array<{
52
+ [key: string]: any;
53
+ }>, indexName: string): Array<{
54
+ [key: string]: any;
55
+ }>;
56
+ addVectors(vectors: number[][], documents: Document[], metadatas?: Record<string, any>[], ids?: string[]): Promise<string[]>;
57
+ addDocuments(documents: Document[]): Promise<string[]>;
58
+ similaritySearch(query: string, k?: number): Promise<Document[]>;
59
+ similaritySearchVectorWithScore(vector: number[], k: number, query: string): Promise<[Document, number][]>;
60
+ }
61
+ export {};
@@ -0,0 +1,548 @@
1
+ import neo4j from "neo4j-driver";
2
+ import * as uuid from "uuid";
3
+ import { Document } from "../document.js";
4
+ import { VectorStore } from "./base.js";
5
+ const DEFAULT_SEARCH_TYPE = "vector";
6
+ const DEFAULT_DISTANCE_STRATEGY = "cosine";
7
+ export class Neo4jVectorStore extends VectorStore {
8
+ _vectorstoreType() {
9
+ return "neo4jvector";
10
+ }
11
+ constructor(embeddings, config) {
12
+ super(embeddings, config);
13
+ Object.defineProperty(this, "driver", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "database", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ Object.defineProperty(this, "preDeleteCollection", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ Object.defineProperty(this, "nodeLabel", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ Object.defineProperty(this, "embeddingNodeProperty", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: void 0
42
+ });
43
+ Object.defineProperty(this, "embeddingDimension", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: void 0
48
+ });
49
+ Object.defineProperty(this, "textNodeProperty", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ Object.defineProperty(this, "keywordIndexName", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ Object.defineProperty(this, "indexName", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: void 0
66
+ });
67
+ Object.defineProperty(this, "retrievalQuery", {
68
+ enumerable: true,
69
+ configurable: true,
70
+ writable: true,
71
+ value: void 0
72
+ });
73
+ Object.defineProperty(this, "searchType", {
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ value: void 0
78
+ });
79
+ Object.defineProperty(this, "distanceStrategy", {
80
+ enumerable: true,
81
+ configurable: true,
82
+ writable: true,
83
+ value: DEFAULT_DISTANCE_STRATEGY
84
+ });
85
+ }
86
+ static async initialize(embeddings, config) {
87
+ const store = new Neo4jVectorStore(embeddings, config);
88
+ await store._initializeDriver(config);
89
+ await store._verifyConnectivity();
90
+ const { preDeleteCollection = false, nodeLabel = "Chunk", textNodeProperty = "text", embeddingNodeProperty = "embedding", keywordIndexName = "keyword", indexName = "vector", retrievalQuery = "", searchType = DEFAULT_SEARCH_TYPE, } = config;
91
+ store.embeddingDimension = (await embeddings.embedQuery("foo")).length;
92
+ store.preDeleteCollection = preDeleteCollection;
93
+ store.nodeLabel = nodeLabel;
94
+ store.textNodeProperty = textNodeProperty;
95
+ store.embeddingNodeProperty = embeddingNodeProperty;
96
+ store.keywordIndexName = keywordIndexName;
97
+ store.indexName = indexName;
98
+ store.retrievalQuery = retrievalQuery;
99
+ store.searchType = searchType;
100
+ if (store.preDeleteCollection) {
101
+ await store._dropIndex();
102
+ }
103
+ return store;
104
+ }
105
+ async _initializeDriver({ url, username, password, database = "neo4j", }) {
106
+ try {
107
+ this.driver = neo4j.driver(url, neo4j.auth.basic(username, password));
108
+ this.database = database;
109
+ }
110
+ catch (error) {
111
+ throw new Error("Could not create a Neo4j driver instance. Please check the connection details.");
112
+ }
113
+ }
114
+ async _verifyConnectivity() {
115
+ await this.driver.verifyAuthentication();
116
+ }
117
+ async close() {
118
+ await this.driver.close();
119
+ }
120
+ async _dropIndex() {
121
+ try {
122
+ await this.query(`
123
+ MATCH (n:\`${this.nodeLabel}\`)
124
+ CALL {
125
+ WITH n
126
+ DETACH DELETE n
127
+ }
128
+ IN TRANSACTIONS OF 10000 ROWS;
129
+ `);
130
+ await this.query(`DROP INDEX ${this.indexName}`);
131
+ }
132
+ catch (error) {
133
+ console.error("An error occurred while dropping the index:", error);
134
+ }
135
+ }
136
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
137
+ async query(query, params = {}) {
138
+ const session = this.driver.session({ database: this.database });
139
+ const result = await session.run(query, params);
140
+ return toObjects(result.records);
141
+ }
142
+ static async fromTexts(texts,
143
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
+ metadatas, embeddings, config) {
145
+ const docs = [];
146
+ for (let i = 0; i < texts.length; i += 1) {
147
+ const metadata = Array.isArray(metadatas) ? metadatas[i] : metadatas;
148
+ const newDoc = new Document({
149
+ pageContent: texts[i],
150
+ metadata,
151
+ });
152
+ docs.push(newDoc);
153
+ }
154
+ return Neo4jVectorStore.fromDocuments(docs, embeddings, config);
155
+ }
156
+ static async fromDocuments(docs, embeddings, config) {
157
+ const { searchType = DEFAULT_SEARCH_TYPE, createIdIndex = true, textNodeProperties = [], } = config;
158
+ const store = await this.initialize(embeddings, config);
159
+ const embeddingDimension = await store.retrieveExistingIndex();
160
+ if (!embeddingDimension) {
161
+ await store.createNewIndex();
162
+ }
163
+ else if (store.embeddingDimension !== embeddingDimension) {
164
+ throw new Error(`Index with name "${store.indexName}" already exists. The provided embedding function and vector index dimensions do not match.
165
+ Embedding function dimension: ${store.embeddingDimension}
166
+ Vector index dimension: ${embeddingDimension}`);
167
+ }
168
+ if (searchType === "hybrid") {
169
+ const ftsNodeLabel = await store.retrieveExistingFtsIndex();
170
+ if (!ftsNodeLabel) {
171
+ await store.createNewKeywordIndex(textNodeProperties);
172
+ }
173
+ else {
174
+ if (ftsNodeLabel !== store.nodeLabel) {
175
+ throw Error("Vector and keyword index don't index the same node label");
176
+ }
177
+ }
178
+ }
179
+ if (createIdIndex) {
180
+ await store.query(`CREATE CONSTRAINT IF NOT EXISTS FOR (n:${store.nodeLabel}) REQUIRE n.id IS UNIQUE;`);
181
+ }
182
+ await store.addDocuments(docs);
183
+ return store;
184
+ }
185
+ static async fromExistingIndex(embeddings, config) {
186
+ const { searchType = DEFAULT_SEARCH_TYPE, keywordIndexName = "keyword" } = config;
187
+ if (searchType === "hybrid" && !keywordIndexName) {
188
+ throw Error("keyword_index name has to be specified when using hybrid search option");
189
+ }
190
+ const store = await this.initialize(embeddings, config);
191
+ const embeddingDimension = await store.retrieveExistingIndex();
192
+ if (!embeddingDimension) {
193
+ throw Error("The specified vector index name does not exist. Make sure to check if you spelled it correctly");
194
+ }
195
+ if (store.embeddingDimension !== embeddingDimension) {
196
+ throw new Error(`The provided embedding function and vector index dimensions do not match.
197
+ Embedding function dimension: ${store.embeddingDimension}
198
+ Vector index dimension: ${embeddingDimension}`);
199
+ }
200
+ if (searchType === "hybrid") {
201
+ const ftsNodeLabel = await store.retrieveExistingFtsIndex();
202
+ if (!ftsNodeLabel) {
203
+ throw Error("The specified keyword index name does not exist. Make sure to check if you spelled it correctly");
204
+ }
205
+ else {
206
+ if (ftsNodeLabel !== store.nodeLabel) {
207
+ throw Error("Vector and keyword index don't index the same node label");
208
+ }
209
+ }
210
+ }
211
+ return store;
212
+ }
213
+ static async fromExistingGraph(embeddings, config) {
214
+ const { textNodeProperties = [], embeddingNodeProperty, searchType = DEFAULT_SEARCH_TYPE, retrievalQuery = "", nodeLabel, } = config;
215
+ let _retrievalQuery = retrievalQuery;
216
+ if (textNodeProperties.length === 0) {
217
+ throw Error("Parameter `text_node_properties` must not be an empty array");
218
+ }
219
+ if (!retrievalQuery) {
220
+ _retrievalQuery = `
221
+ RETURN reduce(str='', k IN ${JSON.stringify(textNodeProperties)} |
222
+ str + '\\n' + k + ': ' + coalesce(node[k], '')) AS text,
223
+ node {.*, \`${embeddingNodeProperty}\`: Null, id: Null, ${textNodeProperties
224
+ .map((prop) => `\`${prop}\`: Null`)
225
+ .join(", ")} } AS metadata, score
226
+ `;
227
+ }
228
+ const store = await this.initialize(embeddings, {
229
+ ...config,
230
+ retrievalQuery: _retrievalQuery,
231
+ });
232
+ const embeddingDimension = await store.retrieveExistingIndex();
233
+ if (!embeddingDimension) {
234
+ await store.createNewIndex();
235
+ }
236
+ else if (store.embeddingDimension !== embeddingDimension) {
237
+ throw new Error(`Index with name ${store.indexName} already exists. The provided embedding function and vector index dimensions do not match.\nEmbedding function dimension: ${store.embeddingDimension}\nVector index dimension: ${embeddingDimension}`);
238
+ }
239
+ if (searchType === "hybrid") {
240
+ const ftsNodeLabel = await store.retrieveExistingFtsIndex(textNodeProperties);
241
+ if (!ftsNodeLabel) {
242
+ await store.createNewKeywordIndex(textNodeProperties);
243
+ }
244
+ else {
245
+ if (ftsNodeLabel !== store.nodeLabel) {
246
+ throw Error("Vector and keyword index don't index the same node label");
247
+ }
248
+ }
249
+ }
250
+ // eslint-disable-next-line no-constant-condition
251
+ while (true) {
252
+ const fetchQuery = `
253
+ MATCH (n:\`${nodeLabel}\`)
254
+ WHERE n.${embeddingNodeProperty} IS null
255
+ AND any(k in $props WHERE n[k] IS NOT null)
256
+ RETURN elementId(n) AS id, reduce(str='', k IN $props |
257
+ str + '\\n' + k + ':' + coalesce(n[k], '')) AS text
258
+ LIMIT 1000
259
+ `;
260
+ const data = await store.query(fetchQuery, { props: textNodeProperties });
261
+ if (!data) {
262
+ continue;
263
+ }
264
+ const textEmbeddings = await embeddings.embedDocuments(data.map((el) => el.text));
265
+ const params = {
266
+ data: data.map((el, index) => ({
267
+ id: el.id,
268
+ embedding: textEmbeddings[index],
269
+ })),
270
+ };
271
+ await store.query(`
272
+ UNWIND $data AS row
273
+ MATCH (n:\`${nodeLabel}\`)
274
+ WHERE elementId(n) = row.id
275
+ CALL db.create.setVectorProperty(n, '${embeddingNodeProperty}', row.embedding)
276
+ YIELD node RETURN count(*)
277
+ `, params);
278
+ if (data.length < 1000) {
279
+ break;
280
+ }
281
+ }
282
+ return store;
283
+ }
284
+ async createNewIndex() {
285
+ const indexQuery = `
286
+ CALL db.index.vector.createNodeIndex(
287
+ $index_name,
288
+ $node_label,
289
+ $embedding_node_property,
290
+ toInteger($embedding_dimension),
291
+ $similarity_metric
292
+ )
293
+ `;
294
+ const parameters = {
295
+ index_name: this.indexName,
296
+ node_label: this.nodeLabel,
297
+ embedding_node_property: this.embeddingNodeProperty,
298
+ embedding_dimension: this.embeddingDimension,
299
+ similarity_metric: this.distanceStrategy,
300
+ };
301
+ await this.query(indexQuery, parameters);
302
+ }
303
+ async retrieveExistingIndex() {
304
+ let indexInformation = await this.query(`
305
+ SHOW INDEXES YIELD name, type, labelsOrTypes, properties, options
306
+ WHERE type = 'VECTOR' AND (name = $index_name
307
+ OR (labelsOrTypes[0] = $node_label AND
308
+ properties[0] = $embedding_node_property))
309
+ RETURN name, labelsOrTypes, properties, options
310
+ `, {
311
+ index_name: this.indexName,
312
+ node_label: this.nodeLabel,
313
+ embedding_node_property: this.embeddingNodeProperty,
314
+ });
315
+ if (indexInformation) {
316
+ indexInformation = this.sortByIndexName(indexInformation, this.indexName);
317
+ try {
318
+ const [index] = indexInformation;
319
+ const [labelOrType] = index.labelsOrTypes;
320
+ const [property] = index.properties;
321
+ this.indexName = index.name;
322
+ this.nodeLabel = labelOrType;
323
+ this.embeddingNodeProperty = property;
324
+ const embeddingDimension = index.options.indexConfig["vector.dimensions"];
325
+ return Number(embeddingDimension);
326
+ }
327
+ catch (error) {
328
+ return null;
329
+ }
330
+ }
331
+ return null;
332
+ }
333
+ async retrieveExistingFtsIndex(textNodeProperties = []) {
334
+ const indexInformation = await this.query(`
335
+ SHOW INDEXES YIELD name, type, labelsOrTypes, properties, options
336
+ WHERE type = 'FULLTEXT' AND (name = $keyword_index_name
337
+ OR (labelsOrTypes = [$node_label] AND
338
+ properties = $text_node_property))
339
+ RETURN name, labelsOrTypes, properties, options
340
+ `, {
341
+ keyword_index_name: this.keywordIndexName,
342
+ node_label: this.nodeLabel,
343
+ text_node_property: textNodeProperties.length > 0
344
+ ? textNodeProperties
345
+ : [this.textNodeProperty],
346
+ });
347
+ if (indexInformation) {
348
+ // Sort the index information by index name
349
+ const sortedIndexInformation = this.sortByIndexName(indexInformation, this.indexName);
350
+ try {
351
+ const [index] = sortedIndexInformation;
352
+ const [labelOrType] = index.labelsOrTypes;
353
+ const [property] = index.properties;
354
+ this.keywordIndexName = index.name;
355
+ this.textNodeProperty = property;
356
+ this.nodeLabel = labelOrType;
357
+ return labelOrType;
358
+ }
359
+ catch (error) {
360
+ return null;
361
+ }
362
+ }
363
+ return null;
364
+ }
365
+ async createNewKeywordIndex(textNodeProperties = []) {
366
+ const nodeProps = textNodeProperties.length > 0
367
+ ? textNodeProperties
368
+ : [this.textNodeProperty];
369
+ // Construct the Cypher query to create a new full text index
370
+ const ftsIndexQuery = `
371
+ CREATE FULLTEXT INDEX ${this.keywordIndexName}
372
+ FOR (n:\`${this.nodeLabel}\`) ON EACH
373
+ [${nodeProps.map((prop) => `n.\`${prop}\``).join(", ")}]
374
+ `;
375
+ await this.query(ftsIndexQuery);
376
+ }
377
+ sortByIndexName(
378
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
379
+ values, indexName
380
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
381
+ ) {
382
+ return values.sort((a, b) => (a.index_name === indexName ? -1 : 0) -
383
+ (b.index_name === indexName ? -1 : 0));
384
+ }
385
+ async addVectors(vectors, documents,
386
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
387
+ metadatas, ids) {
388
+ let _ids = ids;
389
+ let _metadatas = metadatas;
390
+ if (!_ids) {
391
+ _ids = documents.map(() => uuid.v1());
392
+ }
393
+ if (!metadatas) {
394
+ _metadatas = documents.map(() => ({}));
395
+ }
396
+ const importQuery = `
397
+ UNWIND $data AS row
398
+ CALL {
399
+ WITH row
400
+ MERGE (c:\`${this.nodeLabel}\` {id: row.id})
401
+ WITH c, row
402
+ CALL db.create.setVectorProperty(c, '${this.embeddingNodeProperty}', row.embedding)
403
+ YIELD node
404
+ SET c.\`${this.textNodeProperty}\` = row.text
405
+ SET c += row.metadata
406
+ } IN TRANSACTIONS OF 1000 ROWS
407
+ `;
408
+ const parameters = {
409
+ data: documents.map(({ pageContent, metadata }, index) => ({
410
+ text: pageContent,
411
+ metadata: _metadatas ? _metadatas[index] : metadata,
412
+ embedding: vectors[index],
413
+ id: _ids ? _ids[index] : null,
414
+ })),
415
+ };
416
+ await this.query(importQuery, parameters);
417
+ return _ids;
418
+ }
419
+ async addDocuments(documents) {
420
+ const texts = documents.map(({ pageContent }) => pageContent);
421
+ return this.addVectors(await this.embeddings.embedDocuments(texts), documents);
422
+ }
423
+ async similaritySearch(query, k = 4) {
424
+ const embedding = await this.embeddings.embedQuery(query);
425
+ const results = await this.similaritySearchVectorWithScore(embedding, k, query);
426
+ return results.map((result) => result[0]);
427
+ }
428
+ async similaritySearchVectorWithScore(vector, k, query) {
429
+ const defaultRetrieval = `
430
+ RETURN node.${this.textNodeProperty} AS text, score,
431
+ node {.*, ${this.textNodeProperty}: Null,
432
+ ${this.embeddingNodeProperty}: Null, id: Null } AS metadata
433
+ `;
434
+ const retrievalQuery = this.retrievalQuery
435
+ ? this.retrievalQuery
436
+ : defaultRetrieval;
437
+ const readQuery = `${getSearchIndexQuery(this.searchType)} ${retrievalQuery}`;
438
+ const parameters = {
439
+ index: this.indexName,
440
+ k: Number(k),
441
+ embedding: vector,
442
+ keyword_index: this.keywordIndexName,
443
+ query,
444
+ };
445
+ const results = await this.query(readQuery, parameters);
446
+ if (results) {
447
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
448
+ const docs = results.map((result) => [
449
+ new Document({
450
+ pageContent: result.text,
451
+ metadata: Object.fromEntries(Object.entries(result.metadata).filter(([_, v]) => v !== null)),
452
+ }),
453
+ result.score,
454
+ ]);
455
+ return docs;
456
+ }
457
+ return [];
458
+ }
459
+ }
460
+ function toObjects(records) {
461
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
462
+ const recordValues = records.map((record) => {
463
+ const rObj = record.toObject();
464
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
465
+ const out = {};
466
+ Object.keys(rObj).forEach((key) => {
467
+ out[key] = itemIntToString(rObj[key]);
468
+ });
469
+ return out;
470
+ });
471
+ return recordValues;
472
+ }
473
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
474
+ function itemIntToString(item) {
475
+ if (neo4j.isInt(item))
476
+ return item.toString();
477
+ if (Array.isArray(item))
478
+ return item.map((ii) => itemIntToString(ii));
479
+ if (["number", "string", "boolean"].indexOf(typeof item) !== -1)
480
+ return item;
481
+ if (item === null)
482
+ return item;
483
+ if (typeof item === "object")
484
+ return objIntToString(item);
485
+ }
486
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
487
+ function objIntToString(obj) {
488
+ const entry = extractFromNeoObjects(obj);
489
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
490
+ let newObj = null;
491
+ if (Array.isArray(entry)) {
492
+ newObj = entry.map((item) => itemIntToString(item));
493
+ }
494
+ else if (entry !== null && typeof entry === "object") {
495
+ newObj = {};
496
+ Object.keys(entry).forEach((key) => {
497
+ newObj[key] = itemIntToString(entry[key]);
498
+ });
499
+ }
500
+ return newObj;
501
+ }
502
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
503
+ function extractFromNeoObjects(obj) {
504
+ if (
505
+ // eslint-disable-next-line
506
+ obj instanceof neo4j.types.Node ||
507
+ // eslint-disable-next-line
508
+ obj instanceof neo4j.types.Relationship) {
509
+ return obj.properties;
510
+ // eslint-disable-next-line
511
+ }
512
+ else if (obj instanceof neo4j.types.Path) {
513
+ // eslint-disable-next-line
514
+ return [].concat.apply([], extractPathForRows(obj));
515
+ }
516
+ return obj;
517
+ }
518
+ function extractPathForRows(path) {
519
+ let { segments } = path;
520
+ // Zero length path. No relationship, end === start
521
+ if (!Array.isArray(path.segments) || path.segments.length < 1) {
522
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
523
+ segments = [{ ...path, end: null }];
524
+ }
525
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
526
+ return segments.map((segment) => [
527
+ objIntToString(segment.start),
528
+ objIntToString(segment.relationship),
529
+ objIntToString(segment.end),
530
+ ].filter((part) => part !== null));
531
+ }
532
+ function getSearchIndexQuery(searchType) {
533
+ const typeToQueryMap = {
534
+ vector: "CALL db.index.vector.queryNodes($index, $k, $embedding) YIELD node, score",
535
+ hybrid: `
536
+ CALL {
537
+ CALL db.index.vector.queryNodes($index, $k, $embedding) YIELD node, score
538
+ RETURN node, score UNION
539
+ CALL db.index.fulltext.queryNodes($keyword_index, $query, {limit: $k}) YIELD node, score
540
+ WITH collect({node: node, score: score}) AS nodes, max(score) AS max
541
+ UNWIND nodes AS n
542
+ RETURN n.node AS node, (n.score / max) AS score
543
+ }
544
+ WITH node, max(score) AS score ORDER BY score DESC LIMIT toInteger($k)
545
+ `,
546
+ };
547
+ return typeToQueryMap[searchType];
548
+ }
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/llms/yandex.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/llms/yandex.js'
package/llms/yandex.js ADDED
@@ -0,0 +1 @@
1
+ export * from '../dist/llms/yandex.js'