MCPcopy
hub / github.com/allenai/open-instruct / extract_final_answer

Function extract_final_answer

open_instruct/utils.py:1672–1697  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1670
1671
1672def 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 -----------------------------------------------------------------

Callers 1

async_callMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected