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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cea13a
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c86ed06
 
2e2dda5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
'''
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
'''

from collections import namedtuple
from datetime import datetime, timedelta
from dateutil import tz, parser
import itertools
import json
import os
import time
import uuid
import requests
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
from requests_aws4auth import AWS4Auth
from requests.auth import HTTPBasicAuth
from datetime import datetime
import boto3
import streamlit as st
import utilities.mvectors as cb
current_date_time = (datetime.now()).isoformat()
today_ = datetime.today().strftime('%Y-%m-%d')


def handler(input_,session_id):
    DOMAIN_ENDPOINT =   st.session_state.OpenSearchDomainEndpoint #"search-opensearchservi-rimlzstyyeih-3zru5p2nxizobaym45e5inuayq.us-west-2.es.amazonaws.com" 
    REGION = st.session_state.REGION
    #SAGEMAKER_MODEL_ID = st.session_state.SAGEMAKER_MODEL_ID
    BEDROCK_TEXT_MODEL_ID = st.session_state.BEDROCK_TEXT_MODEL_ID
    BEDROCK_MULTIMODAL_MODEL_ID = st.session_state.BEDROCK_MULTIMODAL_MODEL_ID
    SAGEMAKER_SPARSE_MODEL_ID = st.session_state.SAGEMAKER_SPARSE_MODEL_ID
    SAGEMAKER_CrossEncoder_MODEL_ID = st.session_state.SAGEMAKER_CrossEncoder_MODEL_ID
    print("BEDROCK_TEXT_MODEL_ID")
    print(BEDROCK_TEXT_MODEL_ID)
    
    ####### Hybrid Search weights logic for throwing warning to users for inappropriate weights #######
    
    # def my_filtering_function(pair):
    #     key, value = pair
    #     if key.split("-")[0] + " Search" in st.session_state["inputs_"]["searchType"]:
    #         return True  # keep pair in the filtered dictionary
    #     else:
    #         return False  # filter pair out of the dictionary

 
    # filtered_search = dict(filter(my_filtering_function, st.session_state.input_weightage.items()))
    
    # search_types_used = ", ".join(st.session_state["inputs_"]["searchType"])
    
    # if((sum(st.session_state.weights_)!=100 or len(st.session_state["inputs_"]["searchType"])!=len(list(filter(lambda a: a >0, st.session_state.weights_)))) and len(st.session_state["inputs_"]["searchType"])!=1):
    #     st.warning('User Input Error for **WEIGHTS** :-\n\nOne or both of the below conditions was not satisfied, \n1. The total weight of all the selected search type(s): "'+search_types_used+'" should be equal to 100 \n 2. The weight of each of the search types, "'+search_types_used+'" should be greater than 0 \n\n Entered input: '+json.dumps(filtered_search)+'\n\n Please re-enter your weights to satisfy the above conditions and try again',icon = "🚨")
    #     refresh = st.button("Re-Enter")
    #     if(refresh):
    #         st.switch_page('pages/1_Semantic_Search.py')
    #     st.stop()
    
    ####### Auth and connection for OpenSearch domain #######
    credentials = boto3.Session().get_credentials()
    awsauth = HTTPBasicAuth('master',st.secrets['ml_search_demo_api_access'])
    host = 'https://'+DOMAIN_ENDPOINT+'/'
    headers = {"Content-Type": "application/json"}
  
  
    ####### Parsing Inputs from user #######
    
    print("*********")
    print(input_)
    search_types = input_["searchType"]
    
    if("NormType" not in input_.keys()):
        norm_type = "min_max"
    else:
        norm_type = input_["NormType"]
        
    if("CombineType" not in input_.keys()):
        combine_type = "arithmetic_mean"
    else:
        combine_type = input_["CombineType"]
    
    if("weight" not in input_.keys()):
        semantic_weight = 0.5
    else:
        semantic_weight = input_["weight"]
        

    
    query = input_["text"]
    img = input_["image"]
    
    if("sparse" not in input_.keys()):
        sparse = "disabled"
    else:
        sparse = input_["sparse"]
    
    
    k_ = input_["K"]
    image_upload = input_["imageUpload"]
    
    
    
    num_queries = len(search_types)
    
    weights = []
    
    searches = ['Keyword','Vector','Multimodal','NeuralSparse']
    for i in searches:
        weight = input_['weightage'][i+'-weight']/100
        if(weight>0.0):
            weights.append(weight)
            
      
        
    ######## Updating hybrid Search pipeline #######   
    print("Updating Search pipeline with new weights")        
    s_pipeline_payload = {"version": 1234}
    s_pipeline_payload["phase_results_processors"] = [
                {
                    "normalization-processor": {
                    "normalization": {
                        "technique": norm_type
                    },
                    "combination": {
                        "technique": combine_type,
                        "parameters": {
                        "weights": weights
                        }
                    }
                    }
                }
                ]
            

        
    opensearch_search_pipeline = (requests.get(host+'_search/pipeline/hybrid_search_pipeline', auth=awsauth,headers=headers)).text
    if(opensearch_search_pipeline!='{}'):
        path = "_search/pipeline/hybrid_search_pipeline" 
        url = host + path
        r = requests.put(url, auth=awsauth, json=s_pipeline_payload, headers=headers)
        print("Hybrid Search Pipeline updated: "+str(r.status_code))
        ######## Combining hybrid+rerank pipeline ####### 
        opensearch_rerank_pipeline = (requests.get(host+'_search/pipeline/rerank_pipeline', auth=awsauth,headers=headers)).text
    ######## start of Applying LLM filters ####### 
    if(st.session_state.input_rewritten_query!=""):
            filter_ = {"filter": {
                 "bool": {
                     "must": []}}}
            filter_['filter']['bool']['must'] = st.session_state.input_rewritten_query['query']['bool']['must']
    ######## end of Applying LLM filters ####### 
    
    ######### Create the queries for hybrid search #########
    
    
    path = "demostore-search-index-reindex/_search"
    
    url = host + path
    
    hybrid_payload = {
        "_source": {
        "exclude": [
            "product_description_vector","product_multimodal_vector","product_image"
        ]
        },
        "query": {
        "hybrid": {
            "queries": [
            
            #1. keyword query
            #2. vector search query
            #3. multimodal query
            #4. Sparse query
        
            ]
        }
        },"size":k_,
        "highlight": {
    "fields": {
    "product_description": {}
    }
    }}
    
    
            
    if('Keyword Search' in search_types):
        
        keyword_payload = {
                        "match": {
                        "product_description": {
                            "query": query
                        }
                        }
                    }
        if(st.session_state.input_rewritten_query !=""):
            keyword_payload = st.session_state.input_rewritten_query['query']
            
        if(st.session_state.input_manual_filter == "True"):
            keyword_payload['bool']={'filter':[]}
            if(st.session_state.input_category!=None):
                keyword_payload['bool']['filter'].append({"term": {"category": st.session_state.input_category}})
            if(st.session_state.input_gender!=None):
                keyword_payload['bool']['filter'].append({"term": {"gender_affinity": st.session_state.input_gender}})
            if(st.session_state.input_price!=(0,0)):
                keyword_payload['bool']['filter'].append({"range": {"price": {"gte": st.session_state.input_price[0],"lte": st.session_state.input_price[1] }}})
        
            keyword_payload['bool']['must'] = [{
                        "match": {
                        "product_description": {
                            "query": query
                        }
                        }
                    }]            
            del keyword_payload['match']
 
        hybrid_payload["query"]["hybrid"]["queries"].append(keyword_payload)
        
    if('Vector Search' in search_types):
        if(st.session_state.input_mvector_rerank):
            query_vector = cb.vectorise(query,False)
            vector_field = "description_vector"
            vector_payload = {"knn": {}}
            vector_payload["knn"][vector_field]= {
                            "vector":query_vector,
                            "k": k_
                        }
        
#         path3 =  "_plugins/_ml/models/"+BEDROCK_TEXT_MODEL_ID+"/_predict"
        
#         url3 = host+path3
        
#         payload3 = {
#         "parameters": {
#             "inputText": query
#             }
#                 }
        
#         r3 = requests.post(url3, auth=awsauth, json=payload3, headers=headers)
#         vector_ = json.loads(r3.text)
#         #print(r3.text)
#         query_vector = vector_['inference_results'][0]['output'][0]['data']
#         #print(query_vector)
        
#         vector_payload = {
#                         "knn": {
#                         "product_description_vector": {
#                             "vector":query_vector,
#                             #"query_text": query,
#                             #"model_id": BEDROCK_TEXT_MODEL_ID,
#                             "k": k_
#                         }
#                         }
#                     }
        
        #using neural query 
        else:
            vector_payload = {
                        "neural": {
                        "product_description_vector": {
                            "query_text": query,
                            "model_id": BEDROCK_TEXT_MODEL_ID,
                            "k": k_
                        }
                        }
                    }
        
        ###### start of efficient filter applying #####
        if(st.session_state.input_rewritten_query!=""):
            vector_payload['neural']['product_description_vector']['filter'] = filter_['filter']
            
        if(st.session_state.input_manual_filter == "True"):
            vector_payload['neural']['product_description_vector']['filter'] = {"bool":{"must":[]}}
            if(st.session_state.input_category!=None):
                vector_payload['neural']['product_description_vector']['filter']["bool"]["must"].append({"term": {"category": st.session_state.input_category}})
            if(st.session_state.input_gender!=None):
                vector_payload['neural']['product_description_vector']['filter']["bool"]["must"].append({"term": {"gender_affinity": st.session_state.input_gender}})
            if(st.session_state.input_price!=(0,0)):
                vector_payload['neural']['product_description_vector']['filter']["bool"]["must"].append({"range": {"price": {"gte": st.session_state.input_price[0],"lte": st.session_state.input_price[1] }}})
        ###### end of efficient filter applying #####
        
        hybrid_payload["query"]["hybrid"]["queries"].append(vector_payload)
        
    if('Multimodal Search' in search_types):
        
        multimodal_payload  = {
       
        "neural": {
            "product_multimodal_vector": {
            
            "model_id": BEDROCK_MULTIMODAL_MODEL_ID,
            "k": k_
            }
            }
            }
        
        
        if(image_upload == 'yes' and query == ""):
            multimodal_payload["neural"]["product_multimodal_vector"]["query_image"] =  img
        if(image_upload == 'no' and query != ""):
            multimodal_payload["neural"]["product_multimodal_vector"]["query_text"] =  query
        if(image_upload == 'yes' and query != ""):
            
            multimodal_payload["neural"]["product_multimodal_vector"]["query_image"] =  img
            multimodal_payload["neural"]["product_multimodal_vector"]["query_text"] =  query
        
        ###### start of efficient filter applying #####
        if(st.session_state.input_rewritten_query!=""):
            multimodal_payload['neural']['product_multimodal_vector']['filter'] = filter_['filter']
            
        if(st.session_state.input_manual_filter == "True"):
            multimodal_payload['neural']['product_multimodal_vector']['filter'] = {"bool":{"must":[]}}
            if(st.session_state.input_category!=None):
                multimodal_payload['neural']['product_multimodal_vector']['filter']["bool"]["must"].append({"term": {"category": st.session_state.input_category}})
            if(st.session_state.input_gender!=None):
                multimodal_payload['neural']['product_multimodal_vector']['filter']["bool"]["must"].append({"term": {"gender_affinity": st.session_state.input_gender}})
            if(st.session_state.input_price!=(0,0)):
                multimodal_payload['neural']['product_multimodal_vector']['filter']["bool"]["must"].append({"range": {"price": {"gte": st.session_state.input_price[0],"lte": st.session_state.input_price[1] }}})
        
#         print("vector_payload**************")   
#         print(vector_payload)    
        
        ###### end of efficient filter applying #####
        
        hybrid_payload["query"]["hybrid"]["queries"].append(multimodal_payload)
          
          
          
        
    if('NeuralSparse Search' in search_types):
        
        path2 =  "_plugins/_ml/models/"+SAGEMAKER_SPARSE_MODEL_ID+"/_predict"
        
        url2 = host+path2
        
        payload2 = {
        "parameters": {
            "inputs": query
            }
                }
        
        r2 = requests.post(url2, auth=awsauth, json=payload2, headers=headers)
        sparse_ = json.loads(r2.text)
        query_sparse = sparse_["inference_results"][0]["output"][0]["dataAsMap"]["response"][0]
        query_sparse_sorted = {key: value for key, 
               value in sorted(query_sparse.items(), 
                               key=lambda item: item[1],reverse=True)}
        print("text expansion is enabled")
        #print(query_sparse_sorted)
        query_sparse_sorted_filtered = {}
       
        rank_features = []
        for key_ in query_sparse_sorted.keys():
            if(query_sparse_sorted[key_]>=st.session_state.input_sparse_filter):
                feature = {"rank_feature": {"field": "product_description_sparse_vector."+key_,"boost":query_sparse_sorted[key_]}}
                rank_features.append(feature)
                query_sparse_sorted_filtered[key_]=query_sparse_sorted[key_]
            else:
                break
        
        #print(query_sparse_sorted_filtered)
        sparse_payload = {"bool":{"should":rank_features}}
        
        ###### start of efficient filter applying #####
        if(st.session_state.input_rewritten_query!=""):
            sparse_payload['bool']['must'] = filter_['filter']['bool']['must']
            
        if(st.session_state.input_manual_filter == "True"):
            sparse_payload['bool']['filter']=[]
            if(st.session_state.input_category!=None):
                sparse_payload['bool']['filter'].append({"term": {"category": st.session_state.input_category}})
            if(st.session_state.input_gender!=None):
                sparse_payload['bool']['filter'].append({"term": {"gender_affinity": st.session_state.input_gender}})
            if(st.session_state.input_price!=(0,0)):
                sparse_payload['bool']['filter'].append({"range": {"price": {"gte": st.session_state.input_price[0],"lte": st.session_state.input_price[1] }}})
        
      
        ###### end of efficient filter applying #####
            
        # sparse_payload = {
            
        #         "neural_sparse": 
        #         {
        #         "desc_embedding_sparse":
        #             {
        #         "query_text": query,
        #         "model_id": SAGEMAKER_SPARSE_MODEL_ID,
        #         #"max_token_score": 2
        #     }
        #     }
                
        #         }
        
        
        hybrid_payload["query"]["hybrid"]["queries"].append(sparse_payload)
            
            
    docs = []
    
    if(st.session_state.input_sql_query!=""):
        url = host +"_plugins/_sql?format=json"
        payload = {"query":st.session_state.input_sql_query}
        r = requests.post(url, auth=awsauth, json=payload, headers=headers)
    
    if(len(hybrid_payload["query"]["hybrid"]["queries"])==1):
        if(st.session_state.input_mvector_rerank and 'Vector Search' in search_types):
            path = "retail-search-colbert-description-reindex/_search" 
            url = host + path
            r = requests.get(url, auth=awsauth, json=hybrid_payload, headers=headers)
            response_ = json.loads(r.text)
            docs = response_['hits']['hits']
            docs = cb.search(docs)
        else:
            single_query = hybrid_payload["query"]["hybrid"]["queries"][0]
            del hybrid_payload["query"]["hybrid"]
            hybrid_payload["query"] = single_query
            if(st.session_state.re_ranker == 'true' and st.session_state.input_reranker == 'Cohere Rerank'):
                path = "demostore-search-index-reindex/_search?search_pipeline=rerank_pipeline" 
                url = host + path
                hybrid_payload["ext"] = {"rerank": {
                                            "query_context": {
                                                "query_text": query
                                            }
                                            }}
                
            r = requests.get(url, auth=awsauth, json=hybrid_payload, headers=headers)
            response_ = json.loads(r.text)
            docs = response_['hits']['hits']
    
    
    else:
        if( st.session_state.input_hybridType == "OpenSearch Hybrid Query"):
            url_ = url + "?search_pipeline=hybrid_search_pipeline" 
            
            if(st.session_state.re_ranker == 'true' and st.session_state.input_reranker == 'Cohere Rerank'):
                
                url_ = url + "?search_pipeline=hybrid_rerank_pipeline" 
            
                hybrid_payload["ext"] = {"rerank": {
                                          "query_context": {
                                             "query_text": query
                                          }
                                        }}
            r = requests.get(url_, auth=awsauth, json=hybrid_payload, headers=headers)
            response_ = json.loads(r.text)
            docs = response_['hits']['hits']
        
        else:
            all_docs = []
            all_docs_ids = []
            only_hits = []
        
            rrf_hits = []
            for i,query in enumerate(hybrid_payload["query"]["hybrid"]["queries"]):
                payload_ =  {'_source': 
                    {'exclude': ['desc_embedding_bedrock-multimodal', 'desc_embedding_bedrock-text', 'product_description_sparse_vector']}, 
                    'query': query, 
                    'size': k_, 'highlight': {'fields': {'product_description': {}}}}
                
                r_ = requests.get(url, auth=awsauth, json=payload_, headers=headers)
                resp = json.loads(r_.text)
                all_docs.append({"search":list(query.keys())[0],"results":resp['hits']['hits'],"weight":weights[i]})
                only_hits.append(resp['hits']['hits'])
                for hit in resp['hits']['hits']:
                    all_docs_ids.append(hit["_id"])
                    
            
            id_scores = []
            rrf_hits_unsorted = []
            
            for id in all_docs_ids:
                score = 0.0
                for result_set in all_docs:
                    if id in json.dumps(result_set['results']):
                        for n,res in enumerate(result_set['results']):
                            if(res["_id"] == id):
                                score += result_set["weight"] * (1.0 /  (n+1))
                id_scores.append({"id":id,"score":score})
                for only_hit in only_hits:
                    for i_ in only_hit:
                        if(id == i_["_id"]):
                            i_["_score"] = score
                        rrf_hits_unsorted.append(i_)
            docs = sorted(rrf_hits_unsorted, key=lambda x: x['_score'],reverse=True)
            
            
            
    arr = []
    dup = []
    for doc in docs:
        if(doc['_source']['image_url'] not in dup):
            res_ = {
                "desc":doc['_source']['product_description'],
               "caption":doc['_source']['caption'],
                "image_url":doc['_source']['image_url'],
               "category":doc['_source']['category'],
               "price":doc['_source']['price'],
               "gender_affinity":doc['_source']['gender_affinity'],
                "style":doc['_source']['style'],
                
                }
            if('max_score_dict_list_sorted' in doc):
                res_['max_score_dict_list_sorted'] = doc['max_score_dict_list_sorted']
            if('highlight' in doc):
                res_['highlight'] = doc['highlight']['product_description']
            if('NeuralSparse Search' in search_types):
                res_['sparse'] = doc['_source']['product_description_sparse_vector']
                res_['query_sparse'] = query_sparse_sorted_filtered
#             if(st.session_state.input_rekog_label !="" or st.session_state.input_is_rewrite_query == 'enabled'):
#                 res_['rekog'] = {'color':doc['_source']['rekog_color'],'category': doc['_source']['rekog_categories'],'objects':doc['_source']['rekog_objects']}
            
            res_['id'] = doc['_id']
            res_['score'] = doc['_score']
            res_['title'] = doc['_source']['product_description']
            
        
            arr.append(res_)
            dup.append(doc['_source']['image_url'])

    return arr[0:k_]