|
|
|
|
|
import re |
|
|
import numpy as np |
|
|
|
|
|
import tiktoken |
|
|
from langchain.text_splitter import TokenTextSplitter |
|
|
|
|
|
|
|
|
|
|
|
def strip_quotes(text): |
|
|
if text.startswith('"') and text.endswith('"'): |
|
|
return text[1:-1] |
|
|
return text |
|
|
|
|
|
def strtobool(val): |
|
|
val = val.lower() |
|
|
if val in ('yes', 'true', 't', '1'): |
|
|
return True |
|
|
elif val in ('no', 'false', 'f', '0'): |
|
|
return False |
|
|
else: |
|
|
raise ValueError(f"Invalid truth value {val}") |
|
|
|
|
|
|
|
|
def split_camel_case(word): |
|
|
|
|
|
pattern = re.compile(r'(?<=[a-z])(?=[A-Z])') |
|
|
|
|
|
|
|
|
split_word = pattern.sub(' ', word) |
|
|
|
|
|
return split_word |
|
|
|
|
|
|
|
|
|
|
|
def chunk_tokens(tokens, max_len): |
|
|
for i in range(0, len(tokens), max_len): |
|
|
yield tokens[i:i + max_len] |
|
|
|
|
|
|
|
|
def update_nested_dict(d, u): |
|
|
for k, v in u.items(): |
|
|
if isinstance(v, dict): |
|
|
d[k] = update_nested_dict(d.get(k, {}), v) |
|
|
else: |
|
|
d[k] = v |
|
|
return d |
|
|
|
|
|
|
|
|
def cleanInputText(textInputLLM): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
textInputLLM = re.sub(r'\(\'\\n\\n', ' ', textInputLLM) |
|
|
textInputLLM = re.sub(r'\(\"\\n\\n', ' ', textInputLLM) |
|
|
textInputLLM = re.sub(r'\\n\\n\',\)', ' ', textInputLLM) |
|
|
textInputLLM = re.sub(r'\\n\\n\",\)', ' ', textInputLLM) |
|
|
|
|
|
|
|
|
while re.search(r'##\n', textInputLLM): |
|
|
textInputLLM = re.sub(r"##\n", '. ', textInputLLM) |
|
|
while '###' in textInputLLM: |
|
|
textInputLLM = textInputLLM.replace("###", ' ') |
|
|
while '##' in textInputLLM: |
|
|
textInputLLM = textInputLLM.replace("##", ' ') |
|
|
while ' # ' in textInputLLM: |
|
|
textInputLLM = textInputLLM.replace(" # ", ' ') |
|
|
while '--' in textInputLLM: |
|
|
textInputLLM = textInputLLM.replace("--", '-') |
|
|
while re.search(r'\\\\-', textInputLLM): |
|
|
textInputLLM = re.sub(r"\\\\-", '.', textInputLLM) |
|
|
while re.search(r'\*\*\n', textInputLLM): |
|
|
textInputLLM = re.sub(r"\*\*\n", '. ', textInputLLM) |
|
|
while re.search(r'\*\*\*', textInputLLM): |
|
|
textInputLLM = re.sub(r"\*\*\*", ' ', textInputLLM) |
|
|
while re.search(r'\*\*', textInputLLM): |
|
|
textInputLLM = re.sub(r"\*\*", ' ', textInputLLM) |
|
|
while re.search(r' \* ', textInputLLM): |
|
|
textInputLLM = re.sub(r" \* ", ' ', textInputLLM) |
|
|
while re.search(r'is a program of the\n\nInternational Society for Infectious Diseases', textInputLLM): |
|
|
textInputLLM = re.sub( |
|
|
r'is a program of the\n\nInternational Society for Infectious Diseases', |
|
|
'is a program of the International Society for Infectious Diseases', |
|
|
textInputLLM, |
|
|
flags=re.M |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while re.search(r' \*\.', textInputLLM): |
|
|
textInputLLM = re.sub(r' \*\.', ' .', textInputLLM) |
|
|
while ' ' in textInputLLM: |
|
|
textInputLLM = textInputLLM.replace(" ", ' ') |
|
|
while re.search(r'\.\.', textInputLLM): |
|
|
textInputLLM = re.sub(r'\.\.', '.', textInputLLM) |
|
|
while re.search(r'\. \.', textInputLLM): |
|
|
textInputLLM = re.sub(r'\. \.', '.', textInputLLM) |
|
|
|
|
|
|
|
|
textInputLLM = re.sub(r'\(\"\.', ' ', textInputLLM) |
|
|
textInputLLM = re.sub(r'\(\'\.', ' ', textInputLLM) |
|
|
textInputLLM = re.sub(r'\",\)', ' ', textInputLLM) |
|
|
textInputLLM = re.sub(r'\',\)', ' ', textInputLLM) |
|
|
|
|
|
|
|
|
textInputLLM = textInputLLM.strip() |
|
|
|
|
|
return textInputLLM |
|
|
|
|
|
|
|
|
|
|
|
def encoding_getter(encoding_type: str): |
|
|
""" |
|
|
Returns the appropriate encoding based on the given encoding type (either an encoding string or a model name). |
|
|
|
|
|
tiktoken supports three encodings used by OpenAI models: |
|
|
|
|
|
Encoding name OpenAI models |
|
|
cl100k_base gpt-4, gpt-3.5-turbo, text-embedding-ada-002 |
|
|
p50k_base Codex models, text-davinci-002, text-davinci-003 |
|
|
r50k_base (or gpt2) GPT-3 models like davinci |
|
|
|
|
|
https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb |
|
|
|
|
|
""" |
|
|
if "k_base" in encoding_type: |
|
|
return tiktoken.get_encoding(encoding_type) |
|
|
else: |
|
|
try: |
|
|
my_enc = tiktoken.encoding_for_model(encoding_type) |
|
|
return my_enc |
|
|
except Exception as err: |
|
|
my_enc = tiktoken.get_encoding("cl100k_base") |
|
|
return my_enc |
|
|
|
|
|
|
|
|
def tokenizer(string: str, encoding_type: str) -> list: |
|
|
""" |
|
|
Returns the tokens in a text string using the specified encoding. |
|
|
""" |
|
|
encoding = encoding_getter(encoding_type) |
|
|
tokens = encoding.encode(string) |
|
|
return tokens |
|
|
|
|
|
|
|
|
def token_counter(string: str, encoding_type: str) -> int: |
|
|
""" |
|
|
Returns the number of tokens in a text string using the specified encoding. |
|
|
""" |
|
|
num_tokens = len(tokenizer(string, encoding_type)) |
|
|
return num_tokens |
|
|
|
|
|
|
|
|
|
|
|
def extract_words(text, putInLower=False): |
|
|
|
|
|
if putInLower: |
|
|
return [word.lower() for word in re.findall(r'\b\w+\b', text)] |
|
|
else: |
|
|
return [word for word in re.findall(r'\b\w+\b', text)] |
|
|
|
|
|
|
|
|
def all_words_in_list(compound_word, word_list, putInLower=False): |
|
|
words_to_check = extract_words(compound_word, putInLower=putInLower) |
|
|
if putInLower: |
|
|
return all(word.lower() in word_list for word in words_to_check) |
|
|
else: |
|
|
return all(word in word_list for word in words_to_check) |
|
|
|
|
|
|
|
|
def row_to_dict_string(rrrow, columnsDict): |
|
|
formatted_items = [] |
|
|
for col in rrrow.index: |
|
|
if col not in columnsDict: |
|
|
continue |
|
|
value = rrrow[col] |
|
|
|
|
|
if isinstance(value, (int, float)): |
|
|
formatted_items.append(f'"{col}": {value}') |
|
|
else: |
|
|
formatted_items.append( |
|
|
f'"{col}": "{value}"') |
|
|
|
|
|
return '{' + ', '.join(formatted_items) + '}' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rescale_exponential_to_linear(df, column, new_min=0.5, new_max=1.0): |
|
|
|
|
|
original_scores = df[column] |
|
|
|
|
|
|
|
|
min_score = original_scores.min() |
|
|
max_score = original_scores.max() |
|
|
normalized_scores = (original_scores - min_score) / (max_score - min_score) |
|
|
|
|
|
|
|
|
linear_scores = new_min + (normalized_scores * (new_max - new_min)) |
|
|
|
|
|
|
|
|
df[column] = linear_scores |
|
|
|
|
|
return df |
|
|
|
|
|
|
|
|
def rescale_exponential_to_logarithmic(df, column, new_min=0.5, new_max=1.0): |
|
|
|
|
|
epsilon = 1e-10 |
|
|
df[column] = df[column] + epsilon |
|
|
|
|
|
|
|
|
log_transformed_scores = np.log(df[column]) |
|
|
|
|
|
|
|
|
min_score = log_transformed_scores.min() |
|
|
max_score = log_transformed_scores.max() |
|
|
normalized_log_scores = (log_transformed_scores - min_score) / (max_score - min_score) |
|
|
|
|
|
|
|
|
logarithmic_scores = new_min + (normalized_log_scores * (new_max - new_min)) |
|
|
|
|
|
|
|
|
df[column] = logarithmic_scores |
|
|
|
|
|
return df |