Code Interpreter API 是一个集调度中心与沙箱环境于一体的项目,致力于打造全球最好用的代码解释器,旨在提供一个安全、可靠的 API 接口,用于远程运行代码并获取执行结果,加速各类AI Agent开发,是众多AI爱好者的福音。该项目创新性地结合了 Docker 容器技术,实现了对 Python 代码的安全隔离运行。此外,项目还支持将生成的图像数据存储到 PostgreSQL 数据库中,并通过 API 端点进行访问,提供了丰富的数据处理与存储能力。 您可以在这里访问由Fastgpt构建的Demo。
您可以通过访问我们的状态监控页面来实时查看本项目的运行状态、性能指标和可用性信息。
git clone https://github.com/leezhuuuuu/Code-Interpreter-Api.git cd Code-Interpreter-Api
pip install -r requirements.txt
项目使用 config.yaml
作为配置文件。确保该文件中包含以下配置:
请确保已安装 Docker。然后,您可以根据需要选择以下两种方法之一来获取 Docker 镜像:
运行 build.py
,将根据配置文件自动生成 requirements.txt
文件并构建自定义镜像,可根据个人需求自定义配置容器环境依赖:
如果您不想构建镜像,可以直接从 Docker Hub 拉取提前构建好的镜像:
docker pull leezhuuu/code_interpreter:latest
使用以下命令启动项目:
该命令将自动启动 Flask 应用,并在配置的调度中心端口上运行。
通过 POST 或 GET 请求访问 /runcode
端点,可以运行指定的代码。请求数据应包含以下字段:
通过 GET 请求访问 /image/<filename>
端点,可以获取存储在数据库中的图像数据。
{ "languageType": "python", "variables": {}, "code": "print('Hello, World!')" }
{ "output": "Hello, World!\n" }
/runcode?languageType=python&variables={}&code=print('Hello, World!')
{ "output": "Hello, World!\n" }
{ "languageType": "python", "variables": {}, "code": "import matplotlib.pyplot as plt\nimport numpy as np\n\n# Define vertices of the triangle\nvertices = np.array([[0, 0], [1, 0], [0.5, np.sqrt(3)/2], [0, 0]])\n\n# Create a new figure\nplt.figure()\n\n# Plot the triangle\nplt.plot(vertices[:, 0], vertices[:, 1], 'b-')\n\n# Set axis limits\nplt.xlim(-0.1, 1.1)\nplt.ylim(-0.1, 1.1)\n\n# Save the plot as triangle.png\nplt.savefig('triangle.png')\nplt.close()" }
{ "images": { "triangle.png": "https://code.leez.tech/image/cec3bee5-c45b-47c5-814f-7dc90831450e_triangle.png" }, "output": "" }
应用程序返回适当的 HTTP 状态码和错误消息以应对不同场景:
应用程序使用 Docker 在隔离环境中运行代码。您可以选择构建自定义镜像或拉取提前构建好的镜像。
代码执行期间生成的图像存储在 PostgreSQL 数据库中。数据库连接详细信息配置在 config.yaml
中。
应用程序使用线程处理多个并发请求,并使用信号量控制并发请求的数量。
应用程序包含一个并发测试脚本 concurrent_test.py
,可以运行以验证并发功能:
python3 concurrent_test.py
本项目基于 GNU 许可证。详见 LICENSE
文件。
欢迎贡献!请提交问题或拉取请求。
感谢IBM对本项目提供的算力支持。
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4