Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Vector Database Service to allow stages to read and write to VDBs #1225

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5d7b3cb
Added milvus vdb prototype impl
bsuryadevara Sep 26, 2023
4807f3d
Added milvus vdb prototype impl
bsuryadevara Sep 26, 2023
b1f94fb
Added llamaindex and langchain prototypes
bsuryadevara Sep 27, 2023
d912645
doc updates
bsuryadevara Sep 27, 2023
4ecd37f
updates to milvus vd service
bsuryadevara Sep 30, 2023
c18125a
updated search and upsert functions
bsuryadevara Oct 2, 2023
a6ef60e
Added write_to_vector_db stage
bsuryadevara Oct 3, 2023
7389542
Added tests to get started
bsuryadevara Oct 3, 2023
3a31cee
Added tests to get started
bsuryadevara Oct 3, 2023
4cfba55
Added MilvusClient extension class to support missing functions
bsuryadevara Oct 4, 2023
b83f517
Added tests for Milvus vector database serivce
bsuryadevara Oct 4, 2023
b7fee57
Added tests for Milvus vector database service
bsuryadevara Oct 4, 2023
cde18b2
Added tests for Milvus vector database service
bsuryadevara Oct 4, 2023
c9316c0
Added milvus lite to pipeline tests
bsuryadevara Oct 9, 2023
36f1f18
Added tests with milvus lite
bsuryadevara Oct 11, 2023
2f24cc2
Updated Milvus VDB tests
bsuryadevara Oct 11, 2023
9670c97
Merge remote-tracking branch 'upstream/branch-23.11' into 1177-fea-ad…
bsuryadevara Oct 11, 2023
e4b8a02
Updated Milvus VDB tests
bsuryadevara Oct 11, 2023
a5e742e
Added tests with milvus lite
bsuryadevara Oct 11, 2023
3d0e01b
Renamed a file
bsuryadevara Oct 11, 2023
cd52a5f
Feedback changes
bsuryadevara Oct 12, 2023
5ce3402
Feedback changes
bsuryadevara Oct 12, 2023
9e6989a
Removed register stage decorator
bsuryadevara Oct 12, 2023
cf327b5
Ignore pymilvus in the docs
bsuryadevara Oct 13, 2023
a6a6f43
Update variable names
bsuryadevara Oct 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added tests to get started
  • Loading branch information
bsuryadevara committed Oct 3, 2023
commit 7389542b67f43f8c19dbc9b1dd8ee4a5b36b2d7e
5 changes: 2 additions & 3 deletions morpheus/service/milvus_vector_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def _create_schema_field(self, field_conf: dict) -> pymilvus.FieldSchema:

return field_schema

@with_mutex("_mutex")
def create(self, name: str, overwrite: bool = False, **kwargs: dict[str, typing.Any]):
collection_conf = kwargs.get("collection_conf")
auto_id = collection_conf.get("auto_id", False)
Expand Down Expand Up @@ -268,15 +267,15 @@ def search(self, name: str, query: typing.Union[str, dict] = None, **kwargs: dic
self._handler.release_collection(collection_name=name)

@with_mutex("_mutex")
def update(self, name: str, data: typing.Any, **kwargs: dict[str, typing.Any]) -> typing.Any:
def update(self, name: str, data: list[dict], **kwargs: dict[str, typing.Any]) -> typing.Any:
"""
Update data in the vector database.

Parameters
----------
name : str
Name of the resource.
data : typing.Any
data : list[dict]
Data to be updated in the resource.
**kwargs : dict[str, typing.Any]
Extra keyword arguments specific to upsert operation.
Expand Down
3 changes: 2 additions & 1 deletion morpheus/service/vector_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def insert(self, name: str, data: typing.Any, **kwargs: dict[str, typing.Any]) -
pass

@abstractmethod
def insert_dataframe(self, name: str,
def insert_dataframe(self,
name: str,
df: typing.Union[cudf.DataFrame, pd.DataFrame],
**kwargs: dict[str, typing.Any]):
"""
Expand Down
30 changes: 23 additions & 7 deletions morpheus/stages/output/write_to_vector_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import logging
import typing

Expand Down Expand Up @@ -41,6 +40,8 @@ class WriteToVectorDBStage(SinglePortStage):
Pipeline configuration instance.
resource_name : str
The name of the resource managed by this instance.
resource_conf : dict
Additional resource configuration when performing vector database writes.
vdb_service : typing.Union[str, VectorDBService]
Either the name of the vector database service to use or an instance of VectorDBService
for managing the resource.
Expand All @@ -52,22 +53,30 @@ class WriteToVectorDBStage(SinglePortStage):
ValueError
If `vdb_service` is not a valid string (service name) or an instance of VectorDBService.
"""
def __init__(self, config: Config, resource_name: str,

def __init__(self,
config: Config,
resource_name: str,
vdb_service: typing.Union[str, VectorDBService],
**kwargs: dict[str, typing.Any]):

super().__init__(config)

self._resource_name = resource_name
self._resource_conf = {}

if "resource_conf" in kwargs:
self._resource_conf = kwargs.pop("resource_conf")

if isinstance(vdb_service, str):
# If vdb_input is a string, assume it's the service name
# If vdb_service is a string, assume it's the service name
self._vdb_service: VectorDBService = VectorDBServiceFactory.create_instance(service_name=vdb_service,
**kwargs)
elif isinstance(vdb_service, VectorDBService):
# If vdb_input is an instance of VectorDBService, use it directly
# If vdb_service is an instance of VectorDBService, use it directly
self._vdb_service: VectorDBService = vdb_service
else:
raise ValueError("vdb_input must be a string (service name) or an instance of VectorDBService")
raise ValueError("vdb_service must be a string (service name) or an instance of VectorDBService")

@property
def name(self) -> str:
Expand All @@ -89,17 +98,24 @@ def supports_cpp_node(self):
"""Indicates whether this stage supports a C++ node."""
return False

def on_completed(self):
# Close vector database service connection
self._vdb_service.close()

def _build_single(self, builder: mrc.Builder, input_stream: StreamPair) -> StreamPair:

stream = input_stream[0]

def on_data(ctrl_msg: ControlMessage) -> ControlMessage:
result = self._vdb_service.insert_dataframe(name=self._resource_name, df=ctrl_msg.payload().df)
# Insert entries in the dataframe to vector database.
result = self._vdb_service.insert_dataframe(name=self._resource_name,
df=ctrl_msg.payload().df,
**self._resource_conf)
ctrl_msg.set_metadata("insert_response", result)

return ctrl_msg

to_vector_db = builder.make_node(self.unique_name, ops.map(on_data))
to_vector_db = builder.make_node(self.unique_name, ops.map(on_data), ops.on_completed(self.on_completed))

builder.make_edge(stream, to_vector_db)
stream = to_vector_db
Expand Down
174 changes: 144 additions & 30 deletions tests/test_write_to_vector_db_stage_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,63 +32,177 @@
from morpheus.utils.module_ids import TO_CONTROL_MESSAGE


@pytest.fixture(scope="function", name="milvus_vdb_serivce_fixture")
def milvus_vdb_serivce():
vdb_service = MilvusVectorDBService(uri="http://localhost:19530")
return vdb_service


def create_milvus_collection(vdb_service: MilvusVectorDBService):
collection_config = {
"collection_conf": {
"shards": 2,
"auto_id": False,
"consistency_level": "Strong",
"description": "Test collection",
"schema_conf": {
"enable_dynamic_field": True,
"schema_fields": [
{
"name": "id",
"dtype": "int64",
"description": "Primary key for the collection",
"is_primary": True,
},
{
"name": "embedding",
"dtype": "float_vector",
"description": "Embedding vectors",
"is_primary": False,
"dim": 10,
},
{
"name": "age",
"dtype": "int64",
"description ": "Age",
"is_primary": False,
},
],
"description": "Test collection schema",
},
}
}
vdb_service.create(name="test", overwrite=True, **collection_config)


def create_milvus_collection_idx_part(vdb_service: MilvusVectorDBService):
collection_config = {
"collection_conf": {
"shards": 2,
"auto_id": False,
"consistency_level": "Strong",
"description": "Test collection with partition and index",
"index_conf": {
"field_name": "embedding", "metric_type": "L2"
},
"partition_conf": {
"timeout": 1, "partitions": [{
"name": "age_partition", "description": "Partition by age"
}]
},
"schema_conf": {
"enable_dynamic_field": True,
"schema_fields": [
{
"name": "id",
"dtype": "int64",
"description": "Primary key for the collection",
"is_primary": True,
},
{
"name": "embedding",
"dtype": "float_vector",
"description": "Embedding vectors",
"is_primary": False,
"dim": 10,
},
{
"name": "age",
"dtype": "int64",
"description ": "Age",
"is_primary": False,
},
],
"description": "Test collection schema",
},
}
}
vdb_service.create(name="test_idx_part", overwrite=True, **collection_config)


@pytest.mark.use_cpp
def test_write_to_vector_db_stage_with_instance_pipe(config: Config, pipeline_batch_size: int = 256):
def test_write_to_vector_db_stage_with_instance_pipe(milvus_vdb_serivce_fixture,
config: Config,
pipeline_batch_size: int = 256):
config.pipeline_batch_size = pipeline_batch_size

rows_count = 5
dimensions = 10
collection_name = "test"

create_milvus_collection(milvus_vdb_serivce_fixture)

df = cudf.DataFrame({
"id": [i for i in range(rows_count)],
"age": [random.randint(20, 40) for i in range(rows_count)],
"embedding": [[random.random() for _ in range(dimensions)] for _ in range(rows_count)]
})
"id": [i for i in range(rows_count)],
"age": [random.randint(20, 40) for i in range(rows_count)],
"embedding": [[random.random() for _ in range(dimensions)] for _ in range(rows_count)]
})

to_cm_module_config = {
"module_id": TO_CONTROL_MESSAGE,
"module_name": "to_control_message",
"namespace": MORPHEUS_MODULE_NAMESPACE
"module_id": TO_CONTROL_MESSAGE, "module_name": "to_control_message", "namespace": MORPHEUS_MODULE_NAMESPACE
}
vdb_service = MilvusVectorDBService(uri="http://localhost:19530")

pipe = LinearPipeline(config)
pipe.set_source(InMemorySourceStage(config, [df]))
pipe.add_stage(LinearModulesStage(config, to_cm_module_config,
input_port_name="input",
output_port_name="output",
output_type=ControlMessage))
pipe.add_stage(WriteToVectorDBStage(config, resource_name="test", vdb_service=vdb_service))
pipe.add_stage(
LinearModulesStage(config,
to_cm_module_config,
input_port_name="input",
output_port_name="output",
output_type=ControlMessage))
pipe.add_stage(WriteToVectorDBStage(config, resource_name=collection_name, vdb_service=vdb_service))

pipe.run()

actual_count = milvus_vdb_serivce_fixture.count(name=collection_name)
milvus_vdb_serivce_fixture.close()

assert actual_count == 5


@pytest.mark.use_cpp
def test_write_to_vector_db_stage_with_name_pipe(config: Config, pipeline_batch_size: int = 256):
def test_write_to_vector_db_stage_with_name_pipe(milvus_vdb_serivce_fixture,
config: Config,
pipeline_batch_size: int = 256):
config.pipeline_batch_size = pipeline_batch_size

create_milvus_collection_idx_part(milvus_vdb_serivce_fixture)

rows_count = 5
dimensions = 10
collection_name = "test_idx_part"

resource_conf = {"collection_conf": {"partition_name": "age_partition"}}

df = cudf.DataFrame({
"id": [i for i in range(rows_count)],
"age": [random.randint(20, 40) for i in range(rows_count)],
"embedding": [[random.random() for _ in range(dimensions)] for _ in range(rows_count)]
})
"id": [i for i in range(rows_count)],
"age": [random.randint(20, 40) for i in range(rows_count)],
"embedding": [[random.random() for _ in range(dimensions)] for _ in range(rows_count)]
})

to_cm_module_config = {
"module_id": TO_CONTROL_MESSAGE,
"module_name": "to_control_message",
"namespace": MORPHEUS_MODULE_NAMESPACE
"module_id": TO_CONTROL_MESSAGE, "module_name": "to_control_message", "namespace": MORPHEUS_MODULE_NAMESPACE
}

pipe = LinearPipeline(config)
pipe.set_source(InMemorySourceStage(config, [df]))
pipe.add_stage(LinearModulesStage(config, to_cm_module_config,
input_port_name="input",
output_port_name="output",
output_type=ControlMessage))
pipe.add_stage(WriteToVectorDBStage(config,
resource_name="test_collection",
vdb_service="milvus",
uri="http://localhost:19530"))
pipe.set_source(InMemorySourceStage(config, [df], repeat=2))
pipe.add_stage(
LinearModulesStage(config,
to_cm_module_config,
input_port_name="input",
output_port_name="output",
output_type=ControlMessage))
pipe.add_stage(
WriteToVectorDBStage(config,
resource_name=collection_name,
vdb_service="milvus",
uri="http://localhost:19530",
resource_conf=resource_conf))

pipe.run()

actual_count = milvus_vdb_serivce_fixture.count(name=collection_name)
milvus_vdb_serivce_fixture.close()

assert actual_count == 10
Loading