@vectorstores/milvus 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 +21 -0
- package/dist/index.cjs +184 -0
- package/dist/index.d.cts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.edge-light.d.ts +42 -0
- package/dist/index.edge-light.js +182 -0
- package/dist/index.js +182 -0
- package/package.json +54 -0
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,184 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var core = require('@vectorstores/core');
|
|
4
|
+
var env = require('@vectorstores/env');
|
|
5
|
+
var milvus2SdkNode = require('@zilliz/milvus2-sdk-node');
|
|
6
|
+
|
|
7
|
+
function parseScalarFilters(scalarFilters) {
|
|
8
|
+
const condition = scalarFilters.condition ?? "and";
|
|
9
|
+
const filters = [];
|
|
10
|
+
for (const filter of scalarFilters.filters){
|
|
11
|
+
switch(filter.operator){
|
|
12
|
+
case "==":
|
|
13
|
+
case "!=":
|
|
14
|
+
{
|
|
15
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} "${core.parsePrimitiveValue(filter.value)}"`);
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
case "in":
|
|
19
|
+
{
|
|
20
|
+
const filterValue = core.parseArrayValue(filter.value).map((v)=>`"${v}"`).join(", ");
|
|
21
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} [${filterValue}]`);
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
case "nin":
|
|
25
|
+
{
|
|
26
|
+
// Milvus does not support `nin` operator, so we need to manually check every value
|
|
27
|
+
// Expected: not metadata["key"] != "value1" and not metadata["key"] != "value2"
|
|
28
|
+
const filterStr = core.parseArrayValue(filter.value).map((v)=>`metadata["${filter.key}"] != "${v}"`).join(" && ");
|
|
29
|
+
filters.push(filterStr);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case "<":
|
|
33
|
+
case "<=":
|
|
34
|
+
case ">":
|
|
35
|
+
case ">=":
|
|
36
|
+
{
|
|
37
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} ${core.parsePrimitiveValue(filter.value)}`);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
default:
|
|
41
|
+
throw new Error(`Operator ${filter.operator} is not supported.`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return filters.join(` ${condition} `);
|
|
45
|
+
}
|
|
46
|
+
class MilvusVectorStore extends core.BaseVectorStore {
|
|
47
|
+
constructor(init){
|
|
48
|
+
super(init), this.storesText = true, this.isEmbeddingQuery = false, this.flatMetadata = true, this.collectionInitialized = false;
|
|
49
|
+
if (init?.milvusClient) {
|
|
50
|
+
this.milvusClient = init.milvusClient;
|
|
51
|
+
} else {
|
|
52
|
+
const configOrAddress = init?.params?.configOrAddress ?? env.getEnv("MILVUS_ADDRESS");
|
|
53
|
+
const ssl = init?.params?.ssl ?? env.getEnv("MILVUS_SSL") === "true";
|
|
54
|
+
const username = init?.params?.username ?? env.getEnv("MILVUS_USERNAME");
|
|
55
|
+
const password = init?.params?.password ?? env.getEnv("MILVUS_PASSWORD");
|
|
56
|
+
if (!configOrAddress) {
|
|
57
|
+
throw new Error("Must specify MILVUS_ADDRESS via env variable.");
|
|
58
|
+
}
|
|
59
|
+
this.milvusClient = new milvus2SdkNode.MilvusClient(configOrAddress, ssl, username, password, init?.params?.channelOptions);
|
|
60
|
+
}
|
|
61
|
+
this.collectionName = init?.collection ?? "llamacollection";
|
|
62
|
+
this.idKey = init?.idKey ?? "id";
|
|
63
|
+
this.contentKey = init?.contentKey ?? "content";
|
|
64
|
+
this.metadataKey = init?.metadataKey ?? "metadata";
|
|
65
|
+
this.embeddingKey = init?.embeddingKey ?? "embedding";
|
|
66
|
+
}
|
|
67
|
+
client() {
|
|
68
|
+
return this.milvusClient;
|
|
69
|
+
}
|
|
70
|
+
async createCollection() {
|
|
71
|
+
await this.milvusClient.createCollection({
|
|
72
|
+
collection_name: this.collectionName,
|
|
73
|
+
fields: [
|
|
74
|
+
{
|
|
75
|
+
name: this.idKey,
|
|
76
|
+
data_type: milvus2SdkNode.DataType.VarChar,
|
|
77
|
+
is_primary_key: true,
|
|
78
|
+
max_length: 200
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: this.embeddingKey,
|
|
82
|
+
data_type: milvus2SdkNode.DataType.FloatVector,
|
|
83
|
+
dim: 1536
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: this.contentKey,
|
|
87
|
+
data_type: milvus2SdkNode.DataType.VarChar,
|
|
88
|
+
max_length: 9000
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: this.metadataKey,
|
|
92
|
+
data_type: milvus2SdkNode.DataType.JSON
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
});
|
|
96
|
+
await this.milvusClient.createIndex({
|
|
97
|
+
collection_name: this.collectionName,
|
|
98
|
+
field_name: this.embeddingKey
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
async ensureCollection() {
|
|
102
|
+
if (!this.collectionInitialized) {
|
|
103
|
+
await this.milvusClient.connectPromise;
|
|
104
|
+
// Check collection exists
|
|
105
|
+
const isCollectionExist = await this.milvusClient.hasCollection({
|
|
106
|
+
collection_name: this.collectionName
|
|
107
|
+
});
|
|
108
|
+
if (!isCollectionExist.value) {
|
|
109
|
+
await this.createCollection();
|
|
110
|
+
}
|
|
111
|
+
await this.milvusClient.loadCollectionSync({
|
|
112
|
+
collection_name: this.collectionName
|
|
113
|
+
});
|
|
114
|
+
this.collectionInitialized = true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async add(nodes) {
|
|
118
|
+
await this.ensureCollection();
|
|
119
|
+
const result = await this.milvusClient.insert({
|
|
120
|
+
collection_name: this.collectionName,
|
|
121
|
+
data: nodes.map((node)=>{
|
|
122
|
+
const metadata = core.nodeToMetadata(node, true, this.contentKey, this.flatMetadata);
|
|
123
|
+
const entry = {
|
|
124
|
+
[this.idKey]: node.id_,
|
|
125
|
+
[this.embeddingKey]: node.getEmbedding(),
|
|
126
|
+
[this.contentKey]: node.getContent(core.MetadataMode.NONE),
|
|
127
|
+
[this.metadataKey]: metadata
|
|
128
|
+
};
|
|
129
|
+
return entry;
|
|
130
|
+
})
|
|
131
|
+
});
|
|
132
|
+
if (!result.IDs) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
if ("int_id" in result.IDs) {
|
|
136
|
+
return result.IDs.int_id.data.map((i)=>String(i));
|
|
137
|
+
}
|
|
138
|
+
return result.IDs.str_id.data.map((s)=>String(s));
|
|
139
|
+
}
|
|
140
|
+
async delete(refDocId, deleteOptions) {
|
|
141
|
+
await this.ensureCollection();
|
|
142
|
+
await this.milvusClient.delete({
|
|
143
|
+
ids: [
|
|
144
|
+
refDocId
|
|
145
|
+
],
|
|
146
|
+
collection_name: this.collectionName,
|
|
147
|
+
...deleteOptions
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
toMilvusFilter(filters) {
|
|
151
|
+
if (!filters) return undefined;
|
|
152
|
+
// TODO: Milvus also support standard filters, we can add it later
|
|
153
|
+
return parseScalarFilters(filters);
|
|
154
|
+
}
|
|
155
|
+
async query(query, _options) {
|
|
156
|
+
await this.ensureCollection();
|
|
157
|
+
const found = await this.milvusClient.search({
|
|
158
|
+
collection_name: this.collectionName,
|
|
159
|
+
limit: query.similarityTopK,
|
|
160
|
+
vector: query.queryEmbedding,
|
|
161
|
+
filter: this.toMilvusFilter(query.filters)
|
|
162
|
+
});
|
|
163
|
+
const nodes = [];
|
|
164
|
+
const similarities = [];
|
|
165
|
+
const ids = [];
|
|
166
|
+
found.results.forEach((result)=>{
|
|
167
|
+
const node = core.metadataDictToNode(result.metadata);
|
|
168
|
+
node.setContent(result.content);
|
|
169
|
+
nodes.push(node);
|
|
170
|
+
similarities.push(result.score);
|
|
171
|
+
ids.push(String(result.id));
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
nodes,
|
|
175
|
+
similarities,
|
|
176
|
+
ids
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
async persist() {
|
|
180
|
+
// no need to do anything
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
exports.MilvusVectorStore = MilvusVectorStore;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ChannelOptions } from '@grpc/grpc-js';
|
|
2
|
+
import { BaseVectorStore, VectorStoreBaseParams, BaseNode, Metadata, MetadataFilters, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
|
|
3
|
+
import { MilvusClient, ClientConfig, DeleteReq } from '@zilliz/milvus2-sdk-node';
|
|
4
|
+
|
|
5
|
+
declare class MilvusVectorStore extends BaseVectorStore {
|
|
6
|
+
storesText: boolean;
|
|
7
|
+
isEmbeddingQuery?: boolean;
|
|
8
|
+
private flatMetadata;
|
|
9
|
+
private milvusClient;
|
|
10
|
+
private collectionInitialized;
|
|
11
|
+
private collectionName;
|
|
12
|
+
private idKey;
|
|
13
|
+
private contentKey;
|
|
14
|
+
private metadataKey;
|
|
15
|
+
private embeddingKey;
|
|
16
|
+
constructor(init?: Partial<{
|
|
17
|
+
milvusClient: MilvusClient;
|
|
18
|
+
}> & VectorStoreBaseParams & {
|
|
19
|
+
params?: {
|
|
20
|
+
configOrAddress: ClientConfig | string;
|
|
21
|
+
ssl?: boolean;
|
|
22
|
+
username?: string;
|
|
23
|
+
password?: string;
|
|
24
|
+
channelOptions?: ChannelOptions;
|
|
25
|
+
};
|
|
26
|
+
collection?: string;
|
|
27
|
+
idKey?: string;
|
|
28
|
+
contentKey?: string;
|
|
29
|
+
metadataKey?: string;
|
|
30
|
+
embeddingKey?: string;
|
|
31
|
+
});
|
|
32
|
+
client(): MilvusClient;
|
|
33
|
+
private createCollection;
|
|
34
|
+
private ensureCollection;
|
|
35
|
+
add(nodes: BaseNode<Metadata>[]): Promise<string[]>;
|
|
36
|
+
delete(refDocId: string, deleteOptions?: Omit<DeleteReq, "ids">): Promise<void>;
|
|
37
|
+
toMilvusFilter(filters?: MetadataFilters): string | undefined;
|
|
38
|
+
query(query: VectorStoreQuery, _options?: object): Promise<VectorStoreQueryResult>;
|
|
39
|
+
persist(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { MilvusVectorStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ChannelOptions } from '@grpc/grpc-js';
|
|
2
|
+
import { BaseVectorStore, VectorStoreBaseParams, BaseNode, Metadata, MetadataFilters, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
|
|
3
|
+
import { MilvusClient, ClientConfig, DeleteReq } from '@zilliz/milvus2-sdk-node';
|
|
4
|
+
|
|
5
|
+
declare class MilvusVectorStore extends BaseVectorStore {
|
|
6
|
+
storesText: boolean;
|
|
7
|
+
isEmbeddingQuery?: boolean;
|
|
8
|
+
private flatMetadata;
|
|
9
|
+
private milvusClient;
|
|
10
|
+
private collectionInitialized;
|
|
11
|
+
private collectionName;
|
|
12
|
+
private idKey;
|
|
13
|
+
private contentKey;
|
|
14
|
+
private metadataKey;
|
|
15
|
+
private embeddingKey;
|
|
16
|
+
constructor(init?: Partial<{
|
|
17
|
+
milvusClient: MilvusClient;
|
|
18
|
+
}> & VectorStoreBaseParams & {
|
|
19
|
+
params?: {
|
|
20
|
+
configOrAddress: ClientConfig | string;
|
|
21
|
+
ssl?: boolean;
|
|
22
|
+
username?: string;
|
|
23
|
+
password?: string;
|
|
24
|
+
channelOptions?: ChannelOptions;
|
|
25
|
+
};
|
|
26
|
+
collection?: string;
|
|
27
|
+
idKey?: string;
|
|
28
|
+
contentKey?: string;
|
|
29
|
+
metadataKey?: string;
|
|
30
|
+
embeddingKey?: string;
|
|
31
|
+
});
|
|
32
|
+
client(): MilvusClient;
|
|
33
|
+
private createCollection;
|
|
34
|
+
private ensureCollection;
|
|
35
|
+
add(nodes: BaseNode<Metadata>[]): Promise<string[]>;
|
|
36
|
+
delete(refDocId: string, deleteOptions?: Omit<DeleteReq, "ids">): Promise<void>;
|
|
37
|
+
toMilvusFilter(filters?: MetadataFilters): string | undefined;
|
|
38
|
+
query(query: VectorStoreQuery, _options?: object): Promise<VectorStoreQueryResult>;
|
|
39
|
+
persist(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { MilvusVectorStore };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ChannelOptions } from '@grpc/grpc-js';
|
|
2
|
+
import { BaseVectorStore, VectorStoreBaseParams, BaseNode, Metadata, MetadataFilters, VectorStoreQuery, VectorStoreQueryResult } from '@vectorstores/core';
|
|
3
|
+
import { MilvusClient, ClientConfig, DeleteReq } from '@zilliz/milvus2-sdk-node';
|
|
4
|
+
|
|
5
|
+
declare class MilvusVectorStore extends BaseVectorStore {
|
|
6
|
+
storesText: boolean;
|
|
7
|
+
isEmbeddingQuery?: boolean;
|
|
8
|
+
private flatMetadata;
|
|
9
|
+
private milvusClient;
|
|
10
|
+
private collectionInitialized;
|
|
11
|
+
private collectionName;
|
|
12
|
+
private idKey;
|
|
13
|
+
private contentKey;
|
|
14
|
+
private metadataKey;
|
|
15
|
+
private embeddingKey;
|
|
16
|
+
constructor(init?: Partial<{
|
|
17
|
+
milvusClient: MilvusClient;
|
|
18
|
+
}> & VectorStoreBaseParams & {
|
|
19
|
+
params?: {
|
|
20
|
+
configOrAddress: ClientConfig | string;
|
|
21
|
+
ssl?: boolean;
|
|
22
|
+
username?: string;
|
|
23
|
+
password?: string;
|
|
24
|
+
channelOptions?: ChannelOptions;
|
|
25
|
+
};
|
|
26
|
+
collection?: string;
|
|
27
|
+
idKey?: string;
|
|
28
|
+
contentKey?: string;
|
|
29
|
+
metadataKey?: string;
|
|
30
|
+
embeddingKey?: string;
|
|
31
|
+
});
|
|
32
|
+
client(): MilvusClient;
|
|
33
|
+
private createCollection;
|
|
34
|
+
private ensureCollection;
|
|
35
|
+
add(nodes: BaseNode<Metadata>[]): Promise<string[]>;
|
|
36
|
+
delete(refDocId: string, deleteOptions?: Omit<DeleteReq, "ids">): Promise<void>;
|
|
37
|
+
toMilvusFilter(filters?: MetadataFilters): string | undefined;
|
|
38
|
+
query(query: VectorStoreQuery, _options?: object): Promise<VectorStoreQueryResult>;
|
|
39
|
+
persist(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { MilvusVectorStore };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, parsePrimitiveValue, parseArrayValue } from '@vectorstores/core';
|
|
2
|
+
import { getEnv } from '@vectorstores/env';
|
|
3
|
+
import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
|
|
4
|
+
|
|
5
|
+
function parseScalarFilters(scalarFilters) {
|
|
6
|
+
const condition = scalarFilters.condition ?? "and";
|
|
7
|
+
const filters = [];
|
|
8
|
+
for (const filter of scalarFilters.filters){
|
|
9
|
+
switch(filter.operator){
|
|
10
|
+
case "==":
|
|
11
|
+
case "!=":
|
|
12
|
+
{
|
|
13
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} "${parsePrimitiveValue(filter.value)}"`);
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
case "in":
|
|
17
|
+
{
|
|
18
|
+
const filterValue = parseArrayValue(filter.value).map((v)=>`"${v}"`).join(", ");
|
|
19
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} [${filterValue}]`);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case "nin":
|
|
23
|
+
{
|
|
24
|
+
// Milvus does not support `nin` operator, so we need to manually check every value
|
|
25
|
+
// Expected: not metadata["key"] != "value1" and not metadata["key"] != "value2"
|
|
26
|
+
const filterStr = parseArrayValue(filter.value).map((v)=>`metadata["${filter.key}"] != "${v}"`).join(" && ");
|
|
27
|
+
filters.push(filterStr);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case "<":
|
|
31
|
+
case "<=":
|
|
32
|
+
case ">":
|
|
33
|
+
case ">=":
|
|
34
|
+
{
|
|
35
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} ${parsePrimitiveValue(filter.value)}`);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Operator ${filter.operator} is not supported.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return filters.join(` ${condition} `);
|
|
43
|
+
}
|
|
44
|
+
class MilvusVectorStore extends BaseVectorStore {
|
|
45
|
+
constructor(init){
|
|
46
|
+
super(init), this.storesText = true, this.isEmbeddingQuery = false, this.flatMetadata = true, this.collectionInitialized = false;
|
|
47
|
+
if (init?.milvusClient) {
|
|
48
|
+
this.milvusClient = init.milvusClient;
|
|
49
|
+
} else {
|
|
50
|
+
const configOrAddress = init?.params?.configOrAddress ?? getEnv("MILVUS_ADDRESS");
|
|
51
|
+
const ssl = init?.params?.ssl ?? getEnv("MILVUS_SSL") === "true";
|
|
52
|
+
const username = init?.params?.username ?? getEnv("MILVUS_USERNAME");
|
|
53
|
+
const password = init?.params?.password ?? getEnv("MILVUS_PASSWORD");
|
|
54
|
+
if (!configOrAddress) {
|
|
55
|
+
throw new Error("Must specify MILVUS_ADDRESS via env variable.");
|
|
56
|
+
}
|
|
57
|
+
this.milvusClient = new MilvusClient(configOrAddress, ssl, username, password, init?.params?.channelOptions);
|
|
58
|
+
}
|
|
59
|
+
this.collectionName = init?.collection ?? "llamacollection";
|
|
60
|
+
this.idKey = init?.idKey ?? "id";
|
|
61
|
+
this.contentKey = init?.contentKey ?? "content";
|
|
62
|
+
this.metadataKey = init?.metadataKey ?? "metadata";
|
|
63
|
+
this.embeddingKey = init?.embeddingKey ?? "embedding";
|
|
64
|
+
}
|
|
65
|
+
client() {
|
|
66
|
+
return this.milvusClient;
|
|
67
|
+
}
|
|
68
|
+
async createCollection() {
|
|
69
|
+
await this.milvusClient.createCollection({
|
|
70
|
+
collection_name: this.collectionName,
|
|
71
|
+
fields: [
|
|
72
|
+
{
|
|
73
|
+
name: this.idKey,
|
|
74
|
+
data_type: DataType.VarChar,
|
|
75
|
+
is_primary_key: true,
|
|
76
|
+
max_length: 200
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: this.embeddingKey,
|
|
80
|
+
data_type: DataType.FloatVector,
|
|
81
|
+
dim: 1536
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: this.contentKey,
|
|
85
|
+
data_type: DataType.VarChar,
|
|
86
|
+
max_length: 9000
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: this.metadataKey,
|
|
90
|
+
data_type: DataType.JSON
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
await this.milvusClient.createIndex({
|
|
95
|
+
collection_name: this.collectionName,
|
|
96
|
+
field_name: this.embeddingKey
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async ensureCollection() {
|
|
100
|
+
if (!this.collectionInitialized) {
|
|
101
|
+
await this.milvusClient.connectPromise;
|
|
102
|
+
// Check collection exists
|
|
103
|
+
const isCollectionExist = await this.milvusClient.hasCollection({
|
|
104
|
+
collection_name: this.collectionName
|
|
105
|
+
});
|
|
106
|
+
if (!isCollectionExist.value) {
|
|
107
|
+
await this.createCollection();
|
|
108
|
+
}
|
|
109
|
+
await this.milvusClient.loadCollectionSync({
|
|
110
|
+
collection_name: this.collectionName
|
|
111
|
+
});
|
|
112
|
+
this.collectionInitialized = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async add(nodes) {
|
|
116
|
+
await this.ensureCollection();
|
|
117
|
+
const result = await this.milvusClient.insert({
|
|
118
|
+
collection_name: this.collectionName,
|
|
119
|
+
data: nodes.map((node)=>{
|
|
120
|
+
const metadata = nodeToMetadata(node, true, this.contentKey, this.flatMetadata);
|
|
121
|
+
const entry = {
|
|
122
|
+
[this.idKey]: node.id_,
|
|
123
|
+
[this.embeddingKey]: node.getEmbedding(),
|
|
124
|
+
[this.contentKey]: node.getContent(MetadataMode.NONE),
|
|
125
|
+
[this.metadataKey]: metadata
|
|
126
|
+
};
|
|
127
|
+
return entry;
|
|
128
|
+
})
|
|
129
|
+
});
|
|
130
|
+
if (!result.IDs) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
if ("int_id" in result.IDs) {
|
|
134
|
+
return result.IDs.int_id.data.map((i)=>String(i));
|
|
135
|
+
}
|
|
136
|
+
return result.IDs.str_id.data.map((s)=>String(s));
|
|
137
|
+
}
|
|
138
|
+
async delete(refDocId, deleteOptions) {
|
|
139
|
+
await this.ensureCollection();
|
|
140
|
+
await this.milvusClient.delete({
|
|
141
|
+
ids: [
|
|
142
|
+
refDocId
|
|
143
|
+
],
|
|
144
|
+
collection_name: this.collectionName,
|
|
145
|
+
...deleteOptions
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
toMilvusFilter(filters) {
|
|
149
|
+
if (!filters) return undefined;
|
|
150
|
+
// TODO: Milvus also support standard filters, we can add it later
|
|
151
|
+
return parseScalarFilters(filters);
|
|
152
|
+
}
|
|
153
|
+
async query(query, _options) {
|
|
154
|
+
await this.ensureCollection();
|
|
155
|
+
const found = await this.milvusClient.search({
|
|
156
|
+
collection_name: this.collectionName,
|
|
157
|
+
limit: query.similarityTopK,
|
|
158
|
+
vector: query.queryEmbedding,
|
|
159
|
+
filter: this.toMilvusFilter(query.filters)
|
|
160
|
+
});
|
|
161
|
+
const nodes = [];
|
|
162
|
+
const similarities = [];
|
|
163
|
+
const ids = [];
|
|
164
|
+
found.results.forEach((result)=>{
|
|
165
|
+
const node = metadataDictToNode(result.metadata);
|
|
166
|
+
node.setContent(result.content);
|
|
167
|
+
nodes.push(node);
|
|
168
|
+
similarities.push(result.score);
|
|
169
|
+
ids.push(String(result.id));
|
|
170
|
+
});
|
|
171
|
+
return {
|
|
172
|
+
nodes,
|
|
173
|
+
similarities,
|
|
174
|
+
ids
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async persist() {
|
|
178
|
+
// no need to do anything
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export { MilvusVectorStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, parsePrimitiveValue, parseArrayValue } from '@vectorstores/core';
|
|
2
|
+
import { getEnv } from '@vectorstores/env';
|
|
3
|
+
import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
|
|
4
|
+
|
|
5
|
+
function parseScalarFilters(scalarFilters) {
|
|
6
|
+
const condition = scalarFilters.condition ?? "and";
|
|
7
|
+
const filters = [];
|
|
8
|
+
for (const filter of scalarFilters.filters){
|
|
9
|
+
switch(filter.operator){
|
|
10
|
+
case "==":
|
|
11
|
+
case "!=":
|
|
12
|
+
{
|
|
13
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} "${parsePrimitiveValue(filter.value)}"`);
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
case "in":
|
|
17
|
+
{
|
|
18
|
+
const filterValue = parseArrayValue(filter.value).map((v)=>`"${v}"`).join(", ");
|
|
19
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} [${filterValue}]`);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case "nin":
|
|
23
|
+
{
|
|
24
|
+
// Milvus does not support `nin` operator, so we need to manually check every value
|
|
25
|
+
// Expected: not metadata["key"] != "value1" and not metadata["key"] != "value2"
|
|
26
|
+
const filterStr = parseArrayValue(filter.value).map((v)=>`metadata["${filter.key}"] != "${v}"`).join(" && ");
|
|
27
|
+
filters.push(filterStr);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case "<":
|
|
31
|
+
case "<=":
|
|
32
|
+
case ">":
|
|
33
|
+
case ">=":
|
|
34
|
+
{
|
|
35
|
+
filters.push(`metadata["${filter.key}"] ${filter.operator} ${parsePrimitiveValue(filter.value)}`);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Operator ${filter.operator} is not supported.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return filters.join(` ${condition} `);
|
|
43
|
+
}
|
|
44
|
+
class MilvusVectorStore extends BaseVectorStore {
|
|
45
|
+
constructor(init){
|
|
46
|
+
super(init), this.storesText = true, this.isEmbeddingQuery = false, this.flatMetadata = true, this.collectionInitialized = false;
|
|
47
|
+
if (init?.milvusClient) {
|
|
48
|
+
this.milvusClient = init.milvusClient;
|
|
49
|
+
} else {
|
|
50
|
+
const configOrAddress = init?.params?.configOrAddress ?? getEnv("MILVUS_ADDRESS");
|
|
51
|
+
const ssl = init?.params?.ssl ?? getEnv("MILVUS_SSL") === "true";
|
|
52
|
+
const username = init?.params?.username ?? getEnv("MILVUS_USERNAME");
|
|
53
|
+
const password = init?.params?.password ?? getEnv("MILVUS_PASSWORD");
|
|
54
|
+
if (!configOrAddress) {
|
|
55
|
+
throw new Error("Must specify MILVUS_ADDRESS via env variable.");
|
|
56
|
+
}
|
|
57
|
+
this.milvusClient = new MilvusClient(configOrAddress, ssl, username, password, init?.params?.channelOptions);
|
|
58
|
+
}
|
|
59
|
+
this.collectionName = init?.collection ?? "llamacollection";
|
|
60
|
+
this.idKey = init?.idKey ?? "id";
|
|
61
|
+
this.contentKey = init?.contentKey ?? "content";
|
|
62
|
+
this.metadataKey = init?.metadataKey ?? "metadata";
|
|
63
|
+
this.embeddingKey = init?.embeddingKey ?? "embedding";
|
|
64
|
+
}
|
|
65
|
+
client() {
|
|
66
|
+
return this.milvusClient;
|
|
67
|
+
}
|
|
68
|
+
async createCollection() {
|
|
69
|
+
await this.milvusClient.createCollection({
|
|
70
|
+
collection_name: this.collectionName,
|
|
71
|
+
fields: [
|
|
72
|
+
{
|
|
73
|
+
name: this.idKey,
|
|
74
|
+
data_type: DataType.VarChar,
|
|
75
|
+
is_primary_key: true,
|
|
76
|
+
max_length: 200
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: this.embeddingKey,
|
|
80
|
+
data_type: DataType.FloatVector,
|
|
81
|
+
dim: 1536
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: this.contentKey,
|
|
85
|
+
data_type: DataType.VarChar,
|
|
86
|
+
max_length: 9000
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: this.metadataKey,
|
|
90
|
+
data_type: DataType.JSON
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
await this.milvusClient.createIndex({
|
|
95
|
+
collection_name: this.collectionName,
|
|
96
|
+
field_name: this.embeddingKey
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async ensureCollection() {
|
|
100
|
+
if (!this.collectionInitialized) {
|
|
101
|
+
await this.milvusClient.connectPromise;
|
|
102
|
+
// Check collection exists
|
|
103
|
+
const isCollectionExist = await this.milvusClient.hasCollection({
|
|
104
|
+
collection_name: this.collectionName
|
|
105
|
+
});
|
|
106
|
+
if (!isCollectionExist.value) {
|
|
107
|
+
await this.createCollection();
|
|
108
|
+
}
|
|
109
|
+
await this.milvusClient.loadCollectionSync({
|
|
110
|
+
collection_name: this.collectionName
|
|
111
|
+
});
|
|
112
|
+
this.collectionInitialized = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async add(nodes) {
|
|
116
|
+
await this.ensureCollection();
|
|
117
|
+
const result = await this.milvusClient.insert({
|
|
118
|
+
collection_name: this.collectionName,
|
|
119
|
+
data: nodes.map((node)=>{
|
|
120
|
+
const metadata = nodeToMetadata(node, true, this.contentKey, this.flatMetadata);
|
|
121
|
+
const entry = {
|
|
122
|
+
[this.idKey]: node.id_,
|
|
123
|
+
[this.embeddingKey]: node.getEmbedding(),
|
|
124
|
+
[this.contentKey]: node.getContent(MetadataMode.NONE),
|
|
125
|
+
[this.metadataKey]: metadata
|
|
126
|
+
};
|
|
127
|
+
return entry;
|
|
128
|
+
})
|
|
129
|
+
});
|
|
130
|
+
if (!result.IDs) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
if ("int_id" in result.IDs) {
|
|
134
|
+
return result.IDs.int_id.data.map((i)=>String(i));
|
|
135
|
+
}
|
|
136
|
+
return result.IDs.str_id.data.map((s)=>String(s));
|
|
137
|
+
}
|
|
138
|
+
async delete(refDocId, deleteOptions) {
|
|
139
|
+
await this.ensureCollection();
|
|
140
|
+
await this.milvusClient.delete({
|
|
141
|
+
ids: [
|
|
142
|
+
refDocId
|
|
143
|
+
],
|
|
144
|
+
collection_name: this.collectionName,
|
|
145
|
+
...deleteOptions
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
toMilvusFilter(filters) {
|
|
149
|
+
if (!filters) return undefined;
|
|
150
|
+
// TODO: Milvus also support standard filters, we can add it later
|
|
151
|
+
return parseScalarFilters(filters);
|
|
152
|
+
}
|
|
153
|
+
async query(query, _options) {
|
|
154
|
+
await this.ensureCollection();
|
|
155
|
+
const found = await this.milvusClient.search({
|
|
156
|
+
collection_name: this.collectionName,
|
|
157
|
+
limit: query.similarityTopK,
|
|
158
|
+
vector: query.queryEmbedding,
|
|
159
|
+
filter: this.toMilvusFilter(query.filters)
|
|
160
|
+
});
|
|
161
|
+
const nodes = [];
|
|
162
|
+
const similarities = [];
|
|
163
|
+
const ids = [];
|
|
164
|
+
found.results.forEach((result)=>{
|
|
165
|
+
const node = metadataDictToNode(result.metadata);
|
|
166
|
+
node.setContent(result.content);
|
|
167
|
+
nodes.push(node);
|
|
168
|
+
similarities.push(result.score);
|
|
169
|
+
ids.push(String(result.id));
|
|
170
|
+
});
|
|
171
|
+
return {
|
|
172
|
+
nodes,
|
|
173
|
+
similarities,
|
|
174
|
+
ids
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async persist() {
|
|
178
|
+
// no need to do anything
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export { MilvusVectorStore };
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vectorstores/milvus",
|
|
3
|
+
"description": "Milvus 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/milvus"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"vitest": "^2.1.5",
|
|
38
|
+
"@vectorstores/core": "0.1.0",
|
|
39
|
+
"@vectorstores/env": "0.1.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@vectorstores/core": "0.1.0",
|
|
43
|
+
"@vectorstores/env": "0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@grpc/grpc-js": "^1.12.2",
|
|
47
|
+
"@zilliz/milvus2-sdk-node": "^2.4.6"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "bunchee",
|
|
51
|
+
"dev": "bunchee --watch",
|
|
52
|
+
"test": "vitest run"
|
|
53
|
+
}
|
|
54
|
+
}
|