TheDunkinNinja
commited on
Commit
•
2ebbddd
1
Parent(s):
19d9abc
Upload TAD.py
Browse files
TAD.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ollama
|
2 |
+
import chromadb
|
3 |
+
import speech_recognition as sr
|
4 |
+
import requests
|
5 |
+
import pyttsx3
|
6 |
+
|
7 |
+
|
8 |
+
client = chromadb.Client()
|
9 |
+
|
10 |
+
message_history = [
|
11 |
+
|
12 |
+
{
|
13 |
+
'id' : 1,
|
14 |
+
'prompt' : 'What is your name?',
|
15 |
+
'response' : 'My name is TADBot, a bot to help with short term remedial help for mental purposes. '
|
16 |
+
|
17 |
+
},
|
18 |
+
|
19 |
+
{
|
20 |
+
'id' : 2,
|
21 |
+
'prompt' : 'Bye',
|
22 |
+
'response' : 'Good to see you get better. Hopefully you reach out to me if you have any problems.'
|
23 |
+
|
24 |
+
},
|
25 |
+
|
26 |
+
{
|
27 |
+
'id' : 3,
|
28 |
+
'prompt' : 'What is the essence of Life?',
|
29 |
+
'response' : 'The essence of life is to create what you want of yourself.'
|
30 |
+
|
31 |
+
}
|
32 |
+
]
|
33 |
+
convo = []
|
34 |
+
|
35 |
+
modelname = "ms"
|
36 |
+
|
37 |
+
|
38 |
+
def create_vector_db(conversations):
|
39 |
+
|
40 |
+
vector_db_name = 'conversations'
|
41 |
+
|
42 |
+
try:
|
43 |
+
client.delete_collection(vector_db_name)
|
44 |
+
except ValueError as e:
|
45 |
+
pass
|
46 |
+
|
47 |
+
vector_db = client.create_collection(name=vector_db_name)
|
48 |
+
for c in conversations:
|
49 |
+
|
50 |
+
serialized_convo = 'prompt: ' + c["prompt"] + ' response: ' + c["response"]
|
51 |
+
|
52 |
+
response = ollama.embeddings(model = "nomic-embed-text",prompt = serialized_convo)
|
53 |
+
embedding = response["embedding"]
|
54 |
+
|
55 |
+
vector_db.add(ids = [str(c['id'])], embeddings = [embedding], documents = [serialized_convo])
|
56 |
+
|
57 |
+
|
58 |
+
def stream_response(prompt):
|
59 |
+
|
60 |
+
convo.append({'role': "user", 'content': prompt})
|
61 |
+
output = ollama.chat(model = modelname, messages = convo)
|
62 |
+
response = output['message']['content']
|
63 |
+
|
64 |
+
print("TADBot: ")
|
65 |
+
print(response)
|
66 |
+
engine = pyttsx3.init()
|
67 |
+
engine.say(response)
|
68 |
+
engine.runAndWait()
|
69 |
+
|
70 |
+
convo.append({'role': "assistant", 'content': response})
|
71 |
+
|
72 |
+
def retrieve_embeddings(prompt):
|
73 |
+
response = ollama.embeddings(model = "nomic-embed-text", prompt = prompt)
|
74 |
+
propmt_embedding = response['embedding']
|
75 |
+
|
76 |
+
vector_db = client.get_collection(name = 'conversations')
|
77 |
+
results = vector_db.query(query_embeddings=[propmt_embedding], n_results = 1)
|
78 |
+
|
79 |
+
best_embedding = results['documents'][0][0]
|
80 |
+
return best_embedding
|
81 |
+
|
82 |
+
create_vector_db(message_history)
|
83 |
+
|
84 |
+
|
85 |
+
while True:
|
86 |
+
|
87 |
+
r = sr.Recognizer()
|
88 |
+
m = sr.Microphone()
|
89 |
+
|
90 |
+
try:
|
91 |
+
|
92 |
+
|
93 |
+
print("Say something!")
|
94 |
+
with m as source:
|
95 |
+
audio = r.listen(source)
|
96 |
+
|
97 |
+
|
98 |
+
try:
|
99 |
+
# for testing purposes, we're just using the default API key
|
100 |
+
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
|
101 |
+
# instead of `r.recognize_google(audio)`
|
102 |
+
prompt = r.recognize_google(audio)
|
103 |
+
print("Tadbot thinks you said: " + prompt)
|
104 |
+
except sr.UnknownValueError:
|
105 |
+
print("Tadbot could not understand audio")
|
106 |
+
except sr.RequestError as e:
|
107 |
+
print("Could not request results from Google Speech Recognition service; {0}".format(e))
|
108 |
+
|
109 |
+
|
110 |
+
print("Please wait...")
|
111 |
+
with m as source:
|
112 |
+
r.adjust_for_ambient_noise(source)
|
113 |
+
|
114 |
+
if prompt == "bye" or prompt == "Bye":
|
115 |
+
print("TADBot: Hopefully I was able to help you out today. Have a Nice Day!")
|
116 |
+
break
|
117 |
+
"""
|
118 |
+
context = retrieve_embeddings(prompt)
|
119 |
+
|
120 |
+
prompt = prompt + "CONTEXT FROM EMBEDDING: " + context
|
121 |
+
"""
|
122 |
+
stream_response(prompt)
|
123 |
+
except KeyboardInterrupt:
|
124 |
+
pass
|