teenaxta commited on
Commit
8fbac75
·
verified ·
1 Parent(s): de99784

Update src/mongo_search.py

Browse files
Files changed (1) hide show
  1. src/mongo_search.py +16 -1
src/mongo_search.py CHANGED
@@ -1,6 +1,7 @@
1
  from utils.mongo_utils import generate_mongodb_query, get_prompt
2
  from langchain_openai import ChatOpenAI, OpenAIEmbeddings
3
  from langchain_core.output_parsers import JsonOutputParser
 
4
 
5
  from utils.utils import timing_decorator
6
 
@@ -16,7 +17,9 @@ class MongoSearch:
16
  self.index_variable = index_variable
17
 
18
  @timing_decorator
19
- def __call__(self, query, k=3, use_filter=True):
 
 
20
 
21
  query_filter = {}
22
  if use_filter:
@@ -52,6 +55,8 @@ class MongoSearch:
52
  # Extract unique makeModel values for the next step
53
  make_model_list = [doc['makeModel'] for doc in first_search_results]
54
 
 
 
55
  # Define the second pipeline
56
  second_pipeline = [
57
  {
@@ -79,6 +84,16 @@ class MongoSearch:
79
  # run pipeline
80
  result = self.collection.aggregate(second_pipeline)
81
 
 
 
 
 
 
 
 
 
 
 
82
  cars = []
83
  for i in result:
84
  cars.append(i)
 
1
  from utils.mongo_utils import generate_mongodb_query, get_prompt
2
  from langchain_openai import ChatOpenAI, OpenAIEmbeddings
3
  from langchain_core.output_parsers import JsonOutputParser
4
+ import random
5
 
6
  from utils.utils import timing_decorator
7
 
 
17
  self.index_variable = index_variable
18
 
19
  @timing_decorator
20
+ def __call__(self, query, k=4, use_filter=True):
21
+
22
+
23
 
24
  query_filter = {}
25
  if use_filter:
 
55
  # Extract unique makeModel values for the next step
56
  make_model_list = [doc['makeModel'] for doc in first_search_results]
57
 
58
+ k = k * 3
59
+
60
  # Define the second pipeline
61
  second_pipeline = [
62
  {
 
84
  # run pipeline
85
  result = self.collection.aggregate(second_pipeline)
86
 
87
+ # Convert the result cursor to a list
88
+ result_list = list(result)
89
+
90
+ # Randomly select k/3 objects from the results
91
+ k_third = k // 3
92
+ selected_cars = random.sample(result_list, min(k_third, len(result_list)))
93
+
94
+ # Update the result with the randomly selected cars
95
+ result = selected_cars
96
+
97
  cars = []
98
  for i in result:
99
  cars.append(i)