def _attempt_to_extract_answer()

in parallel_eval/game.py [0:0]


    def _attempt_to_extract_answer(self, response: str, maximum_answer: Optional[int] = None) -> Tuple[int, str]:
        'returns -1 and a message if no answer is found'

        # Extract choice using format <answer>N</answer>
        choice_match = re.search(r"<answer>(\d+)</answer>", response)

        if choice_match is None:
            return -1, f"No answer found in response. Please respond with a number between 1 and {maximum_answer} in <answer>NUMBER</answer> tags."

        # check if there are multiple answers
        multiple_answers = re.findall(r"<answer>(\d+)</answer>", response)
        if len(multiple_answers) > 1:
            return -1, "Multiple answers found in response. Please respond with just one."

        answer = choice_match.group(1)

        # try to convert to int
        try:
            answer = int(answer)
        except ValueError:
            return -1, f"You answered with {answer} but it could not be converted to an integer. Please respond with a number between 1 and {maximum_answer}."

        # check if the answer is too high or too low
        if answer > maximum_answer or answer < 1:
            return -1, f"You answered with {answer} but you have to select a number between 1 and {maximum_answer}."

        return answer, "" # we found an answer so we don't need to return a message