Create load_llms.py
Browse files- load_llms.py +67 -0
load_llms.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
# populate all models available from GPT4All
|
4 |
+
url = "https://raw.githubusercontent.com/nomic-ai/gpt4all/main/gpt4all-chat/metadata/models3.json"
|
5 |
+
response = urlopen(url)
|
6 |
+
data_json = json.loads(response.read())
|
7 |
+
|
8 |
+
|
9 |
+
def model_choices():
|
10 |
+
model_list = [data_json[i]['filename'] for i in range(len(data_json))]
|
11 |
+
return model_list
|
12 |
+
|
13 |
+
|
14 |
+
# get each models' description
|
15 |
+
model_description = {model['filename']: model['description'] for model in data_json}
|
16 |
+
|
17 |
+
|
18 |
+
def llm_intro(selected_model):
|
19 |
+
html_string = model_description.get(selected_model, "No description available for this model selection.")
|
20 |
+
formatted_description = html_string.replace("<ul>", "").replace("</ul>", "").replace("</li>", "").replace("<br>", "\n").replace("</br>", "").replace("<li>", "\n➤ ")
|
21 |
+
return formatted_description
|
22 |
+
|
23 |
+
|
24 |
+
def remove_endtags(html_string, tags):
|
25 |
+
"""Remove rear HTML tags from the input string."""
|
26 |
+
for tag in tags:
|
27 |
+
html_string = re.sub(fr"</{tag}>", "", html_string)
|
28 |
+
return html_string
|
29 |
+
|
30 |
+
|
31 |
+
def replace_starttags(html_string, replacements):
|
32 |
+
"""Replace starting HTML tags with the corresponding values."""
|
33 |
+
for tag, replacement in replacements.items():
|
34 |
+
html_string = html_string.replace(tag, replacement)
|
35 |
+
return html_string
|
36 |
+
|
37 |
+
|
38 |
+
def format_html_string(html_string):
|
39 |
+
"""Format the HTML string to a readable text format."""
|
40 |
+
tags_to_remove = ["ul", "li", "br"]
|
41 |
+
html_string = remove_endtags(html_string, tags_to_remove)
|
42 |
+
|
43 |
+
tag_replacements = {
|
44 |
+
"<li>": "\n➤ ",
|
45 |
+
"<br>": "\n",
|
46 |
+
"<strong>": "**",
|
47 |
+
"</strong>": "**"
|
48 |
+
}
|
49 |
+
formatted_string = replace_starttags(html_string, tag_replacements)
|
50 |
+
|
51 |
+
return formatted_string
|
52 |
+
|
53 |
+
|
54 |
+
# cache models for faster reloads
|
55 |
+
model_cache = {}
|
56 |
+
|
57 |
+
|
58 |
+
def load_model(model_name):
|
59 |
+
"""
|
60 |
+
This function checks the cache before loading a model.
|
61 |
+
If the model is cached, it returns the cached version.
|
62 |
+
Otherwise, it loads the model, caches it, and then returns it.
|
63 |
+
"""
|
64 |
+
if model_name not in model_cache:
|
65 |
+
model = GPT4All(model_name)
|
66 |
+
model_cache[model_name] = model
|
67 |
+
return model_cache[model_name]
|