File size: 1,204 Bytes
93be2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
from __future__ import annotations

import json
import os
import sys
from typing import Any, Dict

import requests


QDRANT_URL = os.environ.get("QDRANT_URL", "http://localhost:17000")
COLLECTION = os.environ.get("QDRANT_COLLECTION", "elizabeth_embeddings")
VECTOR_SIZE = int(os.environ.get("QDRANT_VECTOR_SIZE", "1536"))
DISTANCE = os.environ.get("QDRANT_DISTANCE", "Cosine")


def ensure_collection() -> Dict[str, Any]:
    r = requests.get(f"{QDRANT_URL}/collections/{COLLECTION}", timeout=5)
    if r.status_code == 200:
        return {"status": "exists"}
    payload = {
        "vectors": {"size": VECTOR_SIZE, "distance": DISTANCE},
        "hnsw_config": {"m": 64, "ef_construct": 128, "full_scan_threshold": 10000},
        "optimizers_config": {"default_segment_number": 2},
    }
    r = requests.put(f"{QDRANT_URL}/collections/{COLLECTION}", json=payload, timeout=30)
    return {"status": "created", "code": r.status_code, "body": r.text}


def main() -> None:
    try:
        res = ensure_collection()
        print(json.dumps(res))
    except Exception as e:
        print(json.dumps({"error": str(e)}))
        sys.exit(1)


if __name__ == "__main__":
    main()