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
updated search and upsert functions
  • Loading branch information
bsuryadevara committed Oct 2, 2023
commit c18125ac30920dc068f4db521513a94c1ec19331
97 changes: 52 additions & 45 deletions morpheus/service/milvus_vector_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pandas as pd
import pymilvus
from pymilvus import Collection
from pymilvus.exceptions import MilvusException

import cudf

Expand Down Expand Up @@ -92,20 +93,13 @@ def list_store_objects(self, **kwargs: dict[str, typing.Any]) -> list[str]:
return self._client.list_collections(**kwargs)

def _create_schema_field(self, field_conf: dict) -> pymilvus.FieldSchema:
dtype = MILVUS_DATA_TYPE_MAP[field_conf["dtype"].lower()]
dim = field_conf.get("dim", None)

if (dtype == pymilvus.DataType.BINARY_VECTOR or dtype == pymilvus.DataType.FLOAT_VECTOR):
if not dim:
raise ValueError(f"Dimensions for {dtype} should not be None")
if not isinstance(dim, int):
raise ValueError(f"Dimensions for {dtype} should be an integer")

field_schema = pymilvus.FieldSchema(name=field_conf["name"],
dtype=dtype,
description=field_conf.get("description", ""),
is_primary=field_conf["is_primary"],
dim=dim)
name = field_conf.pop("name")
dtype = field_conf.pop("dtype")

dtype = MILVUS_DATA_TYPE_MAP[dtype.lower()]

field_schema = pymilvus.FieldSchema(name=name, dtype=dtype, **field_conf)

return field_schema

@with_mutex("_mutex")
Expand All @@ -116,9 +110,10 @@ def create(self, name: str, overwrite: bool = False, **kwargs: dict[str, typing.
partition_conf = collection_conf.get("partition_conf", None)

schema_conf = collection_conf.get("schema_conf")
schema_fields_conf = schema_conf.get("schema_fields")
schema_fields_conf = schema_conf.pop("schema_fields")

index_param = None

if overwrite:
if self.has_store_object(name):
self.drop(name)
Expand All @@ -130,25 +125,22 @@ def create(self, name: str, overwrite: bool = False, **kwargs: dict[str, typing.

schema_fields = [self._create_schema_field(field_conf=field_conf) for field_conf in schema_fields_conf]

schema = pymilvus.CollectionSchema(fields=schema_fields,
auto_id=auto_id,
description=schema_conf.get("description", ""))
schema = pymilvus.CollectionSchema(fields=schema_fields, **schema_conf)

if index_conf:
index_param = self._client.prepare_index_params(field_name=index_conf["field_name"],
index_type=index_conf.get("index_type", None),
metric_type=index_conf.get("metric_type", None),
index_name=index_conf.get("index_name", ""),
timeout=index_conf.get("timeout", 1.0),
params=index_conf.get("params", 1.0))
field_name = index_conf.pop("field_name")
metric_type = index_conf.pop("metric_type")
index_param = self._client.prepare_index_params(field_name=field_name,
metric_type=metric_type,
**index_conf)

self._client.create_collection_with_schema(collection_name=name,
schema=schema,
index_param=index_param,
auto_id=auto_id,
shards_num=collection_conf.get("shards", 2),
consistency_level=collection_conf.get("consistency_level",
"Strong"))
"Strong"))

if partition_conf:
timeout = partition_conf.get("timeout", 1.0)
Expand Down Expand Up @@ -182,6 +174,7 @@ def insert(self, name: str, data: typing.Union[list[list], list[dict], dict], **
def _collection_insert(self, name: str,
data: typing.Union[list[list], list[dict], dict],
**kwargs: dict[str, typing.Any]) -> None:
collection = None
try:
if not self.has_store_object(name):
raise RuntimeError(f"Collection {name} doesn't exist.")
Expand Down Expand Up @@ -255,31 +248,45 @@ def search(self, name: str, query: typing.Union[str, dict] = None, **kwargs: dic
If an error occurs during the search operation.
"""

use_partitions = kwargs.get("use_partitions", False)
collection = pymilvus.Collection(name=name)

try:
if use_partitions:
partitions = kwargs.get("partitions")
collection.load(partitions)
self._handler.load_collection(collection_name=name)
if query is not None:
result = self._client.query(collection_name=name, filter=query, **kwargs)
else:
collection.load()

if query:
result = collection.query(expr=query, **kwargs)
else:
result = collection.search(**kwargs)

if "data" not in kwargs:
raise ValueError("The search operation requires that search vectors be " +
"provided as a keyword argument 'data'.")
data = kwargs.pop("data")
result = self._client.search(collection_name=name, data=data, **kwargs)
self._handler.release_collection(collection_name=name)
return result
except MilvusException as exec:
raise RuntimeError(f"Unable to perform serach: {exec}") from exec

except Exception as exec_info:
raise RuntimeError(f"Error while performing search: {exec_info}") from exec_info
finally:
collection.release()
self._handler.release_collection(collection_name=name)

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

Parameters
----------
name : str
Name of the resource.
data : typing.Any
Data to be updated in the resource.
**kwargs : dict[str, typing.Any]
Extra keyword arguments specific to upsert operation.
"""

if not isinstance(data, list):
raise RuntimeError("Data is not of type list.")

response = self._handler.upsert(collection_name=name, entities=data, **kwargs)

return response

@with_mutex("_mutex")
def delete_by_keys(self, name: str,
Expand Down Expand Up @@ -388,8 +395,8 @@ def drop(self, name: str, **kwargs: dict[str, typing.Any]) -> None:
elif type == "index":
if "field_name" in kwargs and "index_name" in kwargs:
self._handler.drop_index(collection_name=name,
field_name=kwargs["field_name"],
index_name=kwargs["index_name"])
field_name=kwargs["field_name"],
index_name=kwargs["index_name"])
else:
raise ValueError("Mandatory fields missing")

Expand Down
8 changes: 7 additions & 1 deletion morpheus/utils/vector_db_service_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
from pymilvus import DataType

MILVUS_DATA_TYPE_MAP = {
"int8": DataType.INT8,
"int16": DataType.INT16,
"int32": DataType.INT32,
"int64": DataType.INT64,
"bool": DataType.BOOL,
"float": DataType.FLOAT,
"double": DataType.DOUBLE,
"binary_vector": DataType.BINARY_VECTOR,
"float_vector": DataType.FLOAT_VECTOR
"float_vector": DataType.FLOAT_VECTOR,
"string": DataType.STRING,
"varchar": DataType.VARCHAR,
"json": DataType.JSON,
}


Expand Down
Loading