Post-process the LLM generated text to make it valid.
(text: str)
| 56 | |
| 57 | |
| 58 | def post_process(text: str) -> Optional[str]: |
| 59 | """Post-process the LLM generated text to make it valid.""" |
| 60 | if "\n```" not in text: |
| 61 | return None |
| 62 | |
| 63 | # split ```python3 or ```python |
| 64 | text = re.split(r"\n```python3?\n", text)[1] |
| 65 | text = text.split("\n```")[0].strip() |
| 66 | |
| 67 | # perform syntax check |
| 68 | if not syntax_check(text): |
| 69 | print(colored("⚠️ Syntax check failed for the code below:", "red")) |
| 70 | print(text[:256], "..." if len(text) > 256 else "") |
| 71 | return None |
| 72 | |
| 73 | return text |
| 74 | |
| 75 | |
| 76 | # returns: |