Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.chains import RagChain
|
3 |
+
from langchain.vectorstores import Chroma
|
4 |
+
from transformers import RagTokenizer, RagSequenceForGeneration
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
|
7 |
+
# Initialisierung des Sentence-BERT Modells für die Embeddings
|
8 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
9 |
+
|
10 |
+
# Initialisierung von Tokenizer und RAG Modell
|
11 |
+
tokenizer = RagTokenizer.from_pretrained("facebook/rag-sequence-nq")
|
12 |
+
model = RagSequenceForGeneration.from_pretrained("facebook/rag-sequence-nq")
|
13 |
+
|
14 |
+
# Verbindung zur Chroma DB und Laden der Dokumente
|
15 |
+
chroma_db = Chroma(embedding_model=embedding_model, persist_directory = PATH_WORK + CHROMA_DIR)
|
16 |
+
|
17 |
+
# Erstellen eines eigenen Retrievers mit Chroma DB und Embeddings
|
18 |
+
retriever = chroma_db.as_retriever()
|
19 |
+
|
20 |
+
# Erstellung der RAG-Kette mit dem benutzerdefinierten Retriever
|
21 |
+
rag_chain = RagChain(model=model, retriever=retriever, tokenizer=tokenizer, vectorstore=chroma_db)
|
22 |
+
#############################################
|
23 |
+
|
24 |
+
|
25 |
+
def document_retrieval_chroma2():
|
26 |
+
#HF embeddings -----------------------------------
|
27 |
+
#Alternative Embedding - für Vektorstore, um Ähnlichkeitsvektoren zu erzeugen - die ...InstructEmbedding ist sehr rechenaufwendig
|
28 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"})
|
29 |
+
#etwas weniger rechenaufwendig:
|
30 |
+
#embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2", model_kwargs={"device": "cpu"}, encode_kwargs={'normalize_embeddings': False})
|
31 |
+
#oder einfach ohne Langchain:
|
32 |
+
#embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
33 |
+
|
34 |
+
#ChromaDb um die embedings zu speichern
|
35 |
+
db = Chroma(embedding_function = embeddings, persist_directory = PATH_WORK + CHROMA_DIR)
|
36 |
+
print ("Chroma DB bereit ...................")
|
37 |
+
|
38 |
+
return db
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
def get_rag_response(prompt):
|
43 |
+
global rag_chain
|
44 |
+
#rag-chain nutzen, um Antwort zu generieren
|
45 |
+
result = rag_chain({Frage: } : prompt)
|
46 |
+
|
47 |
+
#relevante Dokumente extrahieren
|
48 |
+
docs = result['docs']
|
49 |
+
passages = [doc['text'] for doc in docs]
|
50 |
+
links = doc['url'] for doc in docs
|
51 |
+
|
52 |
+
#Antwort generieren
|
53 |
+
answer = result['output']
|
54 |
+
response = {
|
55 |
+
"answer" : answer,
|
56 |
+
"documents" : [{"link" : link, "passage" : passage} for link, passage in zip(links, passages)]
|
57 |
+
}
|
58 |
+
return response
|
59 |
+
|
60 |
+
|
61 |
+
def chatbot_response (user_input, chat_history=[]):
|
62 |
+
response = get_rag_response(user_input)
|
63 |
+
answer = response['answer']
|
64 |
+
documents = response['documents']
|
65 |
+
doc_links = "\n\n".join([f"Link: {doc['link']} \nAuszüge der Dokumente: {doc['passage']}" for doc in documents])
|
66 |
+
bot_response = f"{answer} \n\nRelevante Dokumente: \n{doc_links}"
|
67 |
+
|
68 |
+
chat_history.append((user_inptu, bot_response))
|
69 |
+
|
70 |
+
return chat_history, chat_history
|
71 |
+
|
72 |
+
|
73 |
+
#############################
|
74 |
+
#GUI.........
|
75 |
+
def user (user_input, history):
|
76 |
+
return "", history + [[user_input, None]]
|
77 |
+
|
78 |
+
with gr.Blocks() as chatbot:
|
79 |
+
chat_interface = gr.Chatbot()
|
80 |
+
msg = gr.Textbox()
|
81 |
+
clear = gr.Button("Löschen")
|
82 |
+
|
83 |
+
#Buttons listener
|
84 |
+
msg.submit(user, [msg, chat_interface], [msg, chat_interface], queue = False). then(chatbot_response, [msg, chat_interface], [chat_interface, chat_interface])
|
85 |
+
|
86 |
+
clear.click(lambda: None, None, chat_interface, queue=False)
|
87 |
+
|
88 |
+
chatbot.launch()
|