-
Notifications
You must be signed in to change notification settings - Fork 7
/
chatbot_12-6-23.py
519 lines (337 loc) · 11.6 KB
/
chatbot_12-6-23.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# 4-14-2023
# updated 12-2-2023 to use new openai api format and new openai text to speech
import warnings
import os
import openai
import pyttsx3
import speech_recognition as sr
import tkinter as tk
from tkinter.ttk import Label
from datetime import datetime
from tkinter import *
import time
from datetime import datetime
import pinecone
import json
import io
from openai import OpenAI
from pygame import mixer
import soundfile as sf
import sounddevice as sd
warnings.filterwarnings("ignore")
def clear_terminal():
os.system('cls')
clear_terminal()
USER_ID = 1
global engine
global APIKEY
global memory
global counter
r = sr.Recognizer()
global windowII
global textForWindowII
AWAKE = True
# Pinecone setup
ENVIRONMENT = 'us-east1-gcp'
pinecone.init(api_key=os.getenv('PINECONEKEY'), environment=ENVIRONMENT)
INDEX_NAME = 'imalive'
NUMBER_OF_RELEVANT_THINGS_TO_RETURN_FROM_PINECONE = 5
# OpenAI Setup
embed_model = "text-embedding-ada-002"
global client
client = OpenAI()
# Main running piece of code
def main():
global memory
memory = os.path.abspath(os.path.dirname(os.path.abspath(__file__))) + "\\" + "memory.txt"
pinecone.init(api_key=os.getenv('PINECONEKEY'), environment='us-east1-gcp')
global longTermMemory
longTermMemory = pinecone.Index(INDEX_NAME)
global counter
counter = 0
# Store your keys on your machine!
global APIKEY
APIKEY = os.getenv('OPENAI_API_KEY')
# Tkinter Setup
root = tk.Tk()
root.geometry('400x500')
root.resizable(False, False)
root.title('Prometheus v2.0')
label = Label(root, text='Click Button or Press Spacebar to Wake')
label.pack(ipadx=10, ipady=10)
lab = Label(root, text="")
inputLabel = Label(root, text = "You: ", wraplength = 300)
outputLabel = Label(root, text = "Response: ", wraplength = 300)
# Fenster 2
# Definition und Festlegung neues Fenster
toplevel = Toplevel()
toplevel.title('result')
toplevel.geometry('400x500')
# Create widgets in the new window
label = tk.Label(toplevel, text="Subconscious", fg='blue')
w = Text(toplevel, height=10, borderwidth=2)
w.insert(1.0, "Waiting to wake... ")
w.configure(inactiveselectbackground=w.cget("selectbackground"))
label.pack()
w.pack()
root.focus_set()
global windowII
windowII = toplevel
global textForWindowII
textForWindowII = w
def mic():
while AWAKE:
lab.config(text='Listening...')
printII('Listening...')
root.update()
#input
text = microphone2()
if text is None:
#printII('I thought I heard something but it was nothing.')
continue
elif text == '' or text == '...' or text == ' ':
#printII('I thought I heard something but it was nothing.')
continue
inputLabel.config(text = "You: \n\n{}".format(text))
lab.config(text='Responding...')
printII('responding')
root.update()
#response = speak(text)
response = queryOpenAITemplate3(text)
# ----- ElevenLabs
#voiceTest.speak(response)
# ----- Windows TTS
#talk(response)
#talk_openai(response)
talk_openai_stream(response)
uploadToMemory(text, response)
outputLabel.config(text = "Response: {}".format(response))
root.update()
lab.config(text='...')
root.update()
button = tk.Button(root, text="Click Me", command=mic)
button.pack(ipadx=10, ipady=10)
def createNew():
global counter
global memory
counter = 0
f = open(memory, 'w')
f.write("")
f.close()
def Sleep():
dream()
clear_terminal()
sleep_button = tk.Button(root, text="Sleep", command=Sleep)
sleep_button.pack(ipadx=10, ipady=10)
button2 = tk.Button(root, text="Restart Prometheus", command=createNew)
button2.pack(ipadx=10, ipady=10)
lab.pack(ipadx=10, ipady=10)
inputLabel.pack(ipadx=10, ipady=10)
outputLabel.pack(ipadx=10, ipady=10)
# Binds the spacebar to the microphone method
root.bind('<space>',lambda event:mic())
root.mainloop()
# Queries OpenAI
def queryOpenAITemplate3(prompt):
global counter
global memory
global APIKEY
global overallSentiment_human
f = open(memory, 'r')
lines = f.read()
f.close()
if len(lines) > 10000:
dream()
lines = ""
# Step 1: Retrieve relevant contexts for the prompt
contexts = retrieve_relevant_metadata(prompt)
contexts_as_strings = [json.dumps({**x, "metadata": json.dumps(x["metadata"], indent=2)}) for x in contexts]
# build our prompt with the retrieved contexts included
prompt_start = (
"This info from your longterm memory might be relevant to the convo.\n\n"
)
prompt_end = (
lines + "\n\n Human:\n\n{}\n\nme:".format(prompt)
)
new_prompt = (
prompt_start +
"\\n\\n---\\n\\n".join(contexts_as_strings) +
prompt_end
)
APIKEY = os.getenv('OPENAI_API_KEY')
openai.api_key = APIKEY
messages = [
{"role": "system", "content": "This is a conversation between Prometheus, a genius scientist and inventor. You are desperate to provide correct answers, improve the world, and make scientific progress. Speak from his POV. Do not ever break character. You have a long term memory which feeds you info when relevant."},
{"role": "user", "content": new_prompt}
]
printII("I think")
response = client.chat.completions.create(
model="gpt-4",
messages=messages
)
#print(response)
# Make them very nice
AI_response = response.choices[0].message.content.strip()
if "Human: " in AI_response:
AI_response = AI_response.split("Human:")[0]
# removes everything after the last occuring period
def remove_post_period(string):
if string[-1] == ".":
return string
else:
index = string.rfind(".")
return string[:index+1]
AI_response = remove_post_period(AI_response)
print("{}\n".format(AI_response))
printII('I speak')
new_prompt = lines + "\n\nHuman:{}\n\nMe:{}".format(prompt, AI_response)
f = open(memory,'w')
f.write(new_prompt)
f.close()
return str(AI_response)
# Queries Pinecone
def retrieve_relevant_metadata(query):
global APIKEY
openai.api_key = APIKEY
global longTermMemory
res = client.embeddings.create(
input=[query],
model=embed_model
)
# Check if 'data' key exists and it has at least one item
#print(res)
xq = res.data[0].embedding
# get relevant contexts
res = longTermMemory.query(xq, top_k=NUMBER_OF_RELEVANT_THINGS_TO_RETURN_FROM_PINECONE, include_metadata=True)
score_threshold = 0 # Set your desired threshold here
filtered_results = [
{"id": x.id, "score": x.score, "metadata": x.metadata}
for x in res['matches']
if x.get('score', 0) > score_threshold
]
# returns a list
return filtered_results
# Condenses the short term memory into a summary
def dream():
global memory
global APIKEY
f = open(memory, 'r')
memories = f.read()
#print(memories)
f.close()
global APIKEY
openai.api_key = APIKEY
new_prompt = "I am a summarizer for a chatbot named Prometheus. I am designed to remember names, dialogue, and other important information. I need to summarize text for better storage and return ONLY the summary:"
# Step 2: Generate the initial Python script using the refined prompt
messages = [
{"role": "system", "content": new_prompt},
{"role": "user", "content": memories}
]
printII("I dream")
# Make them very nice
response = client.chat.completions.create(
model="gpt-4",
messages=messages
)
dream = response.choices[0].message.content.strip()
f = open(memory,'w')
now = datetime.now()
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
f.write("Summary of last conversation:\n {}\n Continue from here.".format(dream))
f.close()
global counter
counter = 0
return
# Speaks with Windows TTS
def talk(text = None):
global counter
global engine
if counter == 0:
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
counter = counter + 1
engine.say(text)
engine.runAndWait()
def talk_openai(text = None):
print("text to speech")
client = OpenAI()
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=text,
)
response.stream_to_file("output.mp3")
mixer.init()
mixer.music.load('output.mp3')
mixer.music.play()
def talk_openai_stream(text = None):
print("text to speech")
def play_audio_stream_from_buffer(buffer):
# Use soundfile to read the audio data from the buffer
with sf.SoundFile(buffer, 'r') as sound_file:
data = sound_file.read(dtype='int16')
sd.play(data, sound_file.samplerate)
sd.wait() # Wait until the audio has finished playing
response = client.audio.speech.create(
model="tts-1",
voice="fable",
input=text,
response_format ="opus"
)
# Create an in-memory buffer to store the streamed audio data
buffer = io.BytesIO()
for chunk in response.iter_bytes(chunk_size=4096):
buffer.write(chunk)
buffer.seek(0)
#response.stream_to_file("output.mp3")
play_audio_stream_from_buffer(buffer)
# Listens with Google voice recognition
def microphone2():
while AWAKE:
with sr.Microphone() as source:
#print('listening...')
audio_data = r.listen(source)
try:
time.sleep(.4)
text = r.recognize_google(audio_data)
return text
except Exception as e:
#print("I didn't understand ya: {}".format(e))
continue
# Displays to Tkinter
def printII(text = None):
global windowII
global textForWindowII
nowString = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
textForWindowII.insert(1.0, '{} - {}\n'.format(nowString, text))
windowII.update()
# Function which returns last word
def lastWord(string):
if string == None:
return ''
#splitting the string
words = string.split()
output = words[-1]
return output.replace(".", "")
# Create an ID for the record in Pinecone
def generate_record_id(USER_ID):
timestamp = int(time.time())
record_id = f"{USER_ID}_{timestamp}"
return record_id
# 'Upsert' to Pinecone
def uploadToMemory(prompt=None, response=None):
global longTermMemory
record_id = generate_record_id(USER_ID)
try:
# Concatenate file name and content
masterVector = f"prompt: {prompt}\nresponse: {response}"
res = client.embeddings.create(input=[masterVector], model=embed_model)
embedding = res.data[0].embedding
metadata = {"prompt": prompt, "response": response}
to_upsert = [{"id": record_id, "values": embedding, "metadata": metadata}]
longTermMemory.upsert(vectors=to_upsert)
except Exception as e:
print("FAILURE: {}".format(e))
if __name__ == "__main__":
main()