@vectorstores/astra 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) vectorstores contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,215 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+
3
+ var astraDbTs = require('@datastax/astra-db-ts');
4
+ var core = require('@vectorstores/core');
5
+ var env = require('@vectorstores/env');
6
+
7
+ class AstraDBVectorStore extends core.BaseVectorStore {
8
+ constructor(init){
9
+ super(init), this.storesText = true, this.flatMetadata = true;
10
+ const token = init?.params?.token ?? env.getEnv("ASTRA_DB_APPLICATION_TOKEN");
11
+ const endpoint = init?.params?.endpoint ?? env.getEnv("ASTRA_DB_API_ENDPOINT");
12
+ if (!token) {
13
+ throw new Error("Must specify ASTRA_DB_APPLICATION_TOKEN via env variable.");
14
+ }
15
+ if (!endpoint) {
16
+ throw new Error("Must specify ASTRA_DB_API_ENDPOINT via env variable.");
17
+ }
18
+ const namespace = init?.params?.namespace ?? env.getEnv("ASTRA_DB_NAMESPACE") ?? "default_keyspace";
19
+ this.astraClient = new astraDbTs.DataAPIClient(token, {
20
+ caller: [
21
+ "LlamaIndexTS"
22
+ ]
23
+ });
24
+ this.astraDB = this.astraClient.db(endpoint, {
25
+ namespace
26
+ });
27
+ this.idKey = init?.idKey ?? "_id";
28
+ this.contentKey = init?.contentKey ?? "content";
29
+ }
30
+ /**
31
+ * Create a new collection in your Astra DB vector database and connects to it.
32
+ * You must call this method or `connect` before adding, deleting, or querying.
33
+ *
34
+ * @param collection - Your new collection's name
35
+ * @param options - CreateCollectionOptions used to set the number of vector dimensions and similarity metric
36
+ * @returns Promise that resolves if the creation did not throw an error.
37
+ */ async createAndConnect(collection, options) {
38
+ this.collection = await this.astraDB.createCollection(collection, options);
39
+ console.debug("Created Astra DB collection");
40
+ return;
41
+ }
42
+ /**
43
+ * Connect to an existing collection in your Astra DB vector database.
44
+ * You must call this method or `createAndConnect` before adding, deleting, or querying.
45
+ *
46
+ * @param collection - Your existing collection's name
47
+ * @returns Promise that resolves if the connection did not throw an error.
48
+ */ async connect(collection) {
49
+ this.collection = await this.astraDB.collection(collection);
50
+ console.debug("Connected to Astra DB collection");
51
+ return;
52
+ }
53
+ /**
54
+ * Get an instance of your Astra DB client.
55
+ * @returns the AstraDB client
56
+ */ client() {
57
+ return this.astraClient;
58
+ }
59
+ /**
60
+ * Add your document(s) to your Astra DB collection.
61
+ *
62
+ * @returns an array of node ids which were added
63
+ */ async add(nodes) {
64
+ if (!this.collection) {
65
+ throw new Error("Must connect to collection before adding.");
66
+ }
67
+ const collection = this.collection;
68
+ if (!nodes || nodes.length === 0) {
69
+ return [];
70
+ }
71
+ const dataToInsert = nodes.map((node)=>{
72
+ const metadata = core.nodeToMetadata(node, true, this.contentKey, this.flatMetadata);
73
+ return {
74
+ $vector: node.getEmbedding(),
75
+ [this.idKey]: node.id_,
76
+ [this.contentKey]: node.getContent(core.MetadataMode.NONE),
77
+ ...metadata
78
+ };
79
+ });
80
+ console.debug(`Adding ${dataToInsert.length} rows to table`);
81
+ const insertResult = await collection.insertMany(dataToInsert);
82
+ return insertResult.insertedIds;
83
+ }
84
+ /**
85
+ * Delete a document from your Astra DB collection.
86
+ *
87
+ * @param refDocId - The id of the document to delete
88
+ * @param deleteOptions - DeleteOneOptions to pass to the delete query
89
+ * @returns Promise that resolves if the delete query did not throw an error.
90
+ */ async delete(refDocId, deleteOptions) {
91
+ if (!this.collection) {
92
+ throw new Error("Must connect to collection before deleting.");
93
+ }
94
+ const collection = this.collection;
95
+ console.debug(`Deleting row with id ${refDocId}`);
96
+ await collection.deleteOne({
97
+ _id: refDocId
98
+ }, deleteOptions);
99
+ }
100
+ /**
101
+ * Query documents from your Astra DB collection to get the closest match to your embedding.
102
+ *
103
+ * @param query - VectorStoreQuery
104
+ * @param options - FindOptions
105
+ */ async query(query, options) {
106
+ if (!this.collection) {
107
+ throw new Error("Must connect to collection before querying.");
108
+ }
109
+ const collection = this.collection;
110
+ const astraFilter = this.toAstraFilter(query.filters);
111
+ const cursor = await collection.find(astraFilter, {
112
+ ...options,
113
+ sort: query.queryEmbedding ? {
114
+ $vector: query.queryEmbedding
115
+ } : options?.sort,
116
+ limit: query.similarityTopK,
117
+ includeSimilarity: true
118
+ });
119
+ const nodes = [];
120
+ const ids = [];
121
+ const similarities = [];
122
+ for await (const row of cursor){
123
+ const { $vector: embedding, $similarity: similarity, [this.idKey]: id, [this.contentKey]: content, ...metadata } = row;
124
+ const node = core.metadataDictToNode(metadata, {
125
+ fallback: {
126
+ id,
127
+ text: content,
128
+ ...metadata
129
+ }
130
+ });
131
+ node.setContent(content);
132
+ ids.push(id);
133
+ similarities.push(similarity);
134
+ nodes.push(node);
135
+ }
136
+ return {
137
+ similarities,
138
+ ids,
139
+ nodes
140
+ };
141
+ }
142
+ toAstraFilter(filters) {
143
+ if (!filters || filters.filters?.length === 0) return {};
144
+ const condition = filters.condition ?? core.FilterCondition.AND;
145
+ const listFilter = filters.filters.map((f)=>this.buildFilterItem(f));
146
+ if (condition === core.FilterCondition.OR) return {
147
+ $or: listFilter
148
+ };
149
+ if (condition === core.FilterCondition.AND) return {
150
+ $and: listFilter
151
+ };
152
+ throw new Error(`Not supported filter condition: ${condition}`);
153
+ }
154
+ buildFilterItem(filter) {
155
+ const { key, operator, value } = filter;
156
+ switch(operator){
157
+ case core.FilterOperator.EQ:
158
+ return {
159
+ [key]: value
160
+ };
161
+ case core.FilterOperator.NE:
162
+ return {
163
+ [key]: {
164
+ $ne: value
165
+ }
166
+ };
167
+ case core.FilterOperator.GT:
168
+ return {
169
+ [key]: {
170
+ $gt: value
171
+ }
172
+ };
173
+ case core.FilterOperator.LT:
174
+ return {
175
+ [key]: {
176
+ $lt: value
177
+ }
178
+ };
179
+ case core.FilterOperator.GTE:
180
+ return {
181
+ [key]: {
182
+ $gte: value
183
+ }
184
+ };
185
+ case core.FilterOperator.LTE:
186
+ return {
187
+ [key]: {
188
+ $lte: value
189
+ }
190
+ };
191
+ case core.FilterOperator.IN:
192
+ return {
193
+ [key]: {
194
+ $in: core.parseArrayValue(value)
195
+ }
196
+ };
197
+ case core.FilterOperator.NIN:
198
+ return {
199
+ [key]: {
200
+ $nin: core.parseArrayValue(value)
201
+ }
202
+ };
203
+ case core.FilterOperator.IS_EMPTY:
204
+ return {
205
+ [key]: {
206
+ $size: 0
207
+ }
208
+ };
209
+ default:
210
+ throw new Error(`Not supported filter operator: ${operator}`);
211
+ }
212
+ }
213
+ }
214
+
215
+ exports.AstraDBVectorStore = AstraDBVectorStore;
@@ -0,0 +1,66 @@
1
+ import { Db, DataAPIClient, Collection } from '@datastax/astra-db-ts';
2
+ import { BaseVectorStore, VectorStoreBaseParams, BaseNode, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
3
+
4
+ declare class AstraDBVectorStore extends BaseVectorStore {
5
+ storesText: boolean;
6
+ flatMetadata: boolean;
7
+ idKey: string;
8
+ contentKey: string;
9
+ private astraClient;
10
+ private astraDB;
11
+ private collection;
12
+ constructor(init?: Partial<AstraDBVectorStore> & {
13
+ params?: {
14
+ token: string;
15
+ endpoint: string;
16
+ namespace?: string;
17
+ };
18
+ } & VectorStoreBaseParams);
19
+ /**
20
+ * Create a new collection in your Astra DB vector database and connects to it.
21
+ * You must call this method or `connect` before adding, deleting, or querying.
22
+ *
23
+ * @param collection - Your new collection's name
24
+ * @param options - CreateCollectionOptions used to set the number of vector dimensions and similarity metric
25
+ * @returns Promise that resolves if the creation did not throw an error.
26
+ */
27
+ createAndConnect(collection: string, options?: Parameters<Db["createCollection"]>[1]): Promise<void>;
28
+ /**
29
+ * Connect to an existing collection in your Astra DB vector database.
30
+ * You must call this method or `createAndConnect` before adding, deleting, or querying.
31
+ *
32
+ * @param collection - Your existing collection's name
33
+ * @returns Promise that resolves if the connection did not throw an error.
34
+ */
35
+ connect(collection: string): Promise<void>;
36
+ /**
37
+ * Get an instance of your Astra DB client.
38
+ * @returns the AstraDB client
39
+ */
40
+ client(): DataAPIClient;
41
+ /**
42
+ * Add your document(s) to your Astra DB collection.
43
+ *
44
+ * @returns an array of node ids which were added
45
+ */
46
+ add(nodes: BaseNode[]): Promise<string[]>;
47
+ /**
48
+ * Delete a document from your Astra DB collection.
49
+ *
50
+ * @param refDocId - The id of the document to delete
51
+ * @param deleteOptions - DeleteOneOptions to pass to the delete query
52
+ * @returns Promise that resolves if the delete query did not throw an error.
53
+ */
54
+ delete(refDocId: string, deleteOptions?: Parameters<Collection["deleteOne"]>[1]): Promise<void>;
55
+ /**
56
+ * Query documents from your Astra DB collection to get the closest match to your embedding.
57
+ *
58
+ * @param query - VectorStoreQuery
59
+ * @param options - FindOptions
60
+ */
61
+ query(query: VectorStoreQuery, options?: Parameters<Collection["find"]>[1]): Promise<VectorStoreQueryResult>;
62
+ private toAstraFilter;
63
+ private buildFilterItem;
64
+ }
65
+
66
+ export { AstraDBVectorStore };
@@ -0,0 +1,66 @@
1
+ import { Db, DataAPIClient, Collection } from '@datastax/astra-db-ts';
2
+ import { BaseVectorStore, VectorStoreBaseParams, BaseNode, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
3
+
4
+ declare class AstraDBVectorStore extends BaseVectorStore {
5
+ storesText: boolean;
6
+ flatMetadata: boolean;
7
+ idKey: string;
8
+ contentKey: string;
9
+ private astraClient;
10
+ private astraDB;
11
+ private collection;
12
+ constructor(init?: Partial<AstraDBVectorStore> & {
13
+ params?: {
14
+ token: string;
15
+ endpoint: string;
16
+ namespace?: string;
17
+ };
18
+ } & VectorStoreBaseParams);
19
+ /**
20
+ * Create a new collection in your Astra DB vector database and connects to it.
21
+ * You must call this method or `connect` before adding, deleting, or querying.
22
+ *
23
+ * @param collection - Your new collection's name
24
+ * @param options - CreateCollectionOptions used to set the number of vector dimensions and similarity metric
25
+ * @returns Promise that resolves if the creation did not throw an error.
26
+ */
27
+ createAndConnect(collection: string, options?: Parameters<Db["createCollection"]>[1]): Promise<void>;
28
+ /**
29
+ * Connect to an existing collection in your Astra DB vector database.
30
+ * You must call this method or `createAndConnect` before adding, deleting, or querying.
31
+ *
32
+ * @param collection - Your existing collection's name
33
+ * @returns Promise that resolves if the connection did not throw an error.
34
+ */
35
+ connect(collection: string): Promise<void>;
36
+ /**
37
+ * Get an instance of your Astra DB client.
38
+ * @returns the AstraDB client
39
+ */
40
+ client(): DataAPIClient;
41
+ /**
42
+ * Add your document(s) to your Astra DB collection.
43
+ *
44
+ * @returns an array of node ids which were added
45
+ */
46
+ add(nodes: BaseNode[]): Promise<string[]>;
47
+ /**
48
+ * Delete a document from your Astra DB collection.
49
+ *
50
+ * @param refDocId - The id of the document to delete
51
+ * @param deleteOptions - DeleteOneOptions to pass to the delete query
52
+ * @returns Promise that resolves if the delete query did not throw an error.
53
+ */
54
+ delete(refDocId: string, deleteOptions?: Parameters<Collection["deleteOne"]>[1]): Promise<void>;
55
+ /**
56
+ * Query documents from your Astra DB collection to get the closest match to your embedding.
57
+ *
58
+ * @param query - VectorStoreQuery
59
+ * @param options - FindOptions
60
+ */
61
+ query(query: VectorStoreQuery, options?: Parameters<Collection["find"]>[1]): Promise<VectorStoreQueryResult>;
62
+ private toAstraFilter;
63
+ private buildFilterItem;
64
+ }
65
+
66
+ export { AstraDBVectorStore };
@@ -0,0 +1,66 @@
1
+ import { Db, DataAPIClient, Collection } from '@datastax/astra-db-ts';
2
+ import { BaseVectorStore, VectorStoreBaseParams, BaseNode, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
3
+
4
+ declare class AstraDBVectorStore extends BaseVectorStore {
5
+ storesText: boolean;
6
+ flatMetadata: boolean;
7
+ idKey: string;
8
+ contentKey: string;
9
+ private astraClient;
10
+ private astraDB;
11
+ private collection;
12
+ constructor(init?: Partial<AstraDBVectorStore> & {
13
+ params?: {
14
+ token: string;
15
+ endpoint: string;
16
+ namespace?: string;
17
+ };
18
+ } & VectorStoreBaseParams);
19
+ /**
20
+ * Create a new collection in your Astra DB vector database and connects to it.
21
+ * You must call this method or `connect` before adding, deleting, or querying.
22
+ *
23
+ * @param collection - Your new collection's name
24
+ * @param options - CreateCollectionOptions used to set the number of vector dimensions and similarity metric
25
+ * @returns Promise that resolves if the creation did not throw an error.
26
+ */
27
+ createAndConnect(collection: string, options?: Parameters<Db["createCollection"]>[1]): Promise<void>;
28
+ /**
29
+ * Connect to an existing collection in your Astra DB vector database.
30
+ * You must call this method or `createAndConnect` before adding, deleting, or querying.
31
+ *
32
+ * @param collection - Your existing collection's name
33
+ * @returns Promise that resolves if the connection did not throw an error.
34
+ */
35
+ connect(collection: string): Promise<void>;
36
+ /**
37
+ * Get an instance of your Astra DB client.
38
+ * @returns the AstraDB client
39
+ */
40
+ client(): DataAPIClient;
41
+ /**
42
+ * Add your document(s) to your Astra DB collection.
43
+ *
44
+ * @returns an array of node ids which were added
45
+ */
46
+ add(nodes: BaseNode[]): Promise<string[]>;
47
+ /**
48
+ * Delete a document from your Astra DB collection.
49
+ *
50
+ * @param refDocId - The id of the document to delete
51
+ * @param deleteOptions - DeleteOneOptions to pass to the delete query
52
+ * @returns Promise that resolves if the delete query did not throw an error.
53
+ */
54
+ delete(refDocId: string, deleteOptions?: Parameters<Collection["deleteOne"]>[1]): Promise<void>;
55
+ /**
56
+ * Query documents from your Astra DB collection to get the closest match to your embedding.
57
+ *
58
+ * @param query - VectorStoreQuery
59
+ * @param options - FindOptions
60
+ */
61
+ query(query: VectorStoreQuery, options?: Parameters<Collection["find"]>[1]): Promise<VectorStoreQueryResult>;
62
+ private toAstraFilter;
63
+ private buildFilterItem;
64
+ }
65
+
66
+ export { AstraDBVectorStore };
@@ -0,0 +1,213 @@
1
+ import { DataAPIClient } from '@datastax/astra-db-ts';
2
+ import { BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, FilterCondition, FilterOperator, parseArrayValue } from '@vectorstores/core';
3
+ import { getEnv } from '@vectorstores/env';
4
+
5
+ class AstraDBVectorStore extends BaseVectorStore {
6
+ constructor(init){
7
+ super(init), this.storesText = true, this.flatMetadata = true;
8
+ const token = init?.params?.token ?? getEnv("ASTRA_DB_APPLICATION_TOKEN");
9
+ const endpoint = init?.params?.endpoint ?? getEnv("ASTRA_DB_API_ENDPOINT");
10
+ if (!token) {
11
+ throw new Error("Must specify ASTRA_DB_APPLICATION_TOKEN via env variable.");
12
+ }
13
+ if (!endpoint) {
14
+ throw new Error("Must specify ASTRA_DB_API_ENDPOINT via env variable.");
15
+ }
16
+ const namespace = init?.params?.namespace ?? getEnv("ASTRA_DB_NAMESPACE") ?? "default_keyspace";
17
+ this.astraClient = new DataAPIClient(token, {
18
+ caller: [
19
+ "LlamaIndexTS"
20
+ ]
21
+ });
22
+ this.astraDB = this.astraClient.db(endpoint, {
23
+ namespace
24
+ });
25
+ this.idKey = init?.idKey ?? "_id";
26
+ this.contentKey = init?.contentKey ?? "content";
27
+ }
28
+ /**
29
+ * Create a new collection in your Astra DB vector database and connects to it.
30
+ * You must call this method or `connect` before adding, deleting, or querying.
31
+ *
32
+ * @param collection - Your new collection's name
33
+ * @param options - CreateCollectionOptions used to set the number of vector dimensions and similarity metric
34
+ * @returns Promise that resolves if the creation did not throw an error.
35
+ */ async createAndConnect(collection, options) {
36
+ this.collection = await this.astraDB.createCollection(collection, options);
37
+ console.debug("Created Astra DB collection");
38
+ return;
39
+ }
40
+ /**
41
+ * Connect to an existing collection in your Astra DB vector database.
42
+ * You must call this method or `createAndConnect` before adding, deleting, or querying.
43
+ *
44
+ * @param collection - Your existing collection's name
45
+ * @returns Promise that resolves if the connection did not throw an error.
46
+ */ async connect(collection) {
47
+ this.collection = await this.astraDB.collection(collection);
48
+ console.debug("Connected to Astra DB collection");
49
+ return;
50
+ }
51
+ /**
52
+ * Get an instance of your Astra DB client.
53
+ * @returns the AstraDB client
54
+ */ client() {
55
+ return this.astraClient;
56
+ }
57
+ /**
58
+ * Add your document(s) to your Astra DB collection.
59
+ *
60
+ * @returns an array of node ids which were added
61
+ */ async add(nodes) {
62
+ if (!this.collection) {
63
+ throw new Error("Must connect to collection before adding.");
64
+ }
65
+ const collection = this.collection;
66
+ if (!nodes || nodes.length === 0) {
67
+ return [];
68
+ }
69
+ const dataToInsert = nodes.map((node)=>{
70
+ const metadata = nodeToMetadata(node, true, this.contentKey, this.flatMetadata);
71
+ return {
72
+ $vector: node.getEmbedding(),
73
+ [this.idKey]: node.id_,
74
+ [this.contentKey]: node.getContent(MetadataMode.NONE),
75
+ ...metadata
76
+ };
77
+ });
78
+ console.debug(`Adding ${dataToInsert.length} rows to table`);
79
+ const insertResult = await collection.insertMany(dataToInsert);
80
+ return insertResult.insertedIds;
81
+ }
82
+ /**
83
+ * Delete a document from your Astra DB collection.
84
+ *
85
+ * @param refDocId - The id of the document to delete
86
+ * @param deleteOptions - DeleteOneOptions to pass to the delete query
87
+ * @returns Promise that resolves if the delete query did not throw an error.
88
+ */ async delete(refDocId, deleteOptions) {
89
+ if (!this.collection) {
90
+ throw new Error("Must connect to collection before deleting.");
91
+ }
92
+ const collection = this.collection;
93
+ console.debug(`Deleting row with id ${refDocId}`);
94
+ await collection.deleteOne({
95
+ _id: refDocId
96
+ }, deleteOptions);
97
+ }
98
+ /**
99
+ * Query documents from your Astra DB collection to get the closest match to your embedding.
100
+ *
101
+ * @param query - VectorStoreQuery
102
+ * @param options - FindOptions
103
+ */ async query(query, options) {
104
+ if (!this.collection) {
105
+ throw new Error("Must connect to collection before querying.");
106
+ }
107
+ const collection = this.collection;
108
+ const astraFilter = this.toAstraFilter(query.filters);
109
+ const cursor = await collection.find(astraFilter, {
110
+ ...options,
111
+ sort: query.queryEmbedding ? {
112
+ $vector: query.queryEmbedding
113
+ } : options?.sort,
114
+ limit: query.similarityTopK,
115
+ includeSimilarity: true
116
+ });
117
+ const nodes = [];
118
+ const ids = [];
119
+ const similarities = [];
120
+ for await (const row of cursor){
121
+ const { $vector: embedding, $similarity: similarity, [this.idKey]: id, [this.contentKey]: content, ...metadata } = row;
122
+ const node = metadataDictToNode(metadata, {
123
+ fallback: {
124
+ id,
125
+ text: content,
126
+ ...metadata
127
+ }
128
+ });
129
+ node.setContent(content);
130
+ ids.push(id);
131
+ similarities.push(similarity);
132
+ nodes.push(node);
133
+ }
134
+ return {
135
+ similarities,
136
+ ids,
137
+ nodes
138
+ };
139
+ }
140
+ toAstraFilter(filters) {
141
+ if (!filters || filters.filters?.length === 0) return {};
142
+ const condition = filters.condition ?? FilterCondition.AND;
143
+ const listFilter = filters.filters.map((f)=>this.buildFilterItem(f));
144
+ if (condition === FilterCondition.OR) return {
145
+ $or: listFilter
146
+ };
147
+ if (condition === FilterCondition.AND) return {
148
+ $and: listFilter
149
+ };
150
+ throw new Error(`Not supported filter condition: ${condition}`);
151
+ }
152
+ buildFilterItem(filter) {
153
+ const { key, operator, value } = filter;
154
+ switch(operator){
155
+ case FilterOperator.EQ:
156
+ return {
157
+ [key]: value
158
+ };
159
+ case FilterOperator.NE:
160
+ return {
161
+ [key]: {
162
+ $ne: value
163
+ }
164
+ };
165
+ case FilterOperator.GT:
166
+ return {
167
+ [key]: {
168
+ $gt: value
169
+ }
170
+ };
171
+ case FilterOperator.LT:
172
+ return {
173
+ [key]: {
174
+ $lt: value
175
+ }
176
+ };
177
+ case FilterOperator.GTE:
178
+ return {
179
+ [key]: {
180
+ $gte: value
181
+ }
182
+ };
183
+ case FilterOperator.LTE:
184
+ return {
185
+ [key]: {
186
+ $lte: value
187
+ }
188
+ };
189
+ case FilterOperator.IN:
190
+ return {
191
+ [key]: {
192
+ $in: parseArrayValue(value)
193
+ }
194
+ };
195
+ case FilterOperator.NIN:
196
+ return {
197
+ [key]: {
198
+ $nin: parseArrayValue(value)
199
+ }
200
+ };
201
+ case FilterOperator.IS_EMPTY:
202
+ return {
203
+ [key]: {
204
+ $size: 0
205
+ }
206
+ };
207
+ default:
208
+ throw new Error(`Not supported filter operator: ${operator}`);
209
+ }
210
+ }
211
+ }
212
+
213
+ export { AstraDBVectorStore };
package/dist/index.js ADDED
@@ -0,0 +1,213 @@
1
+ import { DataAPIClient } from '@datastax/astra-db-ts';
2
+ import { BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, FilterCondition, FilterOperator, parseArrayValue } from '@vectorstores/core';
3
+ import { getEnv } from '@vectorstores/env';
4
+
5
+ class AstraDBVectorStore extends BaseVectorStore {
6
+ constructor(init){
7
+ super(init), this.storesText = true, this.flatMetadata = true;
8
+ const token = init?.params?.token ?? getEnv("ASTRA_DB_APPLICATION_TOKEN");
9
+ const endpoint = init?.params?.endpoint ?? getEnv("ASTRA_DB_API_ENDPOINT");
10
+ if (!token) {
11
+ throw new Error("Must specify ASTRA_DB_APPLICATION_TOKEN via env variable.");
12
+ }
13
+ if (!endpoint) {
14
+ throw new Error("Must specify ASTRA_DB_API_ENDPOINT via env variable.");
15
+ }
16
+ const namespace = init?.params?.namespace ?? getEnv("ASTRA_DB_NAMESPACE") ?? "default_keyspace";
17
+ this.astraClient = new DataAPIClient(token, {
18
+ caller: [
19
+ "LlamaIndexTS"
20
+ ]
21
+ });
22
+ this.astraDB = this.astraClient.db(endpoint, {
23
+ namespace
24
+ });
25
+ this.idKey = init?.idKey ?? "_id";
26
+ this.contentKey = init?.contentKey ?? "content";
27
+ }
28
+ /**
29
+ * Create a new collection in your Astra DB vector database and connects to it.
30
+ * You must call this method or `connect` before adding, deleting, or querying.
31
+ *
32
+ * @param collection - Your new collection's name
33
+ * @param options - CreateCollectionOptions used to set the number of vector dimensions and similarity metric
34
+ * @returns Promise that resolves if the creation did not throw an error.
35
+ */ async createAndConnect(collection, options) {
36
+ this.collection = await this.astraDB.createCollection(collection, options);
37
+ console.debug("Created Astra DB collection");
38
+ return;
39
+ }
40
+ /**
41
+ * Connect to an existing collection in your Astra DB vector database.
42
+ * You must call this method or `createAndConnect` before adding, deleting, or querying.
43
+ *
44
+ * @param collection - Your existing collection's name
45
+ * @returns Promise that resolves if the connection did not throw an error.
46
+ */ async connect(collection) {
47
+ this.collection = await this.astraDB.collection(collection);
48
+ console.debug("Connected to Astra DB collection");
49
+ return;
50
+ }
51
+ /**
52
+ * Get an instance of your Astra DB client.
53
+ * @returns the AstraDB client
54
+ */ client() {
55
+ return this.astraClient;
56
+ }
57
+ /**
58
+ * Add your document(s) to your Astra DB collection.
59
+ *
60
+ * @returns an array of node ids which were added
61
+ */ async add(nodes) {
62
+ if (!this.collection) {
63
+ throw new Error("Must connect to collection before adding.");
64
+ }
65
+ const collection = this.collection;
66
+ if (!nodes || nodes.length === 0) {
67
+ return [];
68
+ }
69
+ const dataToInsert = nodes.map((node)=>{
70
+ const metadata = nodeToMetadata(node, true, this.contentKey, this.flatMetadata);
71
+ return {
72
+ $vector: node.getEmbedding(),
73
+ [this.idKey]: node.id_,
74
+ [this.contentKey]: node.getContent(MetadataMode.NONE),
75
+ ...metadata
76
+ };
77
+ });
78
+ console.debug(`Adding ${dataToInsert.length} rows to table`);
79
+ const insertResult = await collection.insertMany(dataToInsert);
80
+ return insertResult.insertedIds;
81
+ }
82
+ /**
83
+ * Delete a document from your Astra DB collection.
84
+ *
85
+ * @param refDocId - The id of the document to delete
86
+ * @param deleteOptions - DeleteOneOptions to pass to the delete query
87
+ * @returns Promise that resolves if the delete query did not throw an error.
88
+ */ async delete(refDocId, deleteOptions) {
89
+ if (!this.collection) {
90
+ throw new Error("Must connect to collection before deleting.");
91
+ }
92
+ const collection = this.collection;
93
+ console.debug(`Deleting row with id ${refDocId}`);
94
+ await collection.deleteOne({
95
+ _id: refDocId
96
+ }, deleteOptions);
97
+ }
98
+ /**
99
+ * Query documents from your Astra DB collection to get the closest match to your embedding.
100
+ *
101
+ * @param query - VectorStoreQuery
102
+ * @param options - FindOptions
103
+ */ async query(query, options) {
104
+ if (!this.collection) {
105
+ throw new Error("Must connect to collection before querying.");
106
+ }
107
+ const collection = this.collection;
108
+ const astraFilter = this.toAstraFilter(query.filters);
109
+ const cursor = await collection.find(astraFilter, {
110
+ ...options,
111
+ sort: query.queryEmbedding ? {
112
+ $vector: query.queryEmbedding
113
+ } : options?.sort,
114
+ limit: query.similarityTopK,
115
+ includeSimilarity: true
116
+ });
117
+ const nodes = [];
118
+ const ids = [];
119
+ const similarities = [];
120
+ for await (const row of cursor){
121
+ const { $vector: embedding, $similarity: similarity, [this.idKey]: id, [this.contentKey]: content, ...metadata } = row;
122
+ const node = metadataDictToNode(metadata, {
123
+ fallback: {
124
+ id,
125
+ text: content,
126
+ ...metadata
127
+ }
128
+ });
129
+ node.setContent(content);
130
+ ids.push(id);
131
+ similarities.push(similarity);
132
+ nodes.push(node);
133
+ }
134
+ return {
135
+ similarities,
136
+ ids,
137
+ nodes
138
+ };
139
+ }
140
+ toAstraFilter(filters) {
141
+ if (!filters || filters.filters?.length === 0) return {};
142
+ const condition = filters.condition ?? FilterCondition.AND;
143
+ const listFilter = filters.filters.map((f)=>this.buildFilterItem(f));
144
+ if (condition === FilterCondition.OR) return {
145
+ $or: listFilter
146
+ };
147
+ if (condition === FilterCondition.AND) return {
148
+ $and: listFilter
149
+ };
150
+ throw new Error(`Not supported filter condition: ${condition}`);
151
+ }
152
+ buildFilterItem(filter) {
153
+ const { key, operator, value } = filter;
154
+ switch(operator){
155
+ case FilterOperator.EQ:
156
+ return {
157
+ [key]: value
158
+ };
159
+ case FilterOperator.NE:
160
+ return {
161
+ [key]: {
162
+ $ne: value
163
+ }
164
+ };
165
+ case FilterOperator.GT:
166
+ return {
167
+ [key]: {
168
+ $gt: value
169
+ }
170
+ };
171
+ case FilterOperator.LT:
172
+ return {
173
+ [key]: {
174
+ $lt: value
175
+ }
176
+ };
177
+ case FilterOperator.GTE:
178
+ return {
179
+ [key]: {
180
+ $gte: value
181
+ }
182
+ };
183
+ case FilterOperator.LTE:
184
+ return {
185
+ [key]: {
186
+ $lte: value
187
+ }
188
+ };
189
+ case FilterOperator.IN:
190
+ return {
191
+ [key]: {
192
+ $in: parseArrayValue(value)
193
+ }
194
+ };
195
+ case FilterOperator.NIN:
196
+ return {
197
+ [key]: {
198
+ $nin: parseArrayValue(value)
199
+ }
200
+ };
201
+ case FilterOperator.IS_EMPTY:
202
+ return {
203
+ [key]: {
204
+ $size: 0
205
+ }
206
+ };
207
+ default:
208
+ throw new Error(`Not supported filter operator: ${operator}`);
209
+ }
210
+ }
211
+ }
212
+
213
+ export { AstraDBVectorStore };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@vectorstores/astra",
3
+ "description": "Astra Storage for vectorstores",
4
+ "version": "0.1.0",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "edge-light": {
11
+ "types": "./dist/index.edge-light.d.ts",
12
+ "default": "./dist/index.edge-light.js"
13
+ },
14
+ "workerd": {
15
+ "types": "./dist/index.edge-light.d.ts",
16
+ "default": "./dist/index.edge-light.js"
17
+ },
18
+ "require": {
19
+ "types": "./dist/index.d.cts",
20
+ "default": "./dist/index.cjs"
21
+ },
22
+ "import": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ }
26
+ }
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/schiesser/vectorstores.git",
34
+ "directory": "packages/providers/storage/astra"
35
+ },
36
+ "devDependencies": {
37
+ "@vectorstores/core": "0.1.0",
38
+ "@vectorstores/env": "0.1.0"
39
+ },
40
+ "peerDependencies": {
41
+ "@vectorstores/core": "0.1.0",
42
+ "@vectorstores/env": "0.1.0"
43
+ },
44
+ "dependencies": {
45
+ "@datastax/astra-db-ts": "^1.4.1"
46
+ },
47
+ "scripts": {
48
+ "build": "bunchee",
49
+ "dev": "bunchee --watch"
50
+ }
51
+ }