-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbetabot.py
267 lines (254 loc) · 10.1 KB
/
betabot.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import aiml,os,socket,statistics,openai
from py2neo import Graph
from glob import glob
from datetime import datetime
from transformers import pipeline
from dotenv import load_dotenv
load_dotenv()
openai_api = os.getenv("openai_api")
# all variables here
name = "Your input "
input_text = ""
graph = Graph("bolt://localhost:7687", auth=("neo4j", "12345678"))
betabot = aiml.Kernel()
for file in glob("aiml/*.aiml"):
betabot.learn(file)
lambdaCounter = 0
tempCounter = 0
senti_pipe = pipeline(
"sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment"
)
breaker = 1
# openai.api_key = (
# openai_api # make sure to uncomment 'if try in msg' after adding your api key
# )
# ====================
# menu function
def menu(name):
input_text = input(f"{name}> ")
return input_text
def timer():
now = datetime.now()
time = now.strftime("%H:%M:%S")
return time
# Enter the main input/output loop
print("\nHi I am Betabot! Your problem solver, my current capabilities are:\n")
print(
"-> Calculation (write your expression after calculate/solve (i.e. calculate/solve 5+5 )\n-> Basic Chat\n\n\t\tSay Bye before leaving\n"
)
input_text = menu(name)
if input_text != "bye":
from minibots.login_bot import getLogin
resp = getLogin(input_text, graph)
if resp != None:
if "sp" in resp:
print(f"I have allocated you id: '{resp[1]}', you can use this for future.")
name = resp[0].title()
os.mkdir(f"memory/long_term/{name}_{resp[1]}")
print(f"Got it: {name}")
else:
name = resp[0].title()
print(f"Welcome back, {name}")
f = open(f"memory/long_term/{name}_{resp[1]}/episode_{resp[2]}.csv", "w")
f.close()
else:
name = "Your input "
lambdaCounter = graph.evaluate("match (an:AnonymousMemory) return (an.noUsers)")
if lambdaCounter:
lambdaCounter += 1
f = open(f"memory/short_term/anonymous/episode_{lambdaCounter}.csv", "w")
f.close()
else:
anId = graph.evaluate(
"create (an:AnonymousMemory {name:$name,noUsers:$noUsers}) return id(an) ",
name="Anonymous",
noUsers=1,
)
graph.run(
f"match (an:AnonymousMemory),(b:Bot) where id(an) = {anId} and b.id = 0 "
"merge (b)-[:hasAnonymousMemory]->(an)"
)
lambdaCounter = 1
dir = "memory/short_term/anonymous"
if not os.path.exists(dir):
os.mkdir(f"memory/short_term/anonymous")
# -----------------------------------
if resp != None: # This part is for known users
f = open(f"memory/long_term/{name}_{resp[1]}/episode_{resp[2]}.csv", "w")
emotions = list()
while breaker:
betabot.setPredicate("name", name)
betabot.setBotPredicate("master", "Athar")
tempCounter += 1
comb_data = "h" + str(tempCounter)
f.write(comb_data)
f.write("\n")
f.write(input_text)
f.write("\n")
comb_emotions = "hemotion" + str(tempCounter)
f.write(comb_emotions)
f.write("\n")
emotions.append(senti_pipe(input_text)[0]["label"])
f.write(senti_pipe(input_text)[0]["label"])
f.write("\n")
comb_time = "htime" + str(tempCounter)
f.write(comb_time)
f.write("\n")
f.write(timer())
f.write("\n")
if "calculate" in input_text or "solve" in input_text:
from minibots.calculator_bot import callCalculate
msg = str(callCalculate(name, input_text))
else:
msg = betabot.respond(input_text, "Betabot")
# if "try" in msg or "Try" in msg:
# msg = openai.Completion.create(engine="text-davinci-003",prompt=input_text,max_tokens=50,temperature=0.7,n=1,stop = None)
# msg = msg.choices[0].text.strip()
print(f"Betabot> {msg}")
comb_data = "b" + str(tempCounter)
f.write(comb_data)
f.write("\n")
f.write(msg)
f.write("\n")
comb_emotions = "bemotion" + str(tempCounter)
f.write(comb_emotions)
f.write("\n")
emotions.append(senti_pipe(msg)[0]["label"])
f.write(senti_pipe(msg)[0]["label"])
f.write("\n")
comb_time = "btime" + str(tempCounter)
f.write(comb_time)
f.write("\n")
f.write(timer())
f.write("\n")
input_text = menu(name)
if input_text == "bye" or input_text == "good bye":
breaker = 0
container = list()
f.close()
f = open(f"memory/long_term/{name}_{resp[1]}/episode_{resp[2]}.csv", "r")
store = ""
for data in f.read():
if data != "\n":
store += data
else:
container.append(store)
store = ""
f.close()
keyList = list()
valueList = list()
for i in range(len(container)):
if i % 2 == 0:
keyList.append(container[i])
else:
valueList.append(container[i])
userData = str(dict(zip(keyList, valueList)))
emotion = statistics.mode(emotions)
data = {
"episode": resp[2],
"ip": socket.gethostbyname(socket.gethostname()),
"emotion": emotion,
"message": userData,
}
epi = graph.evaluate(
"create (epi:Episode {episode:$episode,ip:$ip,emotion:$emotion,message:$message}) return id(epi)",
**data,
)
userId = resp[1]
resp = graph.evaluate(
f"match (p:User)-[:hasEpisodes]->(epm:EpisodicMemory) where p.id = {userId} return id(epm) "
)
graph.run(f"match (p:User) where p.id = {userId} set p.status = 0")
graph.run(
f"match (epi:Episode),(epm:EpisodicMemory) where id(epm) = {resp} and id(epi) = {epi} "
"merge (epm)-[:has]->(epi) "
)
print("Betabot> Bye!!")
else: # This part is for unknown users
if lambdaCounter > 0:
f = open(f"memory/short_term/anonymous/episode_{lambdaCounter}.csv", "w")
emotions = list()
while breaker:
betabot.setPredicate("name", name)
betabot.setBotPredicate("master", "Athar")
tempCounter += 1
comb_data = "h" + str(tempCounter)
f.write(comb_data)
f.write("\n")
f.write(input_text)
f.write("\n")
comb_emotions = "hemotion" + str(tempCounter)
f.write(comb_emotions)
f.write("\n")
emotions.append(senti_pipe(input_text)[0]["label"])
f.write(senti_pipe(input_text)[0]["label"])
f.write("\n")
comb_time = "htime" + str(tempCounter)
f.write(comb_time)
f.write("\n")
f.write(timer())
f.write("\n")
if "calculate" in input_text or "solve" in input_text:
from minibots.calculator_bot import callCalculate
msg = str(callCalculate(name, input_text))
else:
msg = betabot.respond(input_text, "Betabot")
# if "try" in msg or "Try" in msg:
# msg = openai.Completion.create(engine="text-davinci-003",prompt=input_text,max_tokens=50,temperature=0.7,n=1,stop = None)
print(f"Betabot> {msg}")
comb_data = "b" + str(tempCounter)
f.write(comb_data)
f.write("\n")
f.write(msg)
f.write("\n")
comb_emotions = "bemotion" + str(tempCounter)
f.write(comb_emotions)
f.write("\n")
emotions.append(senti_pipe(msg)[0]["label"])
f.write(senti_pipe(msg)[0]["label"])
f.write("\n")
comb_time = "btime" + str(tempCounter)
f.write(comb_time)
f.write("\n")
f.write(timer())
f.write("\n")
input_text = menu(name)
if input_text == "bye" or input_text == "good bye":
breaker = 0
container = list()
f.close()
f = open(f"memory/short_term/anonymous/episode_{lambdaCounter}.csv", "r")
store = ""
for data in f.read():
if data != "\n":
store += data
else:
container.append(store)
store = ""
f.close()
keyList = list()
valueList = list()
for i in range(len(container)):
if i % 2 == 0:
keyList.append(container[i])
else:
valueList.append(container[i])
userData = str(dict(zip(keyList, valueList)))
emotion = statistics.mode(emotions)
data = {
"episode": lambdaCounter,
"ip": socket.gethostbyname(socket.gethostname()),
"emotion": emotion,
"message": userData,
}
epi = graph.evaluate(
"create (epi:AnonymousEpisode:User {episode:$episode,ip:$ip,emotion:$emotion,message:$message}) return id(epi)",
**data,
)
graph.run(
f"match (epi:AnonymousEpisode),(anony:AnonymousMemory) where id(epi) = {epi} "
"merge (anony)-[:hasAnonymousEpisode]->(epi) "
)
print("Betabot> Bye!!")
else:
print("Betabot> Bye!!")