Spaces:
Paused
Paused
Shreyas094
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -218,89 +218,88 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
218 |
|
219 |
for attempt in range(max_attempts):
|
220 |
try:
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
search_results = google_search(rephrased_query)
|
231 |
-
web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
|
232 |
-
|
233 |
-
if database is None:
|
234 |
-
database = FAISS.from_documents(web_docs, embed)
|
235 |
-
else:
|
236 |
-
database.add_documents(web_docs)
|
237 |
-
|
238 |
-
database.save_local("faiss_database")
|
239 |
-
|
240 |
-
context_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])
|
241 |
-
|
242 |
-
prompt_template = """
|
243 |
-
Answer the question based on the following web search results:
|
244 |
-
Web Search Results:
|
245 |
-
{context}
|
246 |
-
Original Question: {original_question}
|
247 |
-
Rephrased Search Query: {rephrased_query}
|
248 |
-
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
249 |
-
Provide a concise and direct answer to the original question without mentioning the web search or these instructions.
|
250 |
-
Do not include any source information in your answer.
|
251 |
-
"""
|
252 |
-
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
253 |
-
formatted_prompt = prompt_val.format(context=context_str, original_question=question, rephrased_query=rephrased_query)
|
254 |
-
|
255 |
-
else:
|
256 |
-
if database is None:
|
257 |
-
return "No documents available. Please upload documents or enable web search to answer questions."
|
258 |
-
|
259 |
-
retriever = database.as_retriever()
|
260 |
-
relevant_docs = retriever.get_relevant_documents(question)
|
261 |
-
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
262 |
-
|
263 |
-
# Reduce context if we're not on the first attempt
|
264 |
-
if attempt > 0:
|
265 |
-
words = context_str.split()
|
266 |
-
context_str = " ".join(words[:int(len(words) * context_reduction_factor)])
|
267 |
-
|
268 |
-
prompt_template = """
|
269 |
-
Answer the question based on the following context:
|
270 |
-
Context:
|
271 |
-
{context}
|
272 |
-
Current Question: {question}
|
273 |
-
If the context doesn't contain relevant information, state that the information is not available.
|
274 |
-
Provide a concise and direct answer to the question.
|
275 |
-
Do not include any source information in your answer.
|
276 |
-
"""
|
277 |
-
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
278 |
-
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
279 |
-
|
280 |
-
full_response = generate_chunked_response(model, formatted_prompt)
|
281 |
-
|
282 |
-
answer_patterns = [
|
283 |
-
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
284 |
-
r"Provide a concise and direct answer to the question:",
|
285 |
-
r"Answer:",
|
286 |
-
r"Provide a concise and direct answer to the original question without mentioning the web search or these instructions:"
|
287 |
-
]
|
288 |
-
|
289 |
-
for pattern in answer_patterns:
|
290 |
-
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
291 |
-
if len(match) > 1:
|
292 |
-
answer = match[-1].strip()
|
293 |
-
break
|
294 |
-
else:
|
295 |
-
answer = full_response.strip()
|
296 |
|
297 |
-
|
298 |
-
|
299 |
-
sources = set(doc.metadata['source'] for doc in web_docs)
|
300 |
-
sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
|
301 |
-
answer += sources_section
|
302 |
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
|
305 |
except Exception as e:
|
306 |
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
|
|
218 |
|
219 |
for attempt in range(max_attempts):
|
220 |
try:
|
221 |
+
if web_search:
|
222 |
+
original_query = question
|
223 |
+
rephrased_query = rephrase_for_search(original_query, model)
|
224 |
+
print(f"Original query: {original_query}")
|
225 |
+
print(f"Rephrased query: {rephrased_query}")
|
226 |
+
|
227 |
+
if rephrased_query == original_query:
|
228 |
+
print("Warning: Query was not rephrased. Using original query for search.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
|
230 |
+
search_results = google_search(rephrased_query)
|
231 |
+
web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]
|
|
|
|
|
|
|
232 |
|
233 |
+
if database is None:
|
234 |
+
database = FAISS.from_documents(web_docs, embed)
|
235 |
+
else:
|
236 |
+
database.add_documents(web_docs)
|
237 |
+
|
238 |
+
database.save_local("faiss_database")
|
239 |
+
|
240 |
+
context_str = "\n".join([f"Source: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])
|
241 |
+
|
242 |
+
prompt_template = """
|
243 |
+
Answer the question based on the following web search results:
|
244 |
+
Web Search Results:
|
245 |
+
{context}
|
246 |
+
Original Question: {original_question}
|
247 |
+
Rephrased Search Query: {rephrased_query}
|
248 |
+
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
249 |
+
Provide a concise and direct answer to the original question without mentioning the web search or these instructions.
|
250 |
+
Do not include any source information in your answer.
|
251 |
+
"""
|
252 |
+
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
253 |
+
formatted_prompt = prompt_val.format(context=context_str, original_question=question, rephrased_query=rephrased_query)
|
254 |
+
else:
|
255 |
+
if database is None:
|
256 |
+
return "No documents available. Please upload documents or enable web search to answer questions."
|
257 |
+
|
258 |
+
retriever = database.as_retriever()
|
259 |
+
relevant_docs = retriever.get_relevant_documents(question)
|
260 |
+
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
261 |
+
|
262 |
+
# Reduce context if we're not on the first attempt
|
263 |
+
if attempt > 0:
|
264 |
+
words = context_str.split()
|
265 |
+
context_str = " ".join(words[:int(len(words) * context_reduction_factor)])
|
266 |
+
|
267 |
+
prompt_template = """
|
268 |
+
Answer the question based on the following context:
|
269 |
+
Context:
|
270 |
+
{context}
|
271 |
+
Current Question: {question}
|
272 |
+
If the context doesn't contain relevant information, state that the information is not available.
|
273 |
+
Provide a concise and direct answer to the question.
|
274 |
+
Do not include any source information in your answer.
|
275 |
+
"""
|
276 |
+
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
277 |
+
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
278 |
+
|
279 |
+
full_response = generate_chunked_response(model, formatted_prompt)
|
280 |
+
|
281 |
+
answer_patterns = [
|
282 |
+
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
283 |
+
r"Provide a concise and direct answer to the question:",
|
284 |
+
r"Answer:",
|
285 |
+
r"Provide a concise and direct answer to the original question without mentioning the web search or these instructions:"
|
286 |
+
]
|
287 |
+
|
288 |
+
for pattern in answer_patterns:
|
289 |
+
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
290 |
+
if len(match) > 1:
|
291 |
+
answer = match[-1].strip()
|
292 |
+
break
|
293 |
+
else:
|
294 |
+
answer = full_response.strip()
|
295 |
+
|
296 |
+
# Add sources section
|
297 |
+
if web_search:
|
298 |
+
sources = set(doc.metadata['source'] for doc in web_docs)
|
299 |
+
sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
|
300 |
+
answer += sources_section
|
301 |
+
|
302 |
+
return answer
|
303 |
|
304 |
except Exception as e:
|
305 |
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|