File size: 16,977 Bytes
625e9e8 |
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 |
import logging
import chromadb
from sentence_transformers import SentenceTransformer
from typing import List, Dict, Any, Tuple
import os
import json
import re
# Configure basic logging for debugging and tracing
# Use a specific logger name for this module
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
class RetrievalManager:
"""
Manages retrieving documents from a ChromaDB vector database based on a user query.
"""
# Define a set of common stop words to ignore in category matching
STOP_WORDS = {
"a", "an", "the", "is", "are", "and", "or", "but", "for", "with",
"in", "on", "at", "of", "to", "from", "by", "about", "as", "into",
"like", "through", "after", "before", "over", "under", "above", "below",
"up", "down", "out", "off", "then", "once", "here", "there", "when",
"where", "why", "how", "all", "any", "both", "each", "few", "more",
"most", "other", "some", "such", "no", "nor", "not", "only", "own",
"same", "so", "than", "too", "very", "s", "t", "can", "will", "just",
"don", "should", "now", "compare", "x", "y"
}
def __init__(self, db_path: str = "./chroma_db", model_name: str = 'BAAI/bge-large-en-v1.5'):
"""
Initializes the RetrievalManager.
Args:
db_path (str): Path to the ChromaDB database directory.
model_name (str): The name of the sentence-transformer model to use for embeddings.
"""
logger.info(f"Initializing RetrievalManager with db_path='{db_path}' and model='{model_name}'")
# Initialize the ChromaDB client
self.client = chromadb.PersistentClient(path=db_path)
# Load the sentence-transformer model
try:
self.model = SentenceTransformer(model_name)
logger.info(f"Successfully loaded embedding model: {model_name}")
except Exception as e:
logger.error(f"Failed to load sentence-transformer model '{model_name}'. Error: {e}")
raise
# Load pre-computed filterable metadata
self.filterable_metadata = None
try:
with open("filterable_metadata.json", "r") as f:
self.filterable_metadata = json.load(f)
logger.info("Successfully loaded filterable_metadata.json")
except FileNotFoundError:
logger.warning("filterable_metadata.json not found. Filtering by brand/category will be disabled.")
except json.JSONDecodeError:
logger.error("Failed to decode filterable_metadata.json. Filtering by brand/category will be disabled.")
def _generate_query_embedding(self, query: str) -> List[float]:
"""
Generates an embedding for a single query string.
Args:
query (str): The user query string.
Returns:
List[float]: The embedding vector for the query.
"""
logger.info(f"Generating embedding for query: '{query}'")
embedding = self.model.encode(query, convert_to_tensor=False)
logger.info("Finished generating query embedding.")
return embedding.tolist()
def _extract_filters(self, query: str) -> Tuple[Dict[str, Any], str]:
"""
Extracts metadata filters (price, brand, category) from the query string
and returns a ChromaDB-compatible filter dictionary and a cleaned query.
Args:
query (str): The user's raw query.
Returns:
A tuple containing the `where` filter dictionary and the cleaned query string.
"""
cleaned_query = query
filters = []
parts_to_remove = []
# Price filtering
price_patterns = {
"lt": re.compile(r"(?:under|less than|below|max of|\$lt)\s*\$?(\d+\.?\d*)", re.IGNORECASE),
"gt": re.compile(r"(?:over|more than|above|min of|\$gt)\s*\$?(\d+\.?\d*)", re.IGNORECASE),
}
for op, pattern in price_patterns.items():
match = pattern.search(cleaned_query)
if match:
price = float(match.group(1))
filters.append({"price": {f"${op}": price}})
parts_to_remove.append(match.group(0))
# Brand and Category filtering
if self.filterable_metadata:
# Sort by length descending to match longer names first (e.g., "Computers and Laptops" before "Laptops")
brands = sorted(self.filterable_metadata.get("brands", []), key=len, reverse=True)
categories = sorted(self.filterable_metadata.get("categories", []), key=len, reverse=True)
for brand in brands:
pattern = r'\b' + re.escape(brand) + r'\b'
match = re.search(pattern, cleaned_query, re.IGNORECASE)
if match:
filters.append({"brand": brand})
# Do not remove brand from query to preserve semantic meaning
break # Assume only one brand filter is needed
# New, more flexible category matching logic.
# It checks if any significant word from the query appears in an official category name.
category_found = False
query_lower = cleaned_query.lower()
# 1. Priority check for "laptop", "computer", or "PC"
if any(term in query_lower for term in ["laptop", "computer", "pc"]):
# Force category to "Computers and Laptops"
for category in self.filterable_metadata.get("categories", []):
if "Computers and Laptops" in category:
filters.append({"category": category})
category_found = True
break
# 2. If no priority match, use the flexible matching logic
# if not category_found:
# raw_query_words = re.findall(r'\b\w{3,}\b', cleaned_query.lower()) # Extract words of 3+ chars
# # Filter out stop words from the query words to prevent irrelevant category matches
# query_words = [word for word in raw_query_words if word not in self.STOP_WORDS]
# for category in categories:
# for word in query_words:
# # Check if the query word (handling plurals) is a whole word in the category name.
# # e.g., query "cameras" will match category "Cameras and Camcorders".
# pattern = r'\b' + re.escape(word.rstrip('s')) + r's?\b'
# if re.search(pattern, category, re.IGNORECASE):
# filters.append({"category": category})
# category_found = True
# break # Word found, move to next category
# if category_found:
# break # Category found, stop searching
# Category search based on synonyms
# Category search based on synonyms with priority ordering
CATEGORY_SYNONYMS = {
"Computers and Laptops": ["laptop", "laptops", "computer", "notebook", "ultrabook", \
"chromebook", "PC", "desktop", "workstation", "gaming laptop"],
"Cameras and Camcorders": ["camera", "cameras", "camcorder", "photo", "video camera"],
"Gaming Consoles and Accessories": ["console", "gaming", "games", "controller"],
"Smartphones and Accessories": ["phone", "smartphone", "mobile", "case", "charger"],
"Audio Equipment": ["audio", "speaker", "headphone", "earbud", "sound"],
"Televisions and Home Theater Systems": ["tv", "television", "home theater", "soundbar", "tvs"]
}
# Priority list: categories to check first (more specific synonyms)
priority_categories = ["Computers and Laptops", "Cameras and Camcorders", "Smartphones and Accessories"]
matched_categories = set()
query_lower = query.lower()
# Phase 1: Check priority categories for multi-word synonyms first
for category in priority_categories:
multi_word_synonyms = [syn for syn in CATEGORY_SYNONYMS[category] if len(syn.split()) > 1]
for syn in multi_word_synonyms:
if syn in query_lower:
matched_categories.add(category)
break
# Phase 2: If no priority multi-word matches, check all single-word synonyms
if not matched_categories:
for category in CATEGORY_SYNONYMS.keys():
single_word_synonyms = [syn for syn in CATEGORY_SYNONYMS[category] if len(syn.split()) == 1]
if any(syn in query_lower for syn in single_word_synonyms):
matched_categories.add(category)
# Add all matched categories to filters
for category in matched_categories:
filters.append({"category": category})
category_found = True
# # [DEACTIVATED] Remove only the identified price-related parts from the query.
# # This has been deactivated because it weakens the semantic query, leading to less precise results.
# # The full query context is now preserved for the embedding model.
# for part in parts_to_remove:
# cleaned_query = cleaned_query.replace(part, "", 1)
# Construct the final filter dictionary
where_filter = {}
if filters:
if len(filters) > 1:
where_filter = {"$or": filters}
else:
where_filter = filters[0]
# # [DEACTIVATED] Final cleanup of extra spaces.
# cleaned_query = ' '.join(cleaned_query.split()).strip()
if where_filter:
logger.info(f"Extracted filter: {where_filter}")
logger.info(f"Cleaned query for embedding: '{cleaned_query}'")
return where_filter, cleaned_query
def _route_query(self, query: str) -> List[str]:
"""
Determines which collection(s) to query based on keywords in the query.
This provides a simple, fast routing mechanism before performing a semantic search.
Args:
query (str): The user query string.
Returns:
List[str]: A list of collection names to target for the search.
"""
query_lower = query.lower()
# Keywords that suggest a user is interested in opinions or experiences
review_keywords = ["review", "customer", "feedback", "complaints", "say about", "opinion", "experience"]
# Keywords that suggest a user is interested in product details
product_keywords = ["specs", "features", "have", "available", "do you have", \
"specification", "technical details", "price", "warranty"]
target_collections = []
# Check for review-related keywords
if any(keyword in query_lower for keyword in review_keywords):
target_collections.append("reviews")
# Check for product-related keywords
if any(keyword in query_lower for keyword in product_keywords):
target_collections.append("products")
# If no specific keywords are found, default to searching both collections
if not target_collections:
logger.info("No specific keywords found in query, defaulting to both 'products' and 'reviews' collections.")
return ["products", "reviews"]
logger.info(f"Routing query to collections: {target_collections}")
return target_collections
def search(self, query: str) -> Dict[str, List[Dict[str, Any]]]:
"""
Performs a semantic search across relevant collections based on the user query.
This method orchestrates the query processing, routing, and retrieval steps.
Args:
query (str): The user's search query.
Returns:
Dict[str, List[Dict[str, Any]]]: A dictionary where keys are collection names
and values are the retrieval results from ChromaDB.
"""
logger.info(f"--- Starting search for query: '{query}' ---")
# Step 1: Extract filters and get the cleaned query
where_filter, cleaned_query = self._extract_filters(query)
# If cleaning results in an empty string, fall back to the original query for embedding
embedding_query = cleaned_query if cleaned_query else query
logger.info(f"Using query for embedding: '{embedding_query}'")
# Step 2: Determine which collections to search based on the original query's intent
target_collections = self._route_query(query)
# Step 3: Generate an embedding for the (potentially cleaned) query
query_embedding = self._generate_query_embedding(embedding_query)
results = {}
# Step 4: Query each targeted collection with the filter
for collection_name in target_collections:
try:
collection = self.client.get_collection(name=collection_name)
# Set the number of results based on the collection type
n_results = 5 if collection_name == "products" else 8
logger.info(f"Querying '{collection_name}' collection with n_results={n_results} and filter={where_filter}...")
retrieved = collection.query(
query_embeddings=[query_embedding],
n_results=n_results,
where=where_filter if where_filter else None
)
results[collection_name] = retrieved
logger.info(f"Retrieved {len(retrieved.get('ids', [[]])[0])} results from '{collection_name}'.")
except Exception as e:
logger.error(f"Failed to query collection '{collection_name}'. Error: {e}")
results[collection_name] = []
logger.info(f"--- Finished search for query: '{query}' ---")
return results
if __name__ == '__main__':
# This allows the script to be run directly to test the retrieval functionality.
# It assumes the database has been populated by running `vector_db_manager.py` first.
# --- Configuration ---
EMBEDDING_MODEL = 'BAAI/bge-large-en-v1.5'
DB_PATH = "./chroma_db"
# --- Pre-run Check ---
if not os.path.exists(DB_PATH):
logger.error(f"FATAL: ChromaDB path '{DB_PATH}' not found.")
logger.error("Please run 'vector_db_manager.py' first to create and populate the database.")
else:
# --- Retrieval Example ---
logger.info("\n--- Starting Retrieval Test ---")
try:
retrieval_manager = RetrievalManager(db_path=DB_PATH, model_name=EMBEDDING_MODEL)
# Example queries from the retrieval plan
queries = [
"What laptops do you have?",
"Gaming laptops",
"Lightweight laptop",
"What do customers say about battery life?",
"SmartX ProPhone camera reviews",
"Any feedback on the AudioBliss headphones?",
"Show me TVs under $500",
"TechPro gaming laptops over $1000"
]
for query in queries:
print("\n" + "="*60)
print(f"Executing query: '{query}'")
print("="*60)
search_results = retrieval_manager.search(query)
for collection_name, results in search_results.items():
print(f"\n--- Results from '{collection_name}' collection ---")
if results and results.get('documents') and results['documents'][0]:
for i, doc in enumerate(results['documents'][0]):
distance = results['distances'][0][i] if results.get('distances') else 'N/A'
metadata = results['metadatas'][0][i] if results.get('metadatas') else {}
print(f" - Result {i+1} (Distance: {distance:.4f}):")
print(f" ID: {results['ids'][0][i]}")
print(f" Document: {doc[:120].strip()}...")
print(f" Metadata: {metadata}")
else:
print(" No results found in this collection.")
print("\n" + "="*60)
logger.info("--- Retrieval Test Finished ---")
except Exception as e:
logger.error(f"An error occurred during the retrieval test: {e}", exc_info=True)
|