Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -241,10 +241,8 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
241 |
web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
|
242 |
|
243 |
if database is None:
|
244 |
-
# Create the database with web search results if it doesn't exist
|
245 |
database = FAISS.from_documents(web_docs, embed)
|
246 |
else:
|
247 |
-
# Add web search results to the existing database
|
248 |
database.add_documents(web_docs)
|
249 |
|
250 |
database.save_local("faiss_database")
|
@@ -257,7 +255,7 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
257 |
{context}
|
258 |
Current Question: {question}
|
259 |
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
260 |
-
Provide a concise and direct answer to the question
|
261 |
"""
|
262 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
263 |
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
@@ -277,12 +275,20 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
277 |
prompt_val = ChatPromptTemplate.from_template(prompt)
|
278 |
formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)
|
279 |
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
if not web_search:
|
288 |
memory_database[question] = answer
|
|
|
241 |
web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
|
242 |
|
243 |
if database is None:
|
|
|
244 |
database = FAISS.from_documents(web_docs, embed)
|
245 |
else:
|
|
|
246 |
database.add_documents(web_docs)
|
247 |
|
248 |
database.save_local("faiss_database")
|
|
|
255 |
{context}
|
256 |
Current Question: {question}
|
257 |
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
258 |
+
Provide a concise and direct answer to the question:
|
259 |
"""
|
260 |
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
261 |
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
|
|
275 |
prompt_val = ChatPromptTemplate.from_template(prompt)
|
276 |
formatted_prompt = prompt_val.format(history=history_str, context=context_str, question=question)
|
277 |
|
278 |
+
full_response = generate_chunked_response(model, formatted_prompt)
|
279 |
+
|
280 |
+
# Filter out the prompt and extract only the answer
|
281 |
+
answer_pattern = r"(?:Answer:|Provide a concise and direct answer to the question:)([\s\S]*)"
|
282 |
+
match = re.search(answer_pattern, full_response, re.IGNORECASE)
|
283 |
+
|
284 |
+
if match:
|
285 |
+
answer = match.group(1).strip()
|
286 |
+
else:
|
287 |
+
# If no match is found, use the entire response but remove any instructions
|
288 |
+
answer = re.sub(r"(?i)If the .+?instructions:", "", full_response).strip()
|
289 |
+
|
290 |
+
# Remove any numbered list formatting
|
291 |
+
answer = re.sub(r'^\s*\d+\.\s*', '', answer, flags=re.MULTILINE)
|
292 |
|
293 |
if not web_search:
|
294 |
memory_database[question] = answer
|