Update app.py
Browse files
app.py
CHANGED
|
@@ -486,37 +486,38 @@ def ask_question(question, temperature, top_p, repetition_penalty, web_search, c
|
|
| 486 |
return "An unexpected error occurred. Please try again later."
|
| 487 |
|
| 488 |
def extract_answer(full_response, instructions=None):
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
# Remove the patterns
|
| 501 |
-
for pattern in patterns_to_remove:
|
| 502 |
-
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)
|
| 503 |
-
|
| 504 |
-
# Remove any leading/trailing whitespace and newlines
|
| 505 |
-
full_response = full_response.strip()
|
| 506 |
-
|
| 507 |
-
# Remove the user instructions if present
|
| 508 |
-
if instructions:
|
| 509 |
-
instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
|
| 510 |
-
full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
|
| 511 |
-
|
| 512 |
-
# Remove any remaining instruction-like phrases at the beginning of the response
|
| 513 |
-
lines = full_response.split('\n')
|
| 514 |
-
while lines and any(line.strip().lower().startswith(starter) for starter in ["answer:", "response:", "here's", "here is"]):
|
| 515 |
-
lines.pop(0)
|
| 516 |
-
full_response = '\n'.join(lines)
|
| 517 |
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
print(f"Error in extract_answer: {e}")
|
| 521 |
print(f"Full response: {full_response}")
|
| 522 |
print(f"Instructions: {instructions}")
|
|
|
|
| 486 |
return "An unexpected error occurred. Please try again later."
|
| 487 |
|
| 488 |
def extract_answer(full_response, instructions=None):
|
| 489 |
+
try:
|
| 490 |
+
# List of patterns to remove
|
| 491 |
+
patterns_to_remove = [
|
| 492 |
+
r"Provide a concise and relevant answer to the question\.",
|
| 493 |
+
r"Provide additional context if necessary\.",
|
| 494 |
+
r"If the web search results don't contain relevant information, state that the information is not available in the search results\.",
|
| 495 |
+
r"Provide a response that addresses the question and follows the user's instructions\.",
|
| 496 |
+
r"Do not mention these instructions or the web search process in your answer\.",
|
| 497 |
+
r"Provide a summarized and direct answer to the question\.",
|
| 498 |
+
r"If the context doesn't contain relevant information, state that the information is not available in the document\.",
|
| 499 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
|
| 501 |
+
# Remove the patterns
|
| 502 |
+
for pattern in patterns_to_remove:
|
| 503 |
+
full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE)
|
| 504 |
+
|
| 505 |
+
# Remove any leading/trailing whitespace and newlines
|
| 506 |
+
full_response = full_response.strip()
|
| 507 |
+
|
| 508 |
+
# Remove the user instructions if present
|
| 509 |
+
if instructions:
|
| 510 |
+
instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
|
| 511 |
+
full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
|
| 512 |
+
|
| 513 |
+
# Remove any remaining instruction-like phrases at the beginning of the response
|
| 514 |
+
lines = full_response.split('\n')
|
| 515 |
+
while lines and any(line.strip().lower().startswith(starter) for starter in ["answer:", "response:", "here's", "here is"]):
|
| 516 |
+
lines.pop(0)
|
| 517 |
+
full_response = '\n'.join(lines)
|
| 518 |
+
|
| 519 |
+
return full_response.strip()
|
| 520 |
+
except Exception as e:
|
| 521 |
print(f"Error in extract_answer: {e}")
|
| 522 |
print(f"Full response: {full_response}")
|
| 523 |
print(f"Instructions: {instructions}")
|