File size: 7,055 Bytes
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9513cca
 
 
 
 
 
c4ef1cf
9513cca
 
 
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9513cca
 
 
 
 
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9513cca
 
 
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
9513cca
 
 
 
 
 
 
c4ef1cf
9513cca
 
c4ef1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9513cca
 
 
c4ef1cf
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Dict, Optional
from urllib.parse import urlparse


@dataclass(frozen=True)
class QdrantConnection:
    url: str
    api_key: Optional[str]


def _maybe_load_dotenv() -> None:
    try:
        from dotenv import load_dotenv
    except Exception:
        return
    try:
        from pathlib import Path

        if Path(".env").exists():
            load_dotenv(".env")
    except Exception:
        return


def _resolve_qdrant_connection(
    *,
    url: Optional[str] = None,
    api_key: Optional[str] = None,
) -> QdrantConnection:
    import os

    _maybe_load_dotenv()
    resolved_url = (
        url
        or os.getenv("SIGIR_QDRANT_URL")
        or os.getenv("DEST_QDRANT_URL")
        or os.getenv("QDRANT_URL")
    )
    if not resolved_url:
        raise ValueError(
            "Qdrant URL not set (pass url= or set SIGIR_QDRANT_URL/DEST_QDRANT_URL/QDRANT_URL)."
        )
    resolved_key = (
        api_key
        or os.getenv("SIGIR_QDRANT_KEY")
        or os.getenv("SIGIR_QDRANT_API_KEY")
        or os.getenv("DEST_QDRANT_API_KEY")
        or os.getenv("QDRANT_API_KEY")
    )
    return QdrantConnection(url=str(resolved_url), api_key=resolved_key)


def _infer_grpc_port(url: str) -> Optional[int]:
    try:
        if urlparse(url).port == 6333:
            return 6334
    except Exception:
        return None
    return None


class QdrantAdmin:
    def __init__(
        self,
        *,
        url: Optional[str] = None,
        api_key: Optional[str] = None,
        prefer_grpc: bool = False,
        timeout: int = 60,
    ):
        from qdrant_client import QdrantClient

        conn = _resolve_qdrant_connection(url=url, api_key=api_key)
        grpc_port = _infer_grpc_port(conn.url) if prefer_grpc else None
        self.client = QdrantClient(
            url=conn.url,
            api_key=conn.api_key,
            prefer_grpc=bool(prefer_grpc),
            grpc_port=grpc_port,
            timeout=int(timeout),
            check_compatibility=False,
        )

    def get_collection_info(self, *, collection_name: str) -> Dict[str, Any]:
        info = self.client.get_collection(collection_name)
        try:
            return info.model_dump()
        except Exception:
            try:
                return info.dict()
            except Exception:
                return {"collection": str(collection_name), "raw": str(info)}

    def modify_collection_config(
        self,
        *,
        collection_name: str,
        hnsw_config: Optional[Dict[str, Any]] = None,
        collection_params: Optional[Dict[str, Any]] = None,
        timeout: Optional[int] = None,
    ) -> bool:
        """
        Patch collection-level config via Qdrant update_collection.

        Supported keys:
        - hnsw_config: dict for HnswConfigDiff (e.g. on_disk, m, ef_construct, full_scan_threshold)
        - collection_params: dict for CollectionParamsDiff (e.g. on_disk_payload)
        """
        from qdrant_client.http import models as m

        hnsw_diff = m.HnswConfigDiff(**hnsw_config) if isinstance(hnsw_config, dict) else None
        params_diff = (
            m.CollectionParamsDiff(**collection_params)
            if isinstance(collection_params, dict)
            else None
        )
        if hnsw_diff is None and params_diff is None:
            raise ValueError("No changes provided (pass hnsw_config and/or collection_params).")
        return bool(
            self.client.update_collection(
                collection_name=str(collection_name),
                hnsw_config=hnsw_diff,
                collection_params=params_diff,
                timeout=int(timeout) if timeout is not None else None,
            )
        )

    def modify_collection_vector_config(
        self,
        *,
        collection_name: str,
        vectors: Dict[str, Dict[str, Any]],
        timeout: Optional[int] = None,
    ) -> bool:
        """
        Patch vector params under params.vectors[vector_name] using Qdrant update_collection.

        Supported keys per vector:
        - on_disk: bool
        - hnsw_config: dict with optional keys: m, ef_construct, full_scan_threshold, on_disk
        """
        from qdrant_client.http import models as m

        collection_name = str(collection_name)
        info = self.client.get_collection(collection_name)
        existing = set()
        try:
            existing = set((info.config.params.vectors or {}).keys())
        except Exception:
            existing = set()

        missing = [str(k) for k in (vectors or {}).keys() if existing and str(k) not in existing]
        if missing:
            raise ValueError(
                f"Vectors do not exist in collection '{collection_name}': {missing}. Existing: {sorted(existing)}"
            )

        ok = True
        for name, cfg in (vectors or {}).items():
            if not isinstance(cfg, dict):
                raise ValueError(f"vectors['{name}'] must be a dict, got {type(cfg)}")
            hnsw_cfg = cfg.get("hnsw_config")
            hnsw_diff = m.HnswConfigDiff(**hnsw_cfg) if isinstance(hnsw_cfg, dict) else None
            vectors_diff = {
                str(name): m.VectorParamsDiff(
                    on_disk=cfg.get("on_disk", None),
                    hnsw_config=hnsw_diff,
                )
            }

            ok = (
                bool(
                    self.client.update_collection(
                        collection_name=collection_name,
                        vectors_config=vectors_diff,
                        timeout=int(timeout) if timeout is not None else None,
                    )
                )
                and ok
            )

        return ok

    def ensure_collection_all_on_disk(
        self,
        *,
        collection_name: str,
        timeout: Optional[int] = None,
    ) -> Dict[str, Any]:
        """
        Ensure:
        - All existing named vectors have on_disk=True and hnsw_config.on_disk=True
        - Collection hnsw_config.on_disk=True
        - Collection params.on_disk_payload=True
        Returns the post-update collection info (dict).
        """
        collection_name = str(collection_name)
        info = self.client.get_collection(collection_name)
        vectors = {}
        try:
            existing = list((info.config.params.vectors or {}).keys())
        except Exception:
            existing = []
        for vname in existing:
            vectors[str(vname)] = {"on_disk": True, "hnsw_config": {"on_disk": True}}

        if vectors:
            self.modify_collection_vector_config(
                collection_name=collection_name, vectors=vectors, timeout=timeout
            )

        self.modify_collection_config(
            collection_name=collection_name,
            hnsw_config={"on_disk": True},
            collection_params={"on_disk_payload": True},
            timeout=timeout,
        )

        return self.get_collection_info(collection_name=collection_name)