File size: 13,430 Bytes
0061c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import getpass
import faiss
import numpy as np
import io,re
import faiss
import warnings
import requests
from hugchat import hugchat
from rich import print as rp
from hugchat.login import Login
from dotenv import load_dotenv,find_dotenv
import speech_recognition
from TTS.api import TTS
from git import Repo
import time
from playsound import playsound
from langchain import hub
from langchain_core.documents import Document
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains import create_retrieval_chain
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.retrievers import ContextualCompressionRetriever
from langchain_community.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter, Language
from langchain.retrievers.document_compressors import DocumentCompressorPipeline
from langchain_community.document_transformers import EmbeddingsRedundantFilter
from langchain_text_splitters import CharacterTextSplitter
from langchain.retrievers.document_compressors import EmbeddingsFilter

from system_prompts import (default_rag_prompt,story_teller_prompt,todo_parser_prompt,
                            code_generator_prompt,software_tester_prompt,script_debugger_prompt,iteration_controller_prompt,copilot_prompt)
prompts={'default_rag_prompt':default_rag_prompt,
        'story_teller_prompt':story_teller_prompt,
        'todo_parser_prompt':todo_parser_prompt,
        'code_generator_prompt':code_generator_prompt,
        'software_tester_prompt':software_tester_prompt,
        'script_debugger_prompt':script_debugger_prompt,
        'iteration_controller_prompt':iteration_controller_prompt,
        'copilot_prompt':copilot_prompt
        }

load_dotenv(find_dotenv())
warnings.filterwarnings("ignore")
os.environ["USER_AGENT"] = os.getenv("USER_AGENT")

class LLMChatBot:
    def __init__(self, email, password, cookie_path_dir='./cookies/',default_llm=1):
        self.email = email
        self.password = password
        self.current_model = 1
        self.cookie_path_dir = cookie_path_dir
        self.cookies = self.login()
        self.chatbot = hugchat.ChatBot(cookies=self.cookies.get_dict(), 
                                       default_llm = default_llm, #CohereForAI/c4ai-command-r-plus
                                       )
        self.repo_url='https://github.com/langchain-ai/langchain'
        self.default_system_prompt = prompts['default_rag_prompt']
        self.conv_id = None
        self.latest_splitter=None
        self.setup_folders()
        self.embeddings=HuggingFaceEmbeddings(
            model_name="all-MiniLM-L6-v2",
            model_kwargs={'device': 'cpu'},
            encode_kwargs={'normalize_embeddings': True}
        )
        self.create_vectorstore_from_github()
      
        self.setup_retriever()
        self.setup_tts()
        self.setup_speech_recognition()

    def login(self):
        rp("Attempting to log in...")
        sign = Login(self.email, self.password)
        try:
            cookies = sign.login(cookie_dir_path=self.cookie_path_dir, save_cookies=True)
            rp("Login successful!")
            return cookies
        except Exception as e:
            rp(f"Login failed: {e}")
            rp("Attempting manual login with requests...")
            self.manual_login()
            raise

    def manual_login(self):
        login_url = "https://huggingface.co/login"
        session = requests.Session()
        response = session.get(login_url)
        rp("Response Cookies:", response.cookies)
        rp("Response Content:", response.content.decode())
        
        csrf_token = response.cookies.get('csrf_token')
        if not csrf_token:
            rp("CSRF token not found in cookies.")
            return
        
        login_data = {
            'email': self.email,
            'password': self.password,
            'csrf_token': csrf_token
        }
        
        response = session.post(login_url, data=login_data)
        if response.ok:
            rp("Manual login successful!")
        else:
            rp("Manual login failed!")
    def setup_speech_recognition(self):
        self.recognizer = speech_recognition.Recognizer()
    
    def setup_folders(self):
        self.dirs=["test_input"]
        for d in self.dirs:
            os.makedirs(d, exist_ok=True)

    def setup_tts(self, model_name="tts_models/en/ljspeech/fast_pitch"):
        self.tts = TTS(model_name=model_name)

    def __call__(self, text, system_prompt=""): # llama 3
        self.conv_id = self.chatbot.new_conversation(system_prompt=system_prompt, modelIndex=self.current_model, switch_to=True)
        return self.send_message(text)

    def send_message(self, message):
        message_result = self.chatbot.chat(message)
        return message_result.wait_until_done()

    def stream_response(self, message):
        for resp in self.chatbot.query(message, stream=True):
            rp(resp)

    def web_search(self, query):
        query_result = self.chatbot.query(query, web_search=True)
        results = []
        for source in query_result.web_search_sources:
            results.append({
                'link': source.link,
                'title': source.title,
                'hostname': source.hostname
            })
        return results

    def create_new_conversation(self,switch_to=True,  system_prompt = ""):
        self.chatbot.new_conversation(switch_to=switch_to, modelIndex = self.current_model, system_prompt = system_prompt)

    def get_remote_conversations(self):
        return self.chatbot.get_remote_conversations(replace_conversation_list=True)

    def get_local_conversations(self):
        return self.chatbot.get_conversation_list()

    def get_available_models(self):
        return self.chatbot.get_available_llm_models()

    def switch_model(self, index):
        self.chatbot.switch_llm(index)

    def switch_conversation(self, id):
        self.conv_id = id
        self.chatbot.change_conversation(self.conv_id)

    def get_assistants(self):
        return self.chatbot.get_assistant_list_by_page(1)

    def switch_role(self,system_prompt):
        self.chatbot.delete_all_conversations()
        return self.chatbot.new_conversation(switch_to=True, system_prompt=self.default_system_prompt)
    
    def listen_for_speech(self):
        with speech_recognition.Microphone() as source:
            print("Listening...")
            audio = self.recognizer.listen(source)
            
        try:
            text = self.recognizer.recognize_google(audio)
            print(f"You said: {text}")
            return text
        except speech_recognition.UnknownValueError:
            print("Sorry, I couldn't understand that.")
            return None
        except speech_recognition.RequestError as e:
            print(f"Could not request results from Google Speech Recognition service; {e}")
            return None

    def optimized_tts(self, text: str, output_file: str = "output.wav", speaking_rate: float = 3) -> str:
        start_time = time.time()

        self.tts.tts_to_file(
            text=text,
            emotion='scared',
            file_path=output_file,
            speaker=self.tts.speakers[0] if self.tts.speakers else None,
            speaker_wav="tortoise-tts/examples/favorites/emma_stone_courage.mp3",
            language=self.tts.languages[0] if self.tts.languages else None,
            speed=speaking_rate,
            split_sentences=True
        )

        end_time = time.time()
        print(f"TTS generation took {end_time - start_time:.2f} seconds")
        return output_file
    
    @staticmethod
    def Play(file_path):
        playsound(file_path)

    def add_documents_folder(self, folder_path):
        for root, _, files in os.walk(folder_path):
            for file in files:
                file_path = os.path.join(root, file)
                self.add_document(file_path)

    def add_document(self, file_path):
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
            document = Document(page_content=content)
            self.vector_store.add_documents([document])

    def add_document_from_url(self, url):
        response = requests.get(url)
        if response.status_code == 200:
            content = response.text
            document = Document(page_content=content)
            self.vector_store.add_documents([document])
        else:
            print(f"Failed to fetch URL content: {response.status_code}")

    def delete_document(self, document):
        if document in self.vector_store:
            self.vector_store.delete_document(document)
            print(f"Deleted document: {document}")
        else:
            print(f"Document not found: {document}")

    def _add_to_vector_store(self, name, content):
        document = Document(page_content=content)
        self.vector_store.add_documents([document])
        print(f"Added document to vector store: {name}")
        # Example of updating the vectorizer (you might need to adjust based on your actual implementation)
        self.vectorizer.fit_transform(self.vector_store.get_all_documents())
    
    def clone_github_repo(self, repo_url, local_path='./repo'):
        if os.path.exists(local_path):
            print("Repository already cloned.") 
            return local_path
        Repo.clone_from(repo_url, local_path)
        return local_path

    def load_documents_from_github(self, repo_url, file_types=['*.py', '*.md', '*.txt', '*.html']):
        local_repo_path = self.clone_github_repo(repo_url)
        loader = DirectoryLoader(path=local_repo_path, glob=f"**/{{{','.join(file_types)}}}", show_progress=True, recursive=True)
        return loader.load()

    def split_documents(self, documents: list,chunk_s=512,chunk_o=0):
        split_docs = []
        splitter=None
        for doc in documents:
            ext = os.path.splitext(getattr(doc, 'source', '') or getattr(doc, 'filename', ''))[1].lower()
            if ext == '.py':
                splitter = RecursiveCharacterTextSplitter.from_language(language=Language.PYTHON, chunk_size=chunk_s, chunk_overlap=chunk_o)
            elif ext in ['.md', '.markdown']:
                splitter = RecursiveCharacterTextSplitter.from_language(language=Language.MARKDOWN, chunk_size=chunk_s, chunk_overlap=chunk_o)
            elif ext in ['.html', '.htm']:
                splitter = RecursiveCharacterTextSplitter.from_language(language=Language.HTML, chunk_size=chunk_s, chunk_overlap=chunk_o)
            else:
                splitter = CharacterTextSplitter(chunk_size=chunk_s, chunk_overlap=chunk_o, add_start_index=True)
            
            split_docs.extend(splitter.split_documents([doc]))
        return split_docs,splitter


    def setup_retriever(self, k=5, similarity_threshold=0.76):
        self.retriever = self.vectorstore.as_retriever(k=k)
        splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=0, separator=". ")
        redundant_filter = EmbeddingsRedundantFilter(embeddings=self.embeddings)
        relevant_filter = EmbeddingsFilter(embeddings=self.embeddings, similarity_threshold=similarity_threshold)
        pipeline_compressor = DocumentCompressorPipeline(
            transformers=[splitter, redundant_filter, relevant_filter]
        )
        self.compression_retriever = ContextualCompressionRetriever(base_compressor=pipeline_compressor, base_retriever=self.retriever)

    def create_retrieval_chain(self):
        rag_prompt = hub.pull("langchain-ai/retrieval-qa-chat")
        combine_docs_chain = create_stuff_documents_chain(self.bot, rag_prompt)
        self.high_retrieval_chain = create_retrieval_chain(self.compression_retriever, combine_docs_chain)
        self.low_retrieval_chain = create_retrieval_chain(self.retriever, combine_docs_chain)

    def create_vectorstore_from_github(self):
        documents = self.load_documents_from_github(self.repo_url)
        split_docs,splitter = self.split_documents(documents,512,0)
        self.latest_splitter=splitter
        self.vectorstore = FAISS.from_documents(split_docs, self.embeddings)
        print(f"Vectorstore created with {len(split_docs)} documents.")

    def update_vectorstore(self, new_documents):
        split_docs,splitter = self.split_documents(new_documents)
        self.latest_splitter=splitter
        self.vectorstore.add_documents(split_docs)
        print(f"Vectorstore updated with {len(split_docs)} new documents.")
    

    def retrieve_with_chain(self, query, mode='high'):
        if mode == 'high':
            return self.high_retrieval_chain.invoke({"input": query})
        else:
            return self.low_retrieval_chain.invoke({"input": query})
if __name__ == '__main__':
    EMAIL = os.getenv("EMAIL")
    PASSWD =  os.getenv("PASSWD")
    model=1
    chatbot = LLMChatBot(EMAIL, PASSWD, default_llm=model)
    chatbot.create_new_conversation(system_prompt=chatbot.default_system_prompt, switch_to=True)
    #all_models=chatbot.get_available_models()
    #rp(all_models[chatbot.current_model].name) 
    results=chatbot("""Tel me a short crafting survival Scify story of K.U.T.H.O.E.R """)
    audio_path = chatbot.optimized_tts(str(results))
    chatbot.Play(audio_path)
    rp(results)