-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (64 loc) · 2.23 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python3
import openai
from dotenv import load_dotenv
import os
import subprocess
import time
try:
from colorama import init, Fore
except ImportError:
print(Fore.Red + "Colorama is not installed. Installing it...")
subprocess.run(["pip", "install", "colorama"], check=True)
from colorama import init, Fore
# Initialize colorama
init(autoreset=True)
# Install OpenAI 0.28 for error handling
subprocess.run(["pip", "install", "openai==0.28"])
# Specify the path to your .env file inside the 'src' folder
env_file_path = 'src/.env'
# Load environment variables from the specified .env file
load_dotenv(dotenv_path=env_file_path)
# Check if the script is running as root
print(Fore.BLUE + "Checking if the script is running as root!")
time.sleep(2)
if os.geteuid() != 0:
print(Fore.YELLOW + "This script is not running as root, please run it as root!")
exit(1)
time.sleep(1)
# Clear the terminal screen
os.system("clear")
import datetime
now = datetime.datetime.now()
print(Fore.BLUE + "Current date and time:")
print(Fore.CYAN + now.strftime("%d-%m-%Y %H:%M:%S"))
time.sleep(2)
import sys
print(Fore.GREEN + "Python version:")
print(Fore.RED + sys.version)
print(Fore.LIGHTMAGENTA_EX + "Python version:", sys.version_info)
time.sleep(2)
api_key = os.getenv("API_KEY")
if not api_key:
print(Fore.RED + "API_KEY not found in the environment variables.")
sys.exit(1)
openai.api_key = api_key
def chat_with_gpt3(prompt):
response = openai.Completion.create(
model="gpt-3.5-turbo", # You can experiment with different models
prompt=prompt,
max_tokens=150 # Adjust based on your desired response length
)
return response.choices[0].text.strip()
def main():
print(Fore.LIGHTMAGENTA_EX + "Welcome! You can chat with ChatGPT. Type 'exit' to end the conversation.")
conversation_history = ""
while True:
user_input = input(Fore.LIGHTCYAN_EX + "You: ")
if user_input.lower() == 'exit':
break
conversation_history += f"You: {user_input}\n"
response = chat_with_gpt3(conversation_history)
print(Fore.LIGHTBLUE_EX + f"ChatGPT: {response}")
conversation_history += f"ChatGPT: {response}\n"
if __name__ == "__main__":
main()