magnetic's picture
Upload online demo code
7803dd9 verified
raw
history blame
843 Bytes
import os
import sys
import re
prj_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(prj_root_path)
from utils.const import *
class BaseCodeInterpreter:
def __init__(self):
self.dialog = [
{
"role": "system",
"content": CODE_INTERPRETER_SYSTEM_PROMPT,
},
]
@staticmethod
def extract_code_blocks(text: str):
pattern = r"```(?:python\n)?(.*?)```" # Match optional 'python\n' but don't capture it
code_blocks = re.findall(pattern, text, re.DOTALL)
return [block.strip() for block in code_blocks]
def execute_code_and_return_output(self, code_str: str, nb):
_, _ = nb.add_and_run(GUARD_CODE)
outputs, error_flag = nb.add_and_run(code_str)
return outputs, error_flag