File size: 11,143 Bytes
a12c07f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'''
This is the helper py file to save all kinds of ParsedAnswer type. 

ParsedAnswer: 
    Input: language model raw output(words)
    Output: formatted output according to the given pattern
    Input_instrcution: There will be a few lines that could be imported to the model
        as a in-context guidance
'''


import re
from .common import ParsedAnswer
from .exceptions import *
from typing import List


class LeftOrRight(ParsedAnswer):
    
    def __init__(self, l_or_r:str, gpt_raw:str=None):
        if l_or_r.lower().strip() not in ('left', 'right'):
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            # print(gpt_raw)
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            raise GPTOutputParseException("output of LLM should either be 'left' or 'right'")
        self.data = l_or_r.lower().strip()
        self.raw = gpt_raw

    @staticmethod
    def remove_answer_text(s:str):
        # Define the regex pattern to match Python code blocks in Markdown
        pattern = r'```\s*([^`]+)```'
        
        # Use re.findall to extract all matches of the pattern
        matches = re.findall(pattern, s, re.DOTALL)
        
        # Join the matches into a single string
        answer_text = '\n'.join(matches)
        return answer_text

    @staticmethod
    def parser(gpt_raw:str) -> "LeftOrRight":
        """
        @GPT4-doc-begin
            gpt_raw: A block of text that has a single word in a text block, indicated by ```.
                    The word should be either "right" or "left".
                    For example,
                    The question that you've provided asks me to choose the image (left or right) that best aligns with the user text prompt.
                    Though the sample on the left is more realistic, the sample on the right is better aligned with the user prompt.
                    ```
                    right
                    ```
        @GPT4-doc-end
        """
    
        left_or_right = LeftOrRight.remove_answer_text(gpt_raw)
        return LeftOrRight(left_or_right, gpt_raw=gpt_raw) 

    def __str__(self):
        return self.data

class StarredList(ParsedAnswer):
    def __init__(self, list_items:List[str], gpt_raw:str=None):
        self.list_items = list_items 
        self.raw = gpt_raw 

    @staticmethod
    def parse_bullet_points(input_string):
        lines = input_string.split("\n")
        bullet_points = []
        current_bullet_point = ""

        for line in lines:
            if line.startswith("* "):
                if current_bullet_point:
                    bullet_points.append(current_bullet_point.strip())
                    current_bullet_point = ""
                current_bullet_point += line[2:] + " "
            elif current_bullet_point:
                current_bullet_point += line + " "

        if current_bullet_point:
            bullet_points.append(current_bullet_point.strip())

        return bullet_points

    @staticmethod     
    def parser(gpt_raw:str) -> "StarredList":
        """
        @GPT4-doc-begin
            gpt_raw: A new-line separated bulletpoint list that follows the following format:
                
                 Example:
                 * first item
                 * second item
                 ...etc
        @GPT4-doc-end
        """
        list_items = StarredList.parse_bullet_points(gpt_raw)
        return StarredList(list_items=list_items, gpt_raw=gpt_raw)

class PythonExecutableDiffAnswer(ParsedAnswer):
    """ Code (python) difference
    """

    def __init__(self, code_before, code_after, gpt_raw:str=None):
        self.code_from = code_before
        self.code_to = code_after 
        self.raw = gpt_raw

    @staticmethod
    def parser(gpt_raw:str)  -> "PythonExecutableDiffAnswer":
        """ 
        @GPT4-doc-begin
            gpt_raw: A desired code change to a single line, 
                     indicated by "Before:" and "After:" labels,
                     followed by python code blocks that indicate which line should be
                     changed, and to what.
                     Example:
                     
                     Before:
                     ```python
                     a = 1
                     ```
                     After:
                     ```python
                     a = 2 
                     ```
        @GPT4-doc-end 
        """
            
        before_after = gpt_raw.split("After")
        before_text = before_after[0]
        assert "Before" in before_text 
        after_text = before_after[-1]
         
        try: 
            before_string = PythonExecutableAnswer.remove_markdown_code(before_text)
            after_string = PythonExecutableAnswer.remove_markdown_code(after_text)
        except:
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            # print(gpt_raw)
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            raise GPTOutputParseException(f"Invalid input passsed into parsing function. The following could not be parsed:\n{gpt_raw}")
        return PythonExecutableDiffAnswer(code_before=before_string, code_after=after_string, gpt_raw=gpt_raw)

    def __str__(self):
        return str(self.code)


class PythonExecutableAnswer(ParsedAnswer):
    """ Code (Python)
    """
    def __init__(self, code, gpt_raw:str=None):
        self.code = code 
        self.raw = gpt_raw

    @staticmethod
    def remove_markdown_code(s:str):

        if "```python" not in s:
            raise GPTOutputParseException("No ```python found in the input.")
        chunk1 = s.split("```python")[-1]
        if "```"  not in chunk1:
            raise GPTOutputParseException("No ``` found  to close the code block.")
        python_code = chunk1.split("```")[0]
        return python_code

    @staticmethod
    def parser(gpt_raw:str) -> "PythonExecutableAnswer":
        """ 
        @GPT4-doc-begin
            gpt_raw: string that contains a block of code to be executed in python.
                     you should add comments to explain what you are doing.
                     For instance,
                     We will create a variable, a, and increment it.
                     ```python
                     # create a, and give it an initial value
                     a = 1
                     # increment it
                     a += 1
                     ```
        @GPT4-doc-end 
        """
        
        code_string = PythonExecutableAnswer.remove_markdown_code(gpt_raw)     
        return PythonExecutableAnswer(code=code_string, gpt_raw=gpt_raw)

    def __str__(self):
        return str(self.code)


class TextAnswer(ParsedAnswer):
    """ Text, with minimal parsing
    """
    def __init__(self, data, gpt_raw:str=None): 
        self.data = data
        self.raw = gpt_raw

    @staticmethod 
    def parser(gpt_raw:str):
        data = gpt_raw
        return TextAnswer(data, gpt_raw=gpt_raw)

    def __str__ (self):
        return str(self.data)

# Pass
class YesNoWhy(ParsedAnswer):
    """ Yes/No, but with a reason in the end, and/or suggestions.
    """

    def __init__(self, final_answer, reason_or_suggestions, gpt_raw:str=None):
        self.final_answer = final_answer 
        self.reason_or_suggestions = reason_or_suggestions
        self.raw = gpt_raw
        
    @staticmethod
    def parser(gpt_raw:str):
        """
        @GPT4-doc-begin
        Args:
            gpt_raw: 
                string that contains the tag "[#reason]", where you include the reasoning for
                the final decision (yes/no), followed by the tag "[#finalanswer]", followed by the 
                final answer (yes/no).

                For example,
                    [#reason]
                    your reasoning for why you will answer yes should be put here.

                    [#finalanswer]
                    yes.
                
        @GPT4-doc-end
        """

        if "[#reason]" not in gpt_raw:
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            # print(gpt_raw)
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")        
            raise GPTOutputParseException(f"{gpt_raw} should have [#reason] tag")
        if "[#finalanswer]" not in gpt_raw:
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            # print(gpt_raw)
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            raise GPTOutputParseException(f"{gpt_raw} should have [#finalanswer] tag")
        
        gpt_raw_reasoning = "".join(gpt_raw.split("[#reason]")[1:]).split("[#finalanswer]")[0]
        gpt_raw_decision = "".join(gpt_raw.split("[#finalanswer]")[1:])
        
        return YesNoWhy(
                final_answer=str(YesNo.parser(gpt_raw_decision)),
                reason_or_suggestions=str(TextAnswer.parser(gpt_raw_reasoning)), 
                gpt_raw=gpt_raw
        )

    def success(self):
        return self.final_answer== "yes"

    def __str__(self):
        return f"{self.final_answer}: {self.reason_or_suggestions}"
        

class YesNo(ParsedAnswer):
    """ Yes/No
    """ 
    def __init__(self, data, gpt_raw:str=None):
        self.data = data
        self.raw = gpt_raw

    @staticmethod 
    def remove_punctuation(s):
        return re.sub(r'[^\w\s]', '', s)  
        
    @staticmethod
    def parser(gpt_raw:str):
        """
        @GPT4-doc-begin
        Args:
            gpt_raw: string that contains yes/no answers only, and no other words.
            For example,
                yes.
        @GPT4-doc-end
        """
        yesorno = YesNo.remove_punctuation( gpt_raw.lower().strip()) 
        if yesorno== "yes": 
            data = "yes"
        elif yesorno == "no":
            data = "no"
        else:
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            # print(gpt_raw)
            # print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            raise GPTOutputParseException(f"{yesorno} cannot be parsed to yes/no")
        return YesNo(data, gpt_raw=gpt_raw)

    def success(self):
        """ used in the context of judging the completion of a task.
        """
        return self.data == "yes"
            
    def __str__(self) :
        return self.data


class Number(ParsedAnswer):
    """ Yes/No
    """ 
    def __init__(self, data, gpt_raw:str=None):
        self.data = data
        self.raw = gpt_raw

    @staticmethod 
    def remove_punctuation(s):
        return re.sub(r'[^\w\s]', '', s)  
        
    @staticmethod
    def parser(gpt_raw:str) -> "ReadSign":
        """
        @GPT4-doc-begin
            gpt_raw: string that contains just a simple number.
            
                For example,
                
                90

        @GPT4-doc-end
        """

        gpt_out = gpt_raw.strip().strip('.').strip(',').lower()

        if not gpt_out.isdigit():
            raise GPTOutputParseException("output should only contain a number!")

        return Number(gpt_out, gpt_raw=gpt_raw)

    def success(self):
        """ used in the context of judging the completion of a task.
        """
        return self.data == "yes"
            
    def __str__(self) :
        return self.data