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代码写好了,前面也有一层代码过滤的逻辑。

相关推荐
kkkliaoo9 小时前
GPT-5.5 vs Claude 4.7深度对比:在bblabu上如何根据场景选择最佳模型(2026选型指南)
gpt·claude·ccswitch
jike88ai11 小时前
告别海外账号!OpenClaw+88api一站式配置:Claude/GPT/Gemini本地调用全流程
gpt·node.js·api·claude·openclaw·88api
这是谁的博客?17 小时前
AI 领域精选新闻(2026-05-21)
人工智能·gpt·ai·google·大模型·gemini·新闻
SCKJAI18 小时前
边缘AI新标杆,Pandora Orin NX16+GPT-OSS重新定义终端智能
人工智能·gpt
风落无尘2 天前
第九章《语言与理解》 完整学习资料
gpt·rnn·语言模型·transformer
视***间2 天前
端侧大模型落地新标杆:视程空间将GPT-OSS边缘AI深度导入NVIDIA Jetson平台
人工智能·gpt·边缘计算·nvidia·ai算力·gpt-oss·视程空间
这是谁的博客?2 天前
[模型解析] GPT: 模型演进分析从GPT-3到GPT-5.5
gpt·ai·chatgpt·大模型·gpt-3·openai
renhongxia12 天前
从GPT到开源大模型
人工智能·gpt·生成对抗网络·语言模型·自然语言处理·开源
淘矿人3 天前
【AI大模型】AI 大模型推理平台完整测评:8 家主流聚合服务对比分析
人工智能·sql·gpt·学习·github·php
Revio Lab3 天前
如何在国内API方式接入 Claude Opus 4.7、GPT-5.5、GPT Image 2
gpt·github