File size: 20,446 Bytes
2864204
 
 
58b9de9
150bb15
 
 
58b9de9
d7b7dc6
 
58b9de9
d7b7dc6
150bb15
 
2c24f05
5c4aa1e
150bb15
 
 
 
 
5c4aa1e
 
 
2aa9a75
d7b7dc6
58b9de9
2864204
58b9de9
5c4aa1e
150bb15
58b9de9
 
 
 
 
 
 
2864204
 
d7b7dc6
 
58b9de9
 
 
 
 
 
 
 
d7b7dc6
 
 
58b9de9
 
 
 
 
 
 
 
 
 
 
 
 
 
2864204
d7b7dc6
58b9de9
 
 
2864204
 
58b9de9
 
 
 
 
 
d7b7dc6
58b9de9
 
 
 
 
 
 
150bb15
2864204
 
d7b7dc6
 
 
 
2864204
150bb15
d7b7dc6
150bb15
58b9de9
 
 
 
 
 
 
 
2864204
150bb15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2864204
150bb15
 
5c4aa1e
150bb15
5c4aa1e
150bb15
 
 
 
 
5c4aa1e
2aa9a75
 
 
150bb15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2864204
 
d7b7dc6
 
 
 
150bb15
 
 
2aa9a75
5c4aa1e
2aa9a75
 
 
 
 
 
5c4aa1e
2aa9a75
 
150bb15
 
 
 
 
 
 
2aa9a75
150bb15
2aa9a75
150bb15
 
 
 
 
 
 
 
5c4aa1e
150bb15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c4aa1e
 
150bb15
 
 
 
 
 
5c4aa1e
 
150bb15
 
 
2aa9a75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c4aa1e
2aa9a75
 
 
5c4aa1e
2aa9a75
 
 
 
 
 
 
5c4aa1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150bb15
2aa9a75
5c4aa1e
 
 
 
 
 
 
 
 
 
 
150bb15
5c4aa1e
150bb15
 
 
 
 
5c4aa1e
150bb15
 
 
5c4aa1e
2aa9a75
5c4aa1e
 
 
 
 
 
 
 
 
 
 
150bb15
 
 
5c4aa1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150bb15
5c4aa1e
150bb15
 
 
 
5c4aa1e
 
 
 
 
150bb15
2aa9a75
d7b7dc6
 
58b9de9
 
 
2c24f05
 
d7b7dc6
 
2c24f05
58b9de9
 
2c24f05
 
d7b7dc6
2c24f05
d7b7dc6
 
58b9de9
 
 
2c24f05
 
 
 
d7b7dc6
2c24f05
d7b7dc6
58b9de9
d7b7dc6
58b9de9
 
 
 
 
 
 
 
 
d7b7dc6
58b9de9
 
 
 
 
 
d7b7dc6
 
404587d
d7b7dc6
 
 
58b9de9
2c24f05
58b9de9
d7b7dc6
58b9de9
 
d7b7dc6
58b9de9
 
 
2c24f05
150bb15
 
156ef43
2c24f05
5c4aa1e
2c24f05
 
150bb15
 
 
 
2c24f05
150bb15
 
 
 
 
5c4aa1e
 
 
 
 
 
2c24f05
150bb15
 
2c24f05
 
 
 
 
150bb15
 
2c24f05
58b9de9
404587d
58b9de9
404587d
 
 
58b9de9
 
404587d
 
58b9de9
 
 
 
d7b7dc6
58b9de9
 
 
d7b7dc6
404587d
58b9de9
d7b7dc6
 
58b9de9
404587d
d7b7dc6
404587d
 
d7b7dc6
404587d
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import os
import time
from datetime import datetime
import logging
from pathlib import Path  
import requests
import json

import numpy as np
import pandas as pd
import spacy
from sentence_transformers import CrossEncoder
import litellm
# from litellm import completion
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, AutoConfig, pipeline
# from accelerate import PartialState
# from accelerate.inference import prepare_pippy
import torch
import cohere
from openai import OpenAI
import anthropic
import replicate
# pip install -U google-generativeai
import google.generativeai as genai

import src.backend.util as util
import src.envs as envs

litellm.set_verbose=True

# Set up basic configuration for logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

# Load spacy model for word tokenization
nlp = spacy.load("en_core_web_sm")

os.environ["HUGGINGFACE_API_KEY"] =  envs.TOKEN


def load_evaluation_model(model_path):
    """Load the evaluation model from the given path

    Args:
        model_path (str): Path to the evaluation model

    Returns:
        CrossEncoder: The evaluation model
    """
    model = CrossEncoder(model_path)
    return model


class ModelLoadingException(Exception):
    """Exception raised for errors in loading a model.

    Attributes:
        model_id (str): The model identifier.
        revision (str): The model revision.
    """

    def __init__(self, model_id, revision, messages="Error initializing model"):
        self.model_id = model_id
        self.revision = revision
        super().__init__(f"{messages} id={model_id} revision={revision}")


class SummaryGenerator:
    """A class to generate summaries using a causal language model.

    Attributes:
        model (str): huggingface/{model_id}
        api_base (str): https://api-inference.huggingface.co/models/{model_id}
        summaries_df (DataFrame): DataFrame to store generated summaries.
        revision (str): Model revision.
        avg_length (float): Average length of summaries.
        answer_rate (float): Rate of non-empty summaries.
    """

    def __init__(self, model_id, revision):
        """
        Initializes the SummaryGenerator with a model.

        Args:
            model_id (str): Identifier for the model.
            revision (str): Revision of the model.
        """
        self.model_id = model_id
        self.model = f"huggingface/{model_id}"
        self.api_base = f"https://api-inference.huggingface.co/models/{model_id}"
        self.summaries_df = pd.DataFrame()
        self.revision = revision
        self.avg_length = None
        self.answer_rate = None
        self.exceptions = None
        self.local_model = None

    def generate_summaries(self, df, save_path=None):
        """Generate summaries for a given DataFrame of source docs.

        Args:
            df (DataFrame): DataFrame containing source docs.

        Returns:
            summaries_df (DataFrame): Generated summaries by the model.
        """
        exceptions = []
        if (save_path is not None) and os.path.exists(save_path):
            self.summaries_df = pd.read_csv(save_path)
            print(f'Loaded generated summaries from {save_path}')
        else:
            source, summary, dataset = [], [], [] 
            print(f"Total: {df.shape[0]}")
            for index, row in tqdm(df.iterrows(), total=df.shape[0]):
                _source = row['text']
                _dataset = row['dataset']

                system_prompt = envs.SYSTEM_PROMPT
                user_prompt = f"{envs.USER_PROMPT}\nPassage:\n{_source}"

                while True:
                    try:
                        _summary = self.generate_summary(system_prompt, user_prompt)
                        # print(f"Finish index {index}")
                        break
                    except Exception as e:
                        if 'Rate limit reached' in str(e):
                            wait_time = 300
                            current_time = datetime.now().strftime('%H:%M:%S')
                            print(f"Rate limit hit at {current_time}. Waiting for 5 minutes before retrying...")
                            time.sleep(wait_time)
                        elif 'is currently loading' in str(e):
                            wait_time = 200
                            print(f"Model is loading, wait for {wait_time}")
                            time.sleep(wait_time)
                        elif '429' in str(e): # for gemini models
                            wait_time = 60
                            print(f"Quota has reached, wait for {wait_time}")
                            time.sleep(wait_time)
                        else:
                            print(f"Error at index {index}: {e}")
                            _summary = ""
                            exceptions.append(index)
                            break

                summary.append(_summary)
                source.append(_source)
                dataset.append(_dataset)

                # Sleep to prevent hitting rate limits too frequently
                time.sleep(1)

            self.summaries_df = pd.DataFrame(list(zip(source, summary, dataset)),
                                            columns=["source", "summary", "dataset"])

            if save_path is not None:
                print(f'Save summaries to {save_path}')
                fpath = Path(save_path)
                fpath.parent.mkdir(parents=True, exist_ok=True)
                self.summaries_df.to_csv(fpath) 

        self.exceptions = exceptions
        self._compute_avg_length()
        self._compute_answer_rate()

        return self.summaries_df
    
    def generate_summary(self, system_prompt: str, user_prompt: str):
        # Using Together AI API
        using_together_api = False
        together_ai_api_models = ['mixtral', 'dbrx', 'wizardlm', 'llama-3', 'qwen'] #, 'mistralai'
        for together_ai_api_model in together_ai_api_models:
            if together_ai_api_model in self.model_id.lower():
                using_together_api = True
                break
        # if 'mixtral' in self.model_id.lower() or 'dbrx' in self.model_id.lower() or 'wizardlm' in self.model_id.lower(): # For mixtral and dbrx models, use Together AI API
        if using_together_api:
            # print('using together api')
            # suffix = "completions" if ('mixtral' in self.model_id.lower() or 'base' in self.model_id.lower()) else "chat/completions"
            suffix = "chat/completions"
            url = f"https://api.together.xyz/v1/{suffix}"

            payload = {
                "model": self.model_id,
                # "max_tokens": 4096,
                'max_new_tokens': 250,
                "temperature": 0.0,
                # 'repetition_penalty': 1.1 if 'mixtral' in self.model_id.lower() else 1
            }
            payload['messages'] = [{"role": "system", "content": system_prompt},
                                        {"role": "user", "content": user_prompt}]
            headers = {
                "accept": "application/json",
                "content-type": "application/json",
                "Authorization": f"Bearer {os.environ['TOGETHER_API_KEY']}"
            }

            response = requests.post(url, json=payload, headers=headers)
            print(response)
            try:
                result = json.loads(response.text)
                # print(result)
                result = result["choices"][0]
                if 'message' in result:
                    result = result["message"]["content"].strip()
                else:
                    result = result["text"]
                    result_candidates = [result_cancdidate for result_cancdidate in result.split('\n\n') if len(result_cancdidate) > 0]
                    result = result_candidates[0]
                print(result)
            except:
                print(response)
                result = ''
            return result

        # Using OpenAI API
        elif 'gpt' in self.model_id.lower():
            client = OpenAI()
            response = client.chat.completions.create(
                model=self.model_id.replace('openai/',''),
                messages=[{"role": "system", "content": system_prompt},
                        {"role": "user", "content": user_prompt}],
                temperature=0.0,
                max_tokens=250,
            )   
            # print(response)
            result = response.choices[0].message.content
            print(result)
            return result
        
        # Using Google AI API for Gemini models
        elif 'gemini' in self.model_id.lower():
            genai.configure(api_key=os.getenv('GOOGLE_AI_API_KEY'))
            generation_config = {
                "temperature": 0,
                "top_p": 0.95, # cannot change
                "top_k": 0,
                "max_output_tokens": 250,
                # "response_mime_type": "application/json",
            }
            safety_settings = [
                {
                    "category": "HARM_CATEGORY_HARASSMENT",
                    "threshold": "BLOCK_NONE"
                },
                {
                    "category": "HARM_CATEGORY_HATE_SPEECH",
                    "threshold": "BLOCK_NONE"
                },
                {
                    "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                    "threshold": "BLOCK_NONE"
                },
                {
                    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                    "threshold": "BLOCK_NONE"
                },
            ]
            model = genai.GenerativeModel(model_name=self.model_id.lower().split('google/')[-1],
                              generation_config=generation_config,
                              system_instruction=system_prompt,
                              safety_settings=safety_settings)
            # print(model)
            convo = model.start_chat(history=[])
            convo.send_message(user_prompt)
            # print(convo.last)
            result = convo.last.text
            print(result)
            return result

        elif 'snowflake' in self.model_id.lower():
            print("using replicate")
            input = {
                "prompt": user_prompt,
                "temperature": 0,
                "max_new_tokens": 250,
                "stop_sequences": "<|im_end|>",
                "prompt_template": f"<|im_start|>system\n{system_prompt}<|im_end|>\n" + "<|im_start|>user\n{prompt}<|im_end|>\n\n<|im_start|>assistant\n",
            }
            response = replicate.run(
                self.model_id.lower(),
                input=input
            )
            if isinstance(response, list):
                response = ''.join(response)
                print(response)
                print()
            
            return response

        elif 'claude' in self.model_id.lower(): # using anthropic api
            client = anthropic.Anthropic()
            message = client.messages.create(
                model=self.model_id.split('/')[-1],
                max_tokens=250,
                temperature=0,
                system=system_prompt,
                messages=[
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "text",
                                "text": user_prompt
                            }
                        ]
                    }
                ]
            )
            result = message.content[0].text
            print(result)
            return result
                
        # Using HF API or download checkpoints
        elif self.local_model is None:
            # response = litellm.completion(
            #     model='command-r-plus' if 'command' in self.model else self.model,
            #     messages=[{"role": "system", "content": system_prompt},
            #                 {"role": "user", "content": user_prompt}],
            #     temperature=0.0,
            #     max_tokens=256,
            #     api_base=self.api_base,
            # )
            # result = response['choices'][0]['message']['content']
            # print(result)
            # return result
            try: # try use HuggingFace API
                print('using huggingface api')
                response = litellm.completion(
                    model='command-r-plus' if 'command' in self.model else self.model,
                    messages=[{"role": "system", "content": system_prompt},
                                {"role": "user", "content": user_prompt}],
                    temperature=0.0,
                    max_tokens=250,
                    api_base=self.api_base,
                )
                result = response['choices'][0]['message']['content']
                print(result)
                return result
            except Exception as e:
                if 'Rate limit reached' in str(e):
                    wait_time = 300
                    current_time = datetime.now().strftime('%H:%M:%S')
                    print(f"Rate limit hit at {current_time}. Waiting for 5 minutes before retrying...")
                    time.sleep(wait_time)
                else:
                    self.tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf" if 'openelm' in self.model_id.lower() else self.model_id, trust_remote_code=True)
                    print("Tokenizer loaded")
                    self.local_model = AutoModelForCausalLM.from_pretrained(self.model_id, trust_remote_code=True, device_map="auto", torch_dtype="auto")
                    print("Local model loaded")
        
        # Using local model
        if self.local_model: # cannot call API. using local model
            if 'gemma' in self.model_id.lower() or 'mistral-7b' in self.model_id.lower():
                messages=[
                    # gemma-1.1, mistral-7b does not accept system role
                    {"role": "user", "content": system_prompt + ' ' + user_prompt}
                ]
                prompt = self.tokenizer.apply_chat_template(messages,add_generation_prompt=True, tokenize=False)
            
            elif 'phi-2' in self.model_id.lower():
                prompt = system_prompt + '\n' + user_prompt
            
            else:
                messages=[
                    {"role": "system", "content": system_prompt}, # gemma-1.1, mistral-7b does not accept system role
                    {"role": "user", "content": user_prompt}
                ]
                prompt = self.tokenizer.apply_chat_template(messages,add_generation_prompt=True, tokenize=False)
            print(prompt)
            print('-'*50)
            input_ids = self.tokenizer(prompt, return_tensors="pt").to('cuda')
            with torch.no_grad():
                outputs = self.local_model.generate(**input_ids, max_new_tokens=250, do_sample=True, temperature=0.01, pad_token_id=self.tokenizer.eos_token_id)
            result = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            if 'gemma-2' in self.model_id.lower():
                result = result.split(user_prompt + '\nmodel')[-1].strip()
            else:
                result = result.replace(prompt.strip(), '')
            
            print(result)
            return result

    def _compute_avg_length(self):
        """
        Compute the average length of non-empty summaries using SpaCy.
        """
        total_word_count = 0
        total_count = 0

        for summary in self.summaries_df['summary']:
            if util.is_summary_valid(summary):
                doc = nlp(summary)
                words = [token.text for token in doc if token.is_alpha]
                total_word_count += len(words)
                total_count += 1

        self.avg_length = 0 if total_count == 0 else total_word_count / total_count

    def _compute_answer_rate(self):
        """
        Compute the rate of non-empty summaries.
        """
        valid_count = sum(1 for summary in self.summaries_df['summary']
                            if util.is_summary_valid(summary))

        total_count = len(self.summaries_df)

        self.answer_rate = 0 if total_count == 0 else valid_count / total_count


class EvaluationModel:
    """A class to evaluate generated summaries.

    Attributes:
        model (CrossEncoder): The evaluation model.
        scores (list): List of evaluation scores.
        accuracy (float): Accuracy of the summaries.
        hallucination_rate (float): Rate of hallucination in summaries.
    """

    def __init__(self, model_path):
        """
        Initializes the EvaluationModel with a CrossEncoder model.

        Args:
            model_path (str): Path to the CrossEncoder model.
        """
        self.model = load_evaluation_model(model_path)
        self.scores = []
        self.factual_consistency_rate = None
        self.hallucination_rate = None

    def evaluate_hallucination(self, summaries_df):
        """
        Evaluate the hallucination rate in summaries. Updates the 'scores' attribute 
        of the instance with the computed scores.

        Args:
            summaries_df (DataFrame): DataFrame containing source docs and summaries.

        Returns:
            list: List of hallucination scores. Also updates the 'scores' attribute of the instance.
        """
        hem_scores = []
        sources = []
        summaries = []
        source_summary_pairs = util.create_pairs(summaries_df)

        for doc, summary in source_summary_pairs:
            if util.is_summary_valid(summary):
                try:
                    # summary_pieces = summary.split('\n')
                    # summary = summary_pieces[0] if len(summary_pieces[0].strip()) > 0 else summary_pieces[1]
                    summary = summary.replace('<bos>','').replace('<eos>','')
                    score = self.model.predict([doc, summary])# [0]
                    if not isinstance(score, float):
                        try:
                            score = score.item()
                        except:
                            logging.warning(f"Score type mismatch: Expected float, got {type(score)}.")
                            continue
                        # print inconsistent summaries for checking
                        if score < 0.5:
                            print(doc)
                            print('-'*10)
                            print(summary)
                            print('='*20)
                    hem_scores.append(score)
                    sources.append(doc)
                    summaries.append(summary)
                except Exception as e:
                    logging.error(f"Error while running HEM: {e}")
                    raise

        self.scores = hem_scores
        eval_results = {'source': sources, 'summary': summaries, 'HEM scores': hem_scores}
        return hem_scores, eval_results


    def compute_factual_consistency_rate(self, threshold=0.5):
        """
        Compute the factual consistency rate of the evaluated summaries based on
        the previously calculated scores. This method relies on the 'scores'
        attribute being populated, typically via the 'evaluate_hallucination' method.

        Returns:
            float: Factual Consistency Rate. Also updates the 'factual_consistency_rate'
            and 'hallucination_rate' attributes of the instance.

        Raises:
            ValueError: If scores have not been calculated prior to calling this method.
        """
        if not self.scores:
            error_msg = "Scores not calculated. Call evaluate_hallucination() first."
            logging.error(error_msg)
            raise ValueError(error_msg)

        # Use threshold of 0.5 to compute factual_consistency_rate
        num_above_threshold = sum(score >= threshold for score in self.scores)
        num_total = len(self.scores)

        if not num_total:
            raise ValueError("No scores available to compute factual consistency rate.")

        self.factual_consistency_rate = (num_above_threshold / num_total) * 100
        self.hallucination_rate = 100 - self.factual_consistency_rate

        return self.factual_consistency_rate