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编程专用版发布!独立连续编程7小时,简单任务提速10倍,VS Code就能用
gpt·chatgpt
Code_流苏3 天前
AI热点周报(9.7~9.13):阿里Qwen3-Next震撼发布、Claude 增强记忆与服务抖动、OpenAI 聚焦模型规范化...
人工智能·gpt·ai·openai·claude·qwen3-next·架构创新
gptplus4 天前
【重要通知】ChatGPT Plus将于9月16日调整全球充值定价,低价区将被弃用,开发者如何应对?
人工智能·gpt·chatgpt
nju_spy4 天前
GPT 系列论文1-2 两阶段半监督 + zero-shot prompt
人工智能·gpt·nlp·大语言模型·zero-shot·transformer架构·半监督训练
*星星之火*4 天前
【GPT入门】第67课 多模态模型实践: 本地部署文生视频模型和图片推理模型
gpt
技术程序猿华锋4 天前
深度解码OpenAI的2025野心:Codex重生与GPT-5 APIKey获取调用示例
人工智能·vscode·python·gpt·深度学习·编辑器
钝挫力PROGRAMER5 天前
GPT与BERT BGE
人工智能·gpt·bert
edisao5 天前
[特殊字符] 从助手到引擎:基于 GPT 的战略协作系统演示
大数据·人工智能·gpt
陈敬雷-充电了么-CEO兼CTO6 天前
BLIP-2革新多模态预训练:QFormer桥接视觉语言,零样本任务性能飙升10.7%!
人工智能·gpt·机器学习·机器人·多模态·blip·多模态大模型
安思派Anspire6 天前
GPT-OSS 深度解析:OpenAI 最新大语言模型(LLM)架构
gpt·语言模型·架构