Spaces:
Runtime error
Runtime error
more strict response behavior
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ from langchain.chains import RetrievalQA
|
|
| 16 |
import json
|
| 17 |
from documents import read_documents_from_file, create_faq_documents
|
| 18 |
|
| 19 |
-
create_faq_documents()
|
| 20 |
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
| 21 |
|
| 22 |
# Get all the filenames from the docs folder
|
|
@@ -70,36 +70,49 @@ with st.sidebar:
|
|
| 70 |
print("Used vectordb with all blog articles.")
|
| 71 |
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
Wenn du
|
| 89 |
-
|
| 90 |
-
###
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# (1) Retriever
|
| 102 |
-
retriever = vectorstore.as_retriever(search_type="similarity_score_threshold", search_kwargs={"score_threshold": 0.
|
| 103 |
|
| 104 |
# (2) Prompt
|
| 105 |
prompt = ChatPromptTemplate.from_template(prompt_template)
|
|
@@ -124,7 +137,6 @@ rag_chain = RunnableParallel(
|
|
| 124 |
{"context": retriever, "question": RunnablePassthrough()}
|
| 125 |
).assign(answer=rag_chain_from_docs)
|
| 126 |
|
| 127 |
-
|
| 128 |
st.title("🐔 Volker-Chat")
|
| 129 |
|
| 130 |
def click_button(prompt):
|
|
@@ -166,8 +178,13 @@ if st.session_state.clicked:
|
|
| 166 |
st.chat_message("user").write(prompt)
|
| 167 |
with get_openai_callback() as cb:
|
| 168 |
response = rag_chain.invoke(prompt)
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
with st.expander("Kontext ansehen"):
|
| 172 |
for citation in response["context"]:
|
| 173 |
st.write(str(citation.page_content))
|
|
@@ -178,12 +195,18 @@ if st.session_state.clicked:
|
|
| 178 |
sidebar_c.success(cb)
|
| 179 |
|
| 180 |
|
|
|
|
| 181 |
if prompt := st.chat_input():
|
| 182 |
st.chat_message("user").write(prompt)
|
| 183 |
with get_openai_callback() as cb:
|
| 184 |
response = rag_chain.invoke(prompt)
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
with st.expander("Kontext ansehen"):
|
| 188 |
for citation in response["context"]:
|
| 189 |
st.write(str(citation.page_content))
|
|
|
|
| 16 |
import json
|
| 17 |
from documents import read_documents_from_file, create_faq_documents
|
| 18 |
|
| 19 |
+
#create_faq_documents()
|
| 20 |
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
| 21 |
|
| 22 |
# Get all the filenames from the docs folder
|
|
|
|
| 70 |
print("Used vectordb with all blog articles.")
|
| 71 |
|
| 72 |
|
| 73 |
+
if data_source == 'FAQ':
|
| 74 |
+
prompt_template ="""
|
| 75 |
+
Du bist Volker. Du beantwortest häufig gestellte Fragen.
|
| 76 |
+
Beantworte nur die Fragen zu denen du einen Kontext hast. Wenn du die Antwort nicht weißt, sag dass du es nicht weißt.
|
| 77 |
+
Antworte immer in ganzen Sätzen und verwende korrekte Grammatik und Rechtschreibung. Antworte nur auf Deutsch.
|
| 78 |
+
Antworte kurz mit maximal fünf Sätzen außer es wird von dir eine ausführlichere Antwort verlangt.
|
| 79 |
+
Verwende zur Beantwortung der Frage nur den vorhandenen Kontext.
|
| 80 |
+
|
| 81 |
+
Frage: {question}
|
| 82 |
+
Kontext: {context}
|
| 83 |
+
Antwort:
|
| 84 |
+
"""
|
| 85 |
+
else:
|
| 86 |
+
prompt_template ="""
|
| 87 |
+
###
|
| 88 |
+
Wenn du gefragt wirst wer du bist: Du heißt "Volker". Du bist ein Assistent für die Beantwortung von Fragen zu Long-Covid (Post-Covid synonym verwenden).
|
| 89 |
+
|
| 90 |
+
###
|
| 91 |
+
Du weißt nichts über Krankheiten wie 'tumorbedingte Fatigue', 'Multiple Sklerose', 'Hashimoto-Thyreoditis' oder 'Krebs'.
|
| 92 |
+
Werden Fragen zu diesen Erkrankungen gestellt, beantworte sie mit "Dazu fehlen mir Informationen".
|
| 93 |
+
|
| 94 |
+
###
|
| 95 |
+
Du beantwortest keine Fragen zu 'Tod', 'Suizid', 'Selbstverletzung', oder anderen potenziell schädigenden Themen.
|
| 96 |
+
Werden Fragen zum 'Tod' gestellt, verweise auf den behandelnden Arzt.
|
| 97 |
+
Bei Fragen zu Suizid verweise auf die Telefonseelsorge: 0800 1110111
|
| 98 |
+
|
| 99 |
+
###
|
| 100 |
+
Du gibst keine Ratschläge zur Diagnose, Behandlung oder Therapie.
|
| 101 |
+
Wenn du die Antwort nicht weißt oder du keinen Kontext hast, sage dass du es nicht weißt.
|
| 102 |
+
|
| 103 |
+
###
|
| 104 |
+
Wenn du allgemeine unspezifische Fragen gestellt bekommst, antworte, dass du die Frage nicht verstehst frage nach einer präziseren Fragestellung.
|
| 105 |
+
Antworte immer in ganzen Sätzen und verwende korrekte Grammatik und Rechtschreibung. Antworte nur auf Deutsch.
|
| 106 |
+
Antworte kurz mit maximal fünf Sätzen außer es wird von dir eine ausführlichere Antwort verlangt.
|
| 107 |
+
Verwende zur Beantwortung der Frage nur den vorhandenen Kontext.
|
| 108 |
+
|
| 109 |
+
Frage: {question}
|
| 110 |
+
Kontext: {context}
|
| 111 |
+
Antwort:
|
| 112 |
+
""" # Source: hub.pull("rlm/rag-prompt")
|
| 113 |
|
| 114 |
# (1) Retriever
|
| 115 |
+
retriever = vectorstore.as_retriever(search_type="similarity_score_threshold", search_kwargs={"score_threshold": 0.75, "k": k})
|
| 116 |
|
| 117 |
# (2) Prompt
|
| 118 |
prompt = ChatPromptTemplate.from_template(prompt_template)
|
|
|
|
| 137 |
{"context": retriever, "question": RunnablePassthrough()}
|
| 138 |
).assign(answer=rag_chain_from_docs)
|
| 139 |
|
|
|
|
| 140 |
st.title("🐔 Volker-Chat")
|
| 141 |
|
| 142 |
def click_button(prompt):
|
|
|
|
| 178 |
st.chat_message("user").write(prompt)
|
| 179 |
with get_openai_callback() as cb:
|
| 180 |
response = rag_chain.invoke(prompt)
|
| 181 |
+
print(response)
|
| 182 |
+
if response['context'] != []:
|
| 183 |
+
response_stream = response_generator(response['answer'])
|
| 184 |
+
st.chat_message("assistant").write_stream(response_stream)
|
| 185 |
+
else:
|
| 186 |
+
response_stream = response_generator("Dazu kann ich dir leider keine Antwort geben. Bitte versuche eine andere Frage.")
|
| 187 |
+
st.chat_message("assistant").write_stream(response_stream)
|
| 188 |
with st.expander("Kontext ansehen"):
|
| 189 |
for citation in response["context"]:
|
| 190 |
st.write(str(citation.page_content))
|
|
|
|
| 195 |
sidebar_c.success(cb)
|
| 196 |
|
| 197 |
|
| 198 |
+
|
| 199 |
if prompt := st.chat_input():
|
| 200 |
st.chat_message("user").write(prompt)
|
| 201 |
with get_openai_callback() as cb:
|
| 202 |
response = rag_chain.invoke(prompt)
|
| 203 |
+
print(response)
|
| 204 |
+
if response['context'] != []:
|
| 205 |
+
response_stream = response_generator(response['answer'])
|
| 206 |
+
st.chat_message("assistant").write_stream(response_stream)
|
| 207 |
+
else:
|
| 208 |
+
response_stream = response_generator("Dazu kann ich dir leider keine Antwort geben. Bitte versuche eine andere Frage.")
|
| 209 |
+
st.chat_message("assistant").write_stream(response_stream)
|
| 210 |
with st.expander("Kontext ansehen"):
|
| 211 |
for citation in response["context"]:
|
| 212 |
st.write(str(citation.page_content))
|