aibmedia commited on
Commit
5c1eb47
·
verified ·
1 Parent(s): e2fdf95

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +232 -21
main.py CHANGED
@@ -12,6 +12,7 @@ from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
12
 
13
 
14
 
 
15
  API_URL0 = "https://api-inference.huggingface.co/models/sentence-transformers/multi-qa-MiniLM-L6-cos-v1"
16
  API_URL1 = "https://api-inference.huggingface.co/models/sentence-transformers/all-mpnet-base-v2"
17
  API_URL2 = "https://api-inference.huggingface.co/models/sentence-transformers/all-roberta-large-v1"
@@ -50,12 +51,11 @@ async def server_1():
50
  query_sentence = "Obama's first name"
51
  duck_results = []
52
  try:
53
- searchduck = DuckDuckGoSearchResults(output_format="list" ,max_results=20, num_results=20)
54
  duck_results = searchduck.invoke(query_sentence)
55
  except:
56
  print("An exception occurred")
57
-
58
-
59
 
60
  tool = Tool(
61
  name="google_search",
@@ -94,7 +94,7 @@ async def server_1():
94
  response1 = requests.post(API_URL1, headers=headers, json=payload)
95
  response2 = requests.post(API_URL2, headers=headers, json=payload)
96
  response3 = requests.post(API_URL3, headers=headers, json=payload)
97
-
98
  print("type( response0.json() )")
99
  print(type( response0.json() ))
100
  print(type( response1.json() ))
@@ -102,13 +102,35 @@ async def server_1():
102
  print(type( response3.json() ))
103
  if type(response0.json()) == list and type(response1.json()) == list and type(response2.json()) == list and type(response3.json()) == list :
104
  similarity_scores = response0.json() + response1.json() + response2.json() + response3.json()
 
105
  else:
106
  similarity_scores = "There's an error in llm similarity search retrieval"
107
- return all_results
108
 
109
  time.sleep(4)
110
- print(similarity_scores)
111
- print(type(similarity_scores))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  print("length")
113
  print(len(similarity_scores))
114
  key_index = 0
@@ -124,13 +146,13 @@ async def server_1():
124
  print(value_inlist)
125
  print("index ")
126
  print(key_index)
127
- if key_index <= 8 :
128
  resp_list0.append(value_inlist)
129
- if key_index <= 17 and key_index > 8 :
130
  resp_list1.append(value_inlist)
131
- if key_index <= 26 and key_index > 17 :
132
  resp_list2.append(value_inlist)
133
- if key_index <= 35 and key_index > 26 :
134
  resp_list3.append(value_inlist)
135
  key_index = key_index + 1
136
 
@@ -294,6 +316,204 @@ async def server_1():
294
 
295
  else:
296
  print("No reliable similarity found by 4 llms")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  # index_sorted0 = sorted0_with_index[:4]
298
  # index_sorted1 = sorted1_with_index[:4]
299
  # index_sorted2 = sorted2_with_index[:4]
@@ -313,15 +533,6 @@ async def server_1():
313
  # the top 3 indexes must be above .78 similarity score
314
  # the top 3 must have occured 4 times or more in combined_indexes
315
 
316
- time.sleep(4)
317
- return all_results
318
-
319
-
320
- def threadserver():
321
- print('hi')
322
- os.system(' ./mxbai-embed-large-v1-f16.llamafile --server --nobrowser')
323
-
324
-
325
-
326
  if __name__ == '__main__':
327
  app.run(host='0.0.0.0', port=8080)
 
 
12
 
13
 
14
 
15
+
16
  API_URL0 = "https://api-inference.huggingface.co/models/sentence-transformers/multi-qa-MiniLM-L6-cos-v1"
17
  API_URL1 = "https://api-inference.huggingface.co/models/sentence-transformers/all-mpnet-base-v2"
18
  API_URL2 = "https://api-inference.huggingface.co/models/sentence-transformers/all-roberta-large-v1"
 
51
  query_sentence = "Obama's first name"
52
  duck_results = []
53
  try:
54
+ searchduck = DuckDuckGoSearchResults(output_format="list" ,max_results=5, num_results=5)
55
  duck_results = searchduck.invoke(query_sentence)
56
  except:
57
  print("An exception occurred")
58
+
 
59
 
60
  tool = Tool(
61
  name="google_search",
 
94
  response1 = requests.post(API_URL1, headers=headers, json=payload)
95
  response2 = requests.post(API_URL2, headers=headers, json=payload)
96
  response3 = requests.post(API_URL3, headers=headers, json=payload)
97
+ varcontinue_similarity = 0
98
  print("type( response0.json() )")
99
  print(type( response0.json() ))
100
  print(type( response1.json() ))
 
102
  print(type( response3.json() ))
103
  if type(response0.json()) == list and type(response1.json()) == list and type(response2.json()) == list and type(response3.json()) == list :
104
  similarity_scores = response0.json() + response1.json() + response2.json() + response3.json()
105
+ varcontinue_similarity = 1
106
  else:
107
  similarity_scores = "There's an error in llm similarity search retrieval"
108
+ return similarity_scores
109
 
110
  time.sleep(4)
111
+ result_processed = ""
112
+ ## if response is all list
113
+ if varcontinue_similarity == 1 :
114
+ # call processing with 10 google search result or 15 search results
115
+ if len(all_results) == 10 :
116
+ result_processed = process_similarity_10(all_results , similarity_scores )
117
+ if len(all_results) > 10 :
118
+ result_processed = process_similarity_15(all_results , similarity_scores )
119
+ # return all_results
120
+ return result_processed
121
+
122
+
123
+
124
+ def threadserver():
125
+ print('hi')
126
+ os.system(' ./mxbai-embed-large-v1-f16.llamafile --server --nobrowser')
127
+
128
+
129
+
130
+ def process_similarity_15(web_results , similarity_scores):
131
+
132
+ # print(similarity_scores)
133
+ # print(type(similarity_scores))
134
  print("length")
135
  print(len(similarity_scores))
136
  key_index = 0
 
146
  print(value_inlist)
147
  print("index ")
148
  print(key_index)
149
+ if key_index <= 14 :
150
  resp_list0.append(value_inlist)
151
+ if key_index <= 29 and key_index > 14 :
152
  resp_list1.append(value_inlist)
153
+ if key_index <= 44 and key_index > 29 :
154
  resp_list2.append(value_inlist)
155
+ if key_index <= 59 and key_index > 44 :
156
  resp_list3.append(value_inlist)
157
  key_index = key_index + 1
158
 
 
316
 
317
  else:
318
  print("No reliable similarity found by 4 llms")
319
+
320
+ return str( index_sorted0 ) + "," + str( index_sorted1 ) + "," + str( index_sorted2 ) + "," + str( index_sorted3 )
321
+
322
+
323
+
324
+ def process_similarity_10(web_results , similarity_scores):
325
+
326
+ # print(similarity_scores)
327
+ # print(type(similarity_scores))
328
+ print("length")
329
+ print(len(similarity_scores))
330
+ key_index = 0
331
+ #copy + loop to get index
332
+
333
+ r_iterator = 0
334
+ resp_list0 = []
335
+ resp_list1 = []
336
+ resp_list2 = []
337
+ resp_list3 = []
338
+
339
+ for value_inlist in similarity_scores:
340
+ print(value_inlist)
341
+ print("index ")
342
+ print(key_index)
343
+ if key_index <= 9 :
344
+ resp_list0.append(value_inlist)
345
+ if key_index <= 19 and key_index > 9 :
346
+ resp_list1.append(value_inlist)
347
+ if key_index <= 29 and key_index > 19 :
348
+ resp_list2.append(value_inlist)
349
+ if key_index <= 39 and key_index > 29 :
350
+ resp_list3.append(value_inlist)
351
+ key_index = key_index + 1
352
+
353
+ print("The Response list 0 ")
354
+ print(resp_list0)
355
+ print("The Response list 1 ")
356
+ print(resp_list1)
357
+ print("The Response list 2 ")
358
+ print(resp_list2)
359
+ print("The Response list 3 ")
360
+ print(resp_list3)
361
+ # sorted 0 - 3 are sorted lists of score ; we must get their indices which is 0-8 that will be mapped to sentence index
362
+ sorted0 = sorted(resp_list0 , reverse=True)
363
+ sorted1 = sorted(resp_list1 , reverse=True)
364
+ sorted2 = sorted(resp_list2 , reverse=True)
365
+ sorted3 = sorted(resp_list3 , reverse=True)
366
+
367
+ print("the sorted0-3")
368
+ print(sorted0)
369
+ print(sorted1)
370
+ print(sorted2)
371
+ print(sorted3)
372
+ print("end the sorted0-3")
373
+ # Get the index of the sorted list for resp_list0
374
+
375
+ sorted0_with_index = []
376
+ for x in sorted0:
377
+ for y in resp_list0:
378
+ if x == y:
379
+ print("index of sorted0")
380
+ print(resp_list0.index(y))
381
+ if x > 0.90:
382
+ sorted0_with_index.append(resp_list0.index(y))
383
+ print("sorted_with_index")
384
+ print(sorted0_with_index)
385
+ print("sorted0_with_index")
386
+ print(sorted0_with_index)
387
+ sorted1_with_index = []
388
+ for x in sorted1:
389
+ for y in resp_list1:
390
+ if x == y:
391
+ print("index of sorted1")
392
+ print(resp_list1.index(y))
393
+ if y > 0.90:
394
+ sorted1_with_index.append(resp_list1.index(y))
395
+ print("sorted_with_index")
396
+ print(sorted1_with_index)
397
+
398
+ print("sorted1_with_index")
399
+ print(sorted1_with_index)
400
+
401
+ sorted2_with_index = []
402
+ print("b4 for x in sorted2:")
403
+ print("resp_list2:" + str(resp_list2))
404
+ print("sorted:" + str(sorted2))
405
+ for x in sorted2:
406
+ for y in resp_list2:
407
+ if x == y:
408
+ print("index of sorted2")
409
+ print(resp_list2.index(y))
410
+ if y > 0.90:
411
+ sorted2_with_index.append(resp_list2.index(y))
412
+ print("sorted_with_index")
413
+ print(sorted2_with_index)
414
+
415
+ print("sorted2_with_index")
416
+ print(sorted2_with_index)
417
+ sorted3_with_index = []
418
+ print("b4 for x in sorted3:")
419
+ print("resp_list3:" + str(resp_list3))
420
+ for x in sorted3:
421
+ for y in resp_list3:
422
+ if x == y:
423
+ print("index of sorted3")
424
+ print(resp_list3.index(y))
425
+ if y > 0.90:
426
+ sorted3_with_index.append(resp_list3.index(y))
427
+ print("sorted_with_index")
428
+ print(sorted3_with_index)
429
+
430
+ print("sorted0-3_with_index")
431
+ print(sorted0_with_index)
432
+ print(sorted1_with_index)
433
+ print(sorted2_with_index)
434
+ print(sorted3_with_index)
435
+
436
+ index_sorted0 = [] ; index_sorted1 = [] ; index_sorted2 = [] ; index_sorted3 = []
437
+ # lines 158 onwards is about scenario when sorted0_with_index values are greater then .78
438
+ # then combine the top 5 values from each list to get the top 3 of 4 llm
439
+ varcontinue = False
440
+ # we will only continue if each llm has resulted with values greater than .78 & if these llm result list has at least 2
441
+
442
+ if ( len(sorted1_with_index) >= 2 and len(sorted2_with_index) >= 2 and len(sorted3_with_index) >= 2 ) or ( len(sorted0_with_index) >= 2 and len(sorted2_with_index) >= 2 and len(sorted3_with_index) >= 2 ) or ( len(sorted1_with_index) >= 2 and len(sorted0_with_index) >= 2 and len(sorted3_with_index) >= 2 ) or ( len(sorted1_with_index) >= 2 and len(sorted2_with_index) >= 2 and len(sorted0_with_index) >= 2 ):
443
+ # continue variable set to true
444
+ # indent this here # if( check if avarage of each any3 resp_list0-3 average is 0.85 or above ) !!then only continue!!!
445
+ varcontinue = True
446
+ print("continue variable set to true")
447
+ if ( len(sorted0_with_index) >= 2 and len(sorted1_with_index) >= 2 ) or ( len(sorted0_with_index) >= 2 and len(sorted2_with_index) >= 2 ) or ( len(sorted0_with_index) >= 2 and len(sorted3_with_index) >= 2 ) or ( len(sorted1_with_index) >= 2 and len(sorted2_with_index) >= 2 ) or ( len(sorted1_with_index) >= 2 and len(sorted3_with_index) >= 2 ) or ( len(sorted2_with_index) >= 2 and len(sorted3_with_index) >= 2 ):
448
+ # continue variable set to true
449
+ # if( check if avarage of any3 resp_list0-3 average is 0.85 or above )!!then only continue!!!
450
+ varcontinue = True
451
+ print("continue variable set to true")
452
+
453
+ # check if llm 1 - 3 has minimum 3
454
+
455
+ if varcontinue == True:
456
+ if len(sorted0_with_index) < 5 and len(sorted0_with_index) > 0 :
457
+ print("len(sorted0_with_index) < 5 and len(sorted0_with_index) > 0")
458
+ print(len(sorted0_with_index) < 5 and len(sorted0_with_index) > 0)
459
+ print("sorted0_with_index")
460
+ print(sorted0_with_index)
461
+ for x in sorted0_with_index :
462
+ index_sorted0.append(x)
463
+ remaining_padding = 5 - len(index_sorted0)
464
+ while remaining_padding > 0 :
465
+ remaining_padding= remaining_padding - 1
466
+ index_sorted0.append(index_sorted0[0])
467
+
468
+ if len(sorted1_with_index) < 5 and len(sorted1_with_index) > 0 :
469
+ print("len(sorted1_with_index) < 5 and len(sorted1_with_index) > 0")
470
+ print(len(sorted1_with_index) < 5 and len(sorted1_with_index) > 0)
471
+ print("sorted1_with_index")
472
+ print(sorted1_with_index)
473
+ for x in sorted1_with_index :
474
+ index_sorted1.append(x)
475
+ remaining_padding = 5 - len(index_sorted1)
476
+ while remaining_padding > 0 :
477
+ remaining_padding= remaining_padding - 1
478
+ index_sorted1.append(index_sorted1[0])
479
+
480
+ if len(sorted2_with_index) < 5 and len(sorted2_with_index) > 0 :
481
+ print("len(sorted2_with_index) < 5 and len(sorted2_with_index) > 0")
482
+ print(len(sorted2_with_index) < 5 and len(sorted2_with_index) > 0)
483
+ print("sorted2_with_index")
484
+ print(sorted2_with_index)
485
+ for x in sorted2_with_index :
486
+ index_sorted2.append(x)
487
+ remaining_padding = 5 - len(index_sorted2)
488
+ while remaining_padding > 0 :
489
+ remaining_padding= remaining_padding - 1
490
+ index_sorted2.append(index_sorted2[0])
491
+
492
+ if len(sorted3_with_index) < 5 and len(sorted3_with_index) > 0 :
493
+ print("len(sorted3_with_index) < 5 and len(sorted3_with_index) > 0")
494
+ print(len(sorted3_with_index) < 5 and len(sorted3_with_index) > 0)
495
+ print("sorted3_with_index")
496
+ print(sorted3_with_index)
497
+ for x in sorted3_with_index :
498
+ index_sorted3.append(x)
499
+ remaining_padding = 5 - len(index_sorted3)
500
+ while remaining_padding > 0 :
501
+ remaining_padding= remaining_padding - 1
502
+ index_sorted3.append(index_sorted3[0])
503
+
504
+ print("index_sorted0-1")
505
+ print(index_sorted0)
506
+ print(index_sorted1)
507
+ print(index_sorted2)
508
+ print(index_sorted3)
509
+
510
+
511
+ else:
512
+ print("No reliable similarity found by 4 llms")
513
+
514
+
515
+ return str( index_sorted0 ) + "," + str( index_sorted1 ) + "," + str( index_sorted2 ) + "," + str( index_sorted3 )
516
+
517
  # index_sorted0 = sorted0_with_index[:4]
518
  # index_sorted1 = sorted1_with_index[:4]
519
  # index_sorted2 = sorted2_with_index[:4]
 
533
  # the top 3 indexes must be above .78 similarity score
534
  # the top 3 must have occured 4 times or more in combined_indexes
535
 
 
 
 
 
 
 
 
 
 
 
536
  if __name__ == '__main__':
537
  app.run(host='0.0.0.0', port=8080)
538
+