Spaces:
Paused
Paused
Shreyas094
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -64,11 +64,11 @@ class Agent1:
|
|
64 |
|
65 |
def process(self, user_input: str) -> Dict[str, List[Dict[str, str]]]:
|
66 |
queries = self.rephrase_and_split(user_input)
|
67 |
-
print("Rephrased queries:", queries)
|
68 |
results = {}
|
69 |
for query in queries:
|
70 |
results[query] = google_search(query)
|
71 |
-
return results
|
72 |
|
73 |
def load_document(file: NamedTemporaryFile) -> List[Document]:
|
74 |
"""Loads and splits the document into pages."""
|
@@ -240,33 +240,75 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
240 |
max_attempts = 3
|
241 |
context_reduction_factor = 0.7
|
242 |
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
search_results = agent1.process(question)
|
247 |
-
web_docs = []
|
248 |
-
for query, results in search_results.items():
|
249 |
-
web_docs.extend([Document(page_content=result["text"], metadata={"source": result["link"], "query": query}) for result in results if result["text"]])
|
250 |
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
-
|
|
|
|
|
|
|
|
|
|
|
257 |
|
258 |
-
|
|
|
|
|
|
|
259 |
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
266 |
-
Provide a concise and direct answer to the original question without mentioning the web search or these instructions.
|
267 |
-
Do not include any source information in your answer.
|
268 |
-
"""
|
269 |
-
else:
|
270 |
if database is None:
|
271 |
return "No documents available. Please upload documents or enable web search to answer questions."
|
272 |
|
@@ -288,40 +330,35 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search):
|
|
288 |
Do not include any source information in your answer.
|
289 |
"""
|
290 |
|
291 |
-
|
292 |
-
|
293 |
|
294 |
-
|
295 |
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
if web_search:
|
313 |
-
sources = set(doc.metadata['source'] for doc in web_docs)
|
314 |
-
sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
|
315 |
-
answer += sources_section
|
316 |
|
317 |
-
|
318 |
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
|
326 |
return "An unexpected error occurred. Please try again later."
|
327 |
|
|
|
64 |
|
65 |
def process(self, user_input: str) -> Dict[str, List[Dict[str, str]]]:
|
66 |
queries = self.rephrase_and_split(user_input)
|
67 |
+
print("Rephrased queries:", queries)
|
68 |
results = {}
|
69 |
for query in queries:
|
70 |
results[query] = google_search(query)
|
71 |
+
return queries, results
|
72 |
|
73 |
def load_document(file: NamedTemporaryFile) -> List[Document]:
|
74 |
"""Loads and splits the document into pages."""
|
|
|
240 |
max_attempts = 3
|
241 |
context_reduction_factor = 0.7
|
242 |
|
243 |
+
if web_search:
|
244 |
+
queries, search_results = agent1.process(question)
|
245 |
+
all_answers = []
|
|
|
|
|
|
|
|
|
246 |
|
247 |
+
for query in queries:
|
248 |
+
for attempt in range(max_attempts):
|
249 |
+
try:
|
250 |
+
web_docs = [Document(page_content=result["text"], metadata={"source": result["link"], "query": query}) for result in search_results[query] if result["text"]]
|
251 |
+
|
252 |
+
if database is None:
|
253 |
+
database = FAISS.from_documents(web_docs, embed)
|
254 |
+
else:
|
255 |
+
database.add_documents(web_docs)
|
256 |
+
|
257 |
+
database.save_local("faiss_database")
|
258 |
+
|
259 |
+
context_str = "\n".join([f"Query: {doc.metadata['query']}\nSource: {doc.metadata['source']}\nContent: {doc.page_content}" for doc in web_docs])
|
260 |
+
|
261 |
+
prompt_template = """
|
262 |
+
Answer the question based on the following web search results:
|
263 |
+
Web Search Results:
|
264 |
+
{context}
|
265 |
+
Original Question: {question}
|
266 |
+
If the web search results don't contain relevant information, state that the information is not available in the search results.
|
267 |
+
Provide a concise and direct answer to the original question without mentioning the web search or these instructions.
|
268 |
+
Do not include any source information in your answer.
|
269 |
+
"""
|
270 |
+
|
271 |
+
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
272 |
+
formatted_prompt = prompt_val.format(context=context_str, question=query)
|
273 |
+
|
274 |
+
full_response = generate_chunked_response(model, formatted_prompt)
|
275 |
+
|
276 |
+
answer_patterns = [
|
277 |
+
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
278 |
+
r"Provide a concise and direct answer to the question:",
|
279 |
+
r"Answer:",
|
280 |
+
r"Provide a concise and direct answer to the original question without mentioning the web search or these instructions:",
|
281 |
+
r"Do not include any source information in your answer."
|
282 |
+
]
|
283 |
+
|
284 |
+
for pattern in answer_patterns:
|
285 |
+
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
286 |
+
if len(match) > 1:
|
287 |
+
answer = match[-1].strip()
|
288 |
+
break
|
289 |
+
else:
|
290 |
+
answer = full_response.strip()
|
291 |
+
|
292 |
+
all_answers.append(answer)
|
293 |
+
break
|
294 |
|
295 |
+
except Exception as e:
|
296 |
+
print(f"Error in ask_question for query '{query}' (attempt {attempt + 1}): {e}")
|
297 |
+
if "Input validation error" in str(e) and attempt < max_attempts - 1:
|
298 |
+
print(f"Reducing context length for next attempt")
|
299 |
+
elif attempt == max_attempts - 1:
|
300 |
+
all_answers.append(f"I apologize, but I'm having trouble processing the query '{query}' due to its length or complexity.")
|
301 |
|
302 |
+
answer = "\n\n".join(all_answers)
|
303 |
+
sources = set(doc.metadata['source'] for docs in search_results.values() for doc in [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in docs if result["text"]])
|
304 |
+
sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
|
305 |
+
answer += sources_section
|
306 |
|
307 |
+
return answer
|
308 |
+
|
309 |
+
else:
|
310 |
+
for attempt in range(max_attempts):
|
311 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
312 |
if database is None:
|
313 |
return "No documents available. Please upload documents or enable web search to answer questions."
|
314 |
|
|
|
330 |
Do not include any source information in your answer.
|
331 |
"""
|
332 |
|
333 |
+
prompt_val = ChatPromptTemplate.from_template(prompt_template)
|
334 |
+
formatted_prompt = prompt_val.format(context=context_str, question=question)
|
335 |
|
336 |
+
full_response = generate_chunked_response(model, formatted_prompt)
|
337 |
|
338 |
+
answer_patterns = [
|
339 |
+
r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
|
340 |
+
r"Provide a concise and direct answer to the question:",
|
341 |
+
r"Answer:",
|
342 |
+
r"Provide a concise and direct answer to the original question without mentioning the web search or these instructions:",
|
343 |
+
r"Do not include any source information in your answer."
|
344 |
+
]
|
345 |
|
346 |
+
for pattern in answer_patterns:
|
347 |
+
match = re.split(pattern, full_response, flags=re.IGNORECASE)
|
348 |
+
if len(match) > 1:
|
349 |
+
answer = match[-1].strip()
|
350 |
+
break
|
351 |
+
else:
|
352 |
+
answer = full_response.strip()
|
|
|
|
|
|
|
|
|
|
|
353 |
|
354 |
+
return answer
|
355 |
|
356 |
+
except Exception as e:
|
357 |
+
print(f"Error in ask_question (attempt {attempt + 1}): {e}")
|
358 |
+
if "Input validation error" in str(e) and attempt < max_attempts - 1:
|
359 |
+
print(f"Reducing context length for next attempt")
|
360 |
+
elif attempt == max_attempts - 1:
|
361 |
+
return f"I apologize, but I'm having trouble processing your question due to its length or complexity. Could you please try rephrasing it more concisely?"
|
362 |
|
363 |
return "An unexpected error occurred. Please try again later."
|
364 |
|