-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
228 lines (181 loc) · 8.14 KB
/
utils.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
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
from collections import defaultdict
import traceback
import openai
from openai.error import OpenAIError
from tenacity import retry, stop_after_attempt, wait_random_exponential
import tiktoken
import streamlit as st
import pandas as pd
def generate_prompt(system_prompt, separator, context, question):
user_prompt = ""
if system_prompt:
user_prompt += system_prompt + separator
if context:
user_prompt += context + separator
if question:
user_prompt += question + separator
return user_prompt
def generate_chat_prompt(separator, context, question):
user_prompt = ""
if context:
user_prompt += context + separator
if question:
user_prompt += question + separator
return user_prompt
@retry(wait=wait_random_exponential(min=3, max=90), stop=stop_after_attempt(6))
def get_embeddings(text, embedding_model="text-embedding-ada-002"):
response = openai.Embedding.create(
model=embedding_model,
input=text,
)
embedding_vectors = response["data"][0]["embedding"]
return embedding_vectors
@retry(wait=wait_random_exponential(min=3, max=90), stop=stop_after_attempt(6))
def get_completion(config, user_prompt):
try:
response = openai.Completion.create(
model=config["model_name"],
prompt=user_prompt,
temperature=config["temperature"],
max_tokens=config["max_tokens"],
top_p=config["top_p"],
frequency_penalty=config["frequency_penalty"],
presence_penalty=config["presence_penalty"],
)
answer = response["choices"][0]["text"]
answer = answer.strip()
return answer
except OpenAIError as e:
func_name = traceback.extract_stack()[-1].name
st.error(f"Error in {func_name}:\n{type(e).__name__}=> {str(e)}")
@retry(wait=wait_random_exponential(min=3, max=90), stop=stop_after_attempt(6))
def get_chat_completion(config, system_prompt, question):
try:
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": question},
]
response = openai.ChatCompletion.create(
model=config["model_name"],
messages=messages,
temperature=config["temperature"],
max_tokens=config["max_tokens"],
top_p=config["top_p"],
frequency_penalty=config["frequency_penalty"],
presence_penalty=config["presence_penalty"],
)
answer = response["choices"][0]["message"]["content"]
answer = answer.strip()
return answer
except OpenAIError as e:
func_name = traceback.extract_stack()[-1].name
st.error(f"Error in {func_name}:\n{type(e).__name__}=> {str(e)}")
def context_chunking(context, threshold=512, chunk_overlap_limit=0):
encoding = tiktoken.encoding_for_model("text-embedding-ada-002")
contexts_lst = []
while len(encoding.encode(context)) > threshold:
context_temp = encoding.decode(encoding.encode(context)[:threshold])
contexts_lst.append(context_temp)
context = encoding.decode(
encoding.encode(context)[threshold - chunk_overlap_limit :]
)
if context:
contexts_lst.append(context)
return contexts_lst
def generate_csv_report(file, cols, criteria_dict, counter, config):
try:
df = pd.read_csv(file)
if "Questions" not in df.columns or "Contexts" not in df.columns:
raise ValueError(
"Missing Column Names in .csv file: `Questions` and `Contexts`"
)
final_df = pd.DataFrame(columns=cols)
hyperparameters = f"Temperature: {config['temperature']}\nTop P: {config['top_p']} \
\nMax Tokens: {config['max_tokens']}\nFrequency Penalty: {config['frequency_penalty']} \
\nPresence Penalty: {config['presence_penalty']}"
progress_text = "Generation in progress. Please wait..."
my_bar = st.progress(0, text=progress_text)
for idx, row in df.iterrows():
my_bar.progress((idx + 1) / len(df), text=progress_text)
question = row["Questions"]
context = row["Contexts"]
contexts_lst = context_chunking(context)
system_prompts_list = []
answers_list = []
for num in range(counter):
system_prompt_final = "system_prompt_" + str(num + 1)
system_prompts_list.append(eval(system_prompt_final))
if config["model_name"] in [
"text-davinci-003",
"gpt-3.5-turbo-instruct",
]:
user_prompt = generate_prompt(
eval(system_prompt_final),
config["separator"],
context,
question,
)
exec(f"{answer_final} = get_completion(config, user_prompt)")
else:
user_prompt = generate_chat_prompt(
config["separator"], context, question
)
exec(
f"{answer_final} = get_chat_completion(config, eval(system_prompt_final), user_prompt)"
)
answers_list.append(eval(answer_final))
from metrics import Metrics
metrics = Metrics(question, [context] * counter, answers_list, config)
rouge1, rouge2, rougeL = metrics.rouge_score()
rouge_scores = f"Rouge1: {rouge1}, Rouge2: {rouge2}, RougeL: {rougeL}"
metrics = Metrics(question, [contexts_lst] * counter, answers_list, config)
bleu = metrics.bleu_score()
bleu_scores = f"BLEU Score: {bleu}"
metrics = Metrics(question, [context] * counter, answers_list, config)
bert_f1 = metrics.bert_score()
bert_scores = f"BERT F1 Score: {bert_f1}"
answer_relevancy_scores = []
critique_scores = defaultdict(list)
faithfulness_scores = []
for num in range(counter):
answer_final = "answer_" + str(num + 1)
metrics = Metrics(
question, context, eval(answer_final), config, strictness=3
)
answer_relevancy_score = metrics.answer_relevancy()
answer_relevancy_scores.append(
f"Answer #{str(num+1)}: {answer_relevancy_score}"
)
for criteria_name, criteria_desc in criteria_dict.items():
critique_score = metrics.critique(criteria_desc, strictness=3)
critique_scores[criteria_name].append(
f"Answer #{str(num+1)}: {critique_score}"
)
faithfulness_score = metrics.faithfulness(strictness=3)
faithfulness_scores.append(
f"Answer #{str(num+1)}: {faithfulness_score}"
)
answer_relevancy_scores = ";\n".join(answer_relevancy_scores)
faithfulness_scores = ";\n".join(faithfulness_scores)
critique_scores_lst = []
for criteria_name in criteria_dict.keys():
score = ";\n".join(critique_scores[criteria_name])
critique_scores_lst.append(score)
final_df.loc[len(final_df)] = (
[question, context, config["model_name"], hyperparameters]
+ system_prompts_list
+ answers_list
+ [
rouge_scores,
bleu_scores,
bert_scores,
answer_relevancy_score,
faithfulness_score,
]
+ critique_scores_lst
)
my_bar.empty()
return final_df
except Exception as e:
func_name = traceback.extract_stack()[-1].name
st.error(f"Error in {func_name}: {str(e)}, {traceback.format_exc()}")