File size: 1,237 Bytes
00db46c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import re


def extract_response_from_completions(

    completions: list[list[dict[str, str]]],

) -> list[str]:
    """

    Extract the response from the completions.



    Args:

        completions: The completions to extract the response from



    Returns:

        The response from the completions

    """
    return [completion[0]["content"] for completion in completions]


def extract_answer(completion: str) -> str:
    """

    Extract the answer from the completion.



    Args:

        completion: The completion to extract the answer from



    Returns:

        The answer from the completion

    """
    result = re.search(
        r"<answer>\s*(.*?)\s*</answer>", completion, re.DOTALL | re.IGNORECASE
    )
    if result is None:
        return completion.strip()
    return result.group(1).strip()


def extract_answers_from_completions(

    completions: list[list[dict[str, str]]],

) -> list[str]:
    """

    Extract the answers from the completions.



    Args:

        completions: The completions to extract the answers from



    Returns:

        The answers from the completions

    """
    return [extract_answer(completion[-1]["content"]) for completion in completions]