File size: 2,019 Bytes
f7c2fa3
 
 
b1b2c27
79dcf63
 
 
 
b1b2c27
79dcf63
b1b2c27
79dcf63
 
f7c2fa3
79dcf63
f7c2fa3
 
 
 
 
 
79dcf63
cfb3435
 
79dcf63
 
 
 
f7c2fa3
79dcf63
f7c2fa3
 
 
 
b58a992
f7c2fa3
 
 
 
 
 
 
 
 
 
9bde774
f7c2fa3
 
 
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
import json
import logging

def compute_metrics(attributes, total_sentences):
    # Extract relevant information from attributes
    all_relevant_sentence_keys = attributes.get("all_relevant_sentence_keys", [])
    all_utilized_sentence_keys = attributes.get("all_utilized_sentence_keys", [])
    sentence_support_information = attributes.get("sentence_support_information", [])

    # Compute Context Relevance
    context_relevance = len(all_relevant_sentence_keys) / total_sentences if total_sentences else 0
    
    # Compute Context Utilization
    context_utilization = len(all_utilized_sentence_keys) / total_sentences if total_sentences else 0
    
    # Compute Completeness score
    Ri = set(all_relevant_sentence_keys)
    Ui = set(all_utilized_sentence_keys)

    completeness_score = len(Ri & Ui) / len(Ri) if len(Ri) else 0

    # Compute Adherence
    adherence = all(info.get("fully_supported", False) for info in sentence_support_information)
    #adherence = 1 if all(info.get("fully_supported", False) for info in sentence_support_information) else 0
    
    return {
        "Context Relevance": context_relevance,
        "Context Utilization": context_utilization,
        "Completeness Score": completeness_score,
        "Adherence": adherence
    }

def get_metrics(attributes, total_sentences):
    if attributes.content:
        #print(attributes)
        result_content = attributes.content  # Access the content attribute
        # Extract the JSON part from the result_content
        json_start = result_content.find("{")
        json_end = result_content.rfind("}") + 1
        json_str = result_content[json_start:json_end]
        
        try:
            result_json = json.loads(json_str)
            # Compute metrics using the extracted attributes
            metrics = compute_metrics(result_json, total_sentences)
            logging.info(metrics)
            return metrics        
        except json.JSONDecodeError as e:
            logging.error(f"JSONDecodeError: {e}")