Commit
•
fa817a6
1
Parent(s):
467d298
Update app.py
Browse files
app.py
CHANGED
@@ -54,6 +54,7 @@ from langchain.schema import Document
|
|
54 |
import gradio as gr
|
55 |
import tempfile
|
56 |
import timeit
|
|
|
57 |
|
58 |
FILE_LOADER_MAPPING = {
|
59 |
"csv": (CSVLoader, {"encoding": "utf-8"}),
|
@@ -171,11 +172,16 @@ def create_chain(llm, prompt, db):
|
|
171 |
# condense_question_prompt=CONDENSE_QUESTION_PROMPT,
|
172 |
# memory=memory,
|
173 |
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
chain = RetrievalQA.from_chain_type(llm=llm,
|
175 |
chain_type='stuff',
|
176 |
retriever=db.as_retriever(search_kwargs={'k': 3}),
|
177 |
-
return_source_documents=True
|
178 |
-
chain_type_kwargs={'prompt': prompt}
|
179 |
)
|
180 |
return chain
|
181 |
|
@@ -215,6 +221,18 @@ def create_retrieval_qa_bot(loaded_documents):
|
|
215 |
|
216 |
return qa
|
217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
def retrieve_bot_answer(query, loaded_documents):
|
219 |
"""
|
220 |
Retrieves the answer to a given query using a QA bot.
|
@@ -236,8 +254,13 @@ def retrieve_bot_answer(query, loaded_documents):
|
|
236 |
# raise KeyError("Expected 'answer' key in bot_response, but it was not found.")
|
237 |
# result = bot_response['answer']
|
238 |
|
239 |
-
result = bot_response['result']
|
240 |
-
sources = []
|
|
|
|
|
|
|
|
|
|
|
241 |
for source in bot_response["source_documents"]:
|
242 |
sources.append(source.metadata['source'])
|
243 |
return result, sources
|
|
|
54 |
import gradio as gr
|
55 |
import tempfile
|
56 |
import timeit
|
57 |
+
import textwrap
|
58 |
|
59 |
FILE_LOADER_MAPPING = {
|
60 |
"csv": (CSVLoader, {"encoding": "utf-8"}),
|
|
|
172 |
# condense_question_prompt=CONDENSE_QUESTION_PROMPT,
|
173 |
# memory=memory,
|
174 |
# )
|
175 |
+
# chain = RetrievalQA.from_chain_type(llm=llm,
|
176 |
+
# chain_type='stuff',
|
177 |
+
# retriever=db.as_retriever(search_kwargs={'k': 3}),
|
178 |
+
# return_source_documents=True,
|
179 |
+
# chain_type_kwargs={'prompt': prompt}
|
180 |
+
# )
|
181 |
chain = RetrievalQA.from_chain_type(llm=llm,
|
182 |
chain_type='stuff',
|
183 |
retriever=db.as_retriever(search_kwargs={'k': 3}),
|
184 |
+
return_source_documents=True
|
|
|
185 |
)
|
186 |
return chain
|
187 |
|
|
|
221 |
|
222 |
return qa
|
223 |
|
224 |
+
def wrap_text_preserve_newlines(text, width=110):
|
225 |
+
# Split the input text into lines based on newline characters
|
226 |
+
lines = text.split('\n')
|
227 |
+
|
228 |
+
# Wrap each line individually
|
229 |
+
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
230 |
+
|
231 |
+
# Join the wrapped lines back together using newline characters
|
232 |
+
wrapped_text = '\n'.join(wrapped_lines)
|
233 |
+
|
234 |
+
return wrapped_text
|
235 |
+
|
236 |
def retrieve_bot_answer(query, loaded_documents):
|
237 |
"""
|
238 |
Retrieves the answer to a given query using a QA bot.
|
|
|
254 |
# raise KeyError("Expected 'answer' key in bot_response, but it was not found.")
|
255 |
# result = bot_response['answer']
|
256 |
|
257 |
+
# result = bot_response['result']
|
258 |
+
# sources = []
|
259 |
+
# for source in bot_response["source_documents"]:
|
260 |
+
# sources.append(source.metadata['source'])
|
261 |
+
# return result, sources
|
262 |
+
|
263 |
+
result = wrap_text_preserve_newlines(bot_response['result'])
|
264 |
for source in bot_response["source_documents"]:
|
265 |
sources.append(source.metadata['source'])
|
266 |
return result, sources
|