File size: 3,436 Bytes
2ebbddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import ollama 
import chromadb
import speech_recognition as sr
import requests 
import pyttsx3


client = chromadb.Client()

message_history = [

    {
        'id' : 1,
        'prompt' : 'What is your name?',
        'response' : 'My name is TADBot,  a bot to help with short term remedial help for mental purposes. '

    },

    {
        'id' : 2,
        'prompt' : 'Bye',
        'response' : 'Good to see you get better. Hopefully you reach out to me if you have any problems.'

    },

    {
        'id' : 3,
        'prompt' : 'What is the essence of Life?',
        'response' : 'The essence of life is to create what you want of yourself.'

    }
]
convo = []

modelname = "ms"


def create_vector_db(conversations):

    vector_db_name = 'conversations'

    try: 
        client.delete_collection(vector_db_name)
    except ValueError as e:
        pass

    vector_db = client.create_collection(name=vector_db_name)
    for c in conversations:

        serialized_convo = 'prompt: ' + c["prompt"] + ' response: ' + c["response"]

        response = ollama.embeddings(model = "nomic-embed-text",prompt = serialized_convo)
        embedding = response["embedding"]

        vector_db.add(ids = [str(c['id'])], embeddings = [embedding], documents = [serialized_convo])


def stream_response(prompt):
    
    convo.append({'role': "user", 'content': prompt})
    output = ollama.chat(model = modelname, messages = convo)
    response = output['message']['content']

    print("TADBot: ") 
    print(response)
    engine = pyttsx3.init()
    engine.say(response)
    engine.runAndWait()

    convo.append({'role': "assistant", 'content': response})

def retrieve_embeddings(prompt):
    response = ollama.embeddings(model = "nomic-embed-text", prompt = prompt)
    propmt_embedding = response['embedding']

    vector_db = client.get_collection(name = 'conversations')
    results = vector_db.query(query_embeddings=[propmt_embedding], n_results = 1)

    best_embedding = results['documents'][0][0]
    return best_embedding

create_vector_db(message_history)


while True: 
    
    r = sr.Recognizer()
    m = sr.Microphone()
    
    try:


        print("Say something!")
        with m as source: 
            audio = r.listen(source)


        try:
            # for testing purposes, we're just using the default API key
            # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
            # instead of `r.recognize_google(audio)`
            prompt = r.recognize_google(audio)
            print("Tadbot thinks you said: " + prompt)
        except sr.UnknownValueError:
            print("Tadbot could not understand audio")
        except sr.RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e))


        print("Please wait...")
        with m as source: 
            r.adjust_for_ambient_noise(source)

        if  prompt == "bye" or prompt == "Bye": 
            print("TADBot: Hopefully I was able to help you out today. Have a Nice Day!")
            break
        """

        context = retrieve_embeddings(prompt)



        prompt = prompt + "CONTEXT FROM EMBEDDING: " + context

        """
        stream_response(prompt)
    except KeyboardInterrupt:
        pass