Code Interpreter API is a project that integrates a scheduling center with a sandbox environment, dedicated to creating the world's best code interpreter. It aims to provide a secure and reliable API interface for remotely running code and obtaining execution results, accelerating the development of various AI Agents. This project innovatively combines Docker container technology to achieve secure isolated execution of Python code. Additionally, the project supports storing generated image data in a PostgreSQL database and accessing it through API endpoints, offering rich data processing and storage capabilities. You can access a demo built with Fastgpt here.
You can view the real-time running status, performance metrics, and availability information of this project by visiting our status monitoring page.
- Backend Framework: Flask (Python)
- Database: PostgreSQL
- Containerization: Docker
- ORM: SQLAlchemy
- Concurrency Handling: threading, Queue
- Authentication: Bearer Token
- External Requests: requests
- Code Isolation: subprocess
- Multi-language Support: Currently mainly supports Python code execution.
- Image Processing: Supports converting code-generated image data to Base64 format and storing it in the database.
- Docker Container Isolation: Each code execution request runs in an isolated Docker container, ensuring security and resource isolation.
- PostgreSQL Database Integration: Image data can be stored in the database and accessed via RESTful API.
- Authentication: Optional Bearer token authentication to ensure secure access.
- Environment Variables: Configurable through environment variables.
- Error Handling: Comprehensive error handling and timeout management.
- Python 3.8 and above
- Docker
- PostgreSQL
git clone https://github.com/leezhuuuuu/Code-Interpreter-Api.git
cd Code-Interpreter-Api
pip install -r requirements.txt
The project uses config.yaml
as the configuration file. Ensure the file contains the following configurations:
- Domain: For accessing stored images.
- Docker Image: Specify the Docker image used for running code.
- Port Range: Specify port range for Docker containers.
- PostgreSQL Configuration: Including database name, username, password, host, and port.
- Resource Limits: Specify memory and CPU limits for Docker containers.
- Timeout: Specify timeout for code execution.
Ensure Docker is installed. Then, you can choose one of the following two methods to get the Docker image:
Run build.py
, which will automatically generate a requirements.txt
file based on the configuration file and build a custom image. You can customize the container environment dependencies according to your needs:
python3 build.py
If you don't want to build the image, you can directly pull the pre-built image from Docker Hub:
docker pull leezhuuu/code_interpreter:latest
Use the following command to start the project:
python3 center.py
This command will automatically start the Flask application and run it on the configured scheduling center port.
Access the /runcode
endpoint via POST or GET request to run specified code. The request data should include the following fields:
- languageType: The language type of the code (currently only supports Python).
- variables: Optional, variables to be passed to the code.
- code: The code to be executed.
Access the /image/<filename>
endpoint via GET request to retrieve image data stored in the database.
{
"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": ""
}
The application returns appropriate HTTP status codes and error messages for different scenarios:
- 400 Bad Request: Invalid JSON or parameters.
- 401 Unauthorized: Missing or invalid token.
- 405 Method Not Allowed: Invalid HTTP method.
- 504 Gateway Timeout: Request timeout.
The application uses Docker to run code in an isolated environment. You can choose to build a custom image or pull a pre-built image.
Images generated during code execution are stored in the PostgreSQL database. Database connection details are configured in config.yaml
.
The application uses threads to handle multiple concurrent requests and uses semaphores to control the number of concurrent requests.
The application includes a concurrency test script concurrent_test.py
that can be run to verify concurrent functionality:
python3 concurrent_test.py
This project is licensed under the GNU License. See the LICENSE
file for details.
Contributions are welcome! Please submit issues or pull requests.
- leezhuuuuu
- Flask
- Docker
- PostgreSQL
- SQLAlchemy
Special thanks to IBM for providing computing power support for this project.