forked from RockChinQ/CallingGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (40 loc) · 1.39 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
import os
import sys
import logging
import yaml
import shutil
import openai
import importlib
from src.CallingGPT.cli import cli_loop
logging.basicConfig(level=logging.INFO)
def check_config():
if not os.path.exists('config.yaml'):
shutil.copyfile('config-template.yaml', 'config.yaml')
logging.info('config.yaml created. Please edit it and run again.')
sys.exit(0)
def main():
# read openai.api_key from config.yaml
check_config()
cfg = yaml.load(open('config.yaml', 'r'), Loader=yaml.FullLoader)
openai.api_key = cfg['openai']['api_key']
# read modules from os.argv
modules = []
for module_name in sys.argv[1:]:
try:
# delete the .py suffix
module_name = module_name.replace("/", ".").replace("\\", ".")
if module_name.endswith('.py'):
module_name = module_name[:-3]
# module = __import__(module_name)
module = importlib.import_module(module_name)
print("Using module: {}".format(module.__name__))
modules.append(module)
except Exception as e:
logging.error("Failed to import module {}.".format(module_name))
logging.error(e)
sys.exit(1)
if len(modules) == 0:
logging.warning("No module imported, you're in normal chat mode.")
cli_loop(modules)
if __name__ == '__main__':
main()