Skip to content

Commit bb9a5b6

Browse files
Support milvus local
Signed-off-by: junjie.jiang <[email protected]>
1 parent 59bf5e8 commit bb9a5b6

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

pymilvus/client/types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ def groups(self):
763763

764764
class ResourceGroupInfo:
765765
def __init__(self, resource_group: Any) -> None:
766-
767766
self._name = resource_group.name
768767
self._capacity = resource_group.capacity
769768
self._num_available_node = resource_group.num_available_node

pymilvus/orm/connections.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import threading
1616
import time
1717
from typing import Callable, Tuple, Union
18+
import pathlib
1819
from urllib import parse
1920

2021
from pymilvus.client.check import is_legal_address, is_legal_host, is_legal_port
@@ -357,6 +358,30 @@ def connect(
357358
>>> connections.connect("test", host="localhost", port="19530")
358359
"""
359360

361+
if kwargs.get("uri") and parse.urlparse(kwargs["uri"]).scheme.lower() not in [
362+
"unix",
363+
"http",
364+
"https",
365+
"tcp",
366+
]:
367+
# start and connect milvuslite
368+
if kwargs["uri"].endswith("/"):
369+
raise ConnectionConfigException(
370+
message=f"Open local milvus failed, {kwargs['uri']} is not a local file path"
371+
)
372+
parent_path = pathlib.Path(kwargs["uri"]).parent
373+
if not parent_path.is_dir():
374+
raise ConnectionConfigException(
375+
message=f"Open local milvus failed, dir: {str(parent_path)} not exists"
376+
)
377+
378+
from milvus_lite.server_manager import server_manager_instance
379+
380+
local_uri = server_manager_instance.start_and_get_uri(kwargs["uri"])
381+
if local_uri is None:
382+
raise ConnectionConfigException(message="Open local milvus failed")
383+
kwargs["uri"] = local_uri
384+
360385
# kwargs_copy is used for auto reconnect
361386
kwargs_copy = copy.deepcopy(kwargs)
362387
kwargs_copy["user"] = user

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies=[
2929
"pyarrow>=12.0.0",
3030
"azure-storage-blob",
3131
"scipy",
32+
"milvus-lite>=2.4.0",
3233
]
3334

3435
classifiers=[

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ black
3838
requests
3939
minio
4040
azure-storage-blob
41+
milvus-lite>=2.4.0

tests/test_milvus_lite.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
from tempfile import TemporaryDirectory
3+
import numpy as np
4+
5+
from pymilvus.milvus_client import MilvusClient
6+
7+
8+
class TestMilvusLite:
9+
10+
def test_milvus_lite(self):
11+
with TemporaryDirectory(dir='./') as root:
12+
db_file = os.path.join(root, 'test.db')
13+
client = MilvusClient(db_file)
14+
client.create_collection(
15+
collection_name="demo_collection",
16+
dimension=3
17+
)
18+
19+
# Text strings to search from.
20+
docs = [
21+
"Artificial intelligence was founded as an academic discipline in 1956.",
22+
"Alan Turing was the first person to conduct substantial research in AI.",
23+
"Born in Maida Vale, London, Turing was raised in southern England.",
24+
]
25+
26+
vectors = [[np.random.uniform(-1, 1) for _ in range(3) ] for _ in range(len(docs))]
27+
data = [{"id": i, "vector": vectors[i], "text": docs[i], "subject": "history"} for i in range(len(vectors))]
28+
res = client.insert(
29+
collection_name="demo_collection",
30+
data=data
31+
)
32+
assert res["insert_count"] == 3
33+
34+
res = client.search(
35+
collection_name="demo_collection",
36+
data=[vectors[0]],
37+
filter="subject == 'history'",
38+
limit=2,
39+
output_fields=["text", "subject"],
40+
)
41+
assert len(res[0]) == 2
42+
43+
# a query that retrieves all entities matching filter expressions.
44+
res = client.query(
45+
collection_name="demo_collection",
46+
filter="subject == 'history'",
47+
output_fields=["text", "subject"],
48+
)
49+
assert len(res) == 3
50+
51+
# delete
52+
res = client.delete(
53+
collection_name="demo_collection",
54+
filter="subject == 'history'",
55+
)
56+
assert len(res) == 3

0 commit comments

Comments
 (0)