บทนี้จะแนะนำวิธีเริ่มต้นการพัฒนา Milvus โดยใช้ Python อย่างรวดเร็ว
โดยการรันโค้ดตัวอย่างที่เราให้, คุณจะได้เข้าใจเบื้องต้นเกี่ยวกับฟังก์ชันของ Milvus
ความต้องการของเวอร์ชัน
- Milvus 2.3.0
- Python 3 (3.7.1 หรือสูงกว่า)
- PyMilvus 2.3.x
การติดตั้ง Milvus Python SDK
python3 -m pip install pymilvus==
ดาวน์โหลดโค้ดตัวอย่าง
ใช้คำสั่งต่อไปนี้เพื่อดาวน์โหลด hello_milvus.py โดยตรง หรือใช้คำสั่งต่อไปนี้เพื่อดาวน์โหลด
$ wget https://www.tizi365.com/storage/hello_milvus.py
อธิบายโค้ดตัวอย่าง
โค้ดตัวอย่างดำเนินการตามขั้นตอนต่อไปนี้
- นำเข้าแพ็คเกจ PyMilvus:
from pymilvus import (
connections,
utility,
FieldSchema,
CollectionSchema,
DataType,
Collection,
)
- เชื่อมต่อกับเซิร์ฟเวอร์:
connections.connect("default", host="localhost", port="19530")
- สร้างคอลเลกชัน:
fields = [
FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=False),
FieldSchema(name="random", dtype=DataType.DOUBLE),
FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=8)
]
schema = CollectionSchema(fields, "hello_milvus is the simplest demo example, used to introduce the API")
hello_milvus = Collection("hello_milvus", schema)
- แทรกเวกเตอร์เข้าสู่คอลเลกชัน:
import random
entities = [
[i for i in range(3000)], # field pk
[float(random.randrange(-20, -10)) for _ in range(3000)], # field random
[[random.random() for _ in range(8)] for _ in range(3000)], # field embeddings
]
insert_result = hello_milvus.insert(entities)
hello_milvus.flush()
- สร้างดัชนีบนเอนทิตี้:
index = {
"index_type": "IVF_FLAT",
"metric_type": "L2",
"params": {"nlist": 128},
}
hello_milvus.create_index("embeddings", index)
- โหลดคอลเลกชันเข้าสู่หน่วยความจำและดำเนินการค้นหาความคล้ายคลึงเวกเตอร์:
hello_milvus.load()
vectors_to_search = entities[-1][-2:]
search_params = {
"metric_type": "L2",
"params": {"nprobe": 10},
}
result = hello_milvus.search(vectors_to_search, "embeddings", search_params, limit=3, output_fields=["random"])
- ดำเนินการคิวรี่เวกเตอร์:
result = hello_milvus.query(expr="random > -14", output_fields=["random", "embeddings"])
- ดำเนินการค้นหาผสม:
result = hello_milvus.search(vectors_to_search, "embeddings", search_params, limit=3, expr="random > -12", output_fields=["random"])
- ลบเอนทิตี้โดยใช้รหัสหลัก:
expr = f"pk in [{entities[0][0]}, {entities[0][1]}]"
hello_milvus.delete(expr)
- ลบคอลเลกชัน:
utility.drop_collection("hello_milvus")
การรันโค้ดตัวอย่าง
กรุณา execute คำสั่งด้านล่างเพื่อรันโค้ดตัวอย่าง
$ python3 hello_milvus.py
ด้านล่างนี้คือผลลัพธ์ที่ได้และเวลาที่ใช้ในการค้นหา:
=== เชื่อมต่อกับ Milvus ===
มีคอลเลกชันชื่อ hello_milvus อยู่ใน Milvus หรือไม่: ไม่มี
=== สร้างคอลเลกชัน `hello_milvus` ===
=== การเพิ่ม entities ===
จำนวน entities ใน Milvus: 3000
=== สร้างดัชนี IVF_FLAT ===
=== โหลด ===
=== ค้นหาโดยใช้ความคล้ายความเป็นเวกเตอร์ ===
Match: (ระยะ: 0.0, ID: 2998), ฟิลด์สุ่ม: -11.0
Match: (ระยะ: 0.11455299705266953, ID: 1581), ฟิลด์สุ่ม: -18.0
Match: (ระยะ: 0.1232629269361496, ID: 2647), ฟิลด์สุ่ม: -13.0
Match: (ระยะ: 0.0, ID: 2999), ฟิลด์สุ่ม: -11.0
Match: (ระยะ: 0.10560893267393112, ID: 2430), ฟิลด์สุ่ม: -18.0
Match: (ระยะ: 0.13938161730766296, ID: 377), ฟิลด์สุ่ม: -14.0
เวลาค้นหา = 0.2796 วินาที
=== การค้นหาโดยใช้ `random > -14` ===
ผลลัพธ์ของคิวรี่:
-{'pk': 9, 'random': -13.0, 'embeddings': [0.298433, 0.931987, 0.949756, 0.598713, 0.290125, 0.094323, 0.064444, 0.306993]}
เวลาค้นหา = 0.2970 วินาที
=== การค้นหาผสมโดยใช้ `random > -12` ===
Match: (ระยะ: 0.0, ID: 2998), ฟิลด์สุ่ม: -11.0
Match: (ระยะ: 0.15773043036460876, ID: 472), ฟิลด์สุ่ม: -11.0
Match: (ระยะ: 0.3273330628871918, ID: 2146), ฟิลด์สุ่ม: -11.0
Match: (ระยะ: 0.0, ID: 2999), ฟิลด์สุ่ม: -11.0
Match: (ระยะ: 0.15844076871871948, ID: 2218), ฟิลด์สุ่ม: -11.0
Match: (ระยะ: 0.1622171700000763, ID: 1403), ฟิลด์สุ่ม: -11.0
เวลาค้นหา = 0.3028 วินาที
=== การลบโดยใช้สมการ `pk in [0, 1]` ===
การคิวรี่ก่อนการลบที่ใช้สมการ `pk in [0, 1]` -> ผลลัพธ์:
-{'pk': 0, 'random': -18.0, 'embeddings': [0.142279, 0.414248, 0.378628, 0.971863, 0.535941, 0.107011, 0.207052, 0.98182]}
-{'pk': 1, 'random': -15.0, 'embeddings': [0.57512, 0.358512, 0.439131, 0.862369, 0.083284, 0.294493, 0.004961, 0.180082]}
การค้นหาหลังการลบที่ใช้สมการ `pk in [0, 1]` -> ผลลัพธ์: []
=== การลบคอลเลกชัน hello_milvus
===