File size: 8,912 Bytes
32a6937
 
 
8f1a330
32a6937
 
 
 
 
01f4bd7
 
32a6937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01f4bd7
 
32a6937
 
5983ad7
 
 
 
32a6937
01f4bd7
 
 
 
5983ad7
 
 
 
 
 
 
 
 
 
 
 
 
32a6937
 
01f4bd7
 
 
32a6937
 
01f4bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32a6937
 
01f4bd7
 
32a6937
 
01f4bd7
 
32a6937
 
01f4bd7
 
32a6937
 
01f4bd7
 
 
32a6937
01f4bd7
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
# -*- coding:utf-8 -*-
from __future__ import annotations

import json
import logging
import os
import platform
import re
from pathlib import Path
import evaluate
import pandas as pd
import requests
import torch
from tqdm import tqdm
from langchain.memory import ConversationSummaryBufferMemory


class LogRecord(logging.LogRecord):
    def getMessage(self):
        msg = self.msg
        if self.args:
            if isinstance(self.args, dict):
                msg = msg.format(**self.args)
            else:
                msg = msg.format(*self.args)
        return msg


class Logger(logging.Logger):
    def makeRecord(
        self,
        name,
        level,
        fn,
        lno,
        msg,
        args,
        exc_info,
        func=None,
        extra=None,
        sinfo=None,
    ):
        rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func, sinfo)
        if extra is not None:
            for key in extra:
                rv.__dict__[key] = extra[key]
        return rv


def init_settings():
    logging.setLoggerClass(Logger)
    logging.basicConfig(
        level=logging.WARNING,
        format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",
    )


def remove_extra_spaces(text):
    return re.sub(" +", " ", text.strip())


def print_llm_response(llm_response, debug_retrieval=True):
    answer = llm_response["answer"] if "answer" in llm_response else None
    if answer is None:
        answer = llm_response["response"] if "response" in llm_response else None

    if answer is not None:
        print("\n\n***Answer:")
        print(answer)

    source_documents = (
        llm_response["source_documents"] if "source_documents" in llm_response else None
    )
    if source_documents is None:
        source_documents = (
            llm_response["sourceDocs"] if "sourceDocs" in llm_response else None
        )

    if debug_retrieval and source_documents is not None:
        print("\nSources:")
        for index, source in enumerate(source_documents):
            metadata = source["metadata"] if "metadata" in source else source.metadata
            if "page" in metadata:
                print(f" Page:  {metadata['page']}", end="")

            print(
                f" Source {index + 1}: "
                + str(metadata["url"] if "url" in metadata else metadata["source"])
            )
            print(
                source["page_content"]
                if "page_content" in source
                else source.page_content
            )

    if "chat_history" in llm_response:
        print("\nChat History:")
        print(llm_response["chat_history"])


def get_device_types():
    print("Running on: ", platform.platform())
    print("MPS is", "NOT" if not torch.backends.mps.is_available() else "", "available")
    print("CUDA is", "NOT" if not torch.cuda.is_available() else "", "available")
    device_type_available = "cpu"

    if not torch.backends.mps.is_available():
        if not torch.backends.mps.is_built():
            print(
                "MPS not available because the current PyTorch install was not "
                "built with MPS enabled."
            )
        else:
            print(
                "MPS not available because the current MacOS version is not 12.3+ "
                "and/or you do not have an MPS-enabled device on this machine."
            )
    else:
        device_type_available = "mps"

    if torch.cuda.is_available():
        print("CUDA is available, we have found ", torch.cuda.device_count(), " GPU(s)")
        print(torch.cuda.get_device_name(0))
        print("CUDA version: " + torch.version.cuda)
        device_type_available = f"cuda:{torch.cuda.current_device()}"

    return (
        os.environ.get("HF_EMBEDDINGS_DEVICE_TYPE") or device_type_available,
        os.environ.get("HF_PIPELINE_DEVICE_TYPE") or device_type_available,
    )


def ensure_model_is_downloaded(llm_model_type):
    if llm_model_type.startswith("gpt4all"):
        local_path = (
            os.environ.get("GPT4ALL_J_MODEL_PATH")
            if llm_model_type == "gpt4all-j"
            else os.environ.get("GPT4ALL_MODEL_PATH")
        )
        url = (
            os.environ.get("GPT4ALL_J_DOWNLOAD_LINK")
            if llm_model_type == "gpt4all-j"
            else os.environ.get("GPT4ALL_DOWNLOAD_LINK")
        )
    elif llm_model_type == "llamacpp":
        local_path = os.environ.get("LLAMACPP_MODEL_PATH")
        url = os.environ.get("LLAMACPP_DOWNLOAD_LINK")
    elif llm_model_type == "ctransformers":
        local_path = os.environ.get("CTRANSFORMERS_MODEL_PATH")
        url = os.environ.get("CTRANSFORMERS_DOWNLOAD_LINK")
    else:
        raise ValueError(f"wrong model typle: {llm_model_type}")

    path = Path(local_path)

    if path.is_file():
        print(f"model: {local_path} exists")
    else:
        print(f"downloading model: {local_path} from {url} ...")
        path.parent.mkdir(parents=True, exist_ok=True)

        # send a GET request to the URL to download the file. Stream since it's large
        response = requests.get(url, stream=True)

        # open the file in binary mode and write the contents of the response to it in chunks
        # This is a large file, so be prepared to wait.
        with open(local_path, "wb") as f:
            for chunk in tqdm(response.iter_content(chunk_size=8192)):
                if chunk:
                    f.write(chunk)

    return local_path


class CustomizedConversationSummaryBufferMemory(ConversationSummaryBufferMemory):
    def save_context(self, inputs, outputs) -> None:
        for key in outputs:
            if isinstance(outputs[key], str):
                outputs[key] = outputs[key].replace("<|im_end|>", "")
        return super().save_context(inputs, outputs)

    def predict_new_summary(self, messages, existing_summary) -> str:
        return (
            super()
            .predict_new_summary(messages, existing_summary)
            .replace("<|im_end|>", "")
        )


bleu = evaluate.load("bleu")
rouge = evaluate.load("rouge")


def calc_bleu_rouge_scores(predictions, references, debug=False):
    if debug:
        print("predictions:", predictions)
        print("references:", references)

    bleu_scores = bleu.compute(
        predictions=predictions, references=references, max_order=1
    )
    rouge_scores = rouge.compute(predictions=predictions, references=references)
    result = {"bleu_scores": bleu_scores, "rouge_scores": rouge_scores}

    if debug:
        print("result:", result)

    return result


def calc_metrics(df):
    predictions = [df["answer"][i] for i in range(len(df))]
    references = [df["ground_truth"][i] for i in range(len(df))]

    return calc_bleu_rouge_scores(predictions, references)


pattern_abnormal_newlines = re.compile(r"\n{5,}")
pattern_text_repetitions = re.compile(r"\b(\w.+?)\b(\1+)", re.M | re.DOTALL)
exception_pattern = re.compile(r"(\w+\.)\1")


# final version for repetition detection
def detect_repetitions(
    text, debug=False, pattern_text_repetitions=pattern_text_repetitions
):
    subtotals = [0, 0]

    if isinstance(text, str):
        patterns = [pattern_abnormal_newlines, pattern_text_repetitions]
        for i, pattern in enumerate(patterns):
            if debug:
                print(
                    f"----detect {'abnormal newlines' if i == 0 else 'text repetitions'}----"
                )
            matches = pattern.finditer(text)
            for match in matches:
                if debug:
                    print(match)
                    for groupNum in range(0, len(match.groups())):
                        groupNum = groupNum + 1
                        print(
                            "Group {groupNum} found at {start}-{end}: `{group}`".format(
                                groupNum=groupNum,
                                start=match.start(groupNum),
                                end=match.end(groupNum),
                                group=match.group(groupNum),
                            )
                        )

                if exception_pattern.match(match[0]):
                    if debug:
                        print("ignored: ", match[0])
                    continue

                start, end = match.span()
                subtotals[i] += end - start

    result = (subtotals[0], subtotals[1], subtotals[0] + subtotals[1])

    if debug:
        print(result)
    return result


def detect_abnormal_newlines(text, debug=False):
    return detect_repetitions(text, debug=debug)[0]


def detect_text_repetitions(text, debug=False):
    return detect_repetitions(text, debug=debug)[1]


def detect_repetition_scores(text, debug=False):
    newline_score, repetition_score, total_repetitions = detect_repetitions(
        text, debug=debug
    )
    return pd.Series([newline_score, repetition_score, total_repetitions])