Skip to content

Commit e0615ce

Browse files
authored
Merge pull request #49 from A-Baji/dev
3.0.3
2 parents 89377cd + fd04223 commit e0615ce

File tree

14 files changed

+239
-250
lines changed

14 files changed

+239
-250
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.
44

5+
## [3.0.3] - 07-1-2024
6+
7+
### Changed
8+
9+
- update env value handling
10+
- make command help strs dynamic
11+
- reorganize cli to allow reuse in parent
12+
513
## [3.0.2] - 06-29-2024
614

715
### Added
@@ -95,6 +103,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
95103

96104
- switched to `pathlib` for file path parsing
97105

106+
[3.0.3]: https://github.com/A-Baji/discordAI-modelizer/compare/3.0.2...3.0.3
98107
[3.0.2]: https://github.com/A-Baji/discordAI-modelizer/compare/3.0.1...3.0.2
99108
[3.0.1]: https://github.com/A-Baji/discordAI-modelizer/compare/3.0.0...3.0.1
100109
[3.0.0]: https://github.com/A-Baji/discordAI-modelizer/compare/2.0.1...3.0.0

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
ARG PY_VER
22
FROM python:${PY_VER}
33
WORKDIR /main
4-
RUN apt-get install git -y
54
COPY ./requirements.txt ./setup.py ./README.md /main/
65
COPY ./discordai_modelizer /main/discordai_modelizer
76
RUN pip3 install --upgrade pip

discordai_modelizer/command_line/command_line.py

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,43 @@
55
from discordai_modelizer import customize
66
from discordai_modelizer import openai as openai_wrapper
77
from discordai_modelizer.command_line import subparsers
8+
from discordai_modelizer.command_line.subparsers import (
9+
set_openai_help_str,
10+
set_bot_key_help_str,
11+
)
812

913

10-
def discordai_modelizer():
11-
parser = argparse.ArgumentParser(
12-
prog="discordai_modelizer", description="DiscordAI Modelizer CLI"
13-
)
14-
parser.add_argument(
15-
"-V", "--version", action="version", version=f"discordai {version}"
16-
)
14+
def setup_modelizer_commands(parser, is_parent=False):
1715
command = parser.add_subparsers(dest="command")
1816

19-
model = command.add_parser(
20-
"model", description="Commands related to your openAI models"
21-
)
22-
job = command.add_parser("job", description="Commands related to your openAI jobs")
17+
model = command.add_parser("model", help="Manage your OpenAI models")
18+
job = command.add_parser("job", help="Manage your OpenAI jobs")
2319

2420
model_subcommand = model.add_subparsers(dest="subcommand")
2521
job_subcommand = job.add_subparsers(dest="subcommand")
2622

27-
subparsers.setup_model_list(model_subcommand)
28-
subparsers.setup_model_create(model_subcommand)
29-
subparsers.setup_model_delete(model_subcommand)
23+
subparsers.setup_model_list(model_subcommand, is_parent)
24+
subparsers.setup_model_create(model_subcommand, is_parent)
25+
subparsers.setup_model_delete(model_subcommand, is_parent)
3026

31-
subparsers.setup_job_list(job_subcommand)
32-
subparsers.setup_job_info(job_subcommand)
33-
subparsers.setup_job_events(job_subcommand)
34-
subparsers.setup_job_cancel(job_subcommand)
27+
subparsers.setup_job_list(job_subcommand, is_parent)
28+
subparsers.setup_job_info(job_subcommand, is_parent)
29+
subparsers.setup_job_events(job_subcommand, is_parent)
30+
subparsers.setup_job_cancel(job_subcommand, is_parent)
31+
32+
return command, model_subcommand, job_subcommand
3533

36-
args = parser.parse_args()
37-
if hasattr(args, "openai_key"):
38-
openai_wrapper.set_openai_api_key(args.openai_key)
3934

35+
def read_modelizer_args(args, model_subcommand, job_subcommand):
4036
if args.command == "model":
4137
if args.subcommand == "list":
42-
display(openai_wrapper.list_models(os.environ["OPENAI_API_KEY"], args.full))
38+
display(openai_wrapper.list_models(args.openai_key, args.full))
4339
elif args.subcommand == "create":
4440
customize.create_model(
4541
args.channel,
4642
args.user,
4743
args.discord_token,
48-
os.environ["OPENAI_API_KEY"],
44+
args.openai_key,
4945
thought_time=args.thought_time,
5046
thought_max=args.thought_max,
5147
thought_min=args.thought_min,
@@ -59,39 +55,68 @@ def discordai_modelizer():
5955
use_existing=args.use_existing,
6056
)
6157
elif args.subcommand == "delete":
62-
display(
63-
openai_wrapper.delete_model(args.model_id, os.environ["OPENAI_API_KEY"])
64-
)
58+
display(openai_wrapper.delete_model(args.model_id, args.openai_key))
6559
else:
6660
raise argparse.ArgumentError(
6761
model_subcommand,
6862
"Must choose a command from `list`, `create`, or `delete`",
6963
)
7064
elif args.command == "job":
7165
if args.subcommand == "list":
72-
display(openai_wrapper.list_jobs(os.environ["OPENAI_API_KEY"], args.full))
66+
display(openai_wrapper.list_jobs(args.openai_key, args.full))
7367
elif args.subcommand == "info":
74-
display(
75-
openai_wrapper.get_job_info(args.job_id, os.environ["OPENAI_API_KEY"])
76-
)
68+
display(openai_wrapper.get_job_info(args.job_id, args.openai_key))
7769
elif args.subcommand == "events":
78-
display(
79-
openai_wrapper.get_job_events(args.job_id, os.environ["OPENAI_API_KEY"])
80-
)
70+
display(openai_wrapper.get_job_events(args.job_id, args.openai_key))
8171
elif args.subcommand == "cancel":
82-
display(
83-
openai_wrapper.cancel_job(args.job_id, os.environ["OPENAI_API_KEY"])
84-
)
72+
display(openai_wrapper.cancel_job(args.job_id, args.openai_key))
8573
else:
8674
raise argparse.ArgumentError(
8775
job_subcommand,
88-
"Must choose a command from `info`, `events`, or `cancel`",
76+
"Must choose a command from `list`, `info`, `events`, or `cancel`",
8977
)
9078

9179

9280
def display(obj):
9381
print(json.dumps(obj, indent=4))
9482

9583

84+
def set_openai_api_key(key: str, obj, is_parent=False):
85+
if not key and not obj.get("OPENAI_API_KEY"):
86+
raise ValueError(
87+
f"Your OpenAI API key must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
88+
)
89+
else:
90+
return key or obj.get("OPENAI_API_KEY")
91+
92+
93+
def set_bot_token(token: str, obj, is_parent=False):
94+
if not token and not obj.get("DISCORD_BOT_TOKEN"):
95+
raise ValueError(
96+
f"Your Discord bot token must either be passed in as an argument or set {set_bot_key_help_str(is_parent)}",
97+
)
98+
else:
99+
return token or obj.get("DISCORD_BOT_TOKEN")
100+
101+
102+
def discordai_modelizer():
103+
parser = argparse.ArgumentParser(
104+
prog="discordai_modelizer", description="DiscordAI Modelizer CLI"
105+
)
106+
parser.add_argument(
107+
"-V", "--version", action="version", version=f"discordai-modelizer {version}"
108+
)
109+
110+
command, model_subcommand, job_subcommand = setup_modelizer_commands(parser)
111+
112+
args = parser.parse_args()
113+
if hasattr(args, "openai_key"):
114+
args.openai_key = set_openai_api_key(args.openai_key, os.environ)
115+
if hasattr(args, "discord_token"):
116+
args.discord_token = set_bot_token(args.discord_token, os.environ)
117+
118+
read_modelizer_args(args, model_subcommand, job_subcommand)
119+
120+
96121
if __name__ == "__main__":
97122
discordai_modelizer()

discordai_modelizer/command_line/subparsers.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
from argparse import _SubParsersAction, ArgumentParser
1+
def set_openai_help_str(is_parent=False):
2+
return f"{'in your config' if is_parent else 'as the OPENAI_API_KEY environment variable'}"
23

34

4-
def setup_model_list(model_subcommand):
5+
def set_bot_key_help_str(is_parent=False):
6+
return f"{'in your config' if is_parent else 'as the DISCORD_BOT_TOKEN environment variable'}"
7+
8+
9+
def setup_model_list(model_subcommand, is_parent=False):
510
model_list = model_subcommand.add_parser(
6-
"list", description="List your openAi customized models"
11+
"list", help="List your openAi customized models"
712
)
813
model_list_required_named = model_list.add_argument_group(
914
"required named arguments"
@@ -17,7 +22,7 @@ def setup_model_list(model_subcommand):
1722
"--openai-key",
1823
type=str,
1924
dest="openai_key",
20-
help="The openAI API key to list the models for. Must either be passed in as an argument or set as an environment variable",
25+
help=f"The openAI API key to list the models for. Must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
2126
)
2227
model_list_optional_named.add_argument(
2328
"--full",
@@ -28,10 +33,10 @@ def setup_model_list(model_subcommand):
2833
)
2934

3035

31-
def setup_model_create(model_subcommand):
36+
def setup_model_create(model_subcommand, is_parent=False):
3237
model_create = model_subcommand.add_parser(
3338
"create",
34-
description="Create a new openAI customized model by downloading the specified chat logs, parsing them into a usable dataset, and then training a customized model using openai",
39+
help="Create a new openAI customized model by downloading the specified chat logs, parsing them into a usable dataset, and then training a customized model using openai",
3540
)
3641
model_create_required_named = model_create.add_argument_group(
3742
"required named arguments"
@@ -45,14 +50,14 @@ def setup_model_create(model_subcommand):
4550
"--discord-token",
4651
type=str,
4752
dest="discord_token",
48-
help="The discord token for your bot. Must either be passed in as an argument or set as an environment variable",
53+
help=f"The discord token for your bot. Must either be passed in as an argument or set {set_bot_key_help_str(is_parent)}",
4954
)
5055
model_create_required_named.add_argument(
5156
"-o",
5257
"--openai-key",
5358
type=str,
5459
dest="openai_key",
55-
help="The openAI API key to use to create the model. Must either be passed in as an argument or set as an environment variable",
60+
help=f"The openAI API key to use to create the model. Must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
5661
)
5762
model_create_required_named.add_argument(
5863
"-c",
@@ -164,10 +169,10 @@ def setup_model_create(model_subcommand):
164169
)
165170

166171

167-
def setup_model_delete(model_subcommand):
172+
def setup_model_delete(model_subcommand, is_parent=False):
168173
model_delete = model_subcommand.add_parser(
169174
"delete",
170-
description="Delete an openAI customized model",
175+
help="Delete an openAI customized model",
171176
)
172177
model_delete_required_named = model_delete.add_argument_group(
173178
"required named arguments"
@@ -178,7 +183,7 @@ def setup_model_delete(model_subcommand):
178183
"--openai-key",
179184
type=str,
180185
dest="openai_key",
181-
help="The openAI API key associated with the model to delete. Must either be passed in as an argument or set as an environment variable",
186+
help=f"The openAI API key associated with the model to delete. Must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
182187
)
183188
model_delete_required_named.add_argument(
184189
"-m",
@@ -190,9 +195,9 @@ def setup_model_delete(model_subcommand):
190195
)
191196

192197

193-
def setup_job_list(job_subcommand):
198+
def setup_job_list(job_subcommand, is_parent=False):
194199
job_list = job_subcommand.add_parser(
195-
"list", description="List your openAI customization jobs"
200+
"list", help="List your openAI customization jobs"
196201
)
197202
job_list_required_named = job_list.add_argument_group("required named arguments")
198203
job_list_optional_named = job_list.add_argument_group("optional named arguments")
@@ -202,7 +207,7 @@ def setup_job_list(job_subcommand):
202207
"--openai-key",
203208
type=str,
204209
dest="openai_key",
205-
help="The openAI API key to list the jobs for. Must either be passed in as an argument or set as an environment variable",
210+
help=f"The openAI API key to list the jobs for. Must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
206211
)
207212
job_list_optional_named.add_argument(
208213
"--full",
@@ -213,9 +218,9 @@ def setup_job_list(job_subcommand):
213218
)
214219

215220

216-
def setup_job_info(job_subcommand):
221+
def setup_job_info(job_subcommand, is_parent=False):
217222
job_info = job_subcommand.add_parser(
218-
"info", description="Get an openAI customization job's info"
223+
"info", help="Get an openAI customization job's info"
219224
)
220225
job_info_required_named = job_info.add_argument_group("required named arguments")
221226

@@ -224,7 +229,7 @@ def setup_job_info(job_subcommand):
224229
"--openai-key",
225230
type=str,
226231
dest="openai_key",
227-
help="The openAI API key associated with the job to see the info for. Must either be passed in as an argument or set as an environment variable",
232+
help=f"The openAI API key associated with the job to see the info for. Must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
228233
)
229234
job_info_required_named.add_argument(
230235
"-j",
@@ -236,9 +241,9 @@ def setup_job_info(job_subcommand):
236241
)
237242

238243

239-
def setup_job_events(job_subcommand):
244+
def setup_job_events(job_subcommand, is_parent=False):
240245
job_events = job_subcommand.add_parser(
241-
"events", description="Get an openAI customization job's events"
246+
"events", help="Get an openAI customization job's events"
242247
)
243248
job_events_required_named = job_events.add_argument_group(
244249
"required named arguments"
@@ -249,7 +254,7 @@ def setup_job_events(job_subcommand):
249254
"--openai-key",
250255
type=str,
251256
dest="openai_key",
252-
help="The openAI API key associated with the job to see the events for. Must either be passed in as an argument or set as an environment variable",
257+
help=f"The openAI API key associated with the job to see the events for. Must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
253258
)
254259
job_events_required_named.add_argument(
255260
"-j",
@@ -261,9 +266,9 @@ def setup_job_events(job_subcommand):
261266
)
262267

263268

264-
def setup_job_cancel(job_subcommand):
269+
def setup_job_cancel(job_subcommand, is_parent=False):
265270
job_cancel = job_subcommand.add_parser(
266-
"cancel", description="Cancel an openAI customization job"
271+
"cancel", help="Cancel an openAI customization job"
267272
)
268273
job_cancel_required_named = job_cancel.add_argument_group(
269274
"required named arguments"
@@ -274,7 +279,7 @@ def setup_job_cancel(job_subcommand):
274279
"--openai-key",
275280
type=str,
276281
dest="openai_key",
277-
help="The openAI API key associated with the job to cancel. Must either be passed in as an argument or set as an environment variable",
282+
help=f"The openAI API key associated with the job to cancel. Must either be passed in as an argument or set {set_openai_help_str(is_parent)}",
278283
)
279284
job_cancel_required_named.add_argument(
280285
"-j",

0 commit comments

Comments
 (0)