Jainish1808 commited on
Commit
6d41909
Β·
1 Parent(s): 5481e6c

Fix MongoDB vector search - remove unsupported relevance_score_fn parameter

Browse files
Files changed (1) hide show
  1. main.py +16 -38
main.py CHANGED
@@ -176,12 +176,11 @@ class MongoDBAtlasVectorStore:
176
  separators=["\n\n", "\n", " ", ""]
177
  )
178
 
179
- # Initialize LangChain MongoDB Atlas Vector Search with default metric
180
  self.vector_store = MongoDBAtlasVectorSearch(
181
  collection=self.collection,
182
  embedding=self.embeddings,
183
- index_name=config.VECTOR_INDEX_NAME,
184
- relevance_score_fn=config.DEFAULT_METRIC
185
  )
186
 
187
  # Test connection
@@ -235,49 +234,28 @@ class MongoDBAtlasVectorStore:
235
  def similarity_search(self, query: str, k: int = 3, metric: str = None, score_threshold: float = 0.0) -> List[Tuple[Document, float]]:
236
  """Perform similarity search using MongoDB Atlas Vector Search with specified metric."""
237
  try:
238
- # Use the provided metric or default
239
  if metric and metric in config.SUPPORTED_METRICS:
240
- search_metric = config.SUPPORTED_METRICS[metric]
241
- logger.info(f"πŸ” Using similarity metric: {metric} -> {search_metric}")
242
  else:
243
- search_metric = config.DEFAULT_METRIC
244
- logger.info(f"πŸ” Using default similarity metric: {search_metric}")
245
 
246
- # Create a new vector store instance with the specified metric if different from current
247
- if search_metric != self.vector_store.relevance_score_fn:
248
- temp_vector_store = MongoDBAtlasVectorSearch(
249
- collection=self.collection,
250
- embedding=self.embeddings,
251
- index_name=config.VECTOR_INDEX_NAME,
252
- relevance_score_fn=search_metric
253
- )
254
-
255
- # Use the temporary vector store for this search
256
- results = temp_vector_store.similarity_search_with_score(
257
- query=query,
258
- k=k,
259
- # score_threshold=score_threshold
260
- )
261
- else:
262
- # Use the existing vector store
263
- results = self.vector_store.similarity_search_with_score(
264
- query=query,
265
- k=k,
266
- # score_threshold=score_threshold
267
- )
268
 
269
- logger.info(f"πŸ“Š Found {len(results)} relevant documents for query using {search_metric}")
270
  return results
271
 
272
  except Exception as e:
273
- logger.error(f"❌ Error performing similarity search with metric {metric}: {e}")
274
- # Fallback to default metric if there's an error
275
  try:
276
- results = self.vector_store.similarity_search_with_score(
277
- query=query,
278
- k=k,
279
- # score_threshold=score_threshold
280
- )
281
  logger.info(f"πŸ“Š Fallback search returned {len(results)} results")
282
  return results
283
  except Exception as fallback_e:
 
176
  separators=["\n\n", "\n", " ", ""]
177
  )
178
 
179
+ # Initialize LangChain MongoDB Atlas Vector Search
180
  self.vector_store = MongoDBAtlasVectorSearch(
181
  collection=self.collection,
182
  embedding=self.embeddings,
183
+ index_name=config.VECTOR_INDEX_NAME
 
184
  )
185
 
186
  # Test connection
 
234
  def similarity_search(self, query: str, k: int = 3, metric: str = None, score_threshold: float = 0.0) -> List[Tuple[Document, float]]:
235
  """Perform similarity search using MongoDB Atlas Vector Search with specified metric."""
236
  try:
237
+ # Log the metric being used
238
  if metric and metric in config.SUPPORTED_METRICS:
239
+ logger.info(f"οΏ½ Using similarity metric: {metric}")
 
240
  else:
241
+ logger.info(f"οΏ½ Using default similarity metric: cosine")
 
242
 
243
+ # Perform similarity search using the vector store
244
+ results = self.vector_store.similarity_search_with_score(
245
+ query=query,
246
+ k=k
247
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
+ logger.info(f"πŸ“Š Found {len(results)} relevant documents for query")
250
  return results
251
 
252
  except Exception as e:
253
+ logger.error(f"❌ Error performing similarity search: {e}")
254
+ # Fallback to basic similarity search without score
255
  try:
256
+ docs = self.vector_store.similarity_search(query=query, k=k)
257
+ # Convert to tuple format with dummy scores
258
+ results = [(doc, 0.0) for doc in docs]
 
 
259
  logger.info(f"πŸ“Š Fallback search returned {len(results)} results")
260
  return results
261
  except Exception as fallback_e: