Spaces:
Running
Running
Saiteja Solleti
commited on
Commit
·
61bb151
1
Parent(s):
5fed436
last code change
Browse files- app.py +11 -3
- calculatescores.py +24 -7
- formatresultshelper.py +8 -11
app.py
CHANGED
@@ -8,7 +8,7 @@ from sentence_transformers import SentenceTransformer
|
|
8 |
from searchmilvushelper import SearchTopKDocuments
|
9 |
from finetuneresults import FineTuneAndRerankSearchResults
|
10 |
from generationhelper import GenerateAnswer
|
11 |
-
from formatresultshelper import
|
12 |
from calculatescores import CalculateScores
|
13 |
|
14 |
from model import generate_response
|
@@ -51,11 +51,19 @@ reranked_results = FineTuneAndRerankSearchResults(results_for_top10_chunks, rag_
|
|
51 |
|
52 |
answer = GenerateAnswer(query, reranked_results.head(3), PROMPT_MODEL)
|
53 |
|
54 |
-
completion_result =
|
55 |
|
|
|
|
|
|
|
|
|
|
|
56 |
print(completion_result)
|
57 |
|
58 |
-
|
|
|
|
|
|
|
59 |
|
60 |
print(score1)
|
61 |
print(score2)
|
|
|
8 |
from searchmilvushelper import SearchTopKDocuments
|
9 |
from finetuneresults import FineTuneAndRerankSearchResults
|
10 |
from generationhelper import GenerateAnswer
|
11 |
+
from formatresultshelper import FormatAndScores
|
12 |
from calculatescores import CalculateScores
|
13 |
|
14 |
from model import generate_response
|
|
|
51 |
|
52 |
answer = GenerateAnswer(query, reranked_results.head(3), PROMPT_MODEL)
|
53 |
|
54 |
+
completion_result,relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level = FormatAndScores(query, reranked_results.head(1), answer, EVAL_MODEL)
|
55 |
|
56 |
+
|
57 |
+
print(relevant_sentence_keys)
|
58 |
+
print(all_utilized_sentence_keys)
|
59 |
+
print(support_keys)
|
60 |
+
print(support_level)
|
61 |
print(completion_result)
|
62 |
|
63 |
+
document_id = reranked_results.head(1)['doc_id'].values[0]
|
64 |
+
extarcted_row_for_given_id = rag_extracted_data[rag_extracted_data["id"]==document_id]
|
65 |
+
|
66 |
+
score1, score2, score3 = CalculateScores(relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level,extarcted_row_for_given_id)
|
67 |
|
68 |
print(score1)
|
69 |
print(score2)
|
calculatescores.py
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
import formatresultshelper
|
|
|
|
|
|
|
2 |
|
3 |
#Defined as utilized documents / retrieved documents for the query
|
4 |
def compute_context_relevance(relevant_sentences, support_keys):
|
@@ -27,20 +30,20 @@ def compute_context_utilization(relevant_sentences, utilization_levels):
|
|
27 |
return total_utilization_score / total_relevant_sentences
|
28 |
|
29 |
|
30 |
-
def CalculateScores():
|
31 |
#compute Context Relevance
|
32 |
-
contextrel = compute_context_relevance(
|
33 |
print(f"Context Relevance = {contextrel}")
|
34 |
|
35 |
-
contextutil = compute_context_utilization(
|
36 |
print(f"Context Utilization = {contextutil}")
|
37 |
|
38 |
-
compnum = np.intersect1d(
|
39 |
-
completenes = compnum.size / len(
|
40 |
print(f"Completeness = {completenes}")
|
41 |
|
42 |
#Adherence : whether all parts of response are grounded by context
|
43 |
-
for val in
|
44 |
prevval = 1;
|
45 |
if val == False:
|
46 |
adherence = 0 * prevval
|
@@ -51,7 +54,21 @@ def CalculateScores():
|
|
51 |
|
52 |
print(f"Adherence = {adherence}")
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
def mse(actual, predicted):
|
57 |
return (actual - predicted)**2
|
|
|
1 |
import formatresultshelper
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
from sklearn.metrics import mean_squared_error, roc_auc_score
|
5 |
|
6 |
#Defined as utilized documents / retrieved documents for the query
|
7 |
def compute_context_relevance(relevant_sentences, support_keys):
|
|
|
30 |
return total_utilization_score / total_relevant_sentences
|
31 |
|
32 |
|
33 |
+
def CalculateScores(relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level,extarcted_row_for_given_id):
|
34 |
#compute Context Relevance
|
35 |
+
contextrel = compute_context_relevance(relevant_sentence_keys, support_keys)
|
36 |
print(f"Context Relevance = {contextrel}")
|
37 |
|
38 |
+
contextutil = compute_context_utilization(relevant_sentence_keys, all_utilized_sentence_keys)
|
39 |
print(f"Context Utilization = {contextutil}")
|
40 |
|
41 |
+
compnum = np.intersect1d(support_keys, all_utilized_sentence_keys)
|
42 |
+
completenes = compnum.size / len(support_keys)
|
43 |
print(f"Completeness = {completenes}")
|
44 |
|
45 |
#Adherence : whether all parts of response are grounded by context
|
46 |
+
for val in support_level:
|
47 |
prevval = 1;
|
48 |
if val == False:
|
49 |
adherence = 0 * prevval
|
|
|
54 |
|
55 |
print(f"Adherence = {adherence}")
|
56 |
|
57 |
+
context_relevance_score = extarcted_row_for_given_id['relevance_score'].values[0]
|
58 |
+
context_utilization_score = extarcted_row_for_given_id['utilization_score'].values[0]
|
59 |
+
adherence_score = float(extarcted_row_for_given_id['adherence_score'].values[0])
|
60 |
+
print(context_relevance_score)
|
61 |
+
print(context_utilization_score)
|
62 |
+
print(adherence_score)
|
63 |
+
docadherencearr = np.array([adherence_score, 0, 0])
|
64 |
+
adherencearr = np.array([adherence, 0, 0])
|
65 |
+
rmsecontextrel = mse(context_relevance_score, contextrel)
|
66 |
+
rmsecontextutil = mse(context_utilization_score, contextutil)
|
67 |
+
aucscore = roc_auc_score(docadherencearr, adherencearr)
|
68 |
+
|
69 |
+
return rmsecontextrel, rmsecontextutil, aucscore
|
70 |
+
|
71 |
+
|
72 |
|
73 |
def mse(actual, predicted):
|
74 |
return (actual - predicted)**2
|
formatresultshelper.py
CHANGED
@@ -28,7 +28,7 @@ def evaluate_response_with_prompt(templete, query, documents, answer, eval_model
|
|
28 |
|
29 |
return completion
|
30 |
|
31 |
-
def
|
32 |
templete= get_templet_to_calculatescores()
|
33 |
|
34 |
completion_results = evaluate_response_with_prompt(templete, query,documents, answer, eval_model)
|
@@ -62,23 +62,20 @@ def CalculateScoresBasedOnAnswer(query, documents, answer, eval_model):
|
|
62 |
sentence_support_information = data_json['sentence_support_information']
|
63 |
all_utilized_sentence_keys = data_json['all_utilized_sentence_keys']
|
64 |
|
65 |
-
print(relavance_explanation)
|
66 |
-
print(relevant_sentence_keys)
|
67 |
-
print(overall_supported_explanation)
|
68 |
-
print(overall_supported)
|
69 |
-
print(sentence_support_information)
|
70 |
-
print(all_utilized_sentence_keys)
|
71 |
-
|
72 |
support_keys = []
|
73 |
support_level = []
|
74 |
for sentence_support in sentence_support_information:
|
75 |
support_keys += sentence_support['supporting_sentence_keys']
|
76 |
support_level.append(sentence_support['fully_supported'])
|
77 |
|
78 |
-
print(
|
79 |
-
print(
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
return completion_results_response
|
82 |
|
83 |
|
84 |
|
|
|
28 |
|
29 |
return completion
|
30 |
|
31 |
+
def FormatAndScores(query, documents, answer, eval_model):
|
32 |
templete= get_templet_to_calculatescores()
|
33 |
|
34 |
completion_results = evaluate_response_with_prompt(templete, query,documents, answer, eval_model)
|
|
|
62 |
sentence_support_information = data_json['sentence_support_information']
|
63 |
all_utilized_sentence_keys = data_json['all_utilized_sentence_keys']
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
support_keys = []
|
66 |
support_level = []
|
67 |
for sentence_support in sentence_support_information:
|
68 |
support_keys += sentence_support['supporting_sentence_keys']
|
69 |
support_level.append(sentence_support['fully_supported'])
|
70 |
|
71 |
+
print(relavance_explanation)
|
72 |
+
print(relevant_sentence_keys)
|
73 |
+
print(overall_supported_explanation)
|
74 |
+
print(overall_supported)
|
75 |
+
print(sentence_support_information)
|
76 |
+
print(all_utilized_sentence_keys)
|
77 |
|
78 |
+
return completion_results_response,relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level
|
79 |
|
80 |
|
81 |
|