-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.py
168 lines (140 loc) · 5.01 KB
/
builder.py
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
import os.path
from typing import List
import yaml
from modules.api.llm.groq import GroqCloudLLMProvider
from modules.api.llm.openai import OpenAILLMProvider
from modules.api.llm.textgen_webui import TextGenerationWebUILLMProvider
from modules.command_controllers import CommandController
from modules.commands.base import (
BaseCommand,
CommandGlobalChatLLMChatCommand,
CommandPrivateChatLLMChatCommand,
QuickQueryLLMCommand,
)
from modules.commands.decorators import (
admin_only,
deny_empty_prompt,
disabled,
empty_prompt_message_response,
openai_moderated,
)
from modules.logs import get_logger
main_logger = get_logger("main")
gui_logger = get_logger("gui")
class InvalidCommandException(Exception): ...
TYPES = {
"quick-query": QuickQueryLLMCommand,
"command-private": CommandPrivateChatLLMChatCommand,
"command-global": CommandGlobalChatLLMChatCommand,
}
PROVIDERS = {
"open-ai": OpenAILLMProvider,
"groq-cloud": GroqCloudLLMProvider,
"text-generation-webui": TextGenerationWebUILLMProvider,
}
# Traits
WRAPPERS = {
"openai-moderated": openai_moderated,
"admin-only": admin_only,
"empty-prompt-message-response": empty_prompt_message_response,
"disabled": disabled,
"deny-empty-prompt": deny_empty_prompt,
}
CHAT_SETTINGS = {
"prompt-file",
"enable-soft-limit",
"soft-limit-length",
"message-suffix",
"greeting",
"allow-prompt-overwrite",
"allow-long",
"enable-hard-limit",
"hard-limit-length",
"allow-img",
"img-detail",
"img-screen-id"
}
def get_commands_from_yaml() -> List[dict]:
with open("commands.yaml", "r") as file:
data = yaml.safe_load(file)
return data["commands"]
def create_command_from_dict(cmd: dict) -> BaseCommand:
command_dict = {}
command_dict.update(name=cmd["name"])
class_name = f"DynamicCommand{cmd['name']}"
# Command type
try:
type_ = TYPES[cmd["type"]]
except Exception as e:
raise InvalidCommandException(
f"Command type is invalid or missing. Expected one of {list(TYPES.keys())}"
)
# Provider type
try:
provider = PROVIDERS[cmd["provider"]]
command_dict.update(provider=provider)
except Exception as e:
raise InvalidCommandException(
f"Command type is invalid or missing. Expected one of {list(PROVIDERS.keys())}"
)
# Model
if provider != PROVIDERS.get("text-generation-webui"):
try:
model = cmd["model"]
command_dict.update(model=model)
except Exception as e:
raise InvalidCommandException("Model name is invalid or missing.")
# Update command wrappers
if traits := cmd.get("traits"):
wrappers = []
# Reverse the order to make it natural, to make wrappers applied from top to bottom in yaml file.
for wrapper_obj in traits[::-1]:
try:
if isinstance(wrapper_obj, dict):
key = list(wrapper_obj)[0]
values = wrapper_obj[key]
factory = WRAPPERS[key]
wrappers.append(factory(**values))
elif isinstance(wrapper_obj, str):
wrapper = WRAPPERS[wrapper_obj]
wrappers.append(wrapper)
except Exception as e:
gui_logger.warning(f"{e} is not a valid trait.")
command_dict.update(wrappers=wrappers)
# Update command settings
if model_settings := cmd.get("model_settings"):
command_dict.update(model_settings=model_settings)
# Update command settings
if chat_settings := cmd.get("settings"):
# Verify for unknown keys
for option in chat_settings.keys():
if option not in CHAT_SETTINGS:
gui_logger.warning(f'"{option}" is not a valid option.')
# Update dict
command_dict.update(chat_settings=chat_settings)
return type(class_name, (type_,), command_dict)
def load_commands(controller: CommandController) -> None:
if not os.path.exists("./commands.yaml"):
main_logger.info("commands.yaml file is missing")
return None
try:
commands = get_commands_from_yaml()
except Exception as e:
gui_logger.error(f"Failed to to load commands from yaml file. [{e}]")
return None
loaded_commands_count = 0
errors_count = 0
for cmd in commands:
try:
klass = create_command_from_dict(cmd)
chat_command_name = cmd["prefix"] + cmd["name"]
controller.register_command(chat_command_name, klass.as_command(), cmd["name"])
loaded_commands_count += 1
except Exception as e:
errors_count += 1
cmd_name = cmd.get("name", None)
if cmd_name is None:
gui_logger.error(f"Failed to load command. [{e}]")
else:
gui_logger.error(f'Failed to load command "{cmd_name}". [{e}]')
gui_logger.info(f"Loaded {loaded_commands_count} command(s). ({errors_count} errors)")