-
Notifications
You must be signed in to change notification settings - Fork 263
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 Milvus integration for vector create and search #1269
Merged
xzdandy
merged 27 commits into
georgia-tech-db:staging
from
RichardZhangRZ:milvus-integration
Oct 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
9fa1266
Added Milvus integration for vector create and search
RichardZhangRZ 2793799
Merge branch 'staging' into milvus-integration
RichardZhangRZ 0459817
Added docs and switched to using provided values for Milvus
RichardZhangRZ f1c6e6c
Fixed merge conflicts
RichardZhangRZ 02d6d24
Skip Milvus integration test
RichardZhangRZ cc72ac5
Removed unnecessary required param
RichardZhangRZ f6d3202
Fixed linter errors
RichardZhangRZ 11d9bc8
Fixed doc issues
RichardZhangRZ 400aed6
Some quick changes
RichardZhangRZ 8d69263
Removed value
RichardZhangRZ 8965a8e
Merge branch 'staging' into milvus-integration
RichardZhangRZ 3444f17
Added linting suppression
RichardZhangRZ a12c4ee
test commit
RichardZhangRZ 1c17276
Added more words to diciontary
RichardZhangRZ 4815828
Temp change
RichardZhangRZ 0e38d3b
Formatting
RichardZhangRZ d0ef3e7
Revert "Temp change"
RichardZhangRZ 410361b
Skip Milvus installation for testing:
RichardZhangRZ a71dd5d
Resolved merge conflicts
RichardZhangRZ 790dc02
adopted to configuration management changes
RichardZhangRZ b7b5cb6
Add skip marker
RichardZhangRZ 3e77b2e
Add temp change
RichardZhangRZ d67371a
Revert "Add temp change"
RichardZhangRZ e578ed0
formatting
RichardZhangRZ c5ea4e4
Merge branch 'staging' into milvus-integration
xzdandy c0206b1
Remove milvus from circle ci installation
xzdandy 15b4791
Update the documentation to use the new `SET` statement
xzdandy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import atexit | ||
from typing import List | ||
|
||
from evadb.third_party.vector_stores.types import ( | ||
FeaturePayload, | ||
VectorIndexQuery, | ||
VectorIndexQueryResult, | ||
VectorStore, | ||
) | ||
from evadb.utils.generic_utils import try_to_import_milvus_client | ||
|
||
_milvus_client_instance = None | ||
_milvus_server_instance = None | ||
|
||
required_params = ["index_dir"] | ||
|
||
|
||
def get_local_milvus_server(index_dir: str): | ||
global _milvus_server_instance | ||
if _milvus_server_instance is None: | ||
try_to_import_milvus_client() | ||
import milvus | ||
|
||
_milvus_server_instance = milvus.default_server | ||
_milvus_server_instance.set_base_dir(index_dir) | ||
_milvus_server_instance.start() | ||
|
||
# Ensure that local Milvus server is terminated before Python process terminates | ||
atexit.register(_milvus_server_instance.stop) | ||
return _milvus_server_instance | ||
|
||
|
||
def get_milvus_client(server_address: str, server_port: int): | ||
global _milvus_client_instance | ||
if _milvus_client_instance is None: | ||
try_to_import_milvus_client() | ||
import pymilvus | ||
|
||
server_uri = f"http://{server_address}:{server_port}" | ||
_milvus_client_instance = pymilvus.MilvusClient(uri=server_uri) | ||
|
||
return _milvus_client_instance | ||
|
||
|
||
class MilvusVectorStore(VectorStore): | ||
def __init__(self, index_name: str, index_dir: str) -> None: | ||
local_milvus_server = get_local_milvus_server(index_dir) | ||
self._client = get_milvus_client( | ||
server_address=local_milvus_server.server_address, | ||
server_port=local_milvus_server.listen_port, | ||
) | ||
self._collection_name = index_name | ||
|
||
def create(self, vector_dim: int): | ||
if self._collection_name in self._client.list_collections(): | ||
self._client.drop_collection(self._collection_name) | ||
self._client.create_collection( | ||
collection_name=self._collection_name, dimension=vector_dim | ||
) | ||
|
||
def add(self, payload: List[FeaturePayload]): | ||
milvus_data = [ | ||
{ | ||
"id": feature_payload.id, | ||
"vector": feature_payload.embedding.reshape(-1).tolist(), | ||
} | ||
for feature_payload in payload | ||
] | ||
ids = [feature_payload.id for feature_payload in payload] | ||
|
||
# Milvus Client does not have upsert operation, perform delete + insert to emulate it | ||
self._client.delete(collection_name=self._collection_name, pks=ids) | ||
|
||
self._client.insert(collection_name=self._collection_name, data=milvus_data) | ||
xzdandy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def persist(self): | ||
self._client.flush(self._collection_name) | ||
xzdandy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def delete(self) -> None: | ||
self._client.drop_collection( | ||
collection_name=self._collection_name, | ||
) | ||
|
||
def query(self, query: VectorIndexQuery) -> VectorIndexQueryResult: | ||
response = self._client.search( | ||
collection_name=self._collection_name, | ||
data=[query.embedding.reshape(-1).tolist()], | ||
limit=query.top_k, | ||
)[0] | ||
|
||
distances, ids = [], [] | ||
for result in response: | ||
print(result) | ||
distances.append(result["distance"]) | ||
ids.append(result["id"]) | ||
|
||
return VectorIndexQueryResult(distances, ids) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
milvus
since we are not running any test.