Update app.py
Browse files299 unmodified lines30030130230330430530630730330430530630730833 unmodified lines3423433443483493503513523533543553563573583593603613623633643453463473489 unmodified lines3583593603803613623633643843653663673873683693703711 unmodified line373374375395376377378398399379380381401382383384405385386387408409410411412413414415416417418419420421422423388389390391392393429430431432394395396397398399400401402403404405406407408409410411412413414415438416417418419299 unmodified lines max_attempts = 3 context_reduction_factor = 0.7
contextualized_question, topics, entity_tracker = chatbot.process_question(question)
# Convert sets to lists in entity_tracker serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()}
if web_search: contextualized_question, topics, entity_tracker = chatbot.process_question(question) serializable_entity_tracker = {k: list(v) for k, v in entity_tracker.items()} search_results = google_search(contextualized_question) all_answers = []
33 unmodified lines )
full_response = generate_chunked_response(model, formatted_prompt)
answer_patterns = [ r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:", r"Provide a concise and direct answer to the question:", r"Answer:", r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:", r"Do not include any source information in your answer." ]
for pattern in answer_patterns: match = re.split(pattern, full_response, flags=re.IGNORECASE) if len(match) > 1: answer = match[-1].strip() break else: answer = full_response.strip()
answer = extract_answer(full_response) all_answers.append(answer) break
9 unmodified lines
return answer
else: else: # PDF document chat for attempt in range(max_attempts): try: if database is None: return "No documents available. Please upload documents or enable web search to answer questions." return "No documents available. Please upload PDF documents to answer questions."
retriever = database.as_retriever() relevant_docs = retriever.get_relevant_documents(contextualized_question) relevant_docs = retriever.get_relevant_documents(question) context_str = "\n".join([doc.page_content for doc in relevant_docs])
if attempt > 0:1 unmodified line context_str = " ".join(words[:int(len(words) * context_reduction_factor)])
prompt_template = """ Answer the question based on the following context: Answer the question based on the following context from the PDF document: Context: {context} Current Question: {question} If the context doesn't contain relevant information, state that the information is not available. Question: {question} If the context doesn't contain relevant information, state that the information is not available in the document. Provide a summarized and direct answer to the question. Do not include any source information in your answer. """
prompt_val = ChatPromptTemplate.from_template(prompt_template) formatted_prompt = prompt_val.format(context=context_str, question=contextualized_question) formatted_prompt = prompt_val.format(context=context_str, question=question)
full_response = generate_chunked_response(model, formatted_prompt)
answer_patterns = [ r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:", r"Provide a concise and direct answer to the question:", r"Answer:", r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:", r"Do not include any source information in your answer." ]
for pattern in answer_patterns: match = re.split(pattern, full_response, flags=re.IGNORECASE) if len(match) > 1: answer = match[-1].strip() break else: answer = full_response.strip() answer = extract_answer(full_response)
return answer
except Exception as e: print(f"Error in ask_question (attempt {attempt + 1}): {e}") if "Input validation error" in str(e) and attempt < max_attempts - 1: print(f"Reducing context length for next attempt") elif attempt == max_attempts - 1: 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?" if attempt == max_attempts - 1: return f"I apologize, but I'm having trouble processing your question. Could you please try rephrasing it more concisely?"
return "An unexpected error occurred. Please try again later."
def extract_answer(full_response): answer_patterns = [ r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:", r"Provide a concise and direct answer to the question:", r"Answer:", r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:", r"Do not include any source information in your answer." ]
for pattern in answer_patterns: match = re.split(pattern, full_response, flags=re.IGNORECASE) if len(match) > 1: return match[-1].strip() return full_response.strip()
# Gradio interfacewith gr.Blocks() as demo: gr.Markdown("# Enhanced Context-Driven Conversational Chatbot") gr.Markdown("# Enhanced PDF Document Chat and Web Search") with gr.Row(): file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])app.py+29-51