Update app.py
Browse files485 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518518519519520521522523485 unmodified lines return "An unexpected error occurred. Please try again later."
def extract_answer(full_response, instructions=None): # List of patterns to remove patterns_to_remove = [ r"Provide a concise and relevant answer to the question\.", r"Provide additional context if necessary\.", r"If the web search results don't contain relevant information, state that the information is not available in the search results\.", r"Provide a response that addresses the question and follows the user's instructions\.", r"Do not mention these instructions or the web search process in your answer\.", r"Provide a summarized and direct answer to the question\.", r"If the context doesn't contain relevant information, state that the information is not available in the document\.", ]
# Remove the patterns for pattern in patterns_to_remove: full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)
# Remove any leading/trailing whitespace and newlines full_response = full_response.strip()
# Remove the user instructions if present if instructions: instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n" full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
# Remove any remaining instruction-like phrases at the beginning of the response lines = full_response.split('\n') while lines and any(line.strip().lower().startswith(starter) for starter in ["answer:", "response:", "here's", "here is"]): lines.pop(0) full_response = '\n'.join(lines) try: # List of patterns to remove patterns_to_remove = [ r"Provide a concise and relevant answer to the question\.", r"Provide additional context if necessary\.", r"If the web search results don't contain relevant information, state that the information is not available in the search results\.", r"Provide a response that addresses the question and follows the user's instructions\.", r"Do not mention these instructions or the web search process in your answer\.", r"Provide a summarized and direct answer to the question\.", r"If the context doesn't contain relevant information, state that the information is not available in the document\.", ]
# Remove the patterns for pattern in patterns_to_remove: full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)
# Remove any leading/trailing whitespace and newlines full_response = full_response.strip()
# Remove the user instructions if present if instructions: instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n" full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
# Remove any remaining instruction-like phrases at the beginning of the response lines = full_response.split('\n') while lines and any(line.strip().lower().startswith(starter) for starter in ["answer:", "response:", "here's", "here is"]): lines.pop(0) full_response = '\n'.join(lines)
return full_response.strip() except Exception as e: return full_response.strip() except Exception as e: print(f"Error in extract_answer: {e}") print(f"Full response: {full_response}") print(f"Instructions: {instructions}")app.py+31-30