File size: 1,267 Bytes
74cf6bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from qdrant_client import QdrantClient
import logging


def get_qdrant_client() -> QdrantClient:
    """
    Initialize a Qdrant client using environment variables or default to localhost.

    Environment variables:
    - QDRANT_URL: URL for Qdrant server (default: http://localhost:6333)
    - QDRANT_API_KEY: Optional API key for authentication

    Returns:
        QdrantClient: Configured Qdrant client
    """
    # Get configuration from environment variables with defaults
    url = os.getenv("QDRANT_URL", "http://localhost:6333")
    api_key = os.getenv("QDRANT_API_KEY")

    # Configure client with or without API key
    if api_key:
        client = QdrantClient(location=url, api_key=api_key)
        logging.info(f"Connecting to Qdrant at {url} with API key")
    else:
        client = QdrantClient(location=url)
        logging.info(f"Connecting to Qdrant at {url}")

    # Test connection
    try:
        client.get_collections()
        logging.info(f"Successfully connected to Qdrant at {url}")
    except Exception as e:
        logging.error(f"Failed to connect to Qdrant at {url}: {e}")
        # Connection will be tested again when used

    return client


# Initialize global client instance
qdrant_client = get_qdrant_client()