Update app.py
Browse files485 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522485 unmodified linesreturn "An unexpected error occurred. Please try again later."def extract_answer(full_response, instructions=None):# List of patterns to removepatterns_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 patternsfor pattern in patterns_to_remove:full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)# Remove any leading/trailing whitespace and newlinesfull_response = full_response.strip()# Remove the user instructions if presentif 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 responselines = 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:print(f"Error in extract_answer: {e}")print(f"Full response: {full_response}")print(f"Instructions: {instructions}")485 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523485 unmodified linesreturn "An unexpected error occurred. Please try again later."def extract_answer(full_response, instructions=None):try:# List of patterns to removepatterns_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 patternsfor pattern in patterns_to_remove:full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)# Remove any leading/trailing whitespace and newlinesfull_response = full_response.strip()# Remove the user instructions if presentif 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 responselines = 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:print(f"Error in extract_answer: {e}")print(f"Full response: {full_response}")print(f"Instructions: {instructions}")
app.py+31-30