This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGPT.py
87 lines (68 loc) · 2.09 KB
/
ChatGPT.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
#
# Author: Sagar Biswas (sagar040)
#
# https://github.com/sagar040/chatGPT
#
# Version 2.0
from pygments.lexers import get_lexer_by_name
from pygments.formatters import Terminal256Formatter
from pygments import highlight
import style.banner as banner
import style.ColorCode as c
import Core.process as ps
import readline
import openai
import sys
import os
try:
import api.conf as conf
except ModuleNotFoundError:
print("run setup.sh first !")
sys.exit(1)
banner.print_banner()
def api(text):
completions = openai.Completion.create(
engine="text-davinci-002",
prompt=text,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
return completions.choices[0].text
def prompt():
while True:
try:
text = input(f"{c.ul}ChatGPT{c.e} > ")
print(c.e)
if text == "help":
ps.help_menu()
elif text == "clear":
os.system("clear")
elif text == ("save"):
try:
ps.save(text,resp)
except UnboundLocalError:
print(f"{c.r}E:{c.e} Nothing to save")
elif text == "exit":
sys.exit(0)
elif not text:
pass
else:
try:
resp = api(text)
final_syntax = ps.syntax(text)
if final_syntax == "text":
final_syntax = ps.syntax(resp)
print(f'{highlight(resp, lexer=get_lexer_by_name(final_syntax), formatter=Terminal256Formatter(style="monokai"))}')
except openai.error.AuthenticationError as e :
print(f"{c.r}E:{c.e} {e}")
except KeyboardInterrupt:
print(f"{c.r}Exit..{c.e}")
sys.exit(1)
openai.api_key = conf.key
if openai.api_key:
prompt()
else:
print(f"{c.r}E:{c.e} API key not set, run setup.sh file.")