File size: 29,752 Bytes
494b09f
392710b
5335699
46dd541
494b09f
 
 
 
b38b149
494b09f
 
 
e28c0cb
494b09f
 
 
 
 
 
 
 
 
 
d22fd24
 
db6fe0f
1ea56a7
995c030
 
494b09f
6f26ba4
494b09f
 
 
 
 
 
 
 
a73c6b3
 
 
494b09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bca051f
750ba2f
494b09f
 
 
 
 
 
 
 
 
 
 
 
 
 
6f26ba4
 
 
 
 
 
 
494b09f
 
 
 
 
 
 
 
 
 
 
995c030
 
 
 
 
 
1ea56a7
3925794
 
1ea56a7
3925794
0dae490
3925794
 
 
 
 
0dae490
1ea56a7
 
 
494b09f
 
 
 
fbf9ef6
 
 
 
494b09f
15b06e6
 
 
 
 
 
 
494b09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bca051f
494b09f
 
 
 
 
 
bca051f
494b09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bca051f
494b09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bca051f
494b09f
 
5396439
3925794
4126607
494b09f
 
 
bca051f
494b09f
 
 
 
 
 
 
 
 
a37bee9
494b09f
 
 
 
 
 
bca051f
494b09f
 
 
 
 
 
 
 
bca051f
494b09f
 
fbf9ef6
 
750ba2f
fbf9ef6
 
516a384
7273ef0
fbf9ef6
5396439
750ba2f
f2eee5c
 
81b71b0
fbf9ef6
516a384
ab28591
666cb98
5620cfc
 
9a5f1af
fbf9ef6
666cb98
 
fbf9ef6
 
494b09f
 
 
 
 
 
3925794
 
 
494b09f
53dbf30
fbf9ef6
 
bca051f
a73c6b3
fbf9ef6
 
5396439
bca051f
fbf9ef6
402adb3
fac6de6
 
fbf9ef6
5396439
 
bca051f
 
fbf9ef6
 
53dbf30
 
 
 
 
 
 
 
 
 
 
 
7273ef0
53dbf30
 
 
5396439
53dbf30
 
a73c6b3
7273ef0
 
 
8563685
f305257
402adb3
7273ef0
 
5b587ba
6b5a0fd
 
f305257
 
 
53dbf30
 
 
7273ef0
53dbf30
 
 
 
 
 
 
ab82f09
42b4e9e
 
 
 
 
 
 
 
 
 
 
 
 
ab82f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95b85ac
ab82f09
 
d22fd24
53dbf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358265f
bca051f
53dbf30
fa52948
57c5e88
358265f
57c5e88
53dbf30
 
 
 
 
 
 
fa52948
57c5e88
53dbf30
 
 
 
d22fd24
 
 
 
 
 
 
 
53dbf30
 
 
 
 
 
574cec8
53dbf30
 
 
 
 
 
d22fd24
 
 
 
 
 
 
 
 
42b4e9e
 
 
 
 
1ea56a7
d22fd24
 
494b09f
 
 
 
 
 
 
 
 
d22fd24
 
a4dc619
d22fd24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57c5e88
 
a280dec
a4dc619
 
a280dec
fd48538
a4dc619
 
db6fe0f
81b71b0
a280dec
 
d22fd24
5527c4e
15b06e6
5527c4e
15b06e6
5527c4e
 
 
 
a280dec
 
 
 
 
 
 
a4dc619
5335699
 
99995bc
d22fd24
 
 
494b09f
 
 
 
 
 
 
 
 
 
f166064
494b09f
 
53dbf30
 
494b09f
 
 
 
 
 
 
 
a37bee9
494b09f
 
 
 
 
 
 
 
 
 
 
d22fd24
 
494b09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0338b1c
 
 
53dbf30
d22fd24
494b09f
 
 
 
13768d7
4a7de3e
 
 
 
 
 
fcaf284
 
 
 
494b09f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
# mit TOP untersuchen und vorher LLM auskommentieren..
#ssh login:ssh -i "C:\Users\xy0\.ssh\id_ecdsa" [email protected]
#os.environ["CUDA_VISIBLE_DEVICES"] = ""  # Disable CUDA initialization
os.environ["allow_dangerous_deserialization"] = "True"
print(os.getcwd())
embedding_path="/home/user/app/docs/_embeddings/index.faiss"
print(f"Loading FAISS index from: {embedding_path}")
print("Version 11:03")
if not os.path.exists(embedding_path):
    print("File not found!")
HF_KEY=os.getenv('Gated_Repo')
SAIA_KEY = os.getenv('SAIA_KEY')

import spaces
import time
from typing import final
import asyncio

import torch
import gradio as gr
import threading
import re
import csv
import json
import gc
import multiprocessing
import resource


from openai import OpenAI
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.docstore import InMemoryDocstore
from langchain_community.document_loaders import TextLoader
from langchain.docstore.document import Document as LangchainDocument
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
from langchain_core.indexing import index
from langchain_core.vectorstores import VectorStore
from llama_index.core.node_parser import TextSplitter
#from langchain.retrievers import BM25Retriever, EnsembleRetriever
from langchain.retrievers import EnsembleRetriever
from langchain_community.retrievers import BM25Retriever
from llama_index.legacy.vector_stores import FaissVectorStore
from pycparser.ply.yacc import token
from ragatouille import RAGPretrainedModel

from langchain_text_splitters import MarkdownHeaderTextSplitter, CharacterTextSplitter
from sentence_transformers import SentenceTransformer
from sqlalchemy.testing.suite.test_reflection import metadata
from sympy.solvers.diophantine.diophantine import length
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, TextIteratorStreamer
from transformers import pipeline

#DEPR:from langchain.vectorstores import FAISS
import faiss
from langchain_community.vectorstores import FAISS
#DEPR: from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores.utils import DistanceStrategy
from huggingface_hub import login

# Press Umschalt+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

login(token=HF_KEY)
vectorstore=None
rerankingModel=None
bm25_retriever=None
docstore=None

class BSIChatbot:
    embedding_model = None
    llmpipeline = None
    llmtokenizer = None
    vectorstore = None
    images = [None]

    # model_paths = {
    #    'llm_path': 'meta-llama/Llama-3.2-3B-Instruct',
    #    'embed_model_path': 'intfloat/multilingual-e5-large-instruct',
    #    'rerank_model_path': 'domci/ColBERTv2-mmarco-de-0.1'
    # }

    llm_base_url = "https://chat-ai.academiccloud.de/v1"
    llm_remote_model = "qwen2.5-72b-instruct"
    llm_client = OpenAI(
        api_key = SAIA_KEY,
        base_url = llm_base_url
    )

    llm_path = "meta-llama/Llama-3.2-3B-Instruct"
    word_and_embed_model_path = "intfloat/multilingual-e5-large-instruct"
    docs = "/home/user/app/docs"
    #docs = "H:\\Uni\\Master\\Masterarbeit\\Masterarbeit\\daten\\_parsed_embed_test"
    rerankModelPath="AdrienB134/ColBERTv1.0-german-mmarcoDE"
    embedPath="/home/user/app/docs/_embeddings"

    def __init__(self):
        self.embedding_model = None
        #self.vectorstore: VectorStore = None

    def using(self,point=""):
        usage = resource.getrusage(resource.RUSAGE_SELF)
        return '''%s: usertime=%s systime=%s mem=%s mb
               ''' % (point, usage[0], usage[1],
                      usage[2] / 1024.0)

    def cleanResources(self):
        print(f"GPU Memory Allocated: {torch.cuda.memory_allocated() / 1024 / 1024} MB")
        print(f"GPU Memory Cached: {torch.cuda.memory_reserved() / 1024 / 1024} MB")
        multiprocessing.active_children()
        print("processes:")
        print(multiprocessing.active_children())

        for child in multiprocessing.active_children():
            child.terminate()
            child.join()

        #multiprocessing.resource_tracker.unregister('Semaphore')
        torch.cuda.empty_cache()
        gc.collect()

    def initializeEmbeddingModel(self, new_embedding):
        global vectorstore
        RAW_KNOWLEDGE_BASE = []

        #Qdrant:
        #client = QdrantClient(path=saved_db_path)
        #db = Qdrant(client=client, collection_name=self.collection_name, embeddings=embeddings, )

        #Embedding, Vector generation and storing:
        if self.embedding_model is None:
            self.embedding_model = HuggingFaceEmbeddings(
                model_name=self.word_and_embed_model_path,
                multi_process=False,
                model_kwargs={"device": "cuda"},
                encode_kwargs={"normalize_embeddings": True},  # Set `True` for cosine similarity
            )

        #index_cpu = faiss.IndexFlatL2(1024)
        #res = faiss.StandardGpuResources()
        #index_gpu = faiss.index_cpu_to_gpu(res, 0, index_cpu)
        dirList = os.listdir(self.docs)
        if (new_embedding==True):
            for doc in dirList:
                print(doc)
                if (".md" in doc):
                    ##doctxt = TextLoader(docs + "\\" + doc).load()
                    file = open(self.docs + "\\" + doc, 'r', encoding='utf-8')
                    doctxt = file.read()
                    RAW_KNOWLEDGE_BASE.append(LangchainDocument(page_content=doctxt, metadata={"source": doc}))
                    file.close()
                if (".txt" in doc):
                    file = open(self.docs + "\\" + doc, 'r', encoding='cp1252')
                    doctxt = file.read()
                    if doc.replace(".txt",".png") in dirList:
                        RAW_KNOWLEDGE_BASE.append(LangchainDocument(page_content=doctxt, metadata={"source": doc.replace(".txt",".png")}))
                    if doc.replace(".txt",".jpg") in dirList:
                        RAW_KNOWLEDGE_BASE.append(LangchainDocument(page_content=doctxt, metadata={"source": doc.replace(".txt",".jpg")}))
                    file.close()

                    # RAW_KNOWLEDGE_BASE.append(txtLoader)
                    # print(RAW_KNOWLEDGE_BASE)

            # Chunking starts here

            headers_to_split_on = [
                ("#", "Header 1"),
                ("##", "Header 2"),
                ("###", "Header 3"),
                ("####", "Header 4"),
                ("#####", "Header 5"),
            ]

            markdown_splitter = MarkdownHeaderTextSplitter(
                headers_to_split_on=headers_to_split_on,
                strip_headers=True
            )

            tokenizer = AutoTokenizer.from_pretrained(self.word_and_embed_model_path)

            text_splitter = RecursiveCharacterTextSplitter.from_huggingface_tokenizer(
                tokenizer=tokenizer,
                chunk_size=512,  # The maximum number of words in a chunk
                chunk_overlap=0,  # The number of characters to overlap between chunks
                add_start_index=True,  # If `True`, includes chunk's start index in metadata
                strip_whitespace=True,  # If `True`, strips whitespace from the start and end of every document
            )

            ##Was macht man mit start Index herausfinden und wie metadata adden
            docs_processed = []
            for doc in RAW_KNOWLEDGE_BASE:
                #newprint(f"Word-Length in doc:{len(doc.page_content.split())}")
                doc_cache = markdown_splitter.split_text(doc.page_content)
                # print(f"Word-Length in doc_cache after MarkdownSplitter:{len(doc_cache.split())}")
                doc_cache = text_splitter.split_documents(doc_cache)
                # print(f"Word-Length in doc_cache after text_splitter:{len(doc_cache.split())}")
                for chunk in doc_cache:
                    chunk.metadata.update({"source": doc.metadata['source']})
                    #newprint(f"Chunk_Debug len: {len(chunk.page_content.split())} and Chunk:{chunk}")
                # DEBUG:
                # print(f"doc_cache after Metadata added:{doc_cache}\n")
                docs_processed += doc_cache

            #final_docs = []
            #for doc in docs_processed:
            #   final_docs += text_splitter.split_documents([doc])

            #docs_processed = final_docs

            ##Ab hier alt:
            # MARKDOWN_SEPARATORS = [
            #    "\n\n",
            #    "---"
            #    "\n",
            #    " ",
            #    ""
            # ]

            #text_splitter = RecursiveCharacterTextSplitter(
            #    chunk_size=512,  # The maximum number of characters in a chunk
            #    chunk_overlap=100,  # The number of characters to overlap between chunks
            #    add_start_index=True,  # If `True`, includes chunk's start index in metadata
            #    strip_whitespace=True,  # If `True`, strips whitespace from the start and end of every document
            #    separators=MARKDOWN_SEPARATORS,
            #)

            #docs_processed = []
            #for doc in RAW_KNOWLEDGE_BASE:
            #    docs_processed += text_splitter.split_documents([doc])

            #newprint(f"Docs processed:{len(docs_processed)}")
            # Max_Sequence_Length of e5 large instr = 512 Tokens


            # Make sure the maximum length is below embedding size
            lengths = [len(s.page_content) for s in docs_processed]
            print(max(lengths))

            #for l in docs_processed:
            #    print(f"Char-Length:{len(l.page_content.split())}")
            #    print(f"Tokenizer Length: {len(tokenizer.tokenize(l.page_content))}")

            #if (max(lengths) > SentenceTransformer(self.word_and_embed_model_path).max_seq_length):
            #    print(
            #        f'Error: Fit chunking size into embedding model.. Chunk{max(lengths)} is bigger than {SentenceTransformer(self.word_and_embed_model_path).Max_Sequence_Length}')

            start = time.time()
            #docstore = InMemoryDocstore({str(i): doc for i, doc in enumerate(docs_processed)})
            #index_to_docstore_id = {i: str(i) for i in range(len(docs_processed))}
            vectorstore = FAISS.from_documents(docs_processed, self.embedding_model, distance_strategy=DistanceStrategy.COSINE)
            #self.vectorstore = FAISS(
            #    embedding_function=self.embedding_model,
            #    index=index_gpu,
            #    distance_strategy=DistanceStrategy.COSINE,
            #    docstore=docstore,
            #    index_to_docstore_id=index_to_docstore_id
            #)
            #self.vectorstore.from_documents(docs_processed, self.embedding_model)
            #index_cpu = faiss.index_gpu_to_cpu(self.vectorstore.index)
            #self.vectorstore.index = index_cpu
            vectorstore.save_local(self.embedPath)
            #self.vectorstore.index = index_gpu
            end = time.time()
            #newprint("Saving Embeddings took", end-start, "seconds!")
        else:
            start = time.time()
            if vectorstore is None:
                print("Checkpoint: FAISS Vectorstore initialized...")
                vectorstore = FAISS.load_local(self.embedPath, self.embedding_model, allow_dangerous_deserialization=True)
            #self.vectorstore.index = index_gpu
            end = time.time()

            #newprint("Loading Embeddings took", end - start, "seconds!")

    def retrieveSimiliarEmbedding(self, query):
        global vectorstore
        print("Retrieving Embeddings...")
        start = time.time()
        query = f"Instruct: Given a search query, retrieve the relevant passages that answer the query\nQuery:{query}"

        #self.vectorstore.
        #retrieved_chunks = self.vectorstore.similarity_search(query=query, k=20)
        retrieved_chunks = vectorstore.similarity_search(query=query, k=30)
        #finalchunks = []
        #for chunk in retrieved_chunks:
        #    if "---" not in chunk.page_content:
        #        finalchunks.append(chunk)
        #retrieved_chunks = finalchunks
        end = time.time()
        #newrint("Retrieving Chunks with similiar embeddings took", end - start, "seconds!")
        #print("\n==================================Top document==================================")
        #print(retrieved_chunks[0].page_content)
        #print(retrieved_chunks[1].page_content)
        #print(retrieved_chunks[2].page_content)
        #print("==================================Metadata==================================")
        #print(retrieved_chunks[0].metadata)
        #print(retrieved_chunks[1].metadata)
        #print(retrieved_chunks[2].metadata)
        #newprint(f"printing first chunk to see whats inside: {retrieved_chunks[0]}")
        return retrieved_chunks

    def retrieveDocFromFaiss(self):
        global vectorstore
        global docstore
        all_documents = []

        #print(vectorstore.index_to_docstore_id)
        #newprint(vectorstore)
        # Iteriere über alle IDs im index_to_docstore_id
        if docstore is None:
            docstore = vectorstore.docstore._dict.values()

        #for doc_id in vectorstore.index_to_docstore_id.values():
        for entry in docstore:
            # Hole das Dokument aus dem docstore
            #print("DBG:"+doc_id)
            #document = vectorstore.get_by_ids(doc_id)
            #document bleibt leer.. Warum???
            #print("DBG: DOC")
            #print(entry)
            all_documents.append(entry)

        #print("DBG Document1:")
        #print(all_documents)
        return all_documents

    def queryLLM(self,query):
        #resp = self.llmpipeline(chat) Fixen
        return(self.llmpipeline(query)[0]["generated_text"])

    def initializeRerankingModel(self):
        global rerankingModel
        if rerankingModel is None:
            print("Checkpoint: Reranker initialized...")
            rerankingModel = RAGPretrainedModel.from_pretrained(self.rerankModelPath)


    def retrieval(self, query, rerankingStep, hybridSearch):
        global vectorstore
        global bm25_retriever
        global rerankingModel
        if hybridSearch == True:
            allDocs = self.retrieveDocFromFaiss()
            if bm25_retriever is None:
                bm25_retriever = BM25Retriever.from_documents(allDocs)
            #TODO!
            retriever_k=25
            bm25_retriever.k= retriever_k
            vectordb = vectorstore.as_retriever(search_kwargs={"k":retriever_k})
            ensemble_retriever = EnsembleRetriever(retrievers=[bm25_retriever, vectordb], weights=[0.5, 0.5])
            retrieved_chunks = ensemble_retriever.invoke(query)
            #retrieved_chunks = ensemble_retriever.get_relevant_documents(query)
            #newprint("DBG: Number of Chunks retrieved")
            #newprint(len(retrieved_chunks))
        else:
            retrieved_chunks = self.retrieveSimiliarEmbedding(query)
        retrieved_chunks_text = []
        # TODO Irgendwas stimmt hier mit den Listen nicht
        for chunk in retrieved_chunks:
            # TODO Hier noch was smarteres Überlegen für alle Header
            if "Header 1" in chunk.metadata.keys():
                retrieved_chunks_text.append(
                    f"The Document is: '{chunk.metadata['source']}'\nHeader of the Section is: '{chunk.metadata['Header 1']}' and Content of it:{chunk.page_content}")
            else:
                retrieved_chunks_text.append(
                    f"The Document is: '{chunk.metadata['source']}'\nImage Description is: ':{chunk.page_content}")
        i = 1
        for chunk in retrieved_chunks_text:
            #newprint(f"Retrieved Chunk number {i}:\n{chunk}")
            i = i + 1

        if rerankingStep == True:
            if rerankingModel is None:
                self.initializeRerankingModel()
            print("Starting Reranking Chunks...")
            #rerankingModel
            #newprint("DBG:retrieved_chunks_text")
            #newprint(type(retrieved_chunks_text))
            #new print(retrieved_chunks_text)
            with torch.no_grad():
                print("reranking chunks (reverse)..")
                retrieved_chunks_text = rerankingModel.rerank(query, retrieved_chunks_text, k=20)
            #newprint("DBG:retrieved_chunks_text after rerank")
            #newprint(type(retrieved_chunks_text))
            #newprint(retrieved_chunks_text)a
            #for chunk in reversed(retrieved_chunks_text):
                #print(chunk["rank"])
            retrieved_chunks_text = [chunk["content"] for chunk in reversed(retrieved_chunks_text)]

            #retrieved_chunks_text = [chunk["content"] for chunk in retrieved_chunks_text]

            i = 1
            for chunk in retrieved_chunks_text:
                #newprint(f"Reranked Chunk number {i}:\n{chunk}")
                i = i + 1

        context = "\nExtracted documents:\n"
        context += "".join([doc for i, doc in enumerate(retrieved_chunks_text)])

        return query, context

    def queryRemoteLLM(self, systemPrompt, query, summary):
        if summary != True:
            chat_completion = self.llm_client.chat.completions.create(
                messages=[{"role": "system", "content": systemPrompt},
                          {"role": "user", "content": "Step-Back Frage, die neu gestellt werden soll: " + query}],
                model=self.llm_remote_model,
            )
        if summary == True:
            chat_completion = self.llm_client.chat.completions.create(
                messages=[{"role": "system", "content": systemPrompt},
                          {"role": "user", "content": query}],
                model=self.llm_remote_model,
            )
        return chat_completion.choices[0].message.content

    def stepBackPrompt(self, query):
        systemPrompt = """
        Sie sind ein Experte für den IT-Grundschutz des BSI. 
        Ihre Aufgabe ist es, eine Frage neu zu formulieren und sie in eine
        Stepback-Frage umzuformulieren, die nach einem Grundkonzept der Begrifflichkeit fragt. 

        Hier sind ein paar Beispiele:
        Ursprüngliche Frage: Welche Bausteine werden auf einen Webserver angewendet?
        Stepback-Frage: Wie werden Bausteine im IT-Grundschutz angewendet?

        Ursprüngliche Frage: Wer war der Ehemann von Anna Karina von 1968 bis 1974?
        Stepback-Frage: Wer waren die Ehegatten von Anna Karina?

        Ursprüngliche Frage: Welche Inhalte enthält der Standard 200-1?
        Stepback Frage: Welche Standards gibt es im IT-Grundschutz?
        """
        stepBackQuery = self.queryRemoteLLM(systemPrompt, query, False)
        return stepBackQuery

    def ragPromptNew(self, query, rerankingStep, history, stepBackPrompt, returnContext):
        global rerankingModel
        prompt_in_chat_format = [
            {
                "role": "system",
                "content": """You are an helpful Chatbot for the BSI IT-Grundschutz. Using the information contained in the context,
                        give a comprehensive answer to the question.
                        Respond only to the question asked, response should be concise and relevant but also give some context to the question. 
                        Provide the source document when relevant for the understanding.
                        If the answer cannot be deduced from the context, do not give an answer.""",
            },
            {
                "role": "user",
                "content": """Context:
                        {context}
                        ---
                        Chat-History:
                        {history}
                        ---
                        Now here is the question you need to answer.

                        Question: {question}""",
            },
        ]
        # RAG_PROMPT_TEMPLATE = self.llmtokenizer.apply_chat_template(
        #    prompt_in_chat_format, tokenize=False, add_generation_prompt=True
        # )

        # Alles außer letzte Useranfrage, Normaler Query
        query, context = self.retrieval(query, rerankingStep, True)

        if stepBackPrompt == True:
            stepBackQuery = self.stepBackPrompt(query)
            #newprint("DBG stepBackQuery:" + stepBackQuery)
            stepBackQuery, stepBackContext = self.retrieval(stepBackQuery, rerankingStep, True)
            #newprint("DBG stepBackContext:" + stepBackContext)
            sysPrompt = """
            You are an helpful Chatbot for the BSI IT-Grundschutz. Using the information contained in the context,
                give a comprehensive answer to the question.
                Respond only to the question asked, response should be concise and relevant but also give some context to the question. 
                Provide the source document when relevant for the understanding.
                If the answer cannot be deduced from the context, do not give an answer.
                """
            stepBackAnswer = self.queryRemoteLLM(sysPrompt, stepBackQuery, True)
            #newprint("DBG stepBackAnswer:" + stepBackAnswer)
            context += "Übergreifende Frage:" + stepBackQuery + "Übergreifender Context:" + stepBackAnswer

        #def queryRemoteLLM(self, systemPrompt, query, summary):

        if (history != None):
            prompt_in_chat_format[-1]["content"] = prompt_in_chat_format[-1]["content"].format(
                question=query, context=context, history=history[:-1]
            )
        else:
            prompt_in_chat_format[-1]["content"] = prompt_in_chat_format[-1]["content"].format(
                question=query, context=context, history="None"
            )
        final_prompt = prompt_in_chat_format

        # final_prompt = prompt_in_chat_format[-1]["content"].format(
        #    question=query, context=context, history=history[:-1]
        # )

        print(f"DBG: Final-Query:\n{final_prompt}")
        pattern = r"Filename:(.*?);"
        last_value = final_prompt[-1]["content"]

        match = re.findall(pattern, last_value)
        self.images = match

        if (returnContext == False):
            stream = self.llm_client.chat.completions.create(
                messages=final_prompt,
                model=self.llm_remote_model,
                stream=True
            )
            return stream

        else:
            answer = self.llm_client.chat.completions.create(
                messages=final_prompt,
                model=self.llm_remote_model,
                stream=False
            )
            self.cleanResources()
            return answer, context

    def returnImages(self):
        imageList = []
        for image in self.images:
            imageList.append(f"{self.docs}\\{image}")
        return imageList

    def launchGr(self):
        gr.Interface.from_pipeline(self.llmpipeline).launch()

    def generateEvalDataset(self):
        filepath = "/home/user/app/docs/_eval/BSI_Lektion_Ground_Truth.CSV"
        with open(filepath, mode='r', encoding="latin1", errors="replace") as file:
            # Create a CSV reader with DictReader
            csv_reader = csv.DictReader(file, delimiter="|")

            # Initialize an empty list to store the dictionaries
            data_list = []

            # Iterate through each row in the CSV file
            for row in csv_reader:
                # Append each row (as a dictionary) to the list
                data_list.append(row)

        # Print the list of dictionaries
        for data in data_list:
            data["Context"] = None
            data["Answer"] = None

        print(data_list)

        i=1
        #for data in data_list[:3]:
        print("starting to generate evaldataset..")
        for data in data_list:
            print("Eval Entry no:")
            print(i)
            print("GPU Memory Allocated:")
            print(torch.cuda.memory_allocated()/1024/1024/1024)
            print("frage:")
            print(data["Frage"])
            #def ragPromptNew(self, query, rerankingStep, history, stepBackPrompt)
            try:
                print(self.using("PreRag"))
                data["Answer"],data["Context"]  = self.ragPromptNew(data["Frage"],True,None,True, True)
                print(self.using("AfterRag"))
                data["Answer"]=data["Answer"].choices[0].message.content
            except Exception as e:
                print(f"Fehler bei Eintrag {i}: {e}")

            print("DBG: storing Answer")
            print(data["Answer"][:20])
            print("in")
            print(data["Frage"])
            print(data["Frage_index"])
            print(data["Lektion"])
            #print(data)
            i=i+1
            with open('/home/user/app/docs/_eval/eval_dataset.json', 'a') as fout:
                fout.write(json.dumps(data, ensure_ascii=False) + "\n")


        # Print full response as JSON
        # print(chat_completion)

if __name__ == '__main__':
    #RAW_KNOWLEDGE_BASE = []
    #RAW_KNOWLEDGE_BASE.append(LangchainDocument(page_content="1Text", metadata={"source": "bb"}))
    #RAW_KNOWLEDGE_BASE.append(LangchainDocument(page_content="2Text", metadata={"source": "aa"}))
    #RAW_KNOWLEDGE_BASE[0].metadata.update({"NeuerKey":"White"})
    #print(RAW_KNOWLEDGE_BASE)
    #time.sleep(10)

    #{doc.page_content} [{doc.metadata}] => aktuellen Header in jeden Chunk embedden; Doc.Metadata retrieven
    eval = False # generates Eval Dataset
    renewEmbeddings = False
    reranking = True
    stepBackEnable = True

    bot = BSIChatbot()
    bot.initializeEmbeddingModel(renewEmbeddings)
    if reranking == True:
        bot.initializeRerankingModel()
    #TODO: DEBUG:
    #bot.retrieveSimiliarEmbedding("Was ist der IT-Grundschutz?")
    #TODO: DEBUG:
    #time.sleep(10)
    #bot.initializeLLM()
    #bot.retrieveSimiliarEmbedding("Welche Typen von Anforderungen gibt es im IT-Grundschutz?")

    #bot.queryLLM("Welche Typen von Anforderungen gibt es im IT-Grundschutz?")

    #bot.ragPrompt("""
    #Welche Informationen beinhaltet die IT-Grundschutz-Methodik (BSI-Standard 200-2)? Wähle aus den folgenden Antwortmöglichkeiten (mehrere können richtig sein!):
    #A: besonders schutzwürdigen Komponenten,
    #B: methodische Hilfestellungen zur schrittweisen Einführung eines ISMS,
    #C: wie die Informationssicherheit im laufenden Betrieb aufrechterhalten und kontinuierlich verbessert werden kann,
    #D: effiziente Verfahren, um die allgemeinen Anforderungen des BSI-Standards 200-1 zu konkretisieren
    #""", True)
    if (eval==True):
        bot.generateEvalDataset()
    #bot.launchGr()

    with gr.Blocks() as demo:
        with gr.Row() as row:
                with gr.Column(scale=3):
                    chatbot = gr.Chatbot(type="messages")
                    msg = gr.Textbox()
                    clear = gr.Button("Clear")
                    reset = gr.Button("Reset")
                with gr.Column(scale=1):  # Bildergalerie
                    gallery = gr.Gallery(label="Bildergalerie",elem_id="gallery")

        def user(user_message, history: list):
            return "", history + [{"role": "user", "content": user_message}]


        def returnImages():
            # Hier holen wir uns die Bildpfade und wandeln sie in gr.Image-Objekte um
            image_paths = bot.returnImages()
            print(f"returning images: {image_paths}")
            return image_paths

        def gradiobot(history: list):
            start = time.time()
            print(f"DBG: ragQuery hist -1:{history[-1].get('content')}")
            print(f"DBG: ragQuery hist 0:{history[0].get('content')}")
            print(f"DBG: fullHistory: {history}" )
            #bot_response = bot.ragPromptRemote(history[-1].get('content'), reranking, history)
            bot_response = bot.ragPromptNew(history[-1].get('content'), reranking, history, stepBackEnable, False)
            history.append({"role": "assistant", "content": ""})

            image_gallery = returnImages()

            for token in bot_response:
                #if "eot_id" in token.choices[0].delta.content:
                #    token = token.replace("<|eot_id|>","")
                #if token.choices[0].delta.content.startswith("-"):
                #    token = f"\n{token}"
                #if re.match(r"^[1-9]\.",token.choices[0].delta.content):
                #    token = f"\n{token}"
                if token.choices and len(token.choices) > 0:
                    if token.choices[0].delta.content != "":
                        history[-1]['content'] += token.choices[0].delta.content
                        yield history, image_gallery
            end = time.time()
            print("End2End Query took", end - start, "seconds!")

        def resetHistory():
            return []

        msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
            gradiobot, inputs=[chatbot], outputs=[chatbot, gallery]
        )


        clear.click(lambda: None, None, chatbot, queue=False)
        reset.click(resetHistory, outputs=chatbot, queue=False)
    demo.css = """
        #gallery {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 10px;
            height: 400px;
            overflow: auto;
        }
    """
    demo.launch(allowed_paths=["/home/user/app/docs"])

    #Answer: B, C und D => Korrekt!

# See PyCharm help at https://www.jetbrains.com/help/pycharm/