File size: 1,220 Bytes
9d780fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from typing import Any, Dict, List, Optional
from qdrant_client import QdrantClient
from qdrant_client.models import Filter, ScoredPoint # pydantic models
from .models import QdrantCfg
def _to_filter(maybe: Optional[Dict[str, Any]]) -> Optional[Filter]:
if not maybe:
return None
# dict ๊ตฌ์กฐ๊ฐ Qdrant Filter ์คํค๋ง์ ํธํ๋๋ค๋ ๊ฐ์
# (์: {"must": [{"key": "source", "match": {"value": "file.pdf"}}]})
return Filter(**maybe)
def get_client(url: str) -> QdrantClient:
return QdrantClient(url=url)
def ensure_collection(client: QdrantClient, name: str) -> None:
# ์กด์ฌ ํ์ธ (์์ผ๋ฉด ์์ธ)
client.get_collection(name)
def query_points(cfg: QdrantCfg, vector: List[float], limit: int, with_payload: bool) -> List[ScoredPoint]:
client = get_client(cfg.url)
ensure_collection(client, cfg.collection)
qf = _to_filter(cfg.query_filter)
res = client.query_points(
collection_name=cfg.collection,
query=vector,
limit=limit,
query_filter=qf,
with_payload=with_payload
)
# Python client๋ QueryResponse(points=[...]) ํํ๋ฅผ ๋ฐํ
return list(res.points or [])
|