-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: Reorganize the examples (#2340)
Signed-off-by: yangxuan <[email protected]>
- Loading branch information
1 parent
e4505ef
commit 3110139
Showing
69 changed files
with
349 additions
and
880 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.
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.
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
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,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.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.