-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatGPTbot.py
76 lines (63 loc) · 2.33 KB
/
chatGPTbot.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
import os
import glob
import openai
import textract
class Chatbot:
def __init__(self):
self.openai_api_key = os.getenv("OPENAI_API_KEY")
self.chat_history = []
def append_to_chat_history(self, message):
self.chat_history.append(message)
def read_personal_file(self, file_path):
try:
text = textract.process(file_path).decode("utf-8")
return text
except Exception as e:
print(f"Error reading file {file_path}: {e}")
return ""
def collect_user_data(self):
data_directory = "./data"
data_files = glob.glob(os.path.join(data_directory, "*.*"))
user_data = ""
for file in data_files:
file_extension = os.path.splitext(file)[1].lower()
if file_extension in (".pdf", ".docx", ".xlsx", ".xls"):
user_data += self.read_personal_file(file)
else:
with open(file, "r", encoding="utf-8") as f:
user_data += f.read() + "\n"
return user_data
def create_chat_response(self, message):
self.append_to_chat_history(message)
user_data = self.collect_user_data()
messages = [
{"role": "system", "content": "You are the most helpful assistant."},
{"role": "user", "content": message},
{"role": "assistant", "content": message},
]
if user_data:
messages.append({"role": "user", "content": user_data})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=messages,
temperature=1,
max_tokens=256,
top_p=1,
n=1,
stop=None,
frequency_penalty=0,
presence_penalty=0
)
self.append_to_chat_history(response.choices[0].message.content.strip())
return response.choices[0].message.content.strip()
def start_chatting(self):
while True:
user_input = input("User: ")
if user_input.lower() == "exit":
print("Chatbot: Goodbye!")
break
bot_response = self.create_chat_response(user_input)
print("Chatbot:", bot_response)
# Create an instance of the Chatbot class and start the conversation
chatbot = Chatbot()
chatbot.start_chatting()