File size: 2,375 Bytes
2d51ea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import qdrant_client
from qdrant_client.http import models
from qdrant_client.http.models import Distance, VectorParams
import os
from dotenv import load_dotenv

load_dotenv()

class QdrantSetup:
    def __init__(self, host=None, port=None, api_key=None, https=True):
        """
        Initialize Qdrant client - supports both local and cloud instances
        """
        # Check if using cloud instance
        cloud_url = os.getenv("QDRANT_URL")  # For cloud instances
        cloud_api_key = os.getenv("QDRANT_API_KEY")  # For cloud instances

        if cloud_url:
            # Use cloud instance
            self.client = qdrant_client.QdrantClient(
                url=cloud_url,
                api_key=cloud_api_key,
                https=https
            )
        else:
            # Use local instance
            host = host or os.getenv("QDRANT_HOST", "localhost")
            port = port or int(os.getenv("QDRANT_PORT", 6333))
            self.client = qdrant_client.QdrantClient(
                host=host,
                port=port
            )

        self.collection_name = "hindi_poems_stories"
        
    def create_collection(self, vector_size=384):
        """
        Create a collection in Qdrant for storing Hindi text embeddings
        """
        # Check if collection already exists
        collections = self.client.get_collections()
        collection_names = [col.name for col in collections.collections]

        if self.collection_name in collection_names:
            print(f"Collection '{self.collection_name}' already exists.")
            return

        # Create collection with specified vector size
        self.client.create_collection(
            collection_name=self.collection_name,
            vectors_config=VectorParams(size=vector_size, distance=Distance.COSINE),
        )
        
        print(f"Collection '{self.collection_name}' created successfully.")
    
    def get_client(self):
        """
        Return the Qdrant client instance
        """
        return self.client
    
    def get_collection_name(self):
        """
        Return the collection name
        """
        return self.collection_name

if __name__ == "__main__":
    # Initialize Qdrant setup
    qdrant_setup = QdrantSetup()
    
    # Create collection
    qdrant_setup.create_collection()
    
    print("Qdrant setup completed!")