-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi cherry picks from master branch (#2349)
- Added grpc as a valid protocol for uri (#2090) - build(deps): bump urllib3 from 1.26.18 to 1.26.19 (#2140) - build(deps): bump certifi from 2023.7.22 to 2024.7.4 (#2170) - feat(pymilvus/settings.py): Load configuration without altering the environment (#2192) - feat: Add compact, get_server_version and flush api (#2326) - Fix typo and correct grammar (#2333) - Update return type of describe_role to Dict (#2337) - enhance: Reorganize the examples (#2340) Related: #2166, #2325, #2332 Signed-off-by: yangxuan <[email protected]> Co-authored-by: Bruno Faria <[email protected]> Co-authored-by: Bruno Faria <[email protected]> Co-authored-by: dependabot[bot] <[email protected]> Co-authored-by: -LAN- <[email protected]> Co-authored-by: zhenshan.cao <[email protected]> Co-authored-by: NamCaoHai <[email protected]>
- Loading branch information
1 parent
31cde42
commit 29bef71
Showing
69 changed files
with
608 additions
and
908 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Examples |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,83 @@ | ||
import time | ||
import numpy as np | ||
from pymilvus import ( | ||
MilvusClient, | ||
) | ||
|
||
fmt = "\n=== {:30} ===\n" | ||
dim = 8 | ||
collection_name = "hello_milvus" | ||
milvus_client = MilvusClient("http://localhost:19530") | ||
|
||
has_collection = milvus_client.has_collection(collection_name, timeout=5) | ||
if has_collection: | ||
milvus_client.drop_collection(collection_name) | ||
milvus_client.create_collection(collection_name, dim, consistency_level="Strong", metric_type="L2") | ||
|
||
rng = np.random.default_rng(seed=19530) | ||
rows = [ | ||
{"id": 1, "vector": rng.random((1, dim))[0], "a": 100}, | ||
{"id": 2, "vector": rng.random((1, dim))[0], "b": 200}, | ||
{"id": 3, "vector": rng.random((1, dim))[0], "c": 300}, | ||
{"id": 4, "vector": rng.random((1, dim))[0], "d": 400}, | ||
{"id": 5, "vector": rng.random((1, dim))[0], "e": 500}, | ||
{"id": 6, "vector": rng.random((1, dim))[0], "f": 600}, | ||
] | ||
|
||
print(fmt.format("Start inserting entities")) | ||
insert_result = milvus_client.insert(collection_name, rows) | ||
print(fmt.format("Inserting entities done")) | ||
print(insert_result) | ||
|
||
upsert_ret = milvus_client.upsert(collection_name, {"id": 2 , "vector": rng.random((1, dim))[0], "g": 100}) | ||
print(upsert_ret) | ||
|
||
print(fmt.format("Start flush")) | ||
milvus_client.flush(collection_name) | ||
print(fmt.format("flush done")) | ||
|
||
result = milvus_client.query(collection_name, "", output_fields = ["count(*)"]) | ||
print(f"final entities in {collection_name} is {result[0]['count(*)']}") | ||
|
||
rows = [ | ||
{"id": 7, "vector": rng.random((1, dim))[0], "g": 700}, | ||
{"id": 8, "vector": rng.random((1, dim))[0], "h": 800}, | ||
{"id": 9, "vector": rng.random((1, dim))[0], "i": 900}, | ||
{"id": 10, "vector": rng.random((1, dim))[0], "j": 1000}, | ||
{"id": 11, "vector": rng.random((1, dim))[0], "k": 1100}, | ||
{"id": 12, "vector": rng.random((1, dim))[0], "l": 1200}, | ||
] | ||
|
||
print(fmt.format("Start inserting entities")) | ||
insert_result = milvus_client.insert(collection_name, rows) | ||
print(fmt.format("Inserting entities done")) | ||
print(insert_result) | ||
|
||
print(fmt.format("Start flush")) | ||
milvus_client.flush(collection_name) | ||
print(fmt.format("flush done")) | ||
|
||
result = milvus_client.query(collection_name, "", output_fields = ["count(*)"]) | ||
print(f"final entities in {collection_name} is {result[0]['count(*)']}") | ||
|
||
print(fmt.format("Start compact")) | ||
job_id = milvus_client.compact(collection_name) | ||
print(f"job_id:{job_id}") | ||
|
||
cnt = 0 | ||
state = milvus_client.get_compaction_state(job_id) | ||
while (state != "Completed" and cnt < 10): | ||
time.sleep(1.0) | ||
state = milvus_client.get_compaction_state(job_id) | ||
print(f"compaction state: {state}") | ||
cnt += 1 | ||
|
||
if state == "Completed": | ||
print(fmt.format("compact done")) | ||
else: | ||
print(fmt.format("compact timeout")) | ||
|
||
result = milvus_client.query(collection_name, "", output_fields = ["count(*)"]) | ||
print(f"final entities in {collection_name} is {result[0]['count(*)']}") | ||
|
||
milvus_client.drop_collection(collection_name) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,57 @@ | ||
import time | ||
import numpy as np | ||
from pymilvus import ( | ||
MilvusClient, | ||
) | ||
|
||
fmt = "\n=== {:30} ===\n" | ||
dim = 8 | ||
collection_name = "hello_milvus" | ||
milvus_client = MilvusClient("http://localhost:19530") | ||
|
||
has_collection = milvus_client.has_collection(collection_name, timeout=5) | ||
if has_collection: | ||
milvus_client.drop_collection(collection_name) | ||
milvus_client.create_collection(collection_name, dim, consistency_level="Strong", metric_type="L2") | ||
|
||
rng = np.random.default_rng(seed=19530) | ||
rows = [ | ||
{"id": 1, "vector": rng.random((1, dim))[0], "a": 100}, | ||
{"id": 2, "vector": rng.random((1, dim))[0], "b": 200}, | ||
{"id": 3, "vector": rng.random((1, dim))[0], "c": 300}, | ||
{"id": 4, "vector": rng.random((1, dim))[0], "d": 400}, | ||
{"id": 5, "vector": rng.random((1, dim))[0], "e": 500}, | ||
{"id": 6, "vector": rng.random((1, dim))[0], "f": 600}, | ||
] | ||
|
||
print(fmt.format("Start inserting entities")) | ||
insert_result = milvus_client.insert(collection_name, rows) | ||
print(fmt.format("Inserting entities done")) | ||
print(insert_result) | ||
|
||
upsert_ret = milvus_client.upsert(collection_name, {"id": 2 , "vector": rng.random((1, dim))[0], "g": 100}) | ||
print(upsert_ret) | ||
|
||
print(fmt.format("Start flush")) | ||
milvus_client.flush(collection_name) | ||
print(fmt.format("flush done")) | ||
|
||
|
||
result = milvus_client.query(collection_name, "", output_fields = ["count(*)"]) | ||
print(f"final entities in {collection_name} is {result[0]['count(*)']}") | ||
|
||
|
||
print(f"start to delete by specifying filter in collection {collection_name}") | ||
delete_result = milvus_client.delete(collection_name, ids=[6]) | ||
print(delete_result) | ||
|
||
|
||
print(fmt.format("Start flush")) | ||
milvus_client.flush(collection_name) | ||
print(fmt.format("flush done")) | ||
|
||
|
||
result = milvus_client.query(collection_name, "", output_fields = ["count(*)"]) | ||
print(f"final entities in {collection_name} is {result[0]['count(*)']}") | ||
|
||
milvus_client.drop_collection(collection_name) |
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,8 @@ | ||
from pymilvus import ( | ||
MilvusClient, | ||
) | ||
|
||
milvus_client = MilvusClient("http://localhost:19530") | ||
|
||
version = milvus_client.get_server_version() | ||
print(f"server version: {version}") |
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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,93 @@ | ||
import numpy as np | ||
from pymilvus import ( | ||
connections, | ||
utility, | ||
FieldSchema, CollectionSchema, DataType, | ||
Collection, | ||
AnnSearchRequest, RRFRanker, WeightedRanker, | ||
) | ||
|
||
fmt = "\n=== {:30} ===\n" | ||
search_latency_fmt = "search latency = {:.4f}s" | ||
num_entities, dim = 3000, 8 | ||
|
||
print(fmt.format("start connecting to Milvus")) | ||
connections.connect("default", host="localhost", port="19530") | ||
|
||
has = utility.has_collection("hello_milvus") | ||
print(f"Does collection hello_milvus exist in Milvus: {has}") | ||
if has: | ||
utility.drop_collection("hello_milvus") | ||
|
||
fields = [ | ||
FieldSchema(name="pk", dtype=DataType.VARCHAR, is_primary=True, auto_id=False, max_length=100), | ||
FieldSchema(name="random", dtype=DataType.DOUBLE), | ||
FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=dim), | ||
FieldSchema(name="embeddings2", dtype=DataType.FLOAT_VECTOR, dim=dim) | ||
] | ||
|
||
schema = CollectionSchema(fields, "hello_milvus is the simplest demo to introduce the APIs") | ||
|
||
print(fmt.format("Create collection `hello_milvus`")) | ||
hello_milvus = Collection("hello_milvus", schema, consistency_level="Strong", num_shards = 4) | ||
|
||
print(fmt.format("Start inserting entities")) | ||
rng = np.random.default_rng(seed=19530) | ||
entities = [ | ||
# provide the pk field because `auto_id` is set to False | ||
[str(i) for i in range(num_entities)], | ||
rng.random(num_entities).tolist(), # field random, only supports list | ||
rng.random((num_entities, dim)), # field embeddings, supports numpy.ndarray and list | ||
rng.random((num_entities, dim)), # field embeddings2, supports numpy.ndarray and list | ||
] | ||
|
||
insert_result = hello_milvus.insert(entities) | ||
|
||
hello_milvus.flush() | ||
print(f"Number of entities in Milvus: {hello_milvus.num_entities}") # check the num_entities | ||
|
||
print(fmt.format("Start Creating index IVF_FLAT")) | ||
index = { | ||
"index_type": "IVF_FLAT", | ||
"metric_type": "L2", | ||
"params": {"nlist": 128}, | ||
} | ||
|
||
hello_milvus.create_index("embeddings", index) | ||
hello_milvus.create_index("embeddings2", index) | ||
|
||
print(fmt.format("Start loading")) | ||
hello_milvus.load() | ||
|
||
field_names = ["embeddings", "embeddings2"] | ||
|
||
req_list = [] | ||
nq = 1 | ||
weights = [0.2, 0.3] | ||
default_limit = 5 | ||
vectors_to_search = [] | ||
|
||
for i in range(len(field_names)): | ||
# 4. generate search data | ||
vectors_to_search = rng.random((nq, dim)) | ||
search_param = { | ||
"data": vectors_to_search, | ||
"anns_field": field_names[i], | ||
"param": {"metric_type": "L2"}, | ||
"limit": default_limit, | ||
"expr": "random > 0.5"} | ||
req = AnnSearchRequest(**search_param) | ||
req_list.append(req) | ||
|
||
hybrid_res = hello_milvus.hybrid_search(req_list, WeightedRanker(*weights), default_limit, output_fields=["random"]) | ||
|
||
print("rank by WightedRanker") | ||
for hits in hybrid_res: | ||
for hit in hits: | ||
print(f" hybrid search hit: {hit}") | ||
|
||
print("rank by RRFRanker") | ||
hybrid_res = hello_milvus.hybrid_search(req_list, RRFRanker(), default_limit, output_fields=["random"]) | ||
for hits in hybrid_res: | ||
for hit in hits: | ||
print(f" hybrid search hit: {hit}") |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.