Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ from langchain.document_loaders.generic import GenericLoader
|
|
| 16 |
from langchain.document_loaders.parsers import OpenAIWhisperParser
|
| 17 |
from langchain.schema import AIMessage, HumanMessage
|
| 18 |
from langchain.embeddings import HuggingFaceInstructEmbeddings, HuggingFaceEmbeddings, HuggingFaceBgeEmbeddings, HuggingFaceInferenceAPIEmbeddings
|
| 19 |
-
|
| 20 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 21 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 22 |
from langchain.vectorstores import Chroma
|
|
@@ -44,6 +44,12 @@ template = """Antworte in deutsch, wenn es nicht explizit anders gefordert wird.
|
|
| 44 |
llm_template = "Beantworte die Frage am Ende. " + template + "Frage: {question} Hilfreiche Antwort: "
|
| 45 |
rag_template = "Nutze die folgenden Kontext Teile, um die Frage zu beantworten am Ende. " + template + "{context} Frage: {question} Hilfreiche Antwort: "
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
#################################################
|
| 49 |
# Konstanten
|
|
@@ -201,6 +207,7 @@ def document_retrieval_chroma():
|
|
| 201 |
return db
|
| 202 |
|
| 203 |
|
|
|
|
| 204 |
###############################################
|
| 205 |
#Langchain anlegen
|
| 206 |
|
|
@@ -214,12 +221,21 @@ def llm_chain(prompt):
|
|
| 214 |
#prompt mit RAG!!!
|
| 215 |
def rag_chain(prompt, db):
|
| 216 |
rag_template = "Nutze die folgenden Kontext Teile am Ende, um die Frage zu beantworten . " + template + "Frage: " + prompt + "Kontext Teile: "
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
return neu_prompt
|
| 223 |
|
| 224 |
|
| 225 |
|
|
|
|
| 16 |
from langchain.document_loaders.parsers import OpenAIWhisperParser
|
| 17 |
from langchain.schema import AIMessage, HumanMessage
|
| 18 |
from langchain.embeddings import HuggingFaceInstructEmbeddings, HuggingFaceEmbeddings, HuggingFaceBgeEmbeddings, HuggingFaceInferenceAPIEmbeddings
|
| 19 |
+
from langchain.prompts import PromptTemplate
|
| 20 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 21 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 22 |
from langchain.vectorstores import Chroma
|
|
|
|
| 44 |
llm_template = "Beantworte die Frage am Ende. " + template + "Frage: {question} Hilfreiche Antwort: "
|
| 45 |
rag_template = "Nutze die folgenden Kontext Teile, um die Frage zu beantworten am Ende. " + template + "{context} Frage: {question} Hilfreiche Antwort: "
|
| 46 |
|
| 47 |
+
#################################################
|
| 48 |
+
#Konstanten
|
| 49 |
+
LLM_CHAIN_PROMPT = PromptTemplate(input_variables = ["question"],
|
| 50 |
+
template = llm_template)
|
| 51 |
+
RAG_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"],
|
| 52 |
+
template = rag_template)
|
| 53 |
|
| 54 |
#################################################
|
| 55 |
# Konstanten
|
|
|
|
| 207 |
return db
|
| 208 |
|
| 209 |
|
| 210 |
+
|
| 211 |
###############################################
|
| 212 |
#Langchain anlegen
|
| 213 |
|
|
|
|
| 221 |
#prompt mit RAG!!!
|
| 222 |
def rag_chain(prompt, db):
|
| 223 |
rag_template = "Nutze die folgenden Kontext Teile am Ende, um die Frage zu beantworten . " + template + "Frage: " + prompt + "Kontext Teile: "
|
| 224 |
+
rag_chain = RetrievalQA.from_chain_type(API_URL,
|
| 225 |
+
chain_type_kwargs = {"prompt": RAG_CHAIN_PROMPT},
|
| 226 |
+
retriever = db.as_retriever(search_kwargs = {"k": 3}),
|
| 227 |
+
return_source_documents = True)
|
| 228 |
+
result = rag_chain({"query": prompt})
|
| 229 |
+
return result["result"]
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
#retrieved_chunks = db.query(prompt, k=3) #3 passende chunks zum Prompt hinzufügen
|
| 234 |
+
#neu_prompt = rag_template
|
| 235 |
+
#for i, chunk in enumerate(retrieved_chunks):
|
| 236 |
+
#neu_prompt += f"{i+1}. {chunk}\n"
|
| 237 |
|
| 238 |
+
#return neu_prompt
|
| 239 |
|
| 240 |
|
| 241 |
|