def parse_final_answer(final_answer: str | None) -> str | None:
    """Parse the final answer from the final message generated by the agent
    to extract the final answer. The final answer is usually enclosed in the format:
    <<FINAL_ANSWER||
    <insert correct answer here>
    ||FINAL_ANSWER>>
    """
    # to do this first extract the part enclosed in the format <<FINAL_ANSWER|| ... ||FINAL_ANSWER>>
    pattern = re.compile(r'<<FINAL_ANSWER\|\|(.*?)\|\|FINAL_ANSWER>>', re.DOTALL)
    match = pattern.search(final_answer)

    # and then strip it, remove any leading/trailing spaces line breaks etc.
    answer = match.group(1).strip()
    # finally capitalize it
    answer = answer.upper()
    # and then return A, B, C, D depending on whether the answer A, B, C, D is found in the final answer
    for letter in ['A', 'B', 'C', 'D']:
        if letter in answer:
            return letter
Parse the final answer from the final message generated by the agent
    to extract the final answer. The final answer is usually enclosed in the format:
    <<FINAL_ANSWER||
    <insert correct answer here>
    ||FINAL_ANSWER>>
    
evaluation/benchmarks/gpqa/run_infer.py