35
35
from retriever .retriever import Retriever , get_relevant_docs , get_relevant_docs_mq
36
36
from utils .feedback import feedback_kwargs
37
37
38
- from langchain_nvidia_ai_endpoints import ChatNVIDIA
38
+ from langchain_nvidia_ai_endpoints import ChatNVIDIA , NVIDIARerank
39
39
from langchain_core .messages import HumanMessage
40
40
from langchain_core .output_parsers import StrOutputParser
41
41
from langchain_core .prompts import ChatPromptTemplate
@@ -400,52 +400,66 @@ def load_config(cfg_arg):
400
400
if rag_type == 1 :
401
401
augmented_queries = augment_multiple_query (transformed_query ["text" ])
402
402
queries = [transformed_query ["text" ]] + augmented_queries [2 :]
403
- print ("Queries are = " , queries )
403
+ # print("Queries are = ", queries)
404
404
retrieved_documents = []
405
405
retrieved_metadatas = []
406
+ relevant_docs = []
406
407
for query in queries :
407
408
ret_docs ,cons ,srcs = get_relevant_docs (CORE_DIR , query )
408
409
for doc in ret_docs :
409
410
retrieved_documents .append (doc .page_content )
410
411
retrieved_metadatas .append (doc .metadata ['source' ])
412
+ relevant_docs .append (doc )
411
413
print ("length of retrieved docs: " , len (retrieved_documents ))
412
414
#Remove all duplicated documents and retain the original metadata
413
415
unique_documents = []
414
416
unique_documents_metadata = []
415
- for document ,source in zip (retrieved_documents ,retrieved_metadatas ):
417
+ unique_relevant_documents = []
418
+ for idx , (document ,source ) in enumerate (zip (retrieved_documents ,retrieved_metadatas )):
416
419
if document not in unique_documents :
417
420
unique_documents .append (document )
418
421
unique_documents_metadata .append (source )
422
+ unique_relevant_documents .append (relevant_docs [idx ])
419
423
420
424
if len (retrieved_documents ) == 0 :
421
425
context = ""
422
426
print ("not context found context" )
423
427
else :
424
428
print ("length of unique docs: " , len (unique_documents ))
425
- #Instantiate the cross-encoder model and get scores for each retrieved document
426
- cross_encoder = CrossEncoder ('cross-encoder/ms-marco-MiniLM-L-6-v2' ) # ('BAAI/bge-reranker-large')('cross-encoder/ms-marco-MiniLM-L-6-v2')
427
- pairs = [[prompt , doc ] for doc in unique_documents ]
428
- scores = cross_encoder .predict (pairs )
429
- #Sort the scores from highest to least
430
- order_ids = np .argsort (scores )[::- 1 ]
431
- # print(order_ids)
429
+ #Instantiate the re-ranker model and get scores for each retrieved document
432
430
new_updated_documents = []
433
431
new_updated_sources = []
434
- #Get the top 6 scores
435
- if len (order_ids )>= 10 :
436
- for i in range (10 ):
437
- new_updated_documents .append (unique_documents [order_ids [i ]])
438
- new_updated_sources .append (unique_documents_metadata [order_ids [i ]])
432
+ if not config_yaml ['Reranker_NIM' ]:
433
+ print ("\n \n Reranking with Cross-encoder model: " , config_yaml ['reranker_model' ])
434
+ cross_encoder = CrossEncoder (config_yaml ['reranker_model' ])
435
+ pairs = [[prompt , doc ] for doc in unique_documents ]
436
+ scores = cross_encoder .predict (pairs )
437
+ #Sort the scores from highest to least
438
+ order_ids = np .argsort (scores )[::- 1 ]
439
+ #Get the top 10 scores
440
+ if len (order_ids )>= 10 :
441
+ for i in range (10 ):
442
+ new_updated_documents .append (unique_documents [order_ids [i ]])
443
+ new_updated_sources .append (unique_documents_metadata [order_ids [i ]])
444
+ else :
445
+ for i in range (len (order_ids )):
446
+ new_updated_documents .append (unique_documents [order_ids [i ]])
447
+ new_updated_sources .append (unique_documents_metadata [order_ids [i ]])
439
448
else :
440
- for i in range (len (order_ids )):
441
- new_updated_documents .append (unique_documents [order_ids [i ]])
442
- new_updated_sources .append (unique_documents_metadata [order_ids [i ]])
449
+ print ("\n \n Reranking with Retriever Text Reranking NIM model: " , config_yaml ["reranker_model_name" ])
450
+ # Initialize and connect to the running NeMo Retriever Text Reranking NIM
451
+ reranker = NVIDIARerank (model = config_yaml ["reranker_model_name" ],
452
+ base_url = config_yaml ["reranker_api_endpoint_url" ], top_n = 10 )
453
+ reranked_chunks = reranker .compress_documents (query = transformed_query ["text" ], documents = unique_relevant_documents )
454
+ for chunks in reranked_chunks :
455
+ metadata = chunks .metadata
456
+ page_content = chunks .page_content
457
+ new_updated_documents .append (page_content )
458
+ new_updated_sources .append (metadata ['source' ])
443
459
444
- print (new_updated_sources )
445
- print (len (new_updated_documents ))
460
+ print ("Reranking of completed for " , len (new_updated_documents ), " chunks" )
446
461
447
462
context = ""
448
- # sources = ""
449
463
sources = {}
450
464
for doc in new_updated_documents :
451
465
context += doc + "\n \n "
@@ -455,7 +469,7 @@ def load_config(cfg_arg):
455
469
sources [src ] = {"doc_content" : sources [src ]["doc_content" ]+ "\n \n " + new_updated_documents [i ], "doc_metadata" : src }
456
470
else :
457
471
sources [src ] = {"doc_content" : new_updated_documents [i ], "doc_metadata" : src }
458
- print ("length of source docs: " , len (sources ))
472
+ print ("Length of unique source docs: " , len (sources ))
459
473
#Send the top 10 results along with the query to LLM
460
474
461
475
if rag_type == 2 :
@@ -486,7 +500,7 @@ def load_config(cfg_arg):
486
500
487
501
print ("length of unique docs: " , len (unique_documents ))
488
502
#Instantiate the cross-encoder model and get scores for each retrieved document
489
- cross_encoder = CrossEncoder ('BAAI/bge-reranker-large' ) #('cross-encoder/ms-marco-MiniLM-L-6-v2' )
503
+ cross_encoder = CrossEncoder (config_yaml [ 'reranker_model' ] )
490
504
pairs = [[prompt , doc ] for doc in unique_documents ]
491
505
scores = cross_encoder .predict (pairs )
492
506
#Sort the scores from highest to least
@@ -544,7 +558,7 @@ def load_config(cfg_arg):
544
558
545
559
print ("length of unique docs: " , len (unique_documents ))
546
560
#Instantiate the cross-encoder model and get scores for each retrieved document
547
- cross_encoder = CrossEncoder ('cross-encoder/ms-marco-MiniLM-L-6-v2' ) #('BAAI/bge-reranker-large' )
561
+ cross_encoder = CrossEncoder (config_yaml [ 'reranker_model' ] )
548
562
pairs = [[prompt , doc ] for doc in unique_documents ]
549
563
scores = cross_encoder .predict (pairs )
550
564
#Sort the scores from highest to least
0 commit comments