77from openai import OpenAI
88from argparse import ArgumentError
99from discordai_modelizer .gen_dataset import parse_logs , get_lines
10+ from discordai_modelizer .openai import set_openai_api_key
1011
1112MODEL_MAP = {
1213 "davinci" : "davinci-002" ,
1314 "babbage" : "babbage-002" ,
1415}
1516
1617
18+ def set_bot_token (token ):
19+ if token :
20+ os .environ ["DISCORD_BOT_TOKEN" ] = token
21+ elif "DISCORD_BOT_TOKEN" not in os .environ :
22+ raise ValueError (
23+ "Your Discord bot token must either be passed in as an argument or set as an environment variable" ,
24+ )
25+
26+
1727def create_model (
18- bot_token : str ,
19- openai_key : str ,
2028 channel_id : str ,
2129 user_id : str ,
30+ bot_token : str = None ,
31+ openai_key : str = None ,
2232 thought_time = 10 ,
2333 thought_max : int = None ,
2434 thought_min = 4 ,
2535 max_entry_count = 1000 ,
26- offset = "start" ,
36+ offset = 0 ,
2737 select_mode = "sequential" ,
2838 base_model = "none" ,
2939 reverse = False ,
3040 clean = False ,
3141 redownload = False ,
3242 use_existing = False ,
3343):
44+ set_openai_api_key (openai_key )
3445 client = OpenAI ()
3546 channel_user = f"{ channel_id [:4 ]} _{ user_id } "
3647 files_path = pathlib .Path (appdirs .user_data_dir (appname = "discordai" ))
@@ -43,13 +54,7 @@ def create_model(
4354
4455 # Download logs
4556 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- )
57+ set_bot_token (bot_token )
5358
5459 print ("INFO: Exporting chat logs using DiscordChatExporter..." )
5560 print (
@@ -71,7 +76,7 @@ def create_model(
7176 "-c" ,
7277 channel_id ,
7378 "-t" ,
74- bot_token ,
79+ os . environ [ "DISCORD_BOT_TOKEN" ] ,
7580 "-o" ,
7681 f"{ channel_id } _logs.json" ,
7782 "-f" ,
@@ -140,9 +145,6 @@ def create_model(
140145
141146 # Clean up generated files
142147 if clean and not use_existing :
143- try :
144- os .remove (full_dataset_path )
145- except FileNotFoundError :
146- pass
148+ full_dataset_path .unlink ()
147149
148150 client .close ()
0 commit comments