11import argparse
22import json
3+ import os
34from discordai_modelizer import __version__ as version
45from discordai_modelizer import customize
56from 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
70100def display (obj ):
0 commit comments