Skip to content

Commit 94a23eb

Browse files
authored
Merge pull request #47 from A-Baji/dev
3.0.1
2 parents 3c946ef + 1fc2ae3 commit 94a23eb

File tree

7 files changed

+93
-30
lines changed

7 files changed

+93
-30
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
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.1] - 06-27-2024
6+
7+
### Changed
8+
9+
- improve cli argument handling
10+
511
## [3.0.0] - 06-26-2024
612

713
### Added
@@ -75,6 +81,7 @@ Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
7581

7682
- switched to `pathlib` for file path parsing
7783

84+
[3.0.1]: https://github.com/A-Baji/discordAI-modelizer/compare/3.0.0...3.0.1
7885
[3.0.0]: https://github.com/A-Baji/discordAI-modelizer/compare/2.0.1...3.0.0
7986
[2.0.1]: https://github.com/A-Baji/discordAI-modelizer/compare/1.2.2...2.0.1
8087
[2.0.0]: https://github.com/A-Baji/discordAI-modelizer/compare/1.2.2...2.0.0

discordai_modelizer/command_line/command_line.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import json
3+
import os
34
from discordai_modelizer import __version__ as version
45
from discordai_modelizer import customize
56
from discordai_modelizer import openai as openai_wrapper
@@ -33,13 +34,21 @@ def discordai_modelizer():
3334
subparsers.setup_job_cancel(job_subcommand)
3435

3536
args = parser.parse_args()
37+
try:
38+
os.environ["OPENAI_API_KEY"] = args.openai_key or os.environ["OPENAI_API_KEY"]
39+
except KeyError:
40+
raise argparse.ArgumentError(
41+
None,
42+
"Your OpenaAI API key must either be passed in as an argument or set as an environment variable",
43+
)
44+
3645
if args.command == "model":
3746
if args.subcommand == "list":
38-
display(openai_wrapper.list_models(args.openai_key, args.full))
39-
if args.subcommand == "create":
47+
display(openai_wrapper.list_models(os.environ["OPENAI_API_KEY"], args.full))
48+
elif args.subcommand == "create":
4049
customize.create_model(
4150
args.discord_token,
42-
args.openai_key,
51+
os.environ["OPENAI_API_KEY"],
4352
args.channel,
4453
args.user,
4554
thought_time=args.thought_time,
@@ -54,17 +63,38 @@ def discordai_modelizer():
5463
redownload=args.redownload,
5564
use_existing=args.use_existing,
5665
)
57-
if args.subcommand == "delete":
58-
display(openai_wrapper.delete_model(args.openai_key, args.model_id))
66+
elif args.subcommand == "delete":
67+
display(
68+
openai_wrapper.delete_model(args.model_id, os.environ["OPENAI_API_KEY"])
69+
)
70+
else:
71+
raise argparse.ArgumentError(
72+
model_subcommand,
73+
"Must choose a command from `list`, `create`, or `delete`",
74+
)
5975
elif args.command == "job":
6076
if args.subcommand == "list":
61-
display(openai_wrapper.list_jobs(args.openai_key, args.full))
62-
if args.subcommand == "info":
63-
display(openai_wrapper.get_job_info(args.openai_key, args.job_id))
64-
if args.subcommand == "events":
65-
display(openai_wrapper.get_job_events(args.openai_key, args.job_id))
66-
if args.subcommand == "cancel":
67-
display(openai_wrapper.cancel_job(args.openai_key, args.job_id))
77+
display(openai_wrapper.list_jobs(os.environ["OPENAI_API_KEY"], args.full))
78+
elif args.subcommand == "info":
79+
display(
80+
openai_wrapper.get_job_info(args.job_id, os.environ["OPENAI_API_KEY"])
81+
)
82+
elif args.subcommand == "events":
83+
display(
84+
openai_wrapper.get_job_events(args.job_id, os.environ["OPENAI_API_KEY"])
85+
)
86+
elif args.subcommand == "cancel":
87+
display(
88+
openai_wrapper.cancel_job(args.job_id, os.environ["OPENAI_API_KEY"])
89+
)
90+
display(
91+
openai_wrapper.delete_model(args.model_id, os.environ["OPENAI_API_KEY"])
92+
)
93+
else:
94+
raise argparse.ArgumentError(
95+
job_subcommand,
96+
"Must choose a command from `info`, `events`, or `cancel`",
97+
)
6898

6999

70100
def display(obj):

discordai_modelizer/command_line/subparsers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from argparse import _SubParsersAction, ArgumentParser
2+
3+
14
def setup_model_list(model_subcommand):
25
model_list = model_subcommand.add_parser(
36
"list", description="List your openAi customized models"
@@ -42,7 +45,7 @@ def setup_model_create(model_subcommand):
4245
"--discord-token",
4346
type=str,
4447
dest="discord_token",
45-
help="The discord token for your bot",
48+
help="The discord token for your bot. Must either be passed in as an argument or set as an environment variable",
4649
)
4750
model_create_required_named.add_argument(
4851
"-o",
@@ -54,13 +57,15 @@ def setup_model_create(model_subcommand):
5457
model_create_required_named.add_argument(
5558
"-c",
5659
"--channel",
60+
required=True,
5761
type=str,
5862
dest="channel",
5963
help="The ID of the discord channel you want to use",
6064
)
6165
model_create_required_named.add_argument(
6266
"-u",
6367
"--user",
68+
required=True,
6469
type=str,
6570
dest="user",
6671
help="The unique username of the discord user you want to use",
@@ -178,6 +183,7 @@ def setup_model_delete(model_subcommand):
178183
model_delete_required_named.add_argument(
179184
"-m",
180185
"--model-id",
186+
required=True,
181187
type=str,
182188
dest="model_id",
183189
help="Target model id",
@@ -223,6 +229,7 @@ def setup_job_info(job_subcommand):
223229
job_info_required_named.add_argument(
224230
"-j",
225231
"--job-id",
232+
required=True,
226233
type=str,
227234
dest="job_id",
228235
help="Target job id",
@@ -247,6 +254,7 @@ def setup_job_events(job_subcommand):
247254
job_events_required_named.add_argument(
248255
"-j",
249256
"--job-id",
257+
required=True,
250258
type=str,
251259
dest="job_id",
252260
help="Target job id",
@@ -271,6 +279,7 @@ def setup_job_cancel(job_subcommand):
271279
job_cancel_required_named.add_argument(
272280
"-j",
273281
"--job-id",
282+
required=True,
274283
type=str,
275284
dest="job_id",
276285
help="Target job id",

discordai_modelizer/customize.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pathlib
66

77
from openai import OpenAI
8+
from argparse import ArgumentError
89
from discordai_modelizer.gen_dataset import parse_logs, get_lines
910

1011
MODEL_MAP = {
@@ -30,7 +31,6 @@ def create_model(
3031
redownload=False,
3132
use_existing=False,
3233
):
33-
os.environ["OPENAI_API_KEY"] = openai_key or os.environ["OPENAI_API_KEY"]
3434
client = OpenAI()
3535
channel_user = f"{channel_id[:4]}_{user_id}"
3636
files_path = pathlib.Path(appdirs.user_data_dir(appname="discordai"))
@@ -43,6 +43,14 @@ def create_model(
4343

4444
# Download logs
4545
if (not os.path.isfile(full_logs_path) or redownload) and not use_existing:
46+
try:
47+
bot_token = bot_token or os.environ["DISCORD_BOT_TOKEN"]
48+
except KeyError:
49+
raise ArgumentError(
50+
None,
51+
"Your Discord bot token must either be passed in as an argument or set as an environment variable",
52+
)
53+
4654
print("INFO: Exporting chat logs using DiscordChatExporter...")
4755
print(
4856
"INFO: This may take a few minutes to hours depending on the message count of the channel"

discordai_modelizer/openai.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ def convert_in_place(obj, key: str):
1616
return obj
1717

1818

19-
def list_jobs(openai_key: str, full=False) -> list[dict]:
20-
os.environ["OPENAI_API_KEY"] = openai_key or os.environ["OPENAI_API_KEY"]
19+
def set_openai_api_key(key: str):
20+
try:
21+
os.environ["OPENAI_API_KEY"] = key or os.environ["OPENAI_API_KEY"]
22+
except KeyError:
23+
raise ValueError(
24+
"Your OpenaAI API key must either be passed in as an argument or set as an environment variable",
25+
)
26+
27+
28+
def list_jobs(openai_key: str = None, full=False) -> list[dict]:
29+
set_openai_api_key(openai_key)
2130
client = OpenAI()
2231
finetunes = client.fine_tuning.jobs.list()
2332
client.close()
@@ -48,8 +57,8 @@ def list_jobs(openai_key: str, full=False) -> list[dict]:
4857
]
4958

5059

51-
def list_models(openai_key: str, full=False) -> list[dict]:
52-
os.environ["OPENAI_API_KEY"] = openai_key or os.environ["OPENAI_API_KEY"]
60+
def list_models(openai_key: str = None, full=False) -> list[dict]:
61+
set_openai_api_key(openai_key)
5362
client = OpenAI()
5463
finetunes = client.models.list()
5564
client.close()
@@ -62,8 +71,8 @@ def list_models(openai_key: str, full=False) -> list[dict]:
6271
]
6372

6473

65-
def get_job_info(openai_key: str, job_id: str) -> dict:
66-
os.environ["OPENAI_API_KEY"] = openai_key or os.environ["OPENAI_API_KEY"]
74+
def get_job_info(job_id: str, openai_key: str = None) -> dict:
75+
set_openai_api_key(openai_key)
6776
client = OpenAI()
6877
job = client.fine_tuning.jobs.retrieve(job_id)
6978
client.close()
@@ -77,8 +86,8 @@ def get_job_info(openai_key: str, job_id: str) -> dict:
7786
)
7887

7988

80-
def get_job_events(openai_key: str, job_id: str) -> list[dict]:
81-
os.environ["OPENAI_API_KEY"] = openai_key or os.environ["OPENAI_API_KEY"]
89+
def get_job_events(job_id: str, openai_key: str = None) -> list[dict]:
90+
set_openai_api_key(openai_key)
8291
client = OpenAI()
8392
events = client.fine_tuning.jobs.list_events(job_id).data
8493
client.close()
@@ -91,22 +100,22 @@ def get_job_events(openai_key: str, job_id: str) -> list[dict]:
91100
]
92101

93102

94-
def cancel_job(openai_key: str, job_id: str) -> dict:
95-
os.environ["OPENAI_API_KEY"] = openai_key or os.environ["OPENAI_API_KEY"]
103+
def cancel_job(job_id: str, openai_key: str = None) -> dict:
104+
set_openai_api_key(openai_key)
96105
client = OpenAI()
97106
client.fine_tuning.jobs.cancel(job_id)
98107
client.close()
99108
return {"result": f"Canceled fine-tuning job: {job_id}"}
100109

101110

102-
def delete_model(openai_key: str, model_name: str) -> dict:
111+
def delete_model(model_name: str, openai_key: str = None) -> dict:
103112
confirm = input(
104113
"Are you sure you want to delete this model? This action is not reversable. Y/N: "
105114
)
106115
if confirm.lower() not in ["y", "yes"]:
107116
print("Cancelling model deletion...")
108117
return
109-
os.environ["OPENAI_API_KEY"] = openai_key or os.environ["OPENAI_API_KEY"]
118+
set_openai_api_key(openai_key)
110119
client = OpenAI()
111120
deleted = client.models.delete(model_name).model_dump()
112121
client.close()

discordai_modelizer/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.0.0"
1+
__version__ = "3.0.1"

docker-compose.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# VERSION=$(cat discordai_modelizer/version.py | grep -oP '\d+\.\d+\.\d+') docker compose up --build
22
# docker exec -it discordai-modelizer-app-1 bash
33
# discordai_modelizer model list --full
4-
# discordai_modelizer model create -d $DISCORD_TOKEN -c $CHANNEL_ID -u "$USERNAME"
4+
# discordai_modelizer model create -d $DISCORD_BOT_TOKEN -c $CHANNEL_ID -u "$USERNAME"
55
# discordai_modelizer model delete -m ""
66
# discordai_modelizer job list --full
77
# discordai_modelizer job info -j ftjob-SDOuDLTs6a7xu9LMzdhJKo9w --events
@@ -14,14 +14,14 @@ services:
1414
network_mode: host
1515
working_dir: /main
1616
environment:
17-
- DISCORD_TOKEN
17+
- DISCORD_BOT_TOKEN
1818
- OPENAI_API_KEY
1919
- CHANNEL_ID
2020
- USERNAME
2121
- PYTHONUNBUFFERED=1
2222
env_file: ./.env
2323
volumes:
24-
- ./discordai_modelizer:/usr/local/lib/python3.11/site-packages/discordai_modelizer
24+
- ./discordai_modelizer:/usr/local/lib/python3.12/site-packages/discordai_modelizer
2525
- ./files/:/root/.local/share/discordai/
2626
command:
2727
- sh

0 commit comments

Comments
 (0)