Extract the substring between and . If no match is found, extract the substring after . If neither condition matches, clean the prediction by removing the <|assistant|> tag. If none of the above applies, return the original string. Args: predic
(prediction: str)
| 1670 | |
| 1671 | |
| 1672 | def extract_final_answer(prediction: str) -> str: |
| 1673 | """ |
| 1674 | Extract the substring between <answer> and </answer>. |
| 1675 | If no match is found, extract the substring after </think>. |
| 1676 | If neither condition matches, clean the prediction by removing the <|assistant|> tag. |
| 1677 | If none of the above applies, return the original string. |
| 1678 | |
| 1679 | Args: |
| 1680 | prediction (str): The input string. |
| 1681 | |
| 1682 | Returns: |
| 1683 | str: The extracted substring or the cleaned/original string. |
| 1684 | """ |
| 1685 | answer_match = re.search(r"<answer>(.*?)</answer>", prediction, re.DOTALL) |
| 1686 | if answer_match: |
| 1687 | return answer_match.group(1).strip() |
| 1688 | |
| 1689 | think_match = re.search(r"</think>(.*)", prediction, re.DOTALL) |
| 1690 | if think_match: |
| 1691 | return think_match.group(1).strip() |
| 1692 | |
| 1693 | cleaned = re.sub(r"<\|assistant\|>", "", prediction) |
| 1694 | if cleaned != prediction: |
| 1695 | return cleaned.strip() |
| 1696 | |
| 1697 | return prediction |
| 1698 | |
| 1699 | |
| 1700 | # ---- Runtime leak detection ----------------------------------------------------------------- |