-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
66 lines (51 loc) · 1.54 KB
/
cli.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import argparse
import logging
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from chatgpt import ChatGPT
from config import init_config
from commands import ChatGPTCommand
# globals
chatgpt = None
commands = None
def eval_command(user_input):
splited = user_input.split()
if len(splited) == 0:
return
# eval command
if splited[0] in commands.cmds:
cmd, args = splited[0], splited[1:]
logging.debug(f"{cmd} {args}")
commands(cmd, args)
else:
rsp = commands.session.chat(user_input)
if rsp:
print(f"<ChatGPT>: {rsp.content}")
def main(args):
global chatgpt, commands
config = init_config(args.config)
# start commandline chatting
chatgpt = ChatGPT(
config=config.openai,
save_root=config.save.root,
save_mode=config.get("save_mode", "auto"),
max_history=config.get("init_max_history", 10)
)
user = os.getenv("USER")
session = chatgpt.get_session(user)
commands = ChatGPTCommand(chatgpt, session, user, config)
print(f'[System]: Prompt - "{session.system}"')
while True:
try:
user_input = prompt("<User>: ", history=FileHistory('.history.txt'))
eval_command(user_input)
except KeyboardInterrupt:
pass
except EOFError:
chatgpt.save()
print("Good bye!")
exit(0)
except Exception as e:
import traceback
traceback.print_exc()