File size: 31,924 Bytes
ac6791c
 
92c2a99
 
 
 
 
 
 
 
 
 
 
 
 
56d9758
28a4048
 
5c2bc3a
28a4048
 
92c2a99
28a4048
af539d6
56d9758
92c2a99
003c901
92c2a99
 
 
 
 
 
637171e
92c2a99
 
 
 
 
 
 
 
 
 
 
 
 
02de959
 
 
 
 
 
d3e9e96
 
 
 
 
 
 
92c2a99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0600933
 
 
 
 
dce2ac0
 
 
 
92c2a99
13dfb45
92c2a99
003c901
6798f5d
 
 
bbf919d
ba47ef6
9d6edfb
6798f5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ca4304
6798f5d
b3e2548
92c2a99
 
 
 
13dfb45
5118516
43e8065
92c2a99
 
920908d
92c2a99
 
 
 
10b2753
92c2a99
5118516
92c2a99
 
10b2753
92c2a99
 
 
 
 
5118516
92c2a99
5118516
 
92c2a99
 
 
 
 
5118516
92c2a99
 
0ca4304
 
55302cd
92c2a99
 
 
f1a7d93
 
92c2a99
 
 
55302cd
92c2a99
 
 
0ca4304
92c2a99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ca4304
92c2a99
 
 
 
 
 
 
 
527aebe
0054da1
 
92c2a99
 
 
637171e
527aebe
0054da1
 
92c2a99
 
 
501ff3f
 
92c2a99
9fcc44d
92c2a99
 
 
0054da1
92c2a99
 
 
 
0ca4304
92c2a99
 
 
13dfb45
4e7d540
92c2a99
 
 
 
 
9fcc44d
92c2a99
 
 
 
 
 
0ca4304
 
92c2a99
 
 
 
 
 
 
 
7bd7476
0054da1
92c2a99
 
 
 
 
 
0054da1
92c2a99
 
 
 
 
 
ba47ef6
f11eb0a
92c2a99
 
0ca4304
 
 
92c2a99
 
 
 
 
0054da1
 
cae2ff4
92c2a99
1158d9c
92c2a99
 
0054da1
92c2a99
0ca4304
 
92c2a99
13dfb45
92c2a99
 
61f90b1
92c2a99
0ca4304
92c2a99
 
92d1706
92c2a99
 
 
 
 
 
 
 
78b8854
92c2a99
 
 
 
 
0600933
92c2a99
 
 
 
78b8854
92c2a99
 
 
 
5045ba4
92c2a99
 
 
93d7318
0ca4304
 
92c2a99
 
 
 
 
 
 
 
 
 
 
 
 
 
78b8854
92c2a99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b8854
92c2a99
93d7318
 
 
 
 
e679acf
92c2a99
0ca4304
 
e679acf
0569eb0
92c2a99
 
 
 
 
 
 
 
 
 
0569eb0
92c2a99
 
 
 
 
 
 
 
 
 
78b8854
92c2a99
e679acf
92c2a99
 
0ca4304
 
 
e679acf
1158d9c
 
 
 
 
 
 
 
 
 
78b8854
1158d9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bd735d
1158d9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b8854
1158d9c
 
 
e679acf
1158d9c
 
 
 
 
0ca4304
1158d9c
 
12d4d39
92c2a99
 
 
 
 
 
 
 
 
 
 
 
4590834
92c2a99
 
 
 
 
 
 
 
4590834
92c2a99
 
00a4b63
11b480e
 
 
 
 
eac8e20
11b480e
92c2a99
 
 
71dd3a7
92c2a99
 
 
 
 
 
 
 
 
 
 
 
0ca4304
92c2a99
0176780
 
92c2a99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d867b0
 
0176780
 
2d867b0
0ca4304
 
2d867b0
4de56f6
 
2d867b0
20f289e
0ca4304
2d867b0
 
 
0ca4304
2d867b0
cfcef27
722f026
cfcef27
 
66443af
cfcef27
 
 
 
 
2d867b0
 
cfcef27
 
 
 
722f026
cfcef27
 
 
 
 
 
4b8426d
cfcef27
 
 
 
 
 
 
 
 
 
 
f1e7fd3
7d6857d
92c2a99
 
 
 
f1e7fd3
92c2a99
 
93d7318
 
92c2a99
12d4d39
92c2a99
 
23b9158
92c2a99
 
7d6857d
92c2a99
10772a6
 
 
4b8426d
9c176db
f11eb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bd735d
f11eb0a
 
 
 
359e641
f11eb0a
 
 
 
 
 
 
 
 
 
c5879fd
 
 
 
f11eb0a
 
93d7318
 
f11eb0a
 
 
 
 
c5879fd
 
66443af
f11eb0a
 
 
 
 
 
 
9c176db
f11eb0a
 
 
4f009dc
660ac3a
 
 
92c2a99
0ca4304
df44003
 
0ca4304
61f90b1
23b9158
bbf919d
 
20f289e
1052bf2
 
 
53b5315
660ac3a
 
 
 
 
 
92c2a99
ac92f68
 
 
 
 
 
05be320
5c3afcb
660ac3a
92c2a99
9ba9505
660ac3a
 
9ba9505
 
 
 
 
 
 
92c2a99
 
 
 
 
 
 
 
 
 
 
 
e35594e
92c2a99
 
 
 
 
eb75f54
6e3bf57
92c2a99
 
 
 
 
 
 
 
 
 
 
5245051
26f048e
e679acf
92c2a99
 
 
 
 
 
26f048e
92c2a99
 
 
 
 
 
 
 
 
 
 
 
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#https://medium.com/thedeephub/rag-chatbot-powered-by-langchain-openai-google-generative-ai-and-hugging-face-apis-6a9b9d7d59db
#https://github.com/AlaGrine/RAG_chatabot_with_Langchain/blob/main/RAG_notebook.ipynb


from langchain_community.document_loaders import (
    PyPDFLoader,
    TextLoader,
    DirectoryLoader,
    CSVLoader,
    UnstructuredExcelLoader,
    Docx2txtLoader,
)
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
import tiktoken
import gradio as gr
import csv
import os, tempfile, glob, random
from pathlib import Path
#from IPython.display import Markdown
from PIL import Image
from getpass import getpass
import numpy as np
from itertools import combinations
import pypdf
import requests


# LLM: openai and google_genai
import openai
from langchain_openai import OpenAI, OpenAIEmbeddings, ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_google_genai import GoogleGenerativeAIEmbeddings


# LLM: HuggingFace
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
from langchain_community.llms import HuggingFaceHub

# langchain prompts, memory, chains...
from langchain.prompts import PromptTemplate, ChatPromptTemplate
from langchain.chains import ConversationalRetrievalChain
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
from operator import itemgetter
from langchain_core.runnables import RunnableLambda, RunnableParallel, RunnablePassthrough
from langchain.schema import Document, format_document
from langchain_core.messages import AIMessage, HumanMessage, get_buffer_string

from langchain_google_genai import (
    ChatGoogleGenerativeAI,
    HarmBlockThreshold,
    HarmCategory,
)

# OutputParser
from langchain_core.output_parsers import StrOutputParser

# Chroma: vectorstore
from langchain_community.vectorstores import Chroma

# Contextual Compression
from langchain.retrievers.document_compressors import DocumentCompressorPipeline
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_transformers import EmbeddingsRedundantFilter,LongContextReorder
from langchain.retrievers.document_compressors import EmbeddingsFilter
from langchain.retrievers import ContextualCompressionRetriever

from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import CohereRerank
from langchain_community.llms import Cohere

from langchain.memory import ConversationSummaryBufferMemory,ConversationBufferMemory


from langchain.schema import Document


# Cohere
from langchain.retrievers.document_compressors import CohereRerank
from langchain_community.llms import Cohere

openai_api_key = os.environ['openai_key']
google_api_key = os.environ['gemini_key']
HF_key = os.environ['HF_token']
cohere_api_key = os.environ['cohere_api']

current_dir = os.getcwd()




prompt_templates = {"All Needs Experts": "Respond as if you are combination of all needs assessment experts."}
actor_description = {"All Needs Experts": "<div style='float: left;margin: 0px 5px 0px 5px;'><img src='https://na.weshareresearch.com/wp-content/uploads/2023/04/experts2.jpg' alt='needs expert image' style='width:70px;align:top;'></div>A combination of all needs assessment experts."}



def get_empty_state():
    return { "messages": []}


def download_prompt_templates():
    url = "https://huggingface.co/spaces/ryanrwatkins/needs/raw/main/gurus.txt"
    try:
        response = requests.get(url)
        reader = csv.reader(response.text.splitlines())
        next(reader)  # skip the header row
        for row in reader:
            if len(row) >= 2:
                act = row[0].strip('"')
                prompt = row[1].strip('"')
                description = row[2].strip('"')
                prompt_templates[act] = prompt
                actor_description[act] = description

    except requests.exceptions.RequestException as e:
        print(f"An error occurred while downloading prompt templates: {e}")
        return

    choices = list(prompt_templates.keys())
    choices = choices[:1] + sorted(choices[1:])
    return gr.update(value=choices[0], choices=choices)

def on_prompt_template_change(prompt_template):
    if not isinstance(prompt_template, str): return
    return prompt_templates[prompt_template]

def on_prompt_template_change_description(prompt_template):
    if not isinstance(prompt_template, str): return
    return actor_description[prompt_template]














# set to load only PDF, but could change to set to specific directory, so that other files don't get embeddings

def langchain_document_loader():
    """
    Load documents from the temporary directory (TMP_DIR). 
    Files can be in txt, pdf, CSV or docx format.
    """
    #current_dir = os.getcwd()
    #TMP_DIR = current_dir
    global documents
    documents = []

    """
    txt_loader = DirectoryLoader(
        TMP_DIR.as_posix(), glob="**/*.txt", loader_cls=TextLoader, show_progress=True
    )
    documents.extend(txt_loader.load())
    """
    pdf_loader = DirectoryLoader(
        current_dir, glob="*.pdf", loader_cls=PyPDFLoader, show_progress=True
    )
    documents.extend(pdf_loader.load())
    """
    csv_loader = DirectoryLoader(
        TMP_DIR.as_posix(), glob="**/*.csv", loader_cls=CSVLoader, show_progress=True,
        loader_kwargs={"encoding":"utf8"}
    )
    documents.extend(csv_loader.load())
    
    doc_loader = DirectoryLoader(
        #TMP_DIR.as_posix(),
        current_dir,
        glob="**/*.docx",
        loader_cls=Docx2txtLoader,
        show_progress=True,
    )
    documents.extend(doc_loader.load())
    """
    return documents



langchain_document_loader()

text_splitter = RecursiveCharacterTextSplitter(
    separators = ["\n\n", "\n", " ", ""],    
    chunk_size = 1500,
    chunk_overlap= 200
)

# Text splitting
chunks = text_splitter.split_documents(documents=documents)



# just FYI, does not impact anything

def tiktoken_tokens(documents,model="gpt-3.5-turbo"):
    """Use tiktoken (tokeniser for OpenAI models) to return a list of token lengths per document."""    
    encoding = tiktoken.encoding_for_model(model) # returns the encoding used by the model.
    
    tokens_length = [len(encoding.encode(documents[i].page_content)) for i in range(len(documents))]

    return tokens_length


chunks_length = tiktoken_tokens(chunks,model="gpt-3.5-turbo")

print(f"Number of tokens - Average : {int(np.mean(chunks_length))}")
print(f"Number of tokens - 25% percentile : {int(np.quantile(chunks_length,0.25))}")
print(f"Number of tokens - 50% percentile : {int(np.quantile(chunks_length,0.5))}")
print(f"Number of tokens - 75% percentile : {int(np.quantile(chunks_length,0.75))}")


# For embeddings I am just using the free HF model so others are turned off

def select_embeddings_model(LLM_service="HuggingFace"):
    """Connect to the embeddings API endpoint by specifying 
    the name of the embedding model.
    if LLM_service == "OpenAI":
        embeddings = OpenAIEmbeddings(
            model='text-embedding-ada-002',
            api_key=openai_api_key)
    """

    """
    if LLM_service == "Google":
        embeddings = GoogleGenerativeAIEmbeddings(
            model="models/embedding-001",
            google_api_key=google_api_key,
        )
    """
        
    if LLM_service == "HuggingFace":
        embeddings = HuggingFaceInferenceAPIEmbeddings(    
            api_key=HF_key, 
            #model_name="thenlper/gte-large"
            model_name="sentence-transformers/all-MiniLM-l6-v2"
        )
    print("embedding model selected")
    return embeddings

#embeddings_OpenAI = select_embeddings_model(LLM_service="OpenAI")
#embeddings_google = select_embeddings_model(LLM_service="Google")
embeddings_HuggingFace = select_embeddings_model(LLM_service="HuggingFace")



# Creates the DB that will hold the embedding vectors

def create_vectorstore(embeddings,documents,vectorstore_name):
    """Create a Chroma vector database."""
    persist_directory = (current_dir + "/" + vectorstore_name)
    embedding_function=embeddings
    vector_store = Chroma.from_documents(
        documents=documents,
        embedding=embeddings,
        persist_directory=persist_directory
    )
    print("created Chroma vector database")
    return vector_store



create_vectorstores = True # change to True to create vectorstores

# Then we tell it to store the embeddings in the VectorStore (stickiong with HF for this)

if create_vectorstores:
    """
    vector_store_OpenAI,_ = create_vectorstore(
        embeddings=embeddings_OpenAI,
        documents = chunks,
        vectorstore_name="Vit_All_OpenAI_Embeddings",
    )
    print("vector_store_OpenAI:",vector_store_OpenAI._collection.count(),"chunks.")
    """
    """
    vector_store_google,new_vectorstore_name = create_vectorstore(
        embeddings=embeddings_google,
        documents = chunks,
        vectorstore_name="Vit_All_Google_Embeddings"
    )
    print("vector_store_google:",vector_store_google._collection.count(),"chunks.")
    """
    
    vector_store_HF = create_vectorstore(
        embeddings=embeddings_HuggingFace,
        documents = chunks,
        vectorstore_name="Vit_All_HF_Embeddings"
    )
    print("vector_store_HF:",vector_store_HF._collection.count(),"chunks.")
    
    print("")


# Now we tell it to keep the chromadb persistent so that it can be referenced at any time

"""
vector_store_OpenAI = Chroma(
    persist_directory = LOCAL_VECTOR_STORE_DIR.as_posix() + "/Vit_All_OpenAI_Embeddings",
    embedding_function=embeddings_OpenAI)
print("vector_store_OpenAI:",vector_store_OpenAI._collection.count(),"chunks.")
"""

"""
vector_store_google = Chroma(
    persist_directory = current_dir + "/Vit_All_Google_Embeddings",
    embedding_function=embeddings_google)
print("vector_store_google:",vector_store_google._collection.count(),"chunks.")
"""



vector_store_HF = Chroma(
    persist_directory = current_dir + "/Vit_All_HF_Embeddings",
    embedding_function=embeddings_HuggingFace)
print("vector_store_HF:",vector_store_HF._collection.count(),"chunks.")


# Now we create the code to retrieve embeddings from the vectorstore (again, sticking with HF)

def Vectorstore_backed_retriever(
vectorstore,search_type="similarity",k=10,score_threshold=None
):
    """create a vectorsore-backed retriever
    Parameters: 
        search_type: Defines the type of search that the Retriever should perform.
            Can be "similarity" (default), "mmr", or "similarity_score_threshold"
        k: number of documents to return (Default: 4) 
        score_threshold: Minimum relevance threshold for similarity_score_threshold (default=None)
    """
    print("vector_backed retriever started")
    search_kwargs={}
    if k is not None:
        search_kwargs['k'] = k
    if score_threshold is not None:
        search_kwargs['score_threshold'] = score_threshold
    global retriever
    retriever = vectorstore.as_retriever(
        search_type=search_type,
        search_kwargs=search_kwargs
    )
    print("vector_backed retriever done")
    return retriever

# similarity search
#base_retriever_OpenAI = Vectorstore_backed_retriever(vector_store_OpenAI,"similarity",k=10)
#base_retriever_google = Vectorstore_backed_retriever(vector_store_google,"similarity",k=10)
base_retriever_HF = Vectorstore_backed_retriever(vector_store_HF,"similarity",k=10)



# This next code takes the retrieved embeddings, gets rid of redundant ones, takes out non-useful information, and provides back a shorter embedding for use

def create_compression_retriever(embeddings, base_retriever, chunk_size=500, k=16, similarity_threshold=None):
    """Build a ContextualCompressionRetriever.
    We wrap the the base_retriever (a vectorstore-backed retriever) into a ContextualCompressionRetriever.
    The compressor here is a Document Compressor Pipeline, which splits documents
    into smaller chunks, removes redundant documents, filters out the most relevant documents,
    and reorder the documents so that the most relevant are at the top and bottom of the list.
    
    Parameters:
        embeddings: OpenAIEmbeddings, GoogleGenerativeAIEmbeddings or HuggingFaceInferenceAPIEmbeddings.
        base_retriever: a vectorstore-backed retriever.
        chunk_size (int): Documents will be splitted into smaller chunks using a CharacterTextSplitter with a default chunk_size of 500. 
        k (int): top k relevant chunks to the query are filtered using the EmbeddingsFilter. default =16.
        similarity_threshold : minimum relevance threshold used by the EmbeddingsFilter. default =None.
    """
    print("compression retriever started")
    # 1. splitting documents into smaller chunks
    splitter = CharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=0, separator=". ")
    
    # 2. removing redundant documents
    redundant_filter = EmbeddingsRedundantFilter(embeddings=embeddings)

    # 3. filtering based on relevance to the query    
    relevant_filter = EmbeddingsFilter(embeddings=embeddings, k=k, similarity_threshold=similarity_threshold) # similarity_threshold and top K

    # 4. Reorder the documents 
    
    # Less relevant document will be at the middle of the list and more relevant elements at the beginning or end of the list.
    # Reference: https://python.langchain.com/docs/modules/data_connection/retrievers/long_context_reorder
    reordering = LongContextReorder()

    # 5. Create compressor pipeline and retriever
    
    pipeline_compressor = DocumentCompressorPipeline(
        transformers=[splitter, redundant_filter, relevant_filter, reordering]  
    )
    compression_retriever = ContextualCompressionRetriever(
        base_compressor=pipeline_compressor, 
        base_retriever=base_retriever
    )
    print("compression retriever done")
    return compression_retriever

compression_retriever_HF = create_compression_retriever(
    embeddings=embeddings_HuggingFace,
    base_retriever=base_retriever_HF,
    k=16)


# Can use the following to rank the returned embeddings in order of relevance but all are used anyway so I am skipping for now (can test later)

'''
def CohereRerank_retriever(  
    base_retriever, 
    cohere_api_key,cohere_model="rerank-multilingual-v2.0", top_n=8
):
    """Build a ContextualCompressionRetriever using Cohere Rerank endpoint to reorder the results based on relevance.
    Parameters:
       base_retriever: a Vectorstore-backed retriever
       cohere_api_key: the Cohere API key
       cohere_model: The Cohere model can be either 'rerank-english-v2.0' or 'rerank-multilingual-v2.0', with the latter being the default.
       top_n: top n results returned by Cohere rerank, default = 8.
    """
    print("cohere rerank started")
    compressor = CohereRerank(
        cohere_api_key=cohere_api_key, 
        model=cohere_model, 
        top_n=top_n
    )

    retriever_Cohere = ContextualCompressionRetriever(
        base_compressor=compressor,
        base_retriever=base_retriever
    )
    print("cohere rerank done")
    return retriever_Cohere
'''



#  Don't have to use this, but is brings all the above pieces together in a single series (probably not worth implementing since I have the pieces already)

'''
def retrieval_blocks(
    create_vectorstore=True,# if True a Chroma vectorstore is created, else the Chroma vectorstore will be loaded
    LLM_service="HuggingFace",
    vectorstore_name="Vit_All_HF_Embeddings",
    chunk_size = 1600, chunk_overlap=200, # parameters of the RecursiveCharacterTextSplitter
    retriever_type="Vectorstore_backed_retriever",
    base_retriever_search_type="similarity", base_retriever_k=10, base_retriever_score_threshold=None,
    compression_retriever_k=16,
    cohere_api_key="***", cohere_model="rerank-multilingual-v2.0", cohere_top_n=8,
):
    print("retrieval blocks started")
    """
    Rertieval includes: document loaders, text splitter, vectorstore and retriever. 
    
    Parameters: 
        create_vectorstore (boolean): If True, a new Chroma vectorstore will be created. Otherwise, an existing vectorstore will be loaded.
        LLM_service: OpenAI, Google or HuggingFace.
        vectorstore_name (str): the name of the vectorstore.
        chunk_size and chunk_overlap: parameters of the RecursiveCharacterTextSplitter, default = (1600,200).
        
        retriever_type (str): in [Vectorstore_backed_retriever,Contextual_compression,Cohere_reranker]
        
        base_retriever_search_type: search_type in ["similarity", "mmr", "similarity_score_threshold"], default = similarity.
        base_retriever_k: The most similar vectors to retrieve (default k = 10).  
        base_retriever_score_threshold: score_threshold used by the base retriever, default = None.

        compression_retriever_k: top k documents returned by the compression retriever, default=16
        
        cohere_api_key: Cohere API key
        cohere_model (str): The Cohere model can be either 'rerank-english-v2.0' or 'rerank-multilingual-v2.0', with the latter being the default.
        cohere_top_n: top n results returned by Cohere rerank, default = 8.
   
    Output:
        retriever.
    """
    try:
        # Create new Vectorstore (Chroma index)
        if create_vectorstore: 
            # 1. load documents
            documents = langchain_document_loader(current_dir)
            
            # 2. Text Splitter: split documents to chunks
            text_splitter = RecursiveCharacterTextSplitter(
                separators = ["\n\n", "\n", " ", ""],    
                chunk_size = chunk_size,
                chunk_overlap= chunk_overlap
            )
            chunks = text_splitter.split_documents(documents=documents)
            
            # 3. Embeddings
            embeddings = select_embeddings_model(LLM_service=LLM_service)
        
            # 4. Vectorsore: create Chroma index
            vector_store = create_vectorstore(
                embeddings=embeddings,
                documents = chunks,
                vectorstore_name=vectorstore_name,
            )
    
        # 5. Load a Vectorstore (Chroma index)
        else: 
            embeddings = select_embeddings_model(LLM_service=LLM_service)        
            vector_store = Chroma(
                persist_directory = current_dir + "/" + vectorstore_name,
                embedding_function=embeddings
            )
            
            
        # 6. base retriever: Vector store-backed retriever 
        base_retriever = Vectorstore_backed_retriever(
            vector_store,
            search_type=base_retriever_search_type,
            k=base_retriever_k,
            score_threshold=base_retriever_score_threshold
        )
        retriever = None
        if retriever_type=="Vectorstore_backed_retriever": 
            retriever = base_retriever
    
        # 7. Contextual Compression Retriever
        if retriever_type=="Contextual_compression":    
            retriever = create_compression_retriever(
                embeddings=embeddings,
                base_retriever=base_retriever,
                k=compression_retriever_k,
            )
    
        # 8. CohereRerank retriever
        if retriever_type=="Cohere_reranker":
            retriever = CohereRerank_retriever(
                base_retriever=base_retriever, 
                cohere_api_key=cohere_api_key, 
                cohere_model=cohere_model, 
                top_n=cohere_top_n
            )
    
        print(f"\n{retriever_type} is created successfully!")
        print(f"Relevant documents will be retrieved from vectorstore ({vectorstore_name}) which uses {LLM_service} embeddings \
and has {vector_store._collection.count()} chunks.")
        print("retrieval blocks done")
        return retriever
    except Exception as e:
        print(e)
'''





# Can use any of these LLMs for responses, for now I am Gemini-Pro for the bot  (this is for responses now, not embeddings)


def instantiate_LLM(LLM_provider,api_key,temperature=0.7,top_p=0.95,model_name=None):
    """Instantiate LLM in Langchain.
    Parameters:
        LLM_provider (str): the LLM provider; in ["OpenAI","Google","HuggingFace"]
        model_name (str): in ["gpt-3.5-turbo", "gpt-3.5-turbo-0125", "gpt-4-turbo-preview", 
            "gemini-pro", "mistralai/Mistral-7B-Instruct-v0.2"].            
        api_key (str): google_api_key or openai_api_key or huggingfacehub_api_token 
        temperature (float): Range: 0.0 - 1.0; default = 0.5
        top_p (float): : Range: 0.0 - 1.0; default = 1.
    """
    if LLM_provider == "OpenAI":
        llm = ChatOpenAI(
            api_key=api_key,
            model="gpt-3.5-turbo", # in ["gpt-3.5-turbo", "gpt-3.5-turbo-0125", "gpt-4-turbo-preview"]
            temperature=temperature,
            model_kwargs={
                "top_p": top_p
            }
        )
    if LLM_provider == "Google":
        llm = ChatGoogleGenerativeAI(
            google_api_key=api_key,
            model="gemini-pro", # "gemini-pro"
            temperature=temperature,
            top_p=top_p,
            convert_system_message_to_human=True,
            safety_settings={
                HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
                HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
                HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
                HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
                HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE},
    
        )
    if LLM_provider == "HuggingFace":
        llm = HuggingFaceHub(
            repo_id="mistralai/Mistral-7B-Instruct-v0.2", # "mistralai/Mistral-7B-Instruct-v0.2"
            huggingfacehub_api_token=api_key,
            model_kwargs={
                "temperature":temperature,
                "top_p": top_p,
                "do_sample": True,
                "max_new_tokens":1024
            },
        )
    return llm



#  This creates history (memory) of prior questions. I am using Gemini for this but I left the code if I decide to go to GPT later on.

def create_memory(model_name='gemini-pro',memory_max_token=None):
#def create_memory(model_name='gpt-3.5-turbo',memory_max_token=None):
    """Creates a ConversationSummaryBufferMemory for gpt-3.5-turbo.
    Creates a ConversationBufferMemory for the other models."""
    
    if model_name=="gpt-3.5-turbo":
        if memory_max_token is None:
            memory_max_token = 1024 # max_tokens for 'gpt-3.5-turbo' = 4096
        memory = ConversationSummaryBufferMemory(
            max_token_limit=memory_max_token,
            llm=ChatOpenAI(model_name="gpt-3.5-turbo",openai_api_key=openai_api_key,temperature=0.1),
            return_messages=True,
            memory_key='chat_history',
            output_key="answer",
            input_key="question"
        )
    else:
        memory = ConversationBufferMemory(
            return_messages=True,
            memory_key='chat_history',
            output_key="answer",
            input_key="question",
        )  
    return memory

# Set a small memory_max_token, just to show how older messages are summarized if max_token_limit is exceeded.

memory = create_memory(model_name='gemini-pro',memory_max_token=None)
#memory = create_memory(model_name='gpt-3.5-turbo',memory_max_token=20)

# save history as context for the conversation

memory.save_context(
    inputs={"question":"sample"},
    outputs={"answer":"sample"}
)

# loads the template above
memory.load_memory_variables({})


# Create the prompt template for the conversation

standalone_question_template = """Given the following conversation and a follow up question, 
rephrase the follow up question to be a standalone question, in the English language.\n\n
Chat History:\n{chat_history}\n
Follow Up Input: {question}\n
Standalone question: {question}"""

standalone_question_prompt = PromptTemplate(
    input_variables=['chat_history', 'question'], 
    template=standalone_question_template
)


def answer_template(language="english"):
    """Pass the standalone question along with the chat history and context 
    to the `LLM` wihch will answer"""
    
    template = f"""Answer the question (convert to {language} language if it is not) at the end, using only the following context (delimited by <context></context>).
    Your answer must be in the language at the end. 
    
    <context>
    {{chat_history}}
    
    {{context}} 
    
    </context>
    
    Question: {{question}}
    Language: {language}.
    
    """
    return template

answer_prompt = ChatPromptTemplate.from_template(answer_template())




chain = ConversationalRetrievalChain.from_llm(
    condense_question_prompt=standalone_question_prompt,
    combine_docs_chain_kwargs={'prompt': answer_prompt},
    condense_question_llm=instantiate_LLM(
        LLM_provider="Google",api_key=google_api_key,temperature=0.1,
        model_name="gemini-pro"),
    memory=create_memory("gemini-pro"),
    retriever = compression_retriever_HF,
    #retriever = base_retriever_HF,  #base_retriever_HF
    llm=instantiate_LLM(
        LLM_provider="Google",api_key=google_api_key,temperature=0.7,
        model_name="gemini-pro"),
    chain_type= "stuff",
    verbose= True,
    return_source_documents=True   
)






'''
def create_ConversationalRetrievalChain(
    llm,condense_question_llm,
    retriever,
    chain_type= 'stuff',
    language="english",
    model_name='gemini-pro'
    #model_name='gpt-3.5-turbo'
):
    """Create a ConversationalRetrievalChain.
    First, it passes the follow-up question along with the chat history to an LLM which rephrases 
    the question and generates a standalone query. 
    This query is then sent to the retriever, which fetches relevant documents (context) 
    and passes them along with the standalone question and chat history to an LLM to answer.
    """
    
    # 1. Define the standalone_question prompt. 
    # Pass the follow-up question along with the chat history to the `condense_question_llm`
    # which rephrases the question and generates a standalone question.

    standalone_question_prompt = PromptTemplate(
        input_variables=['chat_history', 'question'], 
        template="""Given the following conversation and a follow up question, 
rephrase the follow up question to be a standalone question, in its original language.\n\n
Chat History:\n{chat_history}\n
Follow Up Input: {question}\n
Standalone question: {question}""")

    # 2. Define the answer_prompt
    # Pass the standalone question + the chat history + the context (retrieved documents) to the `LLM` wihch will answer
    
    answer_prompt = ChatPromptTemplate.from_template(answer_template(language='English'))

    # 3. Add ConversationSummaryBufferMemory for gpt-3.5, and ConversationBufferMemory for the other models
    
    memory = create_memory(model_name)

    # 4. Create the ConversationalRetrievalChain

    chain = ConversationalRetrievalChain.from_llm(
        condense_question_prompt=standalone_question_prompt,
        combine_docs_chain_kwargs={'prompt': answer_prompt},
        #condense_question_llm=condense_question_llm,
        condense_question_llm=instantiate_LLM(
            LLM_provider="Google",api_key=google_api_key,temperature=0.1,
            model_name="gemini-pro"),

        memory=memory,
        retriever = compression_retriever_HF,
        #retriever = base_retriever_HF,  #changed this
        #retriever = retriever,
        #llm=llm,    #changed this
        llm=instantiate_LLM(
                LLM_provider="Google",api_key=google_api_key,temperature=0.5,
                model_name="gemini-pro"),
        chain_type= "stuff",
        #chain_type= chain_type,
        verbose= True,
        return_source_documents=True    
    )

    print("Conversational retriever chain created successfully!")
    
    return chain,memory

'''



def submit_message(prompt, prompt_template, temperature, max_tokens, context_length, state):
    
    
    history = state['messages']


    global prompt_template_name
    prompt_template_name = prompt_template
    print(prompt_template)  # prints who is responding if I move to multiple experts
    print(prompt_templates[prompt_template])
    
    

    completion = chain.invoke({"question":prompt})

    
    chain.memory.load_memory_variables({})

    
    get_empty_state()
    state['content'] = completion
    #state.append(completion.copy())
    
    completion = { "content": completion }

    print("completion text from invoke on line 787")
    print(completion['content']['source_documents']['metadata'])
    print(completion['content']['source_documents']['state']['query_similarity_score'])


    
    #chat_messages = [(prompt_msg['content'], completion['content'])]    
    chat_messages = [(prompt, completion['content']['answer'])] 
    return '', chat_messages,  state    # total_tokens_used_msg,


def clear_conversation():
    return gr.update(value=None, visible=True), None, "", get_empty_state()







css = """
      #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
      #chatbox {min-height: 400px;}
      #header {text-align: center;}
      #prompt_template_preview {padding: 1em; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px; min-height: 150px;}
      #total_tokens_str {text-align: right; font-size: 0.8em; color: #666;}
      #label {font-size: 0.8em; padding: 0.5em; margin: 0;}
      .message { font-size: 1.2em; }
      """

with gr.Blocks(css=css) as demo:
    
    state = gr.State(get_empty_state())


    with gr.Column(elem_id="col-container"):

           
        gr.Markdown("""## Ask questions of our *needs assessment* bot!  
                    """ ,
                    elem_id="header")
        
        
        with gr.Row():
            with gr.Column():
                chatbot = gr.Chatbot(elem_id="chatbox")
                input_message = gr.Textbox(show_label=False, placeholder="Enter your needs assessment question", visible=True).style(container=False)

                btn_submit = gr.Button("Submit")
                #total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
                btn_clear_conversation = gr.Button("Start New Conversation")


            with gr.Column(visible=False):
                prompt_template = gr.Dropdown(label="Choose an Expert:", choices=list(prompt_templates.keys()))
                prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
                with gr.Accordion("Advanced parameters", open=False):
                    temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, label="Flexibility", info="Higher = More AI, Lower = More Expert")
                    max_tokens = gr.Slider(minimum=100, maximum=400, value=200, step=1, label="Length of Response.")
                    context_length = gr.Slider(minimum=1, maximum=5, value=2, step=1, label="Context Length", info="Number of previous questions you have asked.")

   
    btn_submit.click(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot,  state])
    input_message.submit(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot,  state])
    btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot,  state])
    prompt_template.change(on_prompt_template_change_description, inputs=[prompt_template], outputs=[prompt_template_preview])

    
    demo.load(download_prompt_templates, inputs=None, outputs=[prompt_template], queur=False)


demo.queue(concurrency_count=10)
demo.launch(height='800px')