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

相关推荐
前端小盆友16 小时前
从零实现一个GPT 【React + Express】--- 【2】实现对话流和停止生成
前端·gpt·react.js
牛大了202311 天前
【LLM学习】2-简短学习BERT、GPT主流大模型
gpt·学习·bert
1213411 天前
LLM:重构数字世界的“智能操作系统”
gpt·aigc·ai编程·llama·gpu算力
叠叠乐17 天前
ROS2编译的理解,与GPT对话
gpt
蚂蚁数据AntData18 天前
DB-GPT V0.7.2 版本更新:图表组件可视化增强、支持混合搜索 、支持DeepSeek-R1-0528模型等
大数据·数据库·gpt·架构·数据库架构
PacosonSWJTU22 天前
加载GPT-2模型参数报错:TensorFlow不存在
人工智能·gpt·tensorflow
zm-v-1593043398623 天前
GPT-ArcGIS 在生态评价中的综合应用:多因子权重分析与适宜性制图
gpt·arcgis
激进小猪100224 天前
LLM基础5_从零开始实现 GPT 模型
gpt
阿部多瑞 ABU1 个月前
# 从底层架构到应用实践:为何部分大模型在越狱攻击下失守?
gpt·安全·ai·自然语言处理
阿部多瑞 ABU1 个月前
大模型安全测试报告:千问、GPT 全系列、豆包、Claude 表现优异,DeepSeek、Grok-3 与 Kimi 存在安全隐患
gpt·安全·ai