GPT Code Interpreter

前几天,找了一些开源项目阅读学习,主要了解现在执行GPT生成的代码一般的做法是什么样的。

unsetunset使用Ipython执行代码unsetunset

我们Google一搜,很容易就搜到:https://e2b.dev/,可以使用它提供的api在e2b的沙箱中执行生成的Python或JavaScript代码,一开始以为是开源的,仓库为:https://github.com/e2b-dev/code-interpreter.git

浏览源码后,开源的只是一个sdk,即你需要提供api key,然后使用e2b cloud sandbox去执行代码,项目关键代码如下:

go 复制代码
try:
    with self._client.stream(
        "POST",
        f"{self._jupyter_url}/execute",
        json={
            "code": code,
            "context_id": context_id,
            "language": language,
            "env_vars": envs,
        },
        timeout=(request_timeout, timeout, request_timeout, request_timeout),
    ) as response:
        err = extract_exception(response)
        if err:
            raise err

            execution = Execution()

        for line in response.iter_lines():
            parse_output(
                execution,
                line,
                on_stdout=on_stdout,
                on_stderr=on_stderr,
                on_result=on_result,
                on_error=on_error,
            )

            return execution
except httpx.ReadTimeout:
    raise format_execution_timeout_error()
except httpx.TimeoutException:
    raise format_request_timeout_error()

其中jupyter_url这个变量名引起我的注意,Python仔都知道,Jupyter我们常用来写写数据分析和算法相关的notebook,是可以执行代码的,直接ChatGPT调研,就可以发现,我们可以使用ipython来执行代码。

然后我们发现了这个项目:https://github.com/chapyter/chapyter.git

Jupyter Notebook是基于Ipython的,可以使用Ipython包中的InteractiveShell来执行代码,用法如下:

go 复制代码
from IPython.core.interactiveshell import InteractiveShell

# 创建一个 InteractiveShell 实例
shell = InteractiveShell.instance()

# 要执行的代码
code = """
def greet(name):
    return f"Hello, {name}!"

result = greet("Lindee")
print(result)
"""

# 执行代码
shell.run_cell(code)

不负责猜测,e2b可能也用了这种方式来执行。

当然,单纯这样,应该是不行的,因为用户可能会提交恶意代码,即GPT可能会生成自毁代码,所以需要限制执行。

因为e2b这里没有更多信息,所以我们换其他开源项目。

unsetunset使用subprocess.Popenunsetunset

又找到一个code-interpreter:https://github.com/haseeb-heaven/code-interpreter.git

阅读代码,可以找到关键代码如下:

go 复制代码
# libs/code_interpreter.py

def execute_code(self, code, language):
        try:
            language = language.lower()
            self.logger.info(f"Running code: {code[:100]} in language: {language}")

            # Check for code and language validity
            ifnot code or len(code.strip()) == 0:
                return"Code is empty. Cannot execute an empty code."
            
            # Check for compilers on the system
            compilers_status = self._check_compilers(language)
            ifnot compilers_status:
                raise Exception("Compilers not found. Please install compilers on your system.")
            
            if language == "python":
                process = subprocess.Popen(["python", "-c", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                stdout, stderr = process.communicate()
                stdout_output = stdout.decode("utf-8")
                stderr_output = stderr.decode("utf-8")
                self.logger.info(f"Python Output execution: {stdout_output}, Errors: {stderr_output}")
                return stdout_output, stderr_output
            
            elif language == "javascript":
                process = subprocess.Popen(["node", "-e", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                stdout, stderr = process.communicate()
                stdout_output = stdout.decode("utf-8")
                stderr_output = stderr.decode("utf-8")
                self.logger.info(f"JavaScript Output execution: {stdout_output}, Errors: {stderr_output}")
                return stdout_output, stderr_output
            
            else:
                self.logger.info("Unsupported language.")
                raise Exception("Unsupported language.")
                
        except Exception as exception:
            self.logger.error(f"Exception in running code: {str(exception)}")
            raise exception

通过subprocess.Popen来执行Python或JavaScript代码,但跟Ipython一样,依旧会有安全性问题。

这些让cursor基于代码上下文,给我一下解决方案:

  • Sandbo沙箱执行

    • Docker

    • firejail(也是一个沙箱)

  • RestrictedPython执行受限的python代码(定义了Python语法的子集),即有风险的,都不给执行了

  • 对代码进行白名单审察(比如有os.remove的,直接删掉相关代码)

嗯,思路有了,但我想抄代码,不想基于思路直接写,而用cursor生成,我感觉信任度问题,因为自己研究不深入,如果用他生成的代码,可能也会出现意想不到的问题。

unsetunsetautogen的方案unsetunset

autogen是微软的multi-ai-agent相关的项目:

它提供了3种执行代码的方式

  • azure就是用azure提供的python容器去执行,限制比较多,只支持有限的python库

  • 使用docker容器执行,默认使用的 python:3-slim 镜像

  • 使用subprocess执行,只是会在前面做一层过来,避免自毁代码

autogen的代码就可以复制来用了。

unsetunset总结unsetunset

比较标准的解决方法,就是使用docker,然后构建容器去执行,容器启动速度是很快的,但也会有点时间损耗,如果你想省点时间,就用subprocess的方案,毕竟autogen代码写好了,前面也有一层代码过滤的逻辑。

相关推荐
量子位2 天前
GPT-5.4发布:OpenAI首个大一统模型,简直是龙虾原生
gpt·ai编程
深藏blue473 天前
GPT-5.3 Instant 重磅上线!2026最新 ChatGPT 告别说教,国内使用与 Plus 升级教程
gpt·chatgpt·openai
2501_9481142410 天前
【2026架构实战】GPT-5.3与蒸汽数据融合:基于Open Claw与星链4SAPI的RAG落地指南
gpt·架构
小雨中_13 天前
3.1 GPT 系列:Generative Pre-Training(从 GPT-1 到 GPT-3)
人工智能·gpt·深度学习·机器学习·自然语言处理·gpt-3
香芋Yu13 天前
【2026大模型面试圣经】(2)主流大模型架构全景 | GPT/LLaMA/DeepSeek/Qwen深度对比
gpt·面试·架构
一个努力编程人13 天前
NLP领域————GPT算法
人工智能·gpt·自然语言处理
狮子座明仔14 天前
REDSearcher:如何用30B参数的小模型,在深度搜索上击败GPT-o3和Gemini?
人工智能·gpt·深度学习·microsoft·语言模型·自然语言处理
智算菩萨15 天前
2026年春节后,AI大模型格局彻底变了——Claude 4.6、GPT-5.2与六大国产模型全面横评
人工智能·gpt·ai编程
Learner__Q17 天前
GPT模型入门教程:从原理到实现
python·gpt
五月君_18 天前
除夕夜炸场!Qwen 3.5 正式发布:激活仅 17B,性能硬刚 GPT-5.2?
gpt