이 장에서는 Python을 사용하여 Milvus 개발을 빠르게 시작하는 방법을 소개합니다.
제공하는 샘플 코드를 실행함으로써 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는 API를 소개하는 가장 간단한 데모 예제입니다.")
hello_milvus = Collection("hello_milvus", schema)
- 컬렉션에 벡터 삽입:
import random
entities = [
[i for i in range(3000)], # 필드 pk
[float(random.randrange(-20, -10)) for _ in range(3000)], # 필드 random
[[random.random() for _ in range(8)] for _ in range(3000)], # 필드 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")
샘플 코드 실행하기
다음 명령을 실행하여 샘플 코드를 실행합니다.
$ python3 hello_milvus.py
아래는 반환된 결과와 쿼리 지연 시간을 보여줍니다:
=== Milvus에 연결 ===
Milvus에 이름이 hello_milvus인 컬렉션이 있습니까?: False
=== `hello_milvus` 컬렉션 생성 ===
=== 엔티티 삽입 ===
Milvus에 있는 엔티티 수: 3000
=== IVF_FLAT 인덱스 생성 ===
=== 로딩 ===
=== 벡터 유사도를 기반으로 검색 ===
일치: (거리: 0.0, ID: 2998), 랜덤 필드: -11.0
일치: (거리: 0.11455299705266953, ID: 1581), 랜덤 필드: -18.0
일치: (거리: 0.1232629269361496, ID: 2647), 랜덤 필드: -13.0
일치: (거리: 0.0, ID: 2999), 랜덤 필드: -11.0
일치: (거리: 0.10560893267393112, ID: 2430), 랜덤 필드: -18.0
일치: (거리: 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`를 사용한 혼합 검색 수행 ===
일치: (거리: 0.0, ID: 2998), 랜덤 필드: -11.0
일치: (거리: 0.15773043036460876, ID: 472), 랜덤 필드: -11.0
일치: (거리: 0.3273330628871918, ID: 2146), 랜덤 필드: -11.0
일치: (거리: 0.0, ID: 2999), 랜덤 필드: -11.0
일치: (거리: 0.15844076871871948, ID: 2218), 랜덤 필드: -11.0
일치: (거리: 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` 컬렉션 삭제 ===