langchain 0.0.164 → 0.0.166

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 (84) hide show
  1. package/chat_models/portkey.cjs +1 -0
  2. package/chat_models/portkey.d.ts +1 -0
  3. package/chat_models/portkey.js +1 -0
  4. package/dist/cache/momento.cjs +7 -7
  5. package/dist/cache/momento.d.ts +1 -1
  6. package/dist/cache/momento.js +1 -1
  7. package/dist/chat_models/portkey.cjs +159 -0
  8. package/dist/chat_models/portkey.d.ts +17 -0
  9. package/dist/chat_models/portkey.js +155 -0
  10. package/dist/document_loaders/fs/unstructured.cjs +40 -0
  11. package/dist/document_loaders/fs/unstructured.d.ts +27 -0
  12. package/dist/document_loaders/fs/unstructured.js +40 -0
  13. package/dist/embeddings/minimax.cjs +1 -1
  14. package/dist/embeddings/minimax.js +1 -1
  15. package/dist/experimental/chat_models/anthropic_functions.cjs +34 -2
  16. package/dist/experimental/chat_models/anthropic_functions.d.ts +15 -5
  17. package/dist/experimental/chat_models/anthropic_functions.js +34 -2
  18. package/dist/llms/portkey.cjs +147 -0
  19. package/dist/llms/portkey.d.ts +33 -0
  20. package/dist/llms/portkey.js +138 -0
  21. package/dist/llms/sagemaker_endpoint.cjs +76 -14
  22. package/dist/llms/sagemaker_endpoint.d.ts +39 -20
  23. package/dist/llms/sagemaker_endpoint.js +77 -15
  24. package/dist/load/import_constants.cjs +4 -0
  25. package/dist/load/import_constants.js +4 -0
  26. package/dist/load/import_map.cjs +3 -1
  27. package/dist/load/import_map.d.ts +2 -0
  28. package/dist/load/import_map.js +2 -0
  29. package/dist/output_parsers/list.cjs +1 -1
  30. package/dist/output_parsers/list.js +1 -1
  31. package/dist/prompts/chat.cjs +19 -2
  32. package/dist/prompts/chat.d.ts +1 -0
  33. package/dist/prompts/chat.js +19 -2
  34. package/dist/retrievers/self_query/base.cjs +4 -1
  35. package/dist/retrievers/self_query/base.d.ts +3 -2
  36. package/dist/retrievers/self_query/base.js +4 -1
  37. package/dist/retrievers/self_query/index.cjs +2 -2
  38. package/dist/retrievers/self_query/index.d.ts +2 -0
  39. package/dist/retrievers/self_query/index.js +2 -2
  40. package/dist/runnables/remote.cjs +225 -0
  41. package/dist/runnables/remote.d.ts +28 -0
  42. package/dist/runnables/remote.js +221 -0
  43. package/dist/schema/index.cjs +1 -1
  44. package/dist/schema/index.d.ts +1 -1
  45. package/dist/schema/index.js +1 -1
  46. package/dist/schema/runnable/base.cjs +4 -4
  47. package/dist/schema/runnable/base.d.ts +9 -7
  48. package/dist/schema/runnable/base.js +4 -4
  49. package/dist/schema/runnable/remote.cjs +225 -0
  50. package/dist/schema/runnable/remote.d.ts +28 -0
  51. package/dist/schema/runnable/remote.js +221 -0
  52. package/dist/stores/message/momento.cjs +11 -11
  53. package/dist/stores/message/momento.d.ts +1 -1
  54. package/dist/stores/message/momento.js +1 -1
  55. package/dist/util/time.cjs +14 -0
  56. package/dist/util/time.d.ts +6 -0
  57. package/dist/util/time.js +10 -0
  58. package/dist/vectorstores/cassandra.cjs +212 -0
  59. package/dist/vectorstores/cassandra.d.ts +98 -0
  60. package/dist/vectorstores/cassandra.js +208 -0
  61. package/dist/vectorstores/momento_vector_index.cjs +292 -0
  62. package/dist/vectorstores/momento_vector_index.d.ts +135 -0
  63. package/dist/vectorstores/momento_vector_index.js +265 -0
  64. package/dist/vectorstores/mongodb_atlas.cjs +29 -39
  65. package/dist/vectorstores/mongodb_atlas.js +29 -39
  66. package/dist/vectorstores/supabase.cjs +37 -8
  67. package/dist/vectorstores/supabase.d.ts +28 -1
  68. package/dist/vectorstores/supabase.js +37 -8
  69. package/llms/portkey.cjs +1 -0
  70. package/llms/portkey.d.ts +1 -0
  71. package/llms/portkey.js +1 -0
  72. package/package.json +77 -4
  73. package/runnables/remote.cjs +1 -0
  74. package/runnables/remote.d.ts +1 -0
  75. package/runnables/remote.js +1 -0
  76. package/util/time.cjs +1 -0
  77. package/util/time.d.ts +1 -0
  78. package/util/time.js +1 -0
  79. package/vectorstores/cassandra.cjs +1 -0
  80. package/vectorstores/cassandra.d.ts +1 -0
  81. package/vectorstores/cassandra.js +1 -0
  82. package/vectorstores/momento_vector_index.cjs +1 -0
  83. package/vectorstores/momento_vector_index.d.ts +1 -0
  84. package/vectorstores/momento_vector_index.js +1 -0
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CassandraStore = void 0;
4
+ /* eslint-disable prefer-template */
5
+ const cassandra_driver_1 = require("cassandra-driver");
6
+ const base_js_1 = require("./base.cjs");
7
+ const document_js_1 = require("../document.cjs");
8
+ /**
9
+ * Class for interacting with the Cassandra database. It extends the
10
+ * VectorStore class and provides methods for adding vectors and
11
+ * documents, searching for similar vectors, and creating instances from
12
+ * texts or documents.
13
+ */
14
+ class CassandraStore extends base_js_1.VectorStore {
15
+ _vectorstoreType() {
16
+ return "cassandra";
17
+ }
18
+ constructor(embeddings, args) {
19
+ super(embeddings, args);
20
+ Object.defineProperty(this, "client", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "dimensions", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "keyspace", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ Object.defineProperty(this, "primaryKey", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ Object.defineProperty(this, "metadataColumns", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: void 0
49
+ });
50
+ Object.defineProperty(this, "table", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ Object.defineProperty(this, "isInitialized", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: false
61
+ });
62
+ this.client = new cassandra_driver_1.Client(args);
63
+ this.dimensions = args.dimensions;
64
+ this.keyspace = args.keyspace;
65
+ this.table = args.table;
66
+ this.primaryKey = args.primaryKey;
67
+ this.metadataColumns = args.metadataColumns;
68
+ }
69
+ /**
70
+ * Method to save vectors to the Cassandra database.
71
+ * @param vectors Vectors to save.
72
+ * @param documents The documents associated with the vectors.
73
+ * @returns Promise that resolves when the vectors have been added.
74
+ */
75
+ async addVectors(vectors, documents) {
76
+ if (vectors.length === 0) {
77
+ return;
78
+ }
79
+ if (!this.isInitialized) {
80
+ await this.initialize();
81
+ }
82
+ const queries = this.buildInsertQuery(vectors, documents);
83
+ await this.client.batch(queries);
84
+ }
85
+ /**
86
+ * Method to add documents to the Cassandra database.
87
+ * @param documents The documents to add.
88
+ * @returns Promise that resolves when the documents have been added.
89
+ */
90
+ async addDocuments(documents) {
91
+ return this.addVectors(await this.embeddings.embedDocuments(documents.map((d) => d.pageContent)), documents);
92
+ }
93
+ /**
94
+ * Method to search for vectors that are similar to a given query vector.
95
+ * @param query The query vector.
96
+ * @param k The number of similar vectors to return.
97
+ * @returns Promise that resolves with an array of tuples, each containing a Document and a score.
98
+ */
99
+ async similaritySearchVectorWithScore(query, k) {
100
+ if (!this.isInitialized) {
101
+ await this.initialize();
102
+ }
103
+ const queryStr = this.buildSearchQuery(query, k);
104
+ const queryResultSet = await this.client.execute(queryStr);
105
+ return queryResultSet?.rows.map((row, index) => {
106
+ const textContent = row.text;
107
+ const sanitizedRow = Object.assign(row, {});
108
+ delete sanitizedRow.vector;
109
+ delete sanitizedRow.text;
110
+ return [
111
+ new document_js_1.Document({ pageContent: textContent, metadata: sanitizedRow }),
112
+ index,
113
+ ];
114
+ });
115
+ }
116
+ /**
117
+ * Static method to create an instance of CassandraStore from texts.
118
+ * @param texts The texts to use.
119
+ * @param metadatas The metadata associated with the texts.
120
+ * @param embeddings The embeddings to use.
121
+ * @param args The arguments for the CassandraStore.
122
+ * @returns Promise that resolves with a new instance of CassandraStore.
123
+ */
124
+ static async fromTexts(texts, metadatas, embeddings, args) {
125
+ const docs = [];
126
+ for (let index = 0; index < texts.length; index += 1) {
127
+ const metadata = Array.isArray(metadatas) ? metadatas[index] : metadatas;
128
+ const doc = new document_js_1.Document({
129
+ pageContent: texts[index],
130
+ metadata,
131
+ });
132
+ docs.push(doc);
133
+ }
134
+ return CassandraStore.fromDocuments(docs, embeddings, args);
135
+ }
136
+ /**
137
+ * Static method to create an instance of CassandraStore from documents.
138
+ * @param docs The documents to use.
139
+ * @param embeddings The embeddings to use.
140
+ * @param args The arguments for the CassandraStore.
141
+ * @returns Promise that resolves with a new instance of CassandraStore.
142
+ */
143
+ static async fromDocuments(docs, embeddings, args) {
144
+ const instance = new this(embeddings, args);
145
+ await instance.addDocuments(docs);
146
+ return instance;
147
+ }
148
+ /**
149
+ * Static method to create an instance of CassandraStore from an existing
150
+ * index.
151
+ * @param embeddings The embeddings to use.
152
+ * @param args The arguments for the CassandraStore.
153
+ * @returns Promise that resolves with a new instance of CassandraStore.
154
+ */
155
+ static async fromExistingIndex(embeddings, args) {
156
+ const instance = new this(embeddings, args);
157
+ await instance.initialize();
158
+ return instance;
159
+ }
160
+ /**
161
+ * Method to initialize the Cassandra database.
162
+ * @returns Promise that resolves when the database has been initialized.
163
+ */
164
+ async initialize() {
165
+ await this.client.execute(`CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
166
+ ${this.primaryKey.name} ${this.primaryKey.type} PRIMARY KEY,
167
+ text TEXT,
168
+ ${this.metadataColumns.length > 0
169
+ ? this.metadataColumns.map((col) => `${col.name} ${col.type},`)
170
+ : ""}
171
+ vector VECTOR<FLOAT, ${this.dimensions}>
172
+ );`);
173
+ await this.client.execute(`CREATE CUSTOM INDEX IF NOT EXISTS ann_index
174
+ ON ${this.keyspace}.${this.table}(vector) USING 'StorageAttachedIndex';`);
175
+ this.isInitialized = true;
176
+ }
177
+ /**
178
+ * Method to build an CQL query for inserting vectors and documents into
179
+ * the Cassandra database.
180
+ * @param vectors The vectors to insert.
181
+ * @param documents The documents to insert.
182
+ * @returns The CQL query string.
183
+ */
184
+ buildInsertQuery(vectors, documents) {
185
+ const queries = [];
186
+ for (let index = 0; index < vectors.length; index += 1) {
187
+ const vector = vectors[index];
188
+ const document = documents[index];
189
+ const metadataColNames = Object.keys(document.metadata);
190
+ const metadataVals = Object.values(document.metadata);
191
+ const query = `INSERT INTO ${this.keyspace}.${this.table} (vector, text${metadataColNames.length > 0 ? ", " + metadataColNames.join(", ") : ""}) VALUES ([${vector}], '${document.pageContent}'${metadataVals.length > 0
192
+ ? ", " +
193
+ metadataVals
194
+ .map((val) => (typeof val === "number" ? val : `'${val}'`))
195
+ .join(", ")
196
+ : ""});`;
197
+ queries.push(query);
198
+ }
199
+ return queries;
200
+ }
201
+ /**
202
+ * Method to build an CQL query for searching for similar vectors in the
203
+ * Cassandra database.
204
+ * @param query The query vector.
205
+ * @param k The number of similar vectors to return.
206
+ * @returns The CQL query string.
207
+ */
208
+ buildSearchQuery(query, k) {
209
+ return `SELECT * FROM ${this.keyspace}.${this.table} ORDER BY vector ANN OF [${query}] LIMIT ${k || 1};`;
210
+ }
211
+ }
212
+ exports.CassandraStore = CassandraStore;
@@ -0,0 +1,98 @@
1
+ import { DseClientOptions } from "cassandra-driver";
2
+ import { Embeddings } from "../embeddings/base.js";
3
+ import { VectorStore } from "./base.js";
4
+ import { Document } from "../document.js";
5
+ export interface Column {
6
+ type: string;
7
+ name: string;
8
+ }
9
+ export interface CassandraLibArgs extends DseClientOptions {
10
+ table: string;
11
+ keyspace: string;
12
+ dimensions: number;
13
+ primaryKey: Column;
14
+ metadataColumns: Column[];
15
+ }
16
+ /**
17
+ * Class for interacting with the Cassandra database. It extends the
18
+ * VectorStore class and provides methods for adding vectors and
19
+ * documents, searching for similar vectors, and creating instances from
20
+ * texts or documents.
21
+ */
22
+ export declare class CassandraStore extends VectorStore {
23
+ private client;
24
+ private readonly dimensions;
25
+ private readonly keyspace;
26
+ private primaryKey;
27
+ private metadataColumns;
28
+ private readonly table;
29
+ private isInitialized;
30
+ _vectorstoreType(): string;
31
+ constructor(embeddings: Embeddings, args: CassandraLibArgs);
32
+ /**
33
+ * Method to save vectors to the Cassandra database.
34
+ * @param vectors Vectors to save.
35
+ * @param documents The documents associated with the vectors.
36
+ * @returns Promise that resolves when the vectors have been added.
37
+ */
38
+ addVectors(vectors: number[][], documents: Document[]): Promise<void>;
39
+ /**
40
+ * Method to add documents to the Cassandra database.
41
+ * @param documents The documents to add.
42
+ * @returns Promise that resolves when the documents have been added.
43
+ */
44
+ addDocuments(documents: Document[]): Promise<void>;
45
+ /**
46
+ * Method to search for vectors that are similar to a given query vector.
47
+ * @param query The query vector.
48
+ * @param k The number of similar vectors to return.
49
+ * @returns Promise that resolves with an array of tuples, each containing a Document and a score.
50
+ */
51
+ similaritySearchVectorWithScore(query: number[], k: number): Promise<[Document, number][]>;
52
+ /**
53
+ * Static method to create an instance of CassandraStore from texts.
54
+ * @param texts The texts to use.
55
+ * @param metadatas The metadata associated with the texts.
56
+ * @param embeddings The embeddings to use.
57
+ * @param args The arguments for the CassandraStore.
58
+ * @returns Promise that resolves with a new instance of CassandraStore.
59
+ */
60
+ static fromTexts(texts: string[], metadatas: object | object[], embeddings: Embeddings, args: CassandraLibArgs): Promise<CassandraStore>;
61
+ /**
62
+ * Static method to create an instance of CassandraStore from documents.
63
+ * @param docs The documents to use.
64
+ * @param embeddings The embeddings to use.
65
+ * @param args The arguments for the CassandraStore.
66
+ * @returns Promise that resolves with a new instance of CassandraStore.
67
+ */
68
+ static fromDocuments(docs: Document[], embeddings: Embeddings, args: CassandraLibArgs): Promise<CassandraStore>;
69
+ /**
70
+ * Static method to create an instance of CassandraStore from an existing
71
+ * index.
72
+ * @param embeddings The embeddings to use.
73
+ * @param args The arguments for the CassandraStore.
74
+ * @returns Promise that resolves with a new instance of CassandraStore.
75
+ */
76
+ static fromExistingIndex(embeddings: Embeddings, args: CassandraLibArgs): Promise<CassandraStore>;
77
+ /**
78
+ * Method to initialize the Cassandra database.
79
+ * @returns Promise that resolves when the database has been initialized.
80
+ */
81
+ private initialize;
82
+ /**
83
+ * Method to build an CQL query for inserting vectors and documents into
84
+ * the Cassandra database.
85
+ * @param vectors The vectors to insert.
86
+ * @param documents The documents to insert.
87
+ * @returns The CQL query string.
88
+ */
89
+ private buildInsertQuery;
90
+ /**
91
+ * Method to build an CQL query for searching for similar vectors in the
92
+ * Cassandra database.
93
+ * @param query The query vector.
94
+ * @param k The number of similar vectors to return.
95
+ * @returns The CQL query string.
96
+ */
97
+ private buildSearchQuery;
98
+ }
@@ -0,0 +1,208 @@
1
+ /* eslint-disable prefer-template */
2
+ import { Client as CassandraClient } from "cassandra-driver";
3
+ import { VectorStore } from "./base.js";
4
+ import { Document } from "../document.js";
5
+ /**
6
+ * Class for interacting with the Cassandra database. It extends the
7
+ * VectorStore class and provides methods for adding vectors and
8
+ * documents, searching for similar vectors, and creating instances from
9
+ * texts or documents.
10
+ */
11
+ export class CassandraStore extends VectorStore {
12
+ _vectorstoreType() {
13
+ return "cassandra";
14
+ }
15
+ constructor(embeddings, args) {
16
+ super(embeddings, args);
17
+ Object.defineProperty(this, "client", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ Object.defineProperty(this, "dimensions", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ Object.defineProperty(this, "keyspace", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
35
+ Object.defineProperty(this, "primaryKey", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ Object.defineProperty(this, "metadataColumns", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
47
+ Object.defineProperty(this, "table", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "isInitialized", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: false
58
+ });
59
+ this.client = new CassandraClient(args);
60
+ this.dimensions = args.dimensions;
61
+ this.keyspace = args.keyspace;
62
+ this.table = args.table;
63
+ this.primaryKey = args.primaryKey;
64
+ this.metadataColumns = args.metadataColumns;
65
+ }
66
+ /**
67
+ * Method to save vectors to the Cassandra database.
68
+ * @param vectors Vectors to save.
69
+ * @param documents The documents associated with the vectors.
70
+ * @returns Promise that resolves when the vectors have been added.
71
+ */
72
+ async addVectors(vectors, documents) {
73
+ if (vectors.length === 0) {
74
+ return;
75
+ }
76
+ if (!this.isInitialized) {
77
+ await this.initialize();
78
+ }
79
+ const queries = this.buildInsertQuery(vectors, documents);
80
+ await this.client.batch(queries);
81
+ }
82
+ /**
83
+ * Method to add documents to the Cassandra database.
84
+ * @param documents The documents to add.
85
+ * @returns Promise that resolves when the documents have been added.
86
+ */
87
+ async addDocuments(documents) {
88
+ return this.addVectors(await this.embeddings.embedDocuments(documents.map((d) => d.pageContent)), documents);
89
+ }
90
+ /**
91
+ * Method to search for vectors that are similar to a given query vector.
92
+ * @param query The query vector.
93
+ * @param k The number of similar vectors to return.
94
+ * @returns Promise that resolves with an array of tuples, each containing a Document and a score.
95
+ */
96
+ async similaritySearchVectorWithScore(query, k) {
97
+ if (!this.isInitialized) {
98
+ await this.initialize();
99
+ }
100
+ const queryStr = this.buildSearchQuery(query, k);
101
+ const queryResultSet = await this.client.execute(queryStr);
102
+ return queryResultSet?.rows.map((row, index) => {
103
+ const textContent = row.text;
104
+ const sanitizedRow = Object.assign(row, {});
105
+ delete sanitizedRow.vector;
106
+ delete sanitizedRow.text;
107
+ return [
108
+ new Document({ pageContent: textContent, metadata: sanitizedRow }),
109
+ index,
110
+ ];
111
+ });
112
+ }
113
+ /**
114
+ * Static method to create an instance of CassandraStore from texts.
115
+ * @param texts The texts to use.
116
+ * @param metadatas The metadata associated with the texts.
117
+ * @param embeddings The embeddings to use.
118
+ * @param args The arguments for the CassandraStore.
119
+ * @returns Promise that resolves with a new instance of CassandraStore.
120
+ */
121
+ static async fromTexts(texts, metadatas, embeddings, args) {
122
+ const docs = [];
123
+ for (let index = 0; index < texts.length; index += 1) {
124
+ const metadata = Array.isArray(metadatas) ? metadatas[index] : metadatas;
125
+ const doc = new Document({
126
+ pageContent: texts[index],
127
+ metadata,
128
+ });
129
+ docs.push(doc);
130
+ }
131
+ return CassandraStore.fromDocuments(docs, embeddings, args);
132
+ }
133
+ /**
134
+ * Static method to create an instance of CassandraStore from documents.
135
+ * @param docs The documents to use.
136
+ * @param embeddings The embeddings to use.
137
+ * @param args The arguments for the CassandraStore.
138
+ * @returns Promise that resolves with a new instance of CassandraStore.
139
+ */
140
+ static async fromDocuments(docs, embeddings, args) {
141
+ const instance = new this(embeddings, args);
142
+ await instance.addDocuments(docs);
143
+ return instance;
144
+ }
145
+ /**
146
+ * Static method to create an instance of CassandraStore from an existing
147
+ * index.
148
+ * @param embeddings The embeddings to use.
149
+ * @param args The arguments for the CassandraStore.
150
+ * @returns Promise that resolves with a new instance of CassandraStore.
151
+ */
152
+ static async fromExistingIndex(embeddings, args) {
153
+ const instance = new this(embeddings, args);
154
+ await instance.initialize();
155
+ return instance;
156
+ }
157
+ /**
158
+ * Method to initialize the Cassandra database.
159
+ * @returns Promise that resolves when the database has been initialized.
160
+ */
161
+ async initialize() {
162
+ await this.client.execute(`CREATE TABLE IF NOT EXISTS ${this.keyspace}.${this.table} (
163
+ ${this.primaryKey.name} ${this.primaryKey.type} PRIMARY KEY,
164
+ text TEXT,
165
+ ${this.metadataColumns.length > 0
166
+ ? this.metadataColumns.map((col) => `${col.name} ${col.type},`)
167
+ : ""}
168
+ vector VECTOR<FLOAT, ${this.dimensions}>
169
+ );`);
170
+ await this.client.execute(`CREATE CUSTOM INDEX IF NOT EXISTS ann_index
171
+ ON ${this.keyspace}.${this.table}(vector) USING 'StorageAttachedIndex';`);
172
+ this.isInitialized = true;
173
+ }
174
+ /**
175
+ * Method to build an CQL query for inserting vectors and documents into
176
+ * the Cassandra database.
177
+ * @param vectors The vectors to insert.
178
+ * @param documents The documents to insert.
179
+ * @returns The CQL query string.
180
+ */
181
+ buildInsertQuery(vectors, documents) {
182
+ const queries = [];
183
+ for (let index = 0; index < vectors.length; index += 1) {
184
+ const vector = vectors[index];
185
+ const document = documents[index];
186
+ const metadataColNames = Object.keys(document.metadata);
187
+ const metadataVals = Object.values(document.metadata);
188
+ const query = `INSERT INTO ${this.keyspace}.${this.table} (vector, text${metadataColNames.length > 0 ? ", " + metadataColNames.join(", ") : ""}) VALUES ([${vector}], '${document.pageContent}'${metadataVals.length > 0
189
+ ? ", " +
190
+ metadataVals
191
+ .map((val) => (typeof val === "number" ? val : `'${val}'`))
192
+ .join(", ")
193
+ : ""});`;
194
+ queries.push(query);
195
+ }
196
+ return queries;
197
+ }
198
+ /**
199
+ * Method to build an CQL query for searching for similar vectors in the
200
+ * Cassandra database.
201
+ * @param query The query vector.
202
+ * @param k The number of similar vectors to return.
203
+ * @returns The CQL query string.
204
+ */
205
+ buildSearchQuery(query, k) {
206
+ return `SELECT * FROM ${this.keyspace}.${this.table} ORDER BY vector ANN OF [${query}] LIMIT ${k || 1};`;
207
+ }
208
+ }