Skip to content

Commit

Permalink
Support milvuslite
Browse files Browse the repository at this point in the history
Signed-off-by: junjie.jiang <[email protected]>
  • Loading branch information
junjiejiangjjj committed May 7, 2024
1 parent b771a87 commit cd4dc53
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pymilvus/orm/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import threading
import time
from typing import Callable, Tuple, Union
import pathlib
from urllib import parse

from pymilvus.client.check import is_legal_address, is_legal_host, is_legal_port
Expand Down Expand Up @@ -357,6 +358,20 @@ def connect(
>>> connections.connect("test", host="localhost", port="19530")
"""

if kwargs.get("uri") and parse.urlparse(kwargs["uri"]).scheme.lower() not in ["unix", "http", "https", "tcp"]:
# start and connect milvuslite
if kwargs["uri"].endswith("/"):
raise ConnectionConfigException(message=f"Open local milvus failed, {kwargs['uri']} is not a local file path")
parent_path = pathlib.Path(kwargs["uri"]).parent
if not parent_path.is_dir():
raise ConnectionConfigException(message=f"Open local milvus failed, dir: {str(parent_path)} not exists")

from milvus.server_manager import server_manager_instance
local_uri = server_manager_instance.start_and_get_uri(kwargs["uri"])
if local_uri is None:
raise ConnectionConfigException(message="Open local milvus failed")
kwargs["uri"] = local_uri

# kwargs_copy is used for auto reconnect
kwargs_copy = copy.deepcopy(kwargs)
kwargs_copy["user"] = user
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies=[
"pyarrow>=12.0.0",
"azure-storage-blob",
"scipy",
"milvus>=2.4.0,<2.5.0",
]

classifiers=[
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ black
requests
minio
azure-storage-blob
milvus>=2.4.0,<2.5.0
56 changes: 56 additions & 0 deletions tests/test_milvus_lite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
from tempfile import TemporaryDirectory
import numpy as np

from pymilvus.milvus_client import MilvusClient


class TestMilvusLite:

def test_milvus_lite(self):
with TemporaryDirectory(dir='./') as root:
db_file = os.path.join(root, 'test.db')
client = MilvusClient(db_file)
client.create_collection(
collection_name="demo_collection",
dimension=3
)

# Text strings to search from.
docs = [
"Artificial intelligence was founded as an academic discipline in 1956.",
"Alan Turing was the first person to conduct substantial research in AI.",
"Born in Maida Vale, London, Turing was raised in southern England.",
]

vectors = [[np.random.uniform(-1, 1) for _ in range(3) ] for _ in range(len(docs))]
data = [{"id": i, "vector": vectors[i], "text": docs[i], "subject": "history"} for i in range(len(vectors))]
res = client.insert(
collection_name="demo_collection",
data=data
)
assert res["insert_count"] == 3

res = client.search(
collection_name="demo_collection",
data=[vectors[0]],
filter="subject == 'history'",
limit=2,
output_fields=["text", "subject"],
)
assert len(res[0]) == 2

# a query that retrieves all entities matching filter expressions.
res = client.query(
collection_name="demo_collection",
filter="subject == 'history'",
output_fields=["text", "subject"],
)
assert len(res) == 3

# delete
res = client.delete(
collection_name="demo_collection",
filter="subject == 'history'",
)
assert len(res) == 3

0 comments on commit cd4dc53

Please sign in to comment.