|
|
import re |
|
|
|
|
|
|
|
|
def extract_final_answer(output: str) -> str: |
|
|
""" |
|
|
Extracts the text after 'FINAL ANSWER:' in the model's output. |
|
|
Strips whitespace and ensures clean formatting. |
|
|
If the answer is a comma-separated list, ensures a space after each comma. |
|
|
""" |
|
|
output = str(output) |
|
|
marker = "FINAL ANSWER:" |
|
|
lower_output = output.lower() |
|
|
|
|
|
if marker.lower() in lower_output: |
|
|
|
|
|
idx = lower_output.rfind(marker.lower()) |
|
|
raw_answer = output[idx + len(marker) :].strip() |
|
|
|
|
|
|
|
|
cleaned_answer = re.sub(r",\s*", ", ", raw_answer) |
|
|
return cleaned_answer |
|
|
|
|
|
return output |
|
|
|
|
|
|
|
|
def replace_tool_mentions(prompt: str) -> str: |
|
|
|
|
|
prompt = re.sub(r"(?<!\w)`search`(?!\w)", "`web_search`", prompt) |
|
|
prompt = re.sub(r"(?<!\w)`wiki`(?!\w)", "`wikipedia_search`", prompt) |
|
|
|
|
|
|
|
|
|
|
|
prompt = re.sub(r"(?<!\w)(?<!_)search\(", "web_search(", prompt) |
|
|
prompt = re.sub(r"(?<!\w)(?<!_)wiki\(", "wikipedia_search(", prompt) |
|
|
|
|
|
return prompt |
|
|
|