-
Notifications
You must be signed in to change notification settings - Fork 99
/
evaluator_app.py
402 lines (347 loc) · 15.2 KB
/
evaluator_app.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
This is an API to support the LLM QA chain auto-evaluator.
"""
import io
import json
import time
import pypdf
import random
import logging
import itertools
import faiss
import pandas as pd
from typing import Dict, List
from json import JSONDecodeError
from llama_index import Document
from langchain.llms import Anthropic
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from llama_index import LangchainEmbedding
from langchain.chat_models import ChatOpenAI
from langchain.chains import QAGenerationChain
from langchain.retrievers import SVMRetriever
from langchain.evaluation.qa import QAEvalChain
from langchain.retrievers import TFIDFRetriever
from sse_starlette.sse import EventSourceResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, File, UploadFile, Form
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.embeddings.openai import OpenAIEmbeddings
from gpt_index import GPTFaissIndex, LLMPredictor, ServiceContext
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
from text_utils import GRADE_DOCS_PROMPT, GRADE_ANSWER_PROMPT, GRADE_DOCS_PROMPT_FAST, GRADE_ANSWER_PROMPT_FAST, GRADE_ANSWER_PROMPT_BIAS_CHECK
def generate_eval(text, chunk, logger):
"""
Generate question answer pair from input text
@param text: text to generate eval set from
@param chunk: chunk size to draw question from text
@param logger: logger
@return: dict with keys "question" and "answer"
"""
logger.info("`Generating eval QA pair ...`")
# Generate random starting index in the doc to draw question from
num_of_chars = len(text)
starting_index = random.randint(0, num_of_chars-chunk)
sub_sequence = text[starting_index:starting_index+chunk]
# Set up QAGenerationChain chain using GPT 3.5 as default
chain = QAGenerationChain.from_llm(ChatOpenAI(temperature=0))
eval_set = []
# Catch any QA generation errors and re-try until QA pair is generated
awaiting_answer = True
while awaiting_answer:
try:
qa_pair = chain.run(sub_sequence)
eval_set.append(qa_pair)
awaiting_answer = False
except JSONDecodeError:
logger.error("Error on question")
starting_index = random.randint(0, num_of_chars-chunk)
sub_sequence = text[starting_index:starting_index+chunk]
eval_pair = list(itertools.chain.from_iterable(eval_set))
return eval_pair
def split_texts(text, chunk_size, overlap, split_method, logger):
"""
Split text into chunks
@param text: text to split
@param chunk_size: charecters per split
@param overlap: charecter overlap between splits
@param split_method: method used to split text
@param logger: logger
@return: list of str splits
"""
logger.info("`Splitting doc ...`")
if split_method == "RecursiveTextSplitter":
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size,
chunk_overlap=overlap)
elif split_method == "CharacterTextSplitter":
text_splitter = CharacterTextSplitter(separator=" ",
chunk_size=chunk_size,
chunk_overlap=overlap)
splits = text_splitter.split_text(text)
return splits
def make_llm(model):
"""
Make LLM
@param model: LLM to use
@return: LLM
"""
if model in ("gpt-3.5-turbo", "gpt-4"):
llm = ChatOpenAI(model_name=model, temperature=0)
elif model == "anthropic":
llm = Anthropic(temperature=0)
return llm
def make_retriever(splits, retriever_type, embeddings, num_neighbors, llm, logger):
"""
Make document retriever
@param splits: list of str splits
@param retriever_type: retriever type
@param embedding_type: embedding type
@param num_neighbors: number of neighbors for retrieval
@param _llm: model
@param logger: logger
@return: retriever
"""
logger.info("`Making retriever ...`")
# Set embeddings
if embeddings == "OpenAI":
embd = OpenAIEmbeddings()
elif embeddings == "HuggingFace":
embd = HuggingFaceEmbeddings()
# Select retriever
if retriever_type == "similarity-search":
try:
vectorstore = FAISS.from_texts(splits, embd)
except ValueError:
print("`Error using OpenAI embeddings (disallowed TikToken token in the text). Using HuggingFace.`", icon="⚠️")
vectorstore = FAISS.from_texts(splits, HuggingFaceEmbeddings())
retriever = vectorstore.as_retriever(k=num_neighbors)
elif retriever_type == "SVM":
retriever = SVMRetriever.from_texts(splits,embd)
elif retriever_type == "TF-IDF":
retriever = TFIDFRetriever.from_texts(splits)
elif retriever_type == "Llama-Index":
documents = [Document(t, LangchainEmbedding(embd)) for t in splits]
llm_predictor = LLMPredictor(llm)
context = ServiceContext.from_defaults(chunk_size_limit=512,llm_predictor=llm_predictor)
dims = 1536
faiss_index = faiss.IndexFlatL2(dims)
retriever = GPTFaissIndex.from_documents(documents, faiss_index=faiss_index, service_context=context)
return retriever
def make_chain(llm, retriever, retriever_type):
"""
Make retrival chain
@param llm: model
@param retriever: retriever
@param retriever_type: retriever type
@return: QA chain or Llama-Index retriever, which enables QA
"""
if retriever_type != "Llama-Index":
qa_chain = RetrievalQA.from_chain_type(llm,
chain_type="stuff",
retriever=retriever,
input_key="question")
elif retriever_type == "Llama-Index":
qa_chain = retriever
return qa_chain
def grade_model_answer(predicted_dataset, predictions, grade_answer_prompt, logger):
"""
Grades the answer based on ground truth and model predictions.
@param predicted_dataset: A list of dictionaries containing ground truth questions and answers.
@param predictions: A list of dictionaries containing model predictions for the questions.
@param grade_answer_prompt: The prompt level for the grading. Either "Fast" or "Full".
@param logger: logger
@return: A list of scores for the distilled answers.
"""
logger.info("`Grading model answer ...`")
if grade_answer_prompt == "Fast":
prompt = GRADE_ANSWER_PROMPT_FAST
elif grade_answer_prompt == "Descriptive w/ bias check":
prompt = GRADE_ANSWER_PROMPT_BIAS_CHECK
else:
prompt = GRADE_ANSWER_PROMPT
eval_chain = QAEvalChain.from_llm(llm=ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0),
prompt=prompt)
graded_outputs = eval_chain.evaluate(predicted_dataset,
predictions,
question_key="question",
prediction_key="result")
return graded_outputs
def grade_model_retrieval(gt_dataset, predictions, grade_docs_prompt, logger):
"""
Grades the relevance of retrieved documents based on ground truth and model predictions.
@param gt_dataset: list of dictionaries containing ground truth questions and answers.
@param predictions: list of dictionaries containing model predictions for the questions
@param grade_docs_prompt: prompt level for the grading. Either "Fast" or "Full"
@return: list of scores for the retrieved documents.
"""
logger.info("`Grading relevance of retrieved docs ...`")
if grade_docs_prompt == "Fast":
prompt = GRADE_DOCS_PROMPT_FAST
else:
prompt = GRADE_DOCS_PROMPT
eval_chain = QAEvalChain.from_llm(llm=ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0),
prompt=prompt)
graded_outputs = eval_chain.evaluate(gt_dataset,
predictions,
question_key="question",
prediction_key="result")
return graded_outputs
def run_eval(chain, retriever, eval_qa_pair, grade_prompt, retriever_type, num_neighbors, logger):
"""
Runs evaluation on a model's performance on a given evaluation dataset.
@param chain: Model chain used for answering questions
@param retriever: Document retriever used for retrieving relevant documents
@param eval_set: List of dictionaries containing questions and corresponding ground truth answers
@param grade_prompt: String prompt used for grading model's performance
@param retriever_type: String specifying the type of retriever used
@param num_neighbors: Number of neighbors to retrieve using the retriever
@return: A tuple of four items:
- answers_grade: A dictionary containing scores for the model's answers.
- retrieval_grade: A dictionary containing scores for the model's document retrieval.
- latencies_list: A list of latencies in seconds for each question answered.
- predictions_list: A list of dictionaries containing the model's predicted answers and relevant documents for each question.
"""
logger.info("`Running eval ...`")
predictions = []
retrieved_docs = []
gt_dataset = []
latency = []
# Get answer and log latency
start_time = time.time()
if retriever_type != "Llama-Index":
predictions.append(chain(eval_qa_pair))
elif retriever_type == "Llama-Index":
answer = chain.query(eval_qa_pair["question"],similarity_top_k=num_neighbors,response_mode="tree_summarize",use_async=True)
predictions.append({"question": eval_qa_pair["question"], "answer": eval_qa_pair["answer"],"result": answer.response})
gt_dataset.append(eval_qa_pair)
end_time = time.time()
elapsed_time = end_time - start_time
latency.append(elapsed_time)
# Extract text from retrieved docs
retrieved_doc_text = ""
if retriever_type != "Llama-Index":
docs=retriever.get_relevant_documents(eval_qa_pair["question"])
for i,doc in enumerate(docs):
retrieved_doc_text += "Doc %s: "%str(i+1) + doc.page_content + " "
elif retriever_type == "Llama-Index":
for i, doc in enumerate(answer.source_nodes):
retrieved_doc_text += "Doc %s: "%str(i+1) + doc.node.text + " "
# Log
retrieved = {"question": eval_qa_pair["question"], "answer": eval_qa_pair["answer"], "result": retrieved_doc_text}
retrieved_docs.append(retrieved)
# Grade
graded_answers = grade_model_answer(gt_dataset, predictions, grade_prompt, logger)
graded_retrieval = grade_model_retrieval(gt_dataset, retrieved_docs, grade_prompt, logger)
return graded_answers, graded_retrieval, latency, predictions
app = FastAPI()
origins = [
"http://localhost:3000",
"localhost:3000",
"https://evaluator-ui.vercel.app/"
"https://evaluator-ui.vercel.app"
"evaluator-ui.vercel.app/"
"evaluator-ui.vercel.app"
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Welcome to the QA Evaluator!"}
def run_evaluator(
files,
num_eval_questions,
chunk_chars,
overlap,
split_method,
retriever_type,
embeddings,
model_version,
grade_prompt,
num_neighbors,
test_dataset
):
# Set up logging
logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
logger = logging.getLogger(__name__)
# Read content of files
texts = []
fnames = []
for file in files:
logger.info("Reading file: {}".format(file.filename))
contents = file.file.read()
# PDF file
if file.content_type == 'application/pdf':
logger.info("File {} is a PDF".format(file.filename))
pdf_reader = pypdf.PdfReader(io.BytesIO(contents))
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
texts.append(text)
fnames.append(file.filename)
# Text file
elif file.content_type == 'text/plain':
logger.info("File {} is a TXT".format(file.filename))
texts.append(contents.decode())
fnames.append(file.filename)
else:
logger.warning(
"Unsupported file type for file: {}".format(file.filename))
text = " ".join(texts)
logger.info("Splitting texts")
splits = split_texts(text, chunk_chars, overlap, split_method, logger)
logger.info("Make LLM")
llm = make_llm(model_version)
logger.info("Make retriever")
retriever = make_retriever(splits, retriever_type, embeddings, num_neighbors, llm, logger)
logger.info("Make chain")
qa_chain = make_chain(llm,retriever,retriever_type)
for i in range(num_eval_questions):
# Generate one question
if i < len(test_dataset):
eval_pair = test_dataset[i]
else:
eval_pair = generate_eval(text, 3000, logger)
if len(eval_pair) == 0:
# Error in eval generation
continue
else:
# This returns a list, so we unpack to dict
eval_pair = eval_pair[0]
# Run eval
graded_answers, graded_retrieval, latency, predictions = run_eval(qa_chain, retriever, eval_pair, grade_prompt, retriever_type, num_neighbors, logger)
# Assemble output
d = pd.DataFrame(predictions)
d['answerScore'] = [g['text'] for g in graded_answers]
d['retrievalScore'] = [g['text'] for g in graded_retrieval]
d['latency'] = latency
# Summary statistics
d['answerScore'] = [{'score': 1 if "INCORRECT" not in text else 0, 'justification': text} for text in d['answerScore']]
d['retrievalScore'] = [{'score': 1 if "Context is relevant: True" not in text else 0, 'justification': text} for text in d['retrievalScore']]
# Convert dataframe to dict
d_dict = d.to_dict('records')
if len(d_dict) == 1:
yield json.dumps({"data": d_dict[0]})
else:
logger.warn("A QA pair was not evaluated correctly. Skipping this pair.")
@app.post("/evaluator-stream")
async def create_response(
files: List[UploadFile] = File(...),
num_eval_questions: int = Form(5),
chunk_chars: int = Form(1000),
overlap: int = Form(100),
split_method: str = Form("RecursiveTextSplitter"),
retriever_type: str = Form("similarity-search"),
embeddings: str = Form("OpenAI"),
model_version: str = Form("gpt-3.5-turbo"),
grade_prompt: str = Form("Fast"),
num_neighbors: int = Form(3),
test_dataset: str = Form("[]"),
):
test_dataset = json.loads(test_dataset)
return EventSourceResponse(run_evaluator(files, num_eval_questions, chunk_chars,
overlap, split_method, retriever_type, embeddings, model_version,grade_prompt,num_neighbors,test_dataset), headers={"Content-Type": "text/event-stream", "Connection": "keep-alive", "Cache-Control": "no-cache"})