-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
114 lines (85 loc) · 2.66 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import subprocess
import importlib
import ctypes
import sys
import os
def set_window_title(title):
ctypes.windll.kernel32.SetConsoleTitleW(title)
executable_dir = os.path.dirname(os.path.abspath(__file__))
version = "v1.0.9 BETA"
set_window_title("FreePyChatGPT " + version)
def clear_console():
if os.name == "nt":
_ = os.system("cls")
else:
_ = os.system("clear")
print("FREEPYCHATGPT: Checking if dependencies are installed...")
def install_module(module_name):
try:
importlib.import_module(module_name)
except ImportError:
print(f"FREEPYCHATGPT: {module_name} not found!")
print(f"FREEPYCHATGPT: Installing {module_name}...")
subprocess.check_call(
[sys.executable, "-m", "pip", "install", module_name])
required_modules = ["colorama", "requests", "json", "time"]
for module in required_modules:
install_module(module)
print("\nImporting Modules...")
import requests
print("\n'Requests' imported!")
import json
print("\n'JSON' imported!")
import time
print("\n'Time' imported!")
import colorama as color
color.init()
print("\n'Colorama' imported!")
time.sleep(0.5)
clear_console()
print(color.Fore.GREEN +
f"Welcome to FreePyChatGPT by Kathool! {version}\n" +
color.Style.RESET_ALL)
conversation_history = []
def get_completions(prompt):
global conversation_history
url = "https://free.churchless.tech/v1/chat/completions" #url = "https://chatgpt-api.shn.hk/v1/" # Alternate API Endpoint
headers = {
"Content-Type": "application/json",
}
data = {
"messages": conversation_history + [{
"role": "user",
"content": prompt
}],
"max_tokens": 100,
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json()
else:
return None
def user_input():
global conversation_history
prompt_text = input(color.Fore.YELLOW + "\nYou: " + color.Style.RESET_ALL)
conversation_history.append({
"role":
"system",
"content":
"You are an AI language model. Please respond with a greeting."
})
conversation_history.append({"role": "user", "content": prompt_text})
response_data = get_completions(prompt_text)
return response_data
while True:
response_data = user_input()
if response_data:
response_str = response_data["choices"][0]["message"]["content"]
conversation_history.append({"role": "assistant", "content": response_str})
print(color.Fore.CYAN + "\nAI: " + color.Style.RESET_ALL + response_str)
else:
print(color.Fore.CYAN + "\nAI: " + color.Fore.RED +
"Sorry, there was an error processing your request." +
color.Style.RESET_ALL)
break
input("Press Enter to exit...")