-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
60 lines (52 loc) · 1.64 KB
/
chatbot.py
File metadata and controls
60 lines (52 loc) · 1.64 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
import os
import sys
import openai
import prompt_toolkit
from dotenv import load_dotenv
load_dotenv('./.env')
import textwrap
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_response(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=500,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.6,
n=1,
stop=None,
temperature=0.9,
)
message = response.choices[0].text.strip() # type: ignore
message = textwrap.fill(message, width=60)
numbered_response = ""
lines = message.split("\n")
for i, line in enumerate(lines):
numbered_response += str(i+1) + ". " + line + "\n"
return message
def chat_prompts():
while True:
endmsg = 'Goodbye! See you next time.'
user_input = input("You: ")
if user_input.lower() in ["bye", "goodbye", "end", "stop", "exit"]:
print("ChatBOT: ",(endmsg))
sys.exit()
else:
prompt = (f"User: {user_input} \nChatBOT: ")
response = generate_response(prompt)
print("\nChatBOT:",response)
print('')
continue
def main():
os.system('cls' if os.name == 'nt' else 'clear')
app_name = "ChatBOT (OpenAI)\n\nTo END session, type: 'bye, goodbye, end or exit"
print(f'{"-" * 48}')
print(f'{" " * 12}{app_name}{" " * 12}')
print(f'{"-" * 48}')
if not openai.api_key:
print("ERROR: No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
sys.exit()
chat_prompts()
if __name__ == '__main__':
main()