Class Introduction
Function
Provides a Milvus-based vector database. For collections within the same database, add, add_sparse, and add_dense_and_sparse must be used independently. If they are used together, operations will fail.
Prototype
from mx_rag.storage.vectorstore import MilvusDB MilvusDB(client, collection_name, search_mode, auto_id, index_type, metric_type, auto_flush)
Parameters
Parameter |
Data Type |
Required/Optional |
Description |
|---|---|---|---|
client |
MilvusClient |
Required |
MilvusClient instance. For details, see MilvusClient. NOTE:
MilvusClient is controlled by users. Use a secure connection mode. |
collection_name |
String |
Optional |
Collection name, which cannot be empty. The maximum length is 1024 characters. The default value is rag_sdk. |
search_mode |
SearchMode |
Optional |
Retrieval mode. Currently, three modes are supported: DENSE for dense retrieval (default), SPARSE for sparse retrieval, and HYBRID for hybrid retrieval. For more details, see SearchMode. |
auto_id |
Bool |
Optional |
Whether to automatically increment primary keys. The default value is False. |
index_type |
String |
Optional |
Vector retrieval type. Currently, FLAT (default), IVF_FLAT, IVF_PQ, and HNSW are supported. This parameter is valid for dense vectors in dense and hybrid retrieval modes. SPARSE_INVERTED_INDEX is used for sparse vector retrieval and cannot be changed. |
metric_type |
String |
Optional |
Vector distance calculation method, which can be IP, L2 (default), and COSINE. This parameter is valid for dense vectors in dense and hybrid retrieval modes. IP is used for sparse vector distance calculation and cannot be changed. |
auto_flush |
Bool |
Optional |
Whether to automatically update memory data during data changes. The default value is True. |
Return Value
Data Type |
Description |
|---|---|
MilvusDB |
MilvusDB object. |
Example
import getpass
from pymilvus import MilvusClient
from mx_rag.storage.vectorstore import MilvusDB
import numpy as np
client = MilvusClient("https://x.x.x.x:port", user="xxx", password=getpass.getpass(), secure=True, client_pem_path="path_to/client.pem", client_key_path="path_to/client.key", ca_pem_path="path_to/ca.pem", server_name="localhost")
vector_store = MilvusDB.create(client=client, x_dim=1024)
vecs = np.random.randn(3, 1024)
vector_store.add([0, 1, 2], vecs)
print(vector_store.get_all_ids())
vector_store.delete([1])
vector_store.get_all_ids()
print(vector_store.search(vecs[1:2, :].tolist()))
vector_store.update([0], vecs[:1])
vector_store.drop_collection()