-
Notifications
You must be signed in to change notification settings - Fork 9
/
rag-eval.py
141 lines (120 loc) · 4.7 KB
/
rag-eval.py
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
import random
import pandas as pd
import os
import sys
sys.path.append('..')
import openai
openai.api_key = os.environ['OPENAI_API_KEY']
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_community.retrievers import BM25Retriever
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from common import *
from eval import Eval
import threading
from retrying import retry
from func_timeout import func_set_timeout
from config import *
class ThreadSafeDict:
def __init__(self):
self.lock = threading.Lock()
self.data = {}
def get(self, key):
with self.lock:
return self.data.get(key)
def set(self, key, value):
with self.lock:
self.data[key] = value
GLOBAL_RETRIEVAL_CACHE = ThreadSafeDict()
def format_few_shot(data):
ref_str = nr.fewshot(data)
system_prompt = FUNCTION_UTILS[question_type]['few_shot_prompt'].format(reference=ref_str)
user_prompt = FUNCTION_UTILS[question_type]['format_fn'](data)
return [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
def format_reflection(data):
preds = data['Preds']
ans = data['PredAnswers']
ans_ref_str = ''
for i in range(len(ans)):
ans_ref_str += f"Answer {i+1}: {ans[i]}\nReason: {preds[i]}\n\n"
user_prompt = REFLECTION.format(question=data['question'], options=format_options(data['options']), answers=ans_ref_str)
return [{"role": "user", "content": user_prompt}]
def format_reflection_value(data):
preds = data['Preds']
ans = data['PredAnswers']
ans_ref_str = ''
for i in range(len(ans)):
ans_ref_str += f"Answer {i+1}: {ans[i]}\nReason: {preds[i]}\n\n"
user_prompt = REFLECTION_VALUE.format(question=data['question'], answers=ans_ref_str)
return [{"role": "user", "content": user_prompt}]
class NearestReference:
def __init__(self, k=4, embed_type='faiss') -> None:
self.vectorstore = None
self.selector = None
self.k = k
self.embed_type = embed_type
def read_data(self, data_path):
df = pd.read_csv(data_path)
examples = [row.to_dict() for _, row in df.iterrows()]
return examples
def embed_data_path(self, data_path):
data = self.read_data(data_path)
return self.embed_data(data)
def embed_data(self, data):
data_str = [row['question'] for row in data]
print(f'embed_type: {self.embed_type}')
if self.embed_type == 'bm25':
self.retriever = BM25Retriever.from_texts(data_str, metadatas=data)
self.retriever.k = self.k
else:
self.vectorstore = FAISS.from_texts(data_str, embed, metadatas=data)
self.retriever = SemanticSimilarityExampleSelector(
vectorstore=self.vectorstore, k=self.k
)
return self.retriever
@retry
@func_set_timeout(5)
def retrieve(self, question):
if self.embed_type == 'bm25':
res = self.retriever.get_relevant_documents(question)
res = [r.metadata for r in res]
else:
res = self.retriever.select_examples({'question': question})
return res
def fewshot(self, question):
ref = self.retrieve(question['question'])
ref_str = ''
for i, r in enumerate(ref):
ref_str += f"Example {i+1}:\n{format_question_and_answer(r)}\n\n"
return ref_str
if __name__ == '__main__':
task_list = ['ConvFinQA']
for task in task_list:
model='gpt-4o-mini'
assert task in TASK_CONFIG and model in MODELS_CONFIG
embed_type = 'faiss'
question_type = TASK_CONFIG[task]
label_path = f'data/{task}/labeled.csv'
infer_path = f'data/{task}/test.csv'
eval_config = EVAL_UTILS[question_type]
eval_config['format_fn'] = format_few_shot
model_config = MODELS_CONFIG[model]
os.environ['LLM_BASE_URL'] = model_config["url"]
infer_config = {
'type': model_config["method"],
'task': task,
'config': {
"model": model_config['name'],
"temperature": 0.5,
"max_tokens": 1000,
}
}
save_path = f'data/{task}/pseudo_{model}.csv'
embed = OpenAIEmbeddings(model="text-embedding-3-small")
nr = NearestReference(k=1, embed_type=embed_type)
nr.embed_data_path(label_path)
infer_df = pd.read_csv(infer_path)
infer_data = [row.to_dict() for _, row in infer_df.iterrows()]
eval = Eval(examples=infer_data, **infer_config)
eval_acc = eval.eval(**eval_config)
print(f'Inference Accuracy: {eval_acc}')