{"id":23933903,"url":"https://github.com/leezhuuuuu/Code-Interpreter-Api","last_synced_at":"2025-09-11T16:33:09.398Z","repository":{"id":252912304,"uuid":"841866758","full_name":"leezhuuuuu/Code-Interpreter-Api","owner":"leezhuuuuu","description":"Committed to being the best code interpreter in the world.","archived":false,"fork":false,"pushed_at":"2025-01-04T12:02:59.000Z","size":72,"stargazers_count":131,"open_issues_count":3,"forks_count":13,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-01-04T13:19:04.035Z","etag":null,"topics":["code-interpreter","langchain","llm","tools"],"latest_commit_sha":null,"homepage":"https://code.leez.tech/doc","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leezhuuuuu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-13T07:47:58.000Z","updated_at":"2025-01-04T12:03:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e939f4a-4547-4d0f-8533-f837ea0a2bb9","html_url":"https://github.com/leezhuuuuu/Code-Interpreter-Api","commit_stats":null,"previous_names":["leezhuuuuu/code-interpreter-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/leezhuuuuu/Code-Interpreter-Api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leezhuuuuu%2FCode-Interpreter-Api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leezhuuuuu%2FCode-Interpreter-Api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leezhuuuuu%2FCode-Interpreter-Api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leezhuuuuu%2FCode-Interpreter-Api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leezhuuuuu","download_url":"https://codeload.github.com/leezhuuuuu/Code-Interpreter-Api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leezhuuuuu%2FCode-Interpreter-Api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274670404,"owners_count":25328233,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-11T02:00:13.660Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-interpreter","langchain","llm","tools"],"created_at":"2025-01-06T00:29:58.660Z","updated_at":"2025-09-11T16:33:09.075Z","avatar_url":"https://github.com/leezhuuuuu.png","language":"Python","funding_links":[],"categories":["Building"],"sub_categories":["Tools"],"readme":"  # Code Interpreter API 🚀\n\n[English](https://github.com/leezhuuuuu/Code-Interpreter-Api/blob/main/README_EN.md) | [中文](https://github.com/leezhuuuuu/Code-Interpreter-Api/blob/main/README.md)\n\n\n[![](https://img.shields.io/github/license/leezhuuuuu/Code-Interpreter-Api.svg)](LICENSE)\n![](https://img.shields.io/github/stars/leezhuuuuu/Code-Interpreter-Api.svg)\n![](https://img.shields.io/github/forks/leezhuuuuu/Code-Interpreter-Api.svg)\n![](https://img.shields.io/docker/pulls/leezhuuu/code_interpreter.svg)\n\n## 概述 🌟\n\nCode Interpreter API 是一个集调度中心与沙箱环境于一体的项目，致力于打造全球最好用的代码解释器，旨在提供一个安全、可靠的 API 接口，用于远程运行代码并获取执行结果，加速各类AI Agent开发，是众多AI爱好者的福音。该项目创新性地结合了 Docker 容器技术，实现了对 Python 代码的安全隔离运行。此外，项目还支持将生成的图像数据存储到 PostgreSQL 数据库中，并通过 API 端点进行访问，提供了丰富的数据处理与存储能力。\n您可以在[这里](https://fastgpt.leez.tech/chat/share?shareId=emvjwl8hgqu4pj314k7yakpp)访问由Fastgpt构建的Demo。\n\n## 项目状态 📊\n\n您可以通过访问我们的[状态监控页面](https://status.leez.tech/)来实时查看本项目的运行状态、性能指标和可用性信息。\n\n## 技术栈 🛠️\n\n- **后端框架**：Flask (Python)\n- **数据库**：PostgreSQL\n- **容器化**：Docker\n- **ORM**：SQLAlchemy\n- **并发处理**：threading, Queue\n- **身份验证**：Bearer Token\n- **外部请求**：requests\n- **代码隔离**：subprocess\n\n## 特性 🌈\n\n- **多语言支持**：目前主要支持 Python 代码的执行。\n- **图像处理**：支持将代码生成的图像数据转换为 Base64 格式，并可存储在数据库中。\n- **Docker 容器隔离**：每个代码执行请求在独立的 Docker 容器中运行，确保安全性和资源隔离。\n- **PostgreSQL 数据库集成**：图像数据可以存储到数据库中，并通过 RESTful API 进行访问。\n- **身份验证**：可选的 Bearer 令牌身份验证以确保安全访问。\n- **环境变量**：可通过环境变量进行配置。\n- **错误处理**：全面的错误处理和超时管理。\n\n## 运行环境 🖥️\n\n- Python 3.8 及以上\n- Docker\n- PostgreSQL\n\n## 快速开始 🚀\n\n### 1. 克隆项目\n\n```bash\ngit clone https://github.com/leezhuuuuu/Code-Interpreter-Api.git\ncd Code-Interpreter-Api\n```\n\n### 2. 安装依赖\n\n```bash\npip install -r requirements.txt\n```\n\n### 3. 配置文件\n\n项目使用 `config.yaml` 作为配置文件。确保该文件中包含以下配置：\n\n- **域名**：用于访问存储的图像。\n- **Docker 镜像**：指定用于运行代码的 Docker 镜像。\n- **端口范围**：为 Docker 容器指定端口范围。\n- **PostgreSQL 配置**：包括数据库名、用户名、密码、主机和端口。\n- **资源限制**：为 Docker 容器指定内存和 CPU 限制。\n- **超时时间**：指定代码执行的超时时间。\n\n### 4. 获取 Docker 镜像\n\n请确保已安装 Docker。然后，您可以根据需要选择以下两种方法之一来获取 Docker 镜像：\n\n#### 方法一：构建自定义镜像\n\n运行 `build.py`，将根据配置文件自动生成 `requirements.txt` 文件并构建自定义镜像，可根据个人需求自定义配置容器环境依赖：\n\n```bash\npython3 build.py\n```\n\n#### 方法二：拉取提前构建好的镜像\n\n如果您不想构建镜像，可以直接从 Docker Hub 拉取提前构建好的镜像：\n\n```bash\ndocker pull leezhuuu/code_interpreter:latest\n```\n\n### 5. 启动项目\n\n使用以下命令启动项目：\n\n```bash\npython3 center.py\n```\n\n该命令将自动启动 Flask 应用，并在配置的调度中心端口上运行。\n\n## 使用指南 📖\n\n### 1. 运行代码\n\n通过 POST 或 GET 请求访问 `/runcode` 端点，可以运行指定的代码。请求数据应包含以下字段：\n\n- **languageType**：代码的语言类型（当前仅支持 Python）。\n- **variables**：可选，传递给代码的变量。\n- **code**：要执行的代码。\n\n### 2. 访问图像\n\n通过 GET 请求访问 `/image/\u003cfilename\u003e` 端点，可以获取存储在数据库中的图像数据。\n\n## API 端点 🌐\n\n### `POST /runcode`\n\n#### 请求\n\n```json\n{\n  \"languageType\": \"python\",\n  \"variables\": {},\n  \"code\": \"print('Hello, World!')\"\n}\n```\n\n#### 响应\n\n```json\n{\n  \"output\": \"Hello, World!\\n\"\n}\n```\n[![Run in Hoppscotch](https://hopp.sh/badge.svg)](https://hopp.sh/r/hqyKw95KDVnl)\n\n### `GET /runcode`\n\n#### 请求\n\n```\n/runcode?languageType=python\u0026variables={}\u0026code=print('Hello, World!')\n```\n\n#### 响应\n\n```json\n{\n  \"output\": \"Hello, World!\\n\"\n}\n```\n[![Run in Hoppscotch](https://hopp.sh/badge.svg)](https://hopp.sh/r/UEkwk6u6Howq)\n\n### `POST /runcode` 复杂示例\n\n#### 请求\n\n```json\n{\n  \"languageType\": \"python\",\n  \"variables\": {},\n  \"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()\"\n}\n```\n\n#### 响应\n\n```json\n{\n  \"images\": {\n    \"triangle.png\": \"https://code.leez.tech/image/cec3bee5-c45b-47c5-814f-7dc90831450e_triangle.png\"\n  },\n  \"output\": \"\"\n}\n```\n[![Run in Hoppscotch](https://hopp.sh/badge.svg)](https://hopp.sh/r/8c2Q1nPhuTg3)\n\n\n## 错误处理 🚨\n\n应用程序返回适当的 HTTP 状态码和错误消息以应对不同场景：\n\n- **400 Bad Request**：无效的 JSON 或参数。\n- **401 Unauthorized**：缺失或无效的令牌。\n- **405 Method Not Allowed**：无效的 HTTP 方法。\n- **504 Gateway Timeout**：请求超时。\n\n## Docker 集成 🐳\n\n应用程序使用 Docker 在隔离环境中运行代码。您可以选择构建自定义镜像或拉取提前构建好的镜像。\n\n## PostgreSQL 集成 🐘\n\n代码执行期间生成的图像存储在 PostgreSQL 数据库中。数据库连接详细信息配置在 `config.yaml` 中。\n\n## 并发管理 🔄\n\n应用程序使用线程处理多个并发请求，并使用信号量控制并发请求的数量。\n\n## 测试 🧪\n\n### 并发测试\n\n应用程序包含一个并发测试脚本 `concurrent_test.py`，可以运行以验证并发功能：\n\n```bash\npython3 concurrent_test.py\n```\n\n## 许可证 📄\n\n本项目基于 GNU 许可证。详见 `LICENSE` 文件。\n\n## 贡献 🤝\n\n欢迎贡献！请提交问题或拉取请求。\n\n## 作者 ✍️\n\n- leezhuuuuu\n\n## 致谢 🙏\n\n- Flask\n- Docker\n- PostgreSQL\n- SQLAlchemy\n\n## GitHub Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=leezhuuuuu/Code-Interpreter-Api\u0026type=Date)](https://star-history.com/#leezhuuuuu/Code-Interpreter-Api\u0026Date)\n\n## 特别鸣谢\n\n\u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/5/51/IBM_logo.svg\" alt=\"IBM Logo\" width=\"120\"/\u003e\n\n感谢IBM对本项目提供的算力支持。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleezhuuuuu%2FCode-Interpreter-Api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleezhuuuuu%2FCode-Interpreter-Api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleezhuuuuu%2FCode-Interpreter-Api/lists"}