-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
executable file
·125 lines (100 loc) · 3.45 KB
/
chat.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import openai
import os
import pyperclip
import curses
from colorama import Fore, Style
from rich.console import Console
from rich.markdown import Markdown
class Chatbot:
def __init__(self):
openai.api_key = os.getenv('OPENAI_API_KEY')
self.MODEL = "gpt-4"
self.default_history = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
self.messages = self.default_history.copy()
self.count = 1
self.request = ""
self.blank_lines = 0
def chat_completion(self):
response = openai.ChatCompletion.create(model=self.MODEL, messages=self.messages, stream=True)
print(Fore.WHITE)
console = Console(no_color=True)
answer = ""
for chunk in response:
finish_reason = chunk['choices'][0]['finish_reason']
if finish_reason == 'stop':
break
content = chunk['choices'][0]['delta']['content']
answer = answer + content
console.print(content, end="")
print(Style.RESET_ALL)
return answer
def process_query(self, query):
new_messages = self.messages
new_messages.append({'role': 'user', 'content': query})
answer = self.chat_completion()
self.messages = new_messages
self.messages.append({'role': 'assistant', 'content': answer})
self.count = self.count + 1
print()
def paste(self):
try:
text = pyperclip.paste()
print(text)
self.request = self.request + text + '\n'
except Exception as e:
print(e)
def reset(self):
self.messages = self.default_history.copy()
self.count = 1
self.request = ""
def add_line(self, line):
self.request = self.request + line + '\n'
def get_input(self):
try:
print(Fore.WHITE + f"[{self.count}]> " + Fore.YELLOW, end="")
line = input()
if line == "/help" or line == "/?":
help()
return True
if line == "/new" or line == "/":
self.reset()
os.system('clear')
return True
if line == "/paste" or line == "/p":
self.paste()
self.blank_lines = 0
return True
if line == "/exit" or line == '/quit' or line == '/q':
return False
if line == ".":
self.process_query(self.request.strip()) # strip trailing empty lines before processing
self.blank_lines = 0
return True
if line.strip() == "" and self.request.strip() != "":
self.blank_lines += 1
if self.blank_lines >= 2:
self.process_query(self.request.strip())
self.blank_lines = 0
else:
self.blank_lines = 0
self.add_line(line)
return True
except Exception as e:
print(e)
return True
def help():
print()
print("Enter a multi-line prompt")
print("Enter two blank lines or '.' to process the prompt")
print("")
print("commands:")
print("/new or / - reset the history")
print("/quit /exit /q - to exit")
print("/paste - paste from clipboard")
print()
if __name__ == '__main__':
bot = Chatbot()
running = True
while (running):
running = bot.get_input()