Unified client for vector database operations. This class routes operations to the appropriate client implementation based on the database type specified in configuration.
| 349 | |
| 350 | |
| 351 | class VectorDBClient: |
| 352 | """ |
| 353 | Unified client for vector database operations. This class routes operations to the appropriate |
| 354 | client implementation based on the database type specified in configuration. |
| 355 | """ |
| 356 | |
| 357 | def __init__(self, endpoint_name: str | None = None, query_params: dict[str, Any] | None = None): |
| 358 | """ |
| 359 | Initialize the database client. |
| 360 | |
| 361 | Args: |
| 362 | endpoint_name: Optional name of the endpoint to use (for backward compatibility) |
| 363 | query_params: Optional query parameters for overriding endpoint |
| 364 | """ |
| 365 | self.query_params = query_params or {} |
| 366 | self.endpoint_name = endpoint_name # Store the endpoint name |
| 367 | self.db_type = None # Will be set based on the primary endpoint |
| 368 | |
| 369 | # Check if query_params specifies a database endpoint override |
| 370 | if self.query_params: |
| 371 | # Check for 'db' or 'retrieval_backend' parameter |
| 372 | param_endpoint = self.query_params.get('db') or self.query_params.get('retrieval_backend') |
| 373 | if CONFIG.is_development_mode(): |
| 374 | print(f"[RETRIEVER] Development mode - param_endpoint from query_params: {param_endpoint}") |
| 375 | if param_endpoint: |
| 376 | # Handle case where param_endpoint might be a list |
| 377 | if isinstance(param_endpoint, list): |
| 378 | if len(param_endpoint) > 0: |
| 379 | param_endpoint = param_endpoint[0] |
| 380 | logger.warning(f"'db' parameter was a list, using first element: {param_endpoint}") |
| 381 | else: |
| 382 | logger.error("'db' parameter is an empty list") |
| 383 | param_endpoint = None |
| 384 | |
| 385 | if param_endpoint: |
| 386 | logger.info(f"Using database endpoint from params: {param_endpoint}") |
| 387 | endpoint_name = param_endpoint |
| 388 | |
| 389 | # If specific endpoint requested, validate and use it |
| 390 | if endpoint_name: |
| 391 | try: |
| 392 | if endpoint_name not in CONFIG.retrieval_endpoints: |
| 393 | available_endpoints = list(CONFIG.retrieval_endpoints.keys()) |
| 394 | error_msg = f"Invalid endpoint: '{endpoint_name}'. Available endpoints: {', '.join(available_endpoints)}" |
| 395 | logger.error(error_msg) |
| 396 | raise ValueError(error_msg) |
| 397 | except TypeError as e: |
| 398 | # This can happen if endpoint_name is unhashable (e.g., a list) |
| 399 | error_msg = f"Invalid endpoint name type: {type(endpoint_name).__name__}. Expected string, got: {endpoint_name}" |
| 400 | logger.error(error_msg) |
| 401 | raise ValueError(error_msg) from e |
| 402 | |
| 403 | # For backward compatibility, use only the specified endpoint |
| 404 | endpoint_config = CONFIG.retrieval_endpoints[endpoint_name] |
| 405 | self.enabled_endpoints = {endpoint_name: endpoint_config} |
| 406 | self.db_type = endpoint_config.db_type # Set db_type from the endpoint |
| 407 | logger.info(f"VectorDBClient initialized with specific endpoint: {endpoint_name}") |
| 408 | else: |
no outgoing calls