Gourisankar Padihary
commited on
Commit
·
79dcf63
1
Parent(s):
bd69eee
Compute attributes changes
Browse files- generator/compute_metrics.py +24 -0
- main.py +18 -14
generator/compute_metrics.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def compute_metrics(attributes):
|
2 |
+
# Extract relevant information from attributes
|
3 |
+
all_relevant_sentence_keys = attributes.get("all_relevant_sentence_keys", [])
|
4 |
+
all_utilized_sentence_keys = attributes.get("all_utilized_sentence_keys", [])
|
5 |
+
sentence_support_information = attributes.get("sentence_support_information", [])
|
6 |
+
|
7 |
+
# Compute Context Relevance
|
8 |
+
context_relevance = len(all_relevant_sentence_keys) / len(sentence_support_information) if sentence_support_information else 0
|
9 |
+
|
10 |
+
# Compute Context Utilization
|
11 |
+
context_utilization = len(all_utilized_sentence_keys) / len(sentence_support_information) if sentence_support_information else 0
|
12 |
+
|
13 |
+
# Compute Completeness
|
14 |
+
completeness = all(info.get("fully_supported", False) for info in sentence_support_information)
|
15 |
+
|
16 |
+
# Compute Adherence
|
17 |
+
adherence = attributes.get("overall_supported", False)
|
18 |
+
|
19 |
+
return {
|
20 |
+
"Context Relevance": context_relevance,
|
21 |
+
"Context Utilization": context_utilization,
|
22 |
+
"Completeness": completeness,
|
23 |
+
"Adherence": adherence
|
24 |
+
}
|
main.py
CHANGED
@@ -6,6 +6,7 @@ from retriever.retrieve_documents import retrieve_top_k_documents
|
|
6 |
from generator.initialize_llm import initialize_llm
|
7 |
from generator.generate_response import generate_response
|
8 |
from generator.extract_attributes import extract_attributes
|
|
|
9 |
|
10 |
# Configure logging
|
11 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -24,37 +25,36 @@ def main():
|
|
24 |
# Embed the documents
|
25 |
vector_store = embed_documents(documents)
|
26 |
logging.info("Documents embedded")
|
27 |
-
|
28 |
-
# Initialize the LLM
|
29 |
-
llm = initialize_llm()
|
30 |
-
logging.info("LLM initialized")
|
31 |
-
|
32 |
# Sample question
|
33 |
sample_question = dataset[0]['question']
|
34 |
logging.info(f"Sample question: {sample_question}")
|
35 |
|
36 |
# Retrieve relevant documents
|
37 |
relevant_docs = retrieve_top_k_documents(vector_store, sample_question, top_k=5)
|
38 |
-
logging.info("Relevant documents retrieved :
|
39 |
# Log each retrieved document individually
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Generate a response using the relevant documents
|
44 |
-
response, source_docs = generate_response(llm, vector_store, sample_question)
|
45 |
logging.info("Response generated")
|
46 |
|
47 |
# Print the response
|
48 |
-
print(f"Response: {response}")
|
49 |
-
print(f"Source Documents: {source_docs}")
|
50 |
|
51 |
-
|
52 |
-
attributes = extract_attributes(sample_question,
|
53 |
|
54 |
# Only proceed if the content is not empty
|
55 |
if attributes.content:
|
56 |
result_content = attributes.content # Access the content attribute
|
57 |
-
|
58 |
# Extract the JSON part from the result_content
|
59 |
json_start = result_content.find("{")
|
60 |
json_end = result_content.rfind("}") + 1
|
@@ -63,6 +63,10 @@ def main():
|
|
63 |
try:
|
64 |
result_json = json.loads(json_str)
|
65 |
print(json.dumps(result_json, indent=2))
|
|
|
|
|
|
|
|
|
66 |
except json.JSONDecodeError as e:
|
67 |
logging.error(f"JSONDecodeError: {e}")
|
68 |
|
|
|
6 |
from generator.initialize_llm import initialize_llm
|
7 |
from generator.generate_response import generate_response
|
8 |
from generator.extract_attributes import extract_attributes
|
9 |
+
from generator.compute_metrics import compute_metrics
|
10 |
|
11 |
# Configure logging
|
12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
25 |
# Embed the documents
|
26 |
vector_store = embed_documents(documents)
|
27 |
logging.info("Documents embedded")
|
28 |
+
|
|
|
|
|
|
|
|
|
29 |
# Sample question
|
30 |
sample_question = dataset[0]['question']
|
31 |
logging.info(f"Sample question: {sample_question}")
|
32 |
|
33 |
# Retrieve relevant documents
|
34 |
relevant_docs = retrieve_top_k_documents(vector_store, sample_question, top_k=5)
|
35 |
+
logging.info(f"Relevant documents retrieved :{len(relevant_docs)}")
|
36 |
# Log each retrieved document individually
|
37 |
+
for i, doc in enumerate(relevant_docs):
|
38 |
+
logging.info(f"Relevant document {i+1}: {doc} \n")
|
39 |
+
|
40 |
+
# Initialize the LLM
|
41 |
+
llm = initialize_llm()
|
42 |
+
logging.info("LLM initialized")
|
43 |
|
44 |
# Generate a response using the relevant documents
|
45 |
+
response, source_docs = generate_response(llm, vector_store, sample_question, relevant_docs)
|
46 |
logging.info("Response generated")
|
47 |
|
48 |
# Print the response
|
49 |
+
print(f"Response from LLM: {response}")
|
50 |
+
#print(f"Source Documents: {source_docs}")
|
51 |
|
52 |
+
# Valuations : Extract attributes from the response and source documents
|
53 |
+
attributes = extract_attributes(sample_question, source_docs, response)
|
54 |
|
55 |
# Only proceed if the content is not empty
|
56 |
if attributes.content:
|
57 |
result_content = attributes.content # Access the content attribute
|
|
|
58 |
# Extract the JSON part from the result_content
|
59 |
json_start = result_content.find("{")
|
60 |
json_end = result_content.rfind("}") + 1
|
|
|
63 |
try:
|
64 |
result_json = json.loads(json_str)
|
65 |
print(json.dumps(result_json, indent=2))
|
66 |
+
|
67 |
+
# Compute metrics using the extracted attributes
|
68 |
+
metrics = compute_metrics(result_json)
|
69 |
+
print(metrics)
|
70 |
except json.JSONDecodeError as e:
|
71 |
logging.error(f"JSONDecodeError: {e}")
|
72 |
|