Spaces:
Sleeping
Sleeping
File size: 16,816 Bytes
7dfe46c e547616 7dfe46c e547616 |
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
import logging
import uuid
from typing import Dict, List, Any, Optional, Tuple
from dataclasses import dataclass, asdict
import time
from qdrant_client import QdrantClient
from qdrant_client.http import models
from qdrant_client.http.models import Distance, VectorParams, PointStruct, Filter, FieldCondition, MatchValue
import os
import sys
from dotenv import load_dotenv
load_dotenv()
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.document_processor import DocumentChunk, ChunkMetadata
try:
from logger.custom_logger import CustomLoggerTracker
custom_log = CustomLoggerTracker()
logger = custom_log.get_logger("vector_store")
except ImportError:
# Fallback to standard logging if custom logger not available
logger = logging.getLogger("vector_store")
@dataclass
class SearchResult:
"""Result of vector similarity search."""
chunk: DocumentChunk
similarity_score: float
rerank_score: Optional[float] = None
metadata: Dict[str, Any] = None
def __post_init__(self):
if self.metadata is None:
self.metadata = {}
@dataclass
class IndexStats:
"""Statistics about the vector index."""
total_points: int
collection_name: str
vector_size: int
distance_metric: str
indexed_documents: int
last_updated: str
class QdrantVectorStore:
def __init__(self, config: Dict[str, Any]):
self.config = config
self.url = config.get('qdrant_url', 'http://localhost:6333')
self.api_key = config.get('qdrant_api_key')
self.collection_name = config.get('qdrant_collection', 'manufacturing_docs')
self.vector_size = config.get('vector_size', 1024)
self.distance_metric = Distance.COSINE
# Initialize Qdrant client
logger.info(f"Connecting to Qdrant at URL: {os.environ['QDRANT_URL']}")
self.client = QdrantClient(
url="https://50f53cc8-bbb0-4939-8254-8f025a577222.us-west-2-0.aws.cloud.qdrant.io:6333",
api_key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIn0.gHOXbfqPucRwhczrW8s3VSZbconqQ6Rk49Uaz9ZChdE",)
self._ensure_collection_exists()
logger.info(f"Qdrant vector store initialized: {os.environ['QDRANT_URL']}, collection: {self.collection_name}")
def _ensure_collection_exists(self):
try:
# Check if collection exists
collections = self.client.get_collections()
collection_names = [col.name for col in collections.collections]
if self.collection_name not in collection_names:
logger.info(f"Creating collection: {self.collection_name}")
# Create collection with vector configuration
self.client.create_collection(
collection_name=self.collection_name,
vectors_config=VectorParams(
size=self.vector_size,
distance=self.distance_metric
)
)
# Create payload indexes for efficient filtering
self._create_payload_indexes()
logger.info(f"Collection {self.collection_name} created successfully")
else:
logger.debug(f"Collection {self.collection_name} already exists")
except Exception as e:
logger.error(f"Failed to ensure collection exists: {e}")
raise
def _create_payload_indexes(self):
try:
self.client.create_payload_index(
collection_name=self.collection_name,
field_name="document_id",
field_schema=models.KeywordIndexParams())
# Index on document type for filtering by file type
self.client.create_payload_index(
collection_name=self.collection_name,
field_name="document_type",
field_schema=models.KeywordIndexParams())
# Index on page_number for PDF citations
self.client.create_payload_index(
collection_name=self.collection_name,
field_name="page_number",
field_schema=models.IntegerIndexParams())
# Index on worksheet_name for Excel citations
self.client.create_payload_index(
collection_name=self.collection_name,
field_name="worksheet_name",
field_schema=models.KeywordIndexParams())
logger.debug("Payload indexes created successfully")
except Exception as e:
logger.warning(f"Failed to create payload indexes: {e}")
def add_documents(self, chunks: List[DocumentChunk]) -> bool:
if not chunks:
logger.warning("No chunks provided for indexing")
return True
try:
points = []
for chunk in chunks:
if not chunk.embedding:
logger.warning(f"Chunk {chunk.metadata.chunk_id} has no embedding, skipping")
continue
# Create point for Qdrant
point = PointStruct(
id=str(uuid.uuid4()), # Generate unique ID
vector=chunk.embedding,
payload={
# Chunk metadata
"chunk_id": chunk.metadata.chunk_id,
"document_id": chunk.metadata.document_id,
"chunk_index": chunk.metadata.chunk_index,
"content": chunk.content,
# Citation information
"page_number": chunk.metadata.page_number,
"worksheet_name": chunk.metadata.worksheet_name,
"cell_range": chunk.metadata.cell_range,
"section_title": chunk.metadata.section_title,
# References
"image_references": chunk.metadata.image_references,
"table_references": chunk.metadata.table_references,
# Timestamps and confidence
"extraction_timestamp": chunk.metadata.extraction_timestamp.isoformat(),
"confidence_score": chunk.metadata.confidence_score,
# Additional metadata
"content_length": len(chunk.content),
"indexed_at": time.time()
}
)
points.append(point)
if not points:
logger.warning("No valid points to index")
return True
# Upload points to Qdrant
operation_info = self.client.upsert(
collection_name=self.collection_name,
points=points)
logger.info(f"Successfully indexed {len(points)} chunks to Qdrant")
return True
except Exception as e:
logger.error(f"Failed to add documents to vector store: {e}")
return False
def similarity_search(self, query_embedding: List[float], k: int = 10,
filters: Optional[Dict[str, Any]] = None) -> List[SearchResult]:
try:
# Build filter conditions
filter_conditions = self._build_filter_conditions(filters) if filters else None
# Perform search
search_results = self.client.search(
collection_name=self.collection_name,
query_vector=query_embedding,
limit=k,
query_filter=filter_conditions,
with_payload=True,
with_vectors=False # Don't return vectors to save bandwidth
)
# Convert to SearchResult objects
results = []
for result in search_results:
payload = result.payload
# Reconstruct chunk metadata
metadata = ChunkMetadata(
chunk_id=payload.get("chunk_id", ""),
document_id=payload.get("document_id", ""),
chunk_index=payload.get("chunk_index", 0),
page_number=payload.get("page_number"),
worksheet_name=payload.get("worksheet_name"),
cell_range=payload.get("cell_range"),
section_title=payload.get("section_title"),
image_references=payload.get("image_references", []),
table_references=payload.get("table_references", []),
confidence_score=payload.get("confidence_score"))
# Reconstruct document chunk
chunk = DocumentChunk(
content=payload.get("content", ""),
metadata=metadata,
embedding=None # Don't include embedding in results
)
# Create search result
search_result = SearchResult(
chunk=chunk,
similarity_score=result.score,
metadata={
"qdrant_id": result.id,
"content_length": payload.get("content_length", 0),
"indexed_at": payload.get("indexed_at"),
"extraction_timestamp": payload.get("extraction_timestamp")
}
)
results.append(search_result)
logger.debug(f"Found {len(results)} similar chunks")
return results
except Exception as e:
logger.error(f"Similarity search failed: {e}")
return []
def filtered_search(self, query_embedding: List[float], filters: Dict[str, Any],
k: int = 10) -> List[SearchResult]:
return self.similarity_search(query_embedding, k, filters)
def delete_document(self, document_id: str) -> bool:
try:
# Delete points with matching document_id
self.client.delete(
collection_name=self.collection_name,
points_selector=models.FilterSelector(
filter=Filter(
must=[
FieldCondition(
key="document_id",
match=MatchValue(value=document_id)
)
]
)
)
)
logger.info(f"Deleted all chunks for document: {document_id}")
return True
except Exception as e:
logger.error(f"Failed to delete document {document_id}: {e}")
return False
def get_collection_info(self) -> Optional[IndexStats]:
try:
collection_info = self.client.get_collection(self.collection_name)
# Count unique documents
# This is a simplified count - in production you might want to use aggregation
search_results = self.client.scroll(
collection_name=self.collection_name,
limit=10000, # Adjust based on your needs
with_payload=["document_id"],
with_vectors=False
)
unique_documents = set()
for point in search_results[0]:
if point.payload and "document_id" in point.payload:
unique_documents.add(point.payload["document_id"])
return IndexStats(
total_points=collection_info.points_count,
collection_name=self.collection_name,
vector_size=collection_info.config.params.vectors.size,
distance_metric=collection_info.config.params.vectors.distance.name,
indexed_documents=len(unique_documents),
last_updated=time.strftime("%Y-%m-%d %H:%M:%S")
)
except Exception as e:
logger.error(f"Failed to get collection info: {e}")
return None
def _build_filter_conditions(self, filters: Dict[str, Any]) -> Filter:
"""
Build Qdrant filter conditions from filter dictionary.
Args:
filters: Dictionary of filter conditions
Returns:
Qdrant Filter object
"""
conditions = []
# Document ID filter
if "document_id" in filters:
conditions.append(
FieldCondition(
key="document_id",
match=MatchValue(value=filters["document_id"])
)
)
# Document type filter
if "document_type" in filters:
conditions.append(
FieldCondition(
key="document_type",
match=MatchValue(value=filters["document_type"])
)
)
# Page number filter
if "page_number" in filters:
conditions.append(
FieldCondition(
key="page_number",
match=MatchValue(value=filters["page_number"])
)
)
# Worksheet name filter
if "worksheet_name" in filters:
conditions.append(
FieldCondition(
key="worksheet_name",
match=MatchValue(value=filters["worksheet_name"])
)
)
# Content length range filter
if "min_content_length" in filters:
conditions.append(
FieldCondition(
key="content_length",
range=models.Range(gte=filters["min_content_length"])
)
)
if "max_content_length" in filters:
conditions.append(
FieldCondition(
key="content_length",
range=models.Range(lte=filters["max_content_length"])
)
)
return Filter(must=conditions) if conditions else None
def health_check(self) -> bool:
"""
Check if the vector store is healthy and accessible.
Returns:
True if healthy, False otherwise
"""
try:
# Try to get collection info
self.client.get_collection(self.collection_name)
return True
except Exception as e:
logger.error(f"Vector store health check failed: {e}")
return False
def create_collection(self, vector_size: int, distance_metric: Distance = Distance.COSINE) -> bool:
try:
self.client.create_collection(
collection_name=self.collection_name,
vectors_config=VectorParams(
size=vector_size,
distance=distance_metric
)
)
# Update instance variables
self.vector_size = vector_size
self.distance_metric = distance_metric
self._create_payload_indexes()
logger.info(f"Created collection {self.collection_name} with vector size {vector_size}")
return True
except Exception as e:
logger.error(f"Failed to create collection: {e}")
return False
def delete_collection(self) -> bool:
try:
self.client.delete_collection(self.collection_name)
logger.info(f"Deleted collection: {self.collection_name}")
return True
except Exception as e:
logger.error(f"Failed to delete collection: {e}")
return False
if __name__=="__main__":
logger.info(f"Vector store init ..")
config = {
'qdrant_url': os.getenv('QDRANT_URL', 'http://localhost:6333'),
'qdrant_api_key': os.getenv('QDRANT_API_KEY'),
'qdrant_collection': 'manufacturing_docs',
'vector_size': 1024
}
vector_store = QdrantVectorStore(config)
health = vector_store.health_check()
if health:
logger.info("Vector store is healthy and ready.")
else:
logger.error("Vector store is not accessible.") |