-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·306 lines (249 loc) · 8.83 KB
/
main.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Utility for managing LLM task->model configurations in ~/.config/llm-manager/llm.conf and /etc/llm.conf.
#
import argparse
import os
import sys
import pwd
from pathlib import Path
from typing import Dict, List
VERSION = "1.2.0"
def get_user_config_file() -> Path:
"""
Get the path to the user's configuration file, handling cases where the script is run with sudo.
"""
sudo_user = os.environ.get("SUDO_USER")
if sudo_user:
try:
user_info = pwd.getpwnam(sudo_user)
home_dir = user_info.pw_dir
except KeyError:
print(
f"Error: Cannot find home directory for sudo user '{sudo_user}'.",
file=sys.stderr,
)
sys.exit(1)
else:
home_dir = os.environ.get("HOME", str(Path.home()))
return Path(home_dir) / ".config" / "llm-manager" / "llm.conf"
USER_CONFIG_FILE = get_user_config_file()
SYSTEM_CONFIG_FILE = Path("/etc/llm.conf")
COMMENT_MARKERS = ("#", "//")
def parse_arguments() -> argparse.Namespace:
"""
Parse command-line arguments and return the parsed namespace.
Implements a shortcut: if the first argument is not a subcommand,
treat it as 'get <task>'.
"""
parser = argparse.ArgumentParser(
description="Manage LLM model configurations in ~/.config/llm-manager/llm.conf and /etc/llm.conf.",
epilog="Shortcut:\n llm-manager <task> Equivalent to 'llm-manager get <task>'",
formatter_class=argparse.RawTextHelpFormatter,
)
subparsers = parser.add_subparsers(dest="subcommand", title="Commands")
parser_set = subparsers.add_parser(
"set", help="Set or update the model for a specific task"
)
parser_set.add_argument(
"task", type=str, help="Type of the task (e.g., text-generation)"
)
parser_set.add_argument("model", type=str, help="Model value (e.g., gemma2:2b)")
parser_get = subparsers.add_parser("get", help="Get the model for a specific task")
parser_get.add_argument("task", type=str, help="Type of the task to retrieve")
parser_show = subparsers.add_parser("show", help="Show all task-to-model mappings")
parser.add_argument(
"--version",
"-v",
action="version",
version=f"llm-manager version {VERSION}",
help="Show the version of this utility and exit",
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
# If the first argument is not a subcommand or option, treat it as 'get <task>'
if sys.argv[1] not in ["set", "get", "show", "-h", "--help", "-v", "--version"]:
# Insert 'get' as the command
sys.argv.insert(1, "get")
args = parser.parse_args()
return args
def read_config_file(config_file: Path) -> List[str]:
"""
Read the given configuration file and return a list of its lines.
Preserves comments and blank lines.
"""
if config_file.is_file():
try:
with config_file.open("r", encoding="utf-8") as f:
return f.readlines()
except PermissionError:
print(
f"Warning: Permission denied while reading {config_file}.",
file=sys.stderr,
)
return []
except Exception as e:
print(f"Warning: Error reading {config_file}: {e}", file=sys.stderr)
return []
else:
return []
def write_config_file(config_file: Path, lines: List[str]) -> None:
"""
Write the list of lines back to the given configuration file.
Overwrites the existing file.
"""
try:
with config_file.open("w", encoding="utf-8") as f:
for line in lines:
if not line.endswith("\n"):
line += "\n"
f.write(line)
# Set file permissions to read/write for the user only
os.chmod(config_file, 0o600)
except PermissionError:
print(
f"Error: Permission denied while writing to {config_file}.", file=sys.stderr
)
sys.exit(1)
except Exception as e:
print(f"Error: Error writing to {config_file}: {e}", file=sys.stderr)
sys.exit(1)
def parse_config(lines: List[str]) -> Dict[str, str]:
"""
Parse the configuration lines into a dictionary of task to model mappings.
"""
config = {}
for line in lines:
stripped = line.strip()
# Skip empty lines
if not stripped:
continue
# Skip comments
if stripped.startswith(COMMENT_MARKERS):
continue
if "=" in stripped:
key, value = stripped.split("=", 1)
key = key.strip()
value = value.strip()
if key and value:
config[key] = value
else:
print(
f"Warning: Ignoring invalid line in config: {line.strip()}",
file=sys.stderr,
)
else:
print(
f"Warning: Ignoring invalid line in config: {line.strip()}",
file=sys.stderr,
)
continue # Skip lines without the assignment operator
return config
def validate_input(value: str) -> bool:
"""
Validate the task or model input to prevent invalid entries.
"""
if "=" in value or "\n" in value or value.startswith(COMMENT_MARKERS):
return False
return True
def set_model(task: str, model: str) -> None:
"""
Set or update the model for the given task.
Writes only to the user configuration file.
"""
if not validate_input(task) or not validate_input(model):
print("Error: Invalid characters in task or model name.", file=sys.stderr)
sys.exit(1)
# Read user config only
lines = read_config_file(USER_CONFIG_FILE)
updated = False
new_lines = []
for line in lines:
stripped = line.strip()
# Skip empty lines and comments
if not stripped or stripped.startswith(COMMENT_MARKERS):
new_lines.append(line)
continue
# Check if the line contains the assignment operator
if "=" in stripped:
key, _ = stripped.split("=", 1)
key = key.strip()
if key == task:
# Update the line with the new model
new_line = f"{task} = {model}\n"
new_lines.append(new_line)
updated = True
else:
new_lines.append(line)
else:
new_lines.append(line)
if not updated:
# Add the new task at the end
if new_lines and not new_lines[-1].endswith("\n"):
new_lines.append("\n")
new_line = f"{task} = {model}\n"
new_lines.append(new_line)
# Ensure the directory exists
try:
USER_CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True)
except Exception as e:
print(f"Error: Error creating configuration directory: {e}", file=sys.stderr)
sys.exit(1)
# Write back to user config file
write_config_file(USER_CONFIG_FILE, new_lines)
if updated:
print(f"Updated {task} = {model}")
else:
print(f"Set {task} = {model}")
def get_model(task: str) -> None:
"""
Retrieve and print the model for the given task.
"""
# Read system config
system_lines = read_config_file(SYSTEM_CONFIG_FILE)
system_config = parse_config(system_lines)
# Read user config
user_lines = read_config_file(USER_CONFIG_FILE)
user_config = parse_config(user_lines)
# Merge configurations, with user config overriding system config
merged_config = system_config.copy()
merged_config.update(user_config)
model = merged_config.get(task)
if model:
print(model)
else:
print(f"{task} is not set.")
sys.exit(1)
def show_config() -> None:
"""
Display all task-to-model configurations, with user configurations overriding system configurations.
"""
# Read system config
system_lines = read_config_file(SYSTEM_CONFIG_FILE)
system_config = parse_config(system_lines)
# Read user config
user_lines = read_config_file(USER_CONFIG_FILE)
user_config = parse_config(user_lines)
# Merge the configs, with user config overriding system config
merged_config = system_config.copy()
merged_config.update(user_config)
if merged_config:
for task, model in merged_config.items():
print(f"{task} = {model}")
else:
print("No configurations found.")
def main() -> None:
args = parse_arguments()
if args.subcommand == "set":
set_model(args.task, args.model)
elif args.subcommand == "get":
get_model(args.task)
elif args.subcommand == "show":
show_config()
else:
print("Error: No command provided.", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()