-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·65 lines (52 loc) · 1.64 KB
/
main.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
import openai
from dotenv import load_dotenv
from colorama import Fore
import os
# Constants
MODEL_ENGINE = "gpt-4o-mini"
MESSAGE_SYSTEM = "You are a helpful assistant"
messages = [{"role": "system", "content": MESSAGE_SYSTEM}]
# Load the environment variables - set up the OpenAI API client
load_dotenv()
client = openai.OpenAI()
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_chat_completion(user_input=""):
messages.append({"role": "user", "content": user_input})
response = client.Completion.create(
engine=MODEL_ENGINE,
messages=messages,
max_tokens=150,
)
message = response.choices[0].message
messages.append(message)
print(Fore.GREEN + "Bot: " + messages.choices[0].messag.content)
def main():
while True:
print("\n")
print("----------------------------------------\n")
print(" *** 🤖 WELCOME TO THE AI-CHATBOT *** ")
print("\n----------------------------------------")
print("\n================* MENU *================\n")
print("[1]- Start Chat")
print("[2]- Exit")
choice = input("Enter your choice: ")
if choice == "1":
start_chat()
elif choice == "2":
exit()
else:
print("Invalid choice")
def start_chat():
print("to end chat, type 'x'")
print("\n")
print(" NEW CHAT ")
print("---------------------")
while True:
user_input = input(Fore.WHITE + "You: ")
if user_input.lower() == "x":
main()
break
else:
generate_chat_completion(user_input)
if __name__ == "__main__":
main()