Better answer postprocessor for better instruction-aligned models like GPT.
(ori_prompt: str, text: str)
| 44 | |
| 45 | |
| 46 | def gpt_python_postprocess(ori_prompt: str, text: str) -> str: |
| 47 | """Better answer postprocessor for better instruction-aligned models like |
| 48 | GPT.""" |
| 49 | if '```' in text: |
| 50 | blocks = re.findall(r'```(.*?)```', text, re.DOTALL) |
| 51 | if len(blocks) == 0: |
| 52 | text = text.split('```')[1] # fall back to default strategy |
| 53 | else: |
| 54 | text = blocks[0] # fetch the first code block |
| 55 | if not text.startswith('\n'): # in case starting with ```python |
| 56 | text = text[max(text.find('\n') + 1, 0):] |
| 57 | |
| 58 | match_ori = re.search(r'def(.*?)\(', ori_prompt) |
| 59 | match = re.search(r'def(.*?)\(', text) |
| 60 | if match: |
| 61 | if match.group() == match_ori.group(): |
| 62 | text = re.sub('def(.*?)\n', '', text, count=1) |
| 63 | |
| 64 | for c_index, c in enumerate(text[:5]): |
| 65 | if c != ' ': |
| 66 | text = ' ' * (4 - c_index) + text |
| 67 | break |
| 68 | |
| 69 | text = text.split('\n\n\n')[0] |
| 70 | return text |
| 71 | |
| 72 | |
| 73 | def wizardcoder_postprocess(text: str) -> str: |