Skip to content

🛡️Secure and sandboxed API to execute Python code, upload files, install packages, and manage isolated sessions. Ideal for running untrusted code and ensuring application security.

License

Notifications You must be signed in to change notification settings

promptengineers-ai/interpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 Prompt Engineers AI - Secure Code Interpreter (Sandboxed)

Unlock the potential of executing Python code safely and efficiently with the Prompt Engineers AI - Secure Code Interpreter. This API provides a robust sandboxed environment where users can execute Python scripts, upload files, install packages, and manage sessions with complete isolation from your main application code. Each session is meticulously separated, ensuring that the code, files, and packages for one session do not interfere with others, thereby enhancing security and preventing potential conflicts.

Our sandbox is designed with security as a top priority. By isolating execution environments, we mitigate risks associated with running untrusted code. This makes it an ideal solution for developers and organizations who need a safe and controlled way to execute scripts, test code snippets, or provide coding functionalities within their applications.

Key Features:

  • 🛡️ Secure Execution: Run Python code in a sandboxed environment to ensure the applications remains secure and unaffected.
  • 📁 File Management: Upload and manage files within isolated sessions to maintain data integrity and security.
  • 📦 Package Installation: Install required Python packages per session without affecting the global environment.
  • 🕹️ Session Management: Efficiently create, manage, and terminate sessions to maintain clean and organized execution spaces.

Start Interpreter

docker-compose up --build

Test the Langchain Toolkit

## Change Directory
cd toolkit

## Create Virtual Env
python3 -m venv .venv
source .venv/bin/activate

## Install Dependencies
pip install -r requirements.txt

## Run Script to view & test tool
python interpreter.py

Example Requests

Simple Example

curl -X POST http://localhost:8001/execute -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id",
  "code": "print(\"Hello from Interpreter!\")"
}'

## Result
# {"status":"success","output":"Hello from Interpreter!\n","errors":""}

Example curl Request (Verify will error when numpy is not installed):

curl -X POST http://localhost:8001/execute -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id",
  "code": "import os\nimport numpy as np\na = int(os.getenv(\"VAR_A\"))\nb = int(os.getenv(\"VAR_B\"))\nc = int(os.getenv(\"VAR_C\"))\narray = np.array([a, b, c])\nresult = np.sum(array)\nprint(f\"Result of summing [{a}, {b}, {c}] is: {result}\")",
  "env": {
    "VAR_A": "10",
    "VAR_B": "20",
    "VAR_C": "30"
  }
}'

## Result
# {"status":"success","output":"","errors":"Traceback (most recent call last):\n  File \"/tmp/your_session_id/temp_code.py\", line 2, in <module>\n    import numpy as np\nModuleNotFoundError: No module named 'numpy'\n"}

Example curl Request (Install numpy and execute the code):

curl -X POST http://localhost:8001/install -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id",
  "packages": ["numpy"]
}'

## Result
# {"status":"success","installed_packages":["numpy"]}

Execute numpy with Env vars

curl -X POST http://localhost:8001/execute -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id",
  "code": "import os\nimport numpy as np\na = int(os.getenv(\"VAR_A\"))\nb = int(os.getenv(\"VAR_B\"))\nc = int(os.getenv(\"VAR_C\"))\narray = np.array([a, b, c])\nresult = np.sum(array)\nprint(f\"Result of summing [{a}, {b}, {c}] is: {result}\")",
  "env": {
    "VAR_A": "10",
    "VAR_B": "20",
    "VAR_C": "30"
  }
}'

## Result
# {"status":"success","output":"Result of summing [10, 20, 30] is: 60\n","errors":""}

Example curl Request (Verify numpy has been uninstalled):

curl -X POST http://localhost:8001/terminate -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id"
}'

## Result
# {"status":"success","message":"Session your_session_id terminated successfully."}

curl -X POST http://localhost:8001/execute -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id",
  "code": "import os\nimport numpy as np\na = int(os.getenv(\"VAR_A\"))\nb = int(os.getenv(\"VAR_B\"))\nc = int(os.getenv(\"VAR_C\"))\narray = np.array([a, b, c])\nresult = np.sum(array)\nprint(f\"Result of summing [{a}, {b}, {c}] is: {result}\")",
  "env": {
    "VAR_A": "10",
    "VAR_B": "20",
    "VAR_C": "30"
  }
}'

## Result
# {"status":"success","output":"","errors":"Traceback (most recent call last):\n  File \"/tmp/your_session_id/temp_code.py\", line 2, in <module>\n    import numpy as np\nModuleNotFoundError: No module named 'numpy'\n"}

Execute against files

Upload a file for a specific session.

curl -X POST "http://localhost:8001/upload" \
  -H "accept: application/json" \
  -F "session_id=your_session_id" \
  -F "file=@data/AAPL.csv"

## Result
# {"filename":"AAPL.csv","location":"/tmp/your_session_id/AAPL.csv"}

Install pandas pacakage for created session to interact with csv.

curl -X POST http://localhost:8001/install -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id",
  "packages": ["pandas"]
}'

## Result
# {"status":"success","installed_packages":["pandas"]}

Execute to interact with csv

curl -X POST http://localhost:8001/execute -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id",
  "code": "import pandas as pd\nfile_path = \"/tmp/your_session_id/AAPL.csv\"\ndf = pd.read_csv(file_path)\nfirst_row = df.iloc[0]\nprint(first_row.to_json())"
}'

## Result
# {"status":"success","output":"{\"Date\":\"2022-03-16\",\"Open\":157.050003,\"High\":160.0,\"Low\":154.460007,\"Close\":159.589996,\"Adj Close\":158.629059,\"Volume\":102300200}\n","errors":""}

Download file from session

curl -X GET "http://localhost:8001/download?session_id=your_session_id&filename=AAPL.csv" -o AAPL_downloaded.csv

## Result
# File downloaded to workspace

Terminate session to uninstall pacakges and remove files.

curl -X POST http://localhost:8001/terminate -H "Content-Type: application/json" -d '{
  "session_id": "your_session_id"
}'

## Result
# {"status":"success","message":"Session your_session_id terminated successfully."}

About

🛡️Secure and sandboxed API to execute Python code, upload files, install packages, and manage isolated sessions. Ideal for running untrusted code and ensuring application security.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published