File size: 10,882 Bytes
563c5bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
Copyright 2024 Infosys Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''

from llm_explain.service.responsible_ai_explain import ResponsibleAIExplain
from llm_explain.config.logger import CustomLogger
from llm_explain.utility.utility import Utils
from llm_explain.mappers.mappers import UncertainityResponse, TokenImportanceRequest, TokenImportanceResponse, SafeSearchResponse, \
    GoTResponse, GoTRequest, UncertainityRequest, SentimentAnalysisRequest, SentimentAnalysisResponse
import pandas as pd
import joblib
import time

log = CustomLogger()

class Payload:
    def __init__(self, **entries):
        self.__dict__.update(entries)

class ExplainService:
    async def calculate_uncertainty(payload : dict):
        """
        Asynchronously calculate uncertainty metrics for a given response object.
    
        Parameters:
            response_object (dict): The response object containing multiple choices.
            max_tokens (int or None): Maximum number of tokens to consider for the partial string.
            status_text (str or None): Optional status text for progress updates.
        
        Returns:
            dict: Dictionary containing lists of entropies, distances, and the mean choice-level distance.
        """
        try:
            n = payload.choices
            prompt = payload.inputPrompt
            
            response = await ResponsibleAIExplain.calculate_uncertainty(n,prompt)

            return UncertainityResponse(**response)
        except Exception as e:
            log.error(e,exc_info=True)
            raise Exception
    
    async def token_importance(payload: TokenImportanceRequest) -> TokenImportanceResponse:
        try:
            log.debug(f"payload: {payload}")
            prompt = payload.inputPrompt
            modelName = payload.modelName

            separated_words = prompt.split()
            if len(separated_words) <= 2:
                modelName = 'code'

            if modelName == "code":
                try:
                    gpt3tokenizer = joblib.load("../models/gpt3tokenizer.pkl")
                    
                    importance_map_log_df, total_time = await ResponsibleAIExplain.process_importance(Utils.ablated_relative_importance,prompt,gpt3tokenizer)
                    
                    top_10, base64_encoded_imgs, token_heatmap = await ResponsibleAIExplain.analyze_heatmap(importance_map_log_df)

                except Exception as e:
                    start_time = time.time()
                    words = prompt.split()
                    avoid = ['the', 'a', 'an', 'is', 'are', 'was', 'were', 'am', 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'shall', 'would', 'should', 'can', 'could', 'may', 'might', 'must', 'ought','need', 'used', 'to', 'of', 'in', 'on','with']

                    final_words = [word for word in words if word.lower() not in avoid][:2]
                    position = range(len(final_words))
                    importance = [0.7, 0.3]

                    # Create a DataFrame
                    df = pd.DataFrame({
                        'word': final_words,
                        'position': position,
                        'importance': importance
                    })

                    # Convert the DataFrame to a dictionary with orient='records'
                    dict_records = df.to_dict(orient='records')
                    end_time = time.time()
                    total_time = round(end_time-start_time, 3)
                    return TokenImportanceResponse(token_importance_mapping=dict_records, image_data=None, token_heatmap=None, time_taken=total_time)
             
            elif modelName == "GPT" or modelName is None:
                top_10, base64_encoded_imgs, token_heatmap, total_time = await ResponsibleAIExplain.prompt_based_token_importance(prompt)
                
            return TokenImportanceResponse(token_importance_mapping=top_10,image_data=base64_encoded_imgs,token_heatmap=token_heatmap, time_taken=total_time)

        except Exception as e:
            log.error(e,exc_info=True)
            raise

    @staticmethod
    def get_label(score, reverse=False):
        score = int(score) # Convert score to integer
        if reverse:
            return 'Highly' if score <= 30 else 'Moderately' if score <= 70 else 'Less'
        else:
            return 'Less' if score <= 30 else 'Moderately' if score <= 70 else 'Highly'
        
    def sentiment_analysis(payload: SentimentAnalysisRequest) -> SentimentAnalysisResponse:
        log.debug(f"payload: {payload}")

        try:
            obj_explain = ResponsibleAIExplain.sentiment_analysis(text=payload.inputPrompt, 
                                                                  class_names=["Negative","Positive"])
            log.debug(f"obj_explain: {obj_explain}")
            
            List_explain = []
            List_explain.append(obj_explain)

            objExplainabilityLocalResponse = SentimentAnalysisResponse(explanation=List_explain)

            return objExplainabilityLocalResponse
        except Exception as e:
            log.error(e,exc_info=True)
            raise

    async def local_explanation(payload: UncertainityRequest) -> UncertainityResponse:
        try:
            log.debug(f"payload: {payload}")
            prompt = payload.inputPrompt
            response = payload.response
            
            result = await ResponsibleAIExplain.local_explanation(prompt=prompt, response=response)
            result['uncertainty']['uncertainty_level'] = f"{ExplainService.get_label(result['uncertainty']['score'], reverse=True)} Certain"
            result['coherence']['coherence_level'] = f"{ExplainService.get_label(result['coherence']['score'])} Coherent"

            response_obj = UncertainityResponse(**result)

            return response_obj

        except Exception as e:
            log.error(e,exc_info=True)
            raise 

    async def graph_of_thoughts(payload: GoTRequest) -> GoTResponse:
        try:
            log.debug(f"payload: {payload}")
            prompt = payload.inputPrompt
            modelName = payload.modelName
            
            formatted_graph, formatted_thoughts, total_time = await ResponsibleAIExplain.graph_of_thoughts(prompt=prompt, modelName=modelName)

            # Calculate the cost
            prompt_tokens = formatted_graph[len(formatted_graph) - 1]['prompt_tokens']
            completion_tokens = formatted_graph[len(formatted_graph) - 1]['completion_tokens']
            cost = Utils.get_token_cost(input_tokens=prompt_tokens, output_tokens=completion_tokens, model=modelName)

            # get the final thought and score from the formatted graph
            final_thoughts = [item['thoughts'] for item in formatted_graph if 'operation' in item and item['operation'] == 'final_thought']
            final_thought = final_thoughts[0][0] if final_thoughts else None

            if final_thought:
                final_thought_key = final_thought.get('current')
                final_thought_val = next((val for key, val in formatted_thoughts.items() if key == final_thought_key), None)
            else:
                final_thought_val = None

            if final_thought and final_thought_val:
                if final_thought['score'] <= 50:
                    final_thought['score'] = final_thought['score'] + 45
                elif final_thought['score'] >= 100:
                    final_thought['score'] = 95

                label = f"{ExplainService.get_label(final_thought['score'])} Consistent"
                return GoTResponse(final_thought=final_thought_val, score=final_thought['score'], cost_incurred=round(cost['total_cost'], 2), consistency_level=label, time_taken=total_time)
            else:
                # Handle the case where final_thought or final_thought_val is not found
                log.error("Final thought or value not found.")
                raise Exception("Final thought or value not found.")
            
        except Exception as e:
            log.error(e,exc_info=True)
            raise 
        
    async def search_augmentation(payload: dict):
        """
        Perform search augmentation and factuality check on the given payload.

        Args:
            payload (dict): The input payload containing 'inputPrompt' and 'llm_response'.

        Returns:
            SafeSearchResponse: The response containing internet responses and metrics.
        """
        try:
            inputPrompt = payload.inputPrompt
            llmResponse = payload.llm_response

            response = await ResponsibleAIExplain.search_augmentation(inputPrompt, llmResponse)
            internet_responses = [response['internetResponse']]

            # Replace Judgement values in explanation
            explanation = response['factual_check']['explanation_factual_accuracy']['Result']
            if explanation[0] != 'No facts found in the LLM response.':
                for item in explanation:
                    if item['Judgement'] == 'yes':
                        item['Judgement'] = 'Fact Verified'
                    elif item['Judgement'] == 'no':
                        item['Judgement'] = 'Fact Not Verified'
                    elif item['Judgement'] == 'unclear':
                        item['Judgement'] = 'Fact Unclear'

            metrics = [{
                "metricName": 'Factuality Check',
                "score": response['factual_check']['Score'],
                "explanation": explanation
            }]
            
            return SafeSearchResponse(internetResponse=internet_responses, metrics=metrics, time_taken=response['time_taken'])
        except ValueError as e:
            log.error(e, exc_info=True)
            raise
        except Exception as e:
            log.error(e,exc_info=True)
            raise