File size: 7,103 Bytes
4dd206b
 
f231844
4dd206b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f231844
4dd206b
 
 
 
f231844
4dd206b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f231844
4dd206b
 
 
 
 
 
 
 
 
8b0e9e8
f231844
 
8b0e9e8
 
 
 
 
 
4dd206b
 
 
 
 
 
 
 
 
 
 
f231844
 
3951cee
f231844
 
 
 
 
f11763e
f231844
f11763e
f231844
 
 
f11763e
f231844
 
f11763e
f231844
 
 
f11763e
f231844
3951cee
4dd206b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f231844
 
 
 
2f88659
f231844
 
 
 
 
 
 
 
 
 
 
c7467f5
e45d61a
 
4dd206b
 
 
 
f231844
4dd206b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f231844
 
 
 
4dd206b
 
 
 
 
 
f231844
4dd206b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f231844
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
"""
Qdrant vector store abstraction for Aurora backend.
Provides a simple interface for Qdrant operations with proper embedding initialization.
"""

import os
from typing import List, Optional, Dict, Any
from qdrant_client import QdrantClient
from qdrant_client.http import models

# Try preferred import first, fallback to community
try:
    from langchain_qdrant import Qdrant
    QDRANT_AVAILABLE = True
except ImportError:
    try:
        from langchain_community.vectorstores import Qdrant
        QDRANT_AVAILABLE = True
    except ImportError:
        QDRANT_AVAILABLE = False
        Qdrant = None

def init_vector_store(embeddings) -> Optional[Qdrant]:
    """
    Initialize Qdrant vector store with the given embedding function.
    
    Args:
        embeddings: The embedding function (e.g., HuggingFaceEmbeddings)
        
    Returns:
        Qdrant vector store instance or None if initialization fails
    """
    if not QDRANT_AVAILABLE:
        print("❌ Qdrant dependencies not available")
        return None
    
    try:
        # Get configuration from environment
        qdrant_url = os.getenv("QDRANT_URL")
        qdrant_api_key = os.getenv("QDRANT_API_KEY")
        collection_name = os.getenv("QDRANT_COLLECTION", "aurora")
        
        if not qdrant_url:
            print("❌ QDRANT_URL not configured")
            return None
        
        # Initialize Qdrant client
        client_kwargs = {
            "url": qdrant_url,
            "prefer_grpc": False
        }
        
        if qdrant_api_key:
            client_kwargs["api_key"] = qdrant_api_key
        
        client = QdrantClient(**client_kwargs)
        
        # Ensure collection exists with dimension inference
        try:
            # Try to get collection info to check if it exists
            client.get_collection(collection_name)
            print(f"βœ… Using existing Qdrant collection: {collection_name}")
        except Exception:
            # Collection doesn't exist, create it
            print(f"πŸ”„ Creating Qdrant collection: {collection_name}")
            
            # Get embedding dimension
            try:
                test_embedding = embeddings.embed_query("dimension probe")
                embedding_dim = len(test_embedding)
                print(f"πŸ“ Embedding dimension: {embedding_dim}")
            except Exception as e:
                print(f"⚠️  Could not get embedding dimension: {e}")
                # Fallback to common dimension for sentence-transformers
                embedding_dim = 384  # Default for all-MiniLM-L6-v2
                print(f"πŸ“ Using fallback embedding dimension: {embedding_dim}")
            
            # Create collection with cosine distance
            client.create_collection(
                collection_name=collection_name,
                vectors_config=models.VectorParams(
                    size=embedding_dim,
                    distance=models.Distance.COSINE
                )
            )
            print(f"βœ… Created Qdrant collection: {collection_name}")
        
        # Initialize vector store with embeddings
        vector_store = None
        
        # Try different initialization methods
        init_methods = [
            ("embeddings", lambda: Qdrant(client=client, collection_name=collection_name, embeddings=embeddings)),
            ("embedding", lambda: Qdrant(client=client, collection_name=collection_name, embedding=embeddings)),
        ]
        
        for method_name, init_func in init_methods:
            try:
                vector_store = init_func()
                print(f"βœ… Initialized with '{method_name}' parameter")
                break
            except Exception as e:
                print(f"⚠️  {method_name} method failed: {e}")
                continue
        
        if vector_store is None:
            print("❌ All initialization methods failed")
            return None
        
        print(f"Vector store: qdrant β€’ collection={collection_name}")
        
        return vector_store
        
    except Exception as e:
        print(f"❌ Qdrant initialization failed: {e}")
        return None

def vector_count(store: Qdrant) -> int:
    """
    Get the number of vectors in the store.
    
    Args:
        store: Qdrant vector store instance
        
    Returns:
        Number of vectors in the collection
    """
    try:
        if store is None:
            return 0
        
        # Use client.count for robust counting
        try:
            result = store.client.count(store.collection_name, exact=True)
            count = result.count
            return count
        except Exception:
            # Fallback to collection info
            collection_info = store.client.get_collection(store.collection_name)
            if hasattr(collection_info, 'vectors_count'):
                return collection_info.vectors_count
            elif hasattr(collection_info, 'points_count'):
                return collection_info.points_count
            elif hasattr(collection_info, 'status') and hasattr(collection_info.status, 'vectors_count'):
                return collection_info.status.vectors_count
            else:
                return 0
        
    except Exception as e:
        print(f"❌ Could not get vector count: {e}")
        return 0

def add_texts(store: Qdrant, texts: List[str], metadatas: Optional[List[Dict[str, Any]]] = None) -> None:
    """
    Add texts to the vector store.
    
    Args:
        store: Qdrant vector store instance
        texts: List of text documents to add
        metadatas: Optional list of metadata dictionaries
    """
    try:
        if store is None:
            print("⚠️  Vector store not available - skipping add_texts")
            return
        
        if not texts:
            print("⚠️  No texts to add")
            return
        
        # Ensure metadatas is properly formatted
        if metadatas is None:
            metadatas = [{}] * len(texts)
        
        # Add documents in batches
        batch_size = 100  # Reasonable batch size for Qdrant
        total_added = 0
        
        for i in range(0, len(texts), batch_size):
            batch_texts = texts[i:i + batch_size]
            batch_metadatas = metadatas[i:i + batch_size]
            
            store.add_texts(
                texts=batch_texts,
                metadatas=batch_metadatas
            )
            
            total_added += len(batch_texts)
            print(f"πŸ“ Added batch {i//batch_size + 1}: {len(batch_texts)} documents")
        
        print(f"βœ… Added {total_added} documents to vector store")
        
    except Exception as e:
        print(f"❌ Failed to add texts to vector store: {e}")

def is_qdrant_available() -> bool:
    """
    Check if Qdrant is available and properly configured.
    
    Returns:
        True if Qdrant is available and configured, False otherwise
    """
    if not QDRANT_AVAILABLE:
        return False
    
    qdrant_url = os.getenv("QDRANT_URL")
    return qdrant_url is not None