File size: 1,589 Bytes
e93eb3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
import re


def get_turn_title(response_text):
    tag = 'turn_title'
    pattern = fr'<{tag}>(.*?)</{tag}>'
    values = re.findall(pattern, response_text, re.DOTALL)
    values0 = values.copy()
    values = [v.strip() for v in values]
    values = [v for v in values if v]
    if len(values) == 0:
        # then maybe removed too much
        values = values0
    if values:
        turn_title = values[-1]
    else:
        turn_title = response_text[:15] + '...'
    return turn_title


def get_final_answer(response_text, cli_mode=False):
    tag = 'final_answer'
    pattern = fr'<{tag}>(.*?)</{tag}>'
    values = re.findall(pattern, response_text, re.DOTALL)
    values0 = values.copy()
    values = [v.strip() for v in values]
    values = [v for v in values if v]
    if len(values) == 0:
        # then maybe removed too much
        values = values0
    if values:
        if cli_mode:
            response_text = '\n\nFINAL ANSWER:\n\n' + values[-1] + '\n\n'
        else:
            response_text = values[-1]
    else:
        response_text = None
    return response_text


def get_xml_tag_value(response_text, tag, ret_all=True):
    pattern = fr'<{tag}>(.*?)</{tag}>'
    values = re.findall(pattern, response_text, re.DOTALL)
    values0 = values.copy()
    values = [v.strip() for v in values]
    values = [v for v in values if v]
    if len(values) == 0:
        # then maybe removed too much
        values = values0
    if values:
        if ret_all:
            ret = values
        else:
            ret = [values[-1]]
    else:
        ret = []
    return ret