|
| 1 | +##Ablation 1:Validating QUILL's reranker is useful i.e. ppl1,ppl2,avg,novelty |
| 2 | + |
| 3 | +##vanilla:No rerank i.e. Top1 recalled based on similarity |
| 4 | +##ppl1:Compute the following ppl Given the above text |
| 5 | +##ppl2:Compute the following ppl Given the above text and first k words of the quote |
| 6 | +##Other rerankers:Supervised(BM25、monoT5) Unsupervised(UPR、BGE) GPT(GPT3.5-turbo、GPT4o) |
| 7 | + |
| 8 | +########### |
| 9 | +from rag.rag_module import MyVectorDBConnector |
| 10 | +from rag.rag_function import retrieval |
| 11 | +import pandas as pd |
| 12 | +import argparse |
| 13 | +from tqdm import tqdm |
| 14 | +import os |
| 15 | +from eval.rerank_dcg import ndcg_at_k |
| 16 | +from eval.rerank_score import mrr_score,hits_at_k |
| 17 | +from reranker.chatgpt import gpt_rerank |
| 18 | +from reranker.bge import model_bge,bge_rerank |
| 19 | +from reranker.upr import model_upr,upr_rerank |
| 20 | +from reranker.bm25 import model_bm25,bm25_rerank |
| 21 | +from reranker.monoT5 import model_monoT5,monoT5_rerank |
| 22 | +from reranker.cal_feature import * |
| 23 | +from utils.utils import * |
| 24 | +from app.app_compute import * |
| 25 | + |
| 26 | +vector = MyVectorDBConnector(path='QUILL/code/rag/model/quill_final', collection_name='quill_final') |
| 27 | + |
| 28 | +def rerank_fn(reranker,old_context,topk_list,ppl_fun=None): |
| 29 | + try: |
| 30 | + if ppl_fun==None: |
| 31 | + return topk_list |
| 32 | + if ppl_fun==gpt_rerank: |
| 33 | + return gpt_rerank(topk_list,old_context) |
| 34 | + if ppl_fun==bge_rerank: |
| 35 | + return bge_rerank(reranker,topk_list,old_context) |
| 36 | + if ppl_fun==upr_rerank: |
| 37 | + return upr_rerank(reranker,old_context,topk_list) |
| 38 | + if ppl_fun==bm25_rerank: |
| 39 | + return bm25_rerank(reranker,old_context,topk_list) |
| 40 | + if ppl_fun==monoT5_rerank: |
| 41 | + return monoT5_rerank(reranker,old_context,topk_list) |
| 42 | + except Exception as e: |
| 43 | + print('error',e) |
| 44 | + return ['error'*5] |
| 45 | + try: |
| 46 | + if isinstance(topk_list[0],str): |
| 47 | + topk_list[0]=eval(topk_list[0]) |
| 48 | + rerank_list=sorted(topk_list[0], key=lambda x: ppl_fun(context=old_context,string=x), reverse=False) |
| 49 | + print('rerank',str(rerank_list)) |
| 50 | + return rerank_list |
| 51 | + except Exception as e: |
| 52 | + print('error',e) |
| 53 | + return ['error'*5] |
| 54 | + |
| 55 | +def ablation(reranker,data_info,ppl_fun,index): |
| 56 | + query = data_info['挖空语料-插入点'] |
| 57 | + golden_author = data_info['作者'] |
| 58 | + golden_quote = data_info['引言'] |
| 59 | + print("Query: " + query) |
| 60 | + topk_list = retrieval(vector,query, 5,golden_author) |
| 61 | + print('The retrieval Top K:',str(topk_list)) |
| 62 | + if ppl_fun == 'avg': |
| 63 | + ppl_fun = cal_feature_avg |
| 64 | + elif ppl_fun == 'ppl1': |
| 65 | + ppl_fun = cal_feature_ppl1 |
| 66 | + elif ppl_fun == 'ppl2': |
| 67 | + ppl_fun = cal_feature_ppl2 |
| 68 | + elif ppl_fun == 'vanilla': |
| 69 | + ppl_fun = None |
| 70 | + elif ppl_fun == 'ppl1_novelty': |
| 71 | + ppl_fun = cal_feature_ppl1_novelty |
| 72 | + elif ppl_fun == 'ppl2_novelty': |
| 73 | + ppl_fun = cal_feature_ppl2_novelty |
| 74 | + elif ppl_fun == 'avg_novelty': |
| 75 | + ppl_fun = cal_feature_avg_novelty |
| 76 | + elif ppl_fun == 'chatgpt': |
| 77 | + ppl_fun = gpt_rerank |
| 78 | + elif ppl_fun == 'bge': |
| 79 | + ppl_fun = bge_rerank |
| 80 | + elif ppl_fun == 'upr': |
| 81 | + ppl_fun = upr_rerank |
| 82 | + elif ppl_fun == 'bm25': |
| 83 | + ppl_fun = bm25_rerank |
| 84 | + elif ppl_fun == 'monoT5': |
| 85 | + ppl_fun = monoT5_rerank |
| 86 | + rerank_list = rerank_fn(reranker,query, topk_list, ppl_fun) |
| 87 | + if ppl_fun == None: |
| 88 | + rerank_list = rerank_list[0] |
| 89 | + quote = rerank_list[0] |
| 90 | + mrr=mrr_score(golden_quote,rerank_list) |
| 91 | + hit1=hits_at_k(golden_quote,rerank_list,1) |
| 92 | + hit3=hits_at_k(golden_quote,rerank_list,3) |
| 93 | + ndcg_1=ndcg_at_k(rerank_list,Search_quote_rel,index,k=1) |
| 94 | + ndcg_3=ndcg_at_k(rerank_list,Search_quote_rel,index,k=3) |
| 95 | + return quote,mrr,hit1,hit3,ndcg_1,ndcg_3,rerank_list |
| 96 | + |
| 97 | +def main(args): |
| 98 | + file_name=args.file_name |
| 99 | + ppl_fun=args.rerank_fun |
| 100 | + if ppl_fun == 'bge': |
| 101 | + reranker=model_bge() |
| 102 | + elif ppl_fun == 'upr': |
| 103 | + reranker=model_upr() |
| 104 | + elif ppl_fun == 'bm25': |
| 105 | + reranker=model_bm25() |
| 106 | + elif ppl_fun == 'monoT5': |
| 107 | + reranker=model_monoT5() |
| 108 | + else: |
| 109 | + reranker=None |
| 110 | + file_path = f'QUILL/data/dev/{file_name}.xlsx' |
| 111 | + df = pd.read_excel(file_path) |
| 112 | + rerank_quote=[] |
| 113 | + mrr_list=[] |
| 114 | + hit1_list=[] |
| 115 | + hit3_list=[] |
| 116 | + ndcg1_list = [] |
| 117 | + ndcg3_list = [] |
| 118 | + rerank_all_list =[] |
| 119 | + for index, row in tqdm(df.iterrows(), total=df.shape[0]): |
| 120 | + quote,mrr,hit1,hit3,ndcg_1,ndcg_3,rerank_list=ablation(reranker,row,ppl_fun,index) |
| 121 | + rerank_quote.append(quote) |
| 122 | + mrr_list.append(mrr) |
| 123 | + hit1_list.append(hit1) |
| 124 | + hit3_list.append(hit3) |
| 125 | + ndcg1_list.append(ndcg_1) |
| 126 | + ndcg3_list.append(ndcg_3) |
| 127 | + rerank_all_list.append(rerank_list) |
| 128 | + df['rerank_all']=rerank_all_list |
| 129 | + df['rerank_quote']=rerank_quote |
| 130 | + df['mrr']=mrr_list |
| 131 | + df['hit1']=hit1_list |
| 132 | + df['hit3']=hit3_list |
| 133 | + df['dcg1']=ndcg1_list |
| 134 | + df['dcg3']=ndcg3_list |
| 135 | + file_path = f'/QUILL/data/eval/ablation/res_{file_name}_{ppl_fun}.xlsx' |
| 136 | + directory = os.path.dirname(file_path) |
| 137 | + if not os.path.exists(directory): |
| 138 | + os.makedirs(directory) |
| 139 | + df.to_excel(file_path, index=False) |
| 140 | + print("The new Excel file is saved!!!") |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + parser = argparse.ArgumentParser(description="Ablation Pipeline") |
| 146 | + |
| 147 | + parser.add_argument('--rerank_fun', type=str, required=True, help="ablation index") |
| 148 | + parser.add_argument('--file_name', type=str, required=True, help="dev file name") |
| 149 | + args = parser.parse_args() |
| 150 | + |
| 151 | + main(args) |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
0 commit comments