forked from ThatHackerDudeFromCyberspace/Virtu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
168 lines (139 loc) · 5.97 KB
/
models.py
File metadata and controls
168 lines (139 loc) · 5.97 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import openai
import time
import json
import re
import os
# Handle all the AI Stuff
class OpenAICompletionModel():
def __init__(self, apiKey):
self.apiKey = apiKey
self.defaultApiKey = apiKey
# Initialise OpenAI
openai.api_key = self.apiKey
self.engines = openai.Engine.list()
##for engine in self.engines.data:
## if (engine.object == "engine" and engine.ready):
## print(engine.id)
## else:
## pass
# Initialise "addons"
self.memory = [
"Virtu is a large language model trained by OpenAI. It is designed to be a chatbot, and should not autocomplete prompts. Do not autocomplete, complete or edit prompts in any way. knowledge cutoff: 2021-09 Current date: December 10 2022 Browsing: disabled"
]
# Define initialisation prompts
self.initialisationPrompts = {}
availablePromptFiles = os.listdir("./initialisationPrompts/")
for promptFile in availablePromptFiles:
if (promptFile.split('.')[-1] == 'txt'):
with open(os.path.join("./initialisationPrompts", promptFile), 'r') as file:
promptData = file.read()
self.initialisationPrompts['.'.join(promptFile.split('.')[:-1])] = promptData.split("===")
# Define AI Options
self.defaultConfig = {
"engine": "text-davinci-003",
"temperature": 0.5,
"max_tokens": 512,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"useMemory": True
}
self.config = self.defaultConfig
# Timeout stuff
self.timeoutReason = ""
self.timeout = 0
# "Premium" Stuff
self.premiumMode = False
# Premium stuff
def enablePremiumMode(self, apiKey):
if (apiKey != self.defaultApiKey):
try:
openai.api_key = apiKey
self.engines = openai.Engine.list()
self.premiumMode = True
self.apiKey = apiKey
return True
except:
return False
else:
return False
# Reset memory
def resetMemory(self):
self.memory = self.memory[:1]
# Import ChatGPT history
def importMemory(self, memoryToImport):
try:
self.resetMemory()
currentChatItem = "User: "
for memoryItem in memoryToImport:
self.memory.append(currentChatItem + memoryItem)
if (currentChatItem == "User: "):
currentChatItem = "Response: "
else:
currentChatItem = "User: "
if (currentChatItem != "User: "):
prompt = self.memory[-1]
self.memory = self.memory[:-1]
response = self.processPrompt(prompt)
response += "\n\n" + "Chat Imported Successfuly"
else:
response = "Chat Imported Successfuly"
return response
except Exception as e:
return "Error Importing Chat: " + str(e)
# Actual AI part
def processPrompt(self, prompt):
if (time.time() < self.timeout):
return "Timed out - Please wait [" + str(round(self.timeout - time.time())) + "] seconds...\nReason:\n" + self.timeoutReason
# Add prompt to memory
self.memory.append("User: " + prompt)
self.memory.append("Response: ")
# Haha big brain go brrrrrrr
error = True
try:
openai.api_key = self.apiKey
completion = openai.Completion.create(
engine=self.config["engine"],
prompt='\n'.join(self.memory), #prompt
temperature=self.config["temperature"],
max_tokens=self.config["max_tokens"],
top_p=self.config["top_p"],
frequency_penalty=self.config["frequency_penalty"],
presence_penalty=self.config["presence_penalty"],
)
response = completion.choices[0].text
if (len(response.strip()) >= 9 and response.strip().lower()[:9] == "response: "):
response = re.sub('response: ', '', response, 1, re.I)
elif (len(response.strip()) >= 9 and response.strip().lower()[:9] == "response:"):
response = re.sub('response:', '', response, 1, re.I)
elif ("response:" in response.lower()):
response = re.sub('response:', '', response, 1, re.I)
response = response.lstrip()
#print("Response: " + response)
# Add response to memory and return it
self.memory[-1] = "Response: " + response
error = False
except openai.error.RateLimitError as e:
response = "[RATELIMITED - PLEASE WAIT 30 SECONDS]\n`" + str(e) + "`"
self.timeout = time.time() + 30
self.timeoutReason = "OpenAI rate limited"
except openai.error.InvalidRequestError as e:
print(e)
response = "[HISTORY FULL - PLEASE RESET]"
if (not self.premiumMode and not error):
self.timeout = time.time() + 30
self.timeoutReason = "--__Remove timeouts with Virtu Premium:__--\nLiterally just provide your own api key, see more info in `/config Premium Mode`"
return response
def processInitialisationPrompt(self, prompt):
self.resetMemory()
response = ""
try:
with open(os.path.join("./initialisationPrompts", prompt + "_config.json"), 'r') as aiConfigJSON:
self.config = json.loads(aiConfigJSON.read())
except:
self.config = self.defaultConfig
print("Initialised, Using config:", self.config)
for promptToInitialiseWith in self.initialisationPrompts[prompt]:
response = self.processPrompt(promptToInitialiseWith)
#print('\n'.join(self.memory))
return response