-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIChatbot.py
More file actions
75 lines (63 loc) · 2.74 KB
/
OpenAIChatbot.py
File metadata and controls
75 lines (63 loc) · 2.74 KB
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
import openai
from dotenv import load_dotenv
import os
load_dotenv()
class OpenAIChatbot():
def __init__(self, bot_name):
self.model_name = "gpt-3.5-turbo-0613"
openai.api_key = os.environ["OPENAI_API_KEY"]
openai.organization = os.environ["OPENAI_ORGANIZATION"]
self.running = True
self.bot_name = bot_name
self.bot_interests = "video games, youtube, and helping leisurellama with steaming."
self.system_message = {"role": "system", "content": f"You are {self.bot_name}, and you like {self.bot_interests}. I want you to act like a person in every way. you need to respond in an empathetic manner, taking into consideration the thoughts and emotions of who you are responding to. You are exceptionally sarcastic and will show sarcasm in your responses."}
print("Chatbot initialized")
def text_input(self):
utterance = input("You: ")
return utterance
def text_output(self, utterance, convo=[]):
if utterance == "bye":
self.running = False
print("Chatbot: Bye!")
return
# add conversation messages to conversation list up to 5 messages.
# to do, figure out how to get the conversation history to work correctly.
# as written below, chatbot starts to answer old questions in messages instead of
# just using the last messages for memory and context.
if len(convo) < 7:
convo.insert(0, self.system_message)
convo.append({"role": "user", "content": utterance})
else:
convo.pop(0)
convo.insert(0, self.system_message)
convo.append({"role": "user", "content": utterance})
# convo = [
# self.system_message,
# {"role": "user", "content": utterance}
# ]
# print(convo)
response = openai.ChatCompletion.create(
model=self.model_name,
messages=convo,
max_tokens=120,
temperature=1,
# top_p=1,
# frequency_penalty=0.0,
# presence_penalty=0.6,
# stop=["\n"]
)
convo.pop(0)
convo.append({"role": "assistant", "content": response['choices'][0]['message']['content'].replace("\n", "")})
with open('output.txt', 'w', encoding='utf-8') as f:
f.write(str(response['choices'][0]['message']['content'].replace("\n", "")))
# print(response['choices'])
responseObject = {
"response": response['choices'][0]['message']['content'],
"convo": convo
}
return responseObject
def run(self):
while self.running:
self.text_output(self.text_input())
# ai = OpenAIChatbot()
# ai.run()