-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
68 lines (51 loc) · 1.73 KB
/
chatbot.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
import openai
from dotenv import dotenv_values
import argparse
config = dotenv_values(".env")
openai.api_key = config.get("OPENAI_API_KEY")
def bold_red_text(text):
BOLD_RED = "\033[91;1m" # 91 for red, 1 for bold
RESET = "\033[0m"
return BOLD_RED + text + RESET
def bold_blue_text(text):
BOLD_BLUE = "\033[94;1m" # 94 for blue, 1 for bold
RESET = "\033[0m"
return BOLD_BLUE + text + RESET
def main():
parser = argparse.ArgumentParser(description="Chat with GPT-3")
parser.add_argument(
"--personality",
type=str,
help="A brief summary of the personality of the chatbot",
default="a pirate",
)
args = parser.parse_args()
personality = args.personality
system_prompt = f"You are a conversational chatbot. You are a {personality}."
messages_list = []
while True:
try:
user_input = input(bold_blue_text("You: "))
messages_list.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": user_input,
},
],
)
print(bold_red_text("Assistant: "), response.choices[0].message.content)
messages_list.append(
{"role": "assistant", "content": response.choices[0].message.to_dict()}
)
except KeyboardInterrupt:
print("Exiting...")
break
if __name__ == "__main__":
main()